LOCK (original) (raw)
LOCK statement obtains a table-level lock, waiting if necessary for any conflicting locks to be released.
More about LOCK statement can be found:
PostgreSQL - https://www.postgresql.org/docs/11/sql-lock.html
MySQL - https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html
MariaDB - https://mariadb.com/kb/en/library/lock-tables/
Following clauses are supported:
IN(mode)- mode specifies which locks this lock conflicts with.- For PostgreSQL mode can be:
*LOCK_ACCESS_SHARE
*LOCK_ROW_SHARE
*LOCK_ROW_EXCLUSIVE
*LOCK_SHARE_UPDATE_EXCLUSIVE
*LOCK_SHARE
*LOCK_SHARE_ROW_EXCLUSIVE
*LOCK_EXCLUSIVE
*LOCK_ACCESS_EXCLUSIVE - For MySQL and MariaDB mode can be:
*READ
*WRITE
- For PostgreSQL mode can be:
NOWAIT()- locked table should not wait for any conflicting locks to be released. If the specified lock(s) cannot be acquired immediately without waiting, the transaction is aborted.
Example
lockStmt := Address. LOCK(). IN(LOCK_ACCESS_SHARE). NOWAIT()
Debug SQL of above statement:
LOCK TABLE dvds.address IN ACCESS SHARE MODE NOWAIT;
Execute statement
To execute update statement and get sql.Result:
res, err := lockStmt.Exec(db)
Use ExecContext to provide context object to execution.