Add LOCK statement wiki.

This commit is contained in:
go-jet 2019-07-01 13:18:39 +02:00
parent 5e0e2f2908
commit 461911889a
3 changed files with 63 additions and 4 deletions

45
wiki/LOCK.md Normal file
View file

@ -0,0 +1,45 @@
LOCK statement obtains a table-level lock, waiting if necessary for any conflicting locks to be released.
More about LOCK statement in PostgreSQL: https://www.postgresql.org/docs/11/sql-lock.html
Following clauses are supported:
- IN(mode) - mode specifies which locks this lock conflicts with.
Mode can be:
- jet.LOCK_ACCESS_SHARE
- jet.LOCK_ROW_SHARE
- jet.LOCK_ROW_EXCLUSIVE
- jet.LOCK_SHARE_UPDATE_EXCLUSIVE
- jet.LOCK_SHARE
- jet.LOCK_SHARE_ROW_EXCLUSIVE
- jet.LOCK_EXCLUSIVE
- jet.LOCK_ACCESS_EXCLUSIVE
- 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(jet.LOCK_ACCESS_SHARE).
NOWAIT()
```
Debug SQL of above statement:
```sql
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.

View file

@ -124,14 +124,26 @@ OFFSET 11
```
##### 9. FOR clause
Lock mode can be:
- jet.UPDATE()
- jet.NO_KEY_UPDATE()
- jet.SHARE()
- jet.KEY_SHARE()
Lock mode has following clauses:
- NOWAIT()
- SKIP_LOCKED()
```
Go:
.FOR(jet.NO_KEY_UPDATE)
.FOR(jet.NO_KEY_UPDATE().SKIP_LOCKED())
SQL:
FOR NO KEY UPDATE
FOR NO KEY UPDATE SKIP LOCKED
```
### Two forms of select statements in Jet
#### 1. Classical select statement

View file

@ -1,6 +1,7 @@
* [Installation](https://github.com/go-jet/jet/wiki/Installation)
* [Generator](https://github.com/go-jet/jet/wiki/Generator)
* [Model data](https://github.com/go-jet/jet/wiki/Model-data.md)
* [Writing SQL in Go]()
* [Expressions](https://github.com/go-jet/jet/wiki/Expressions)
* [Statements](https://github.com/go-jet/jet/wiki/Statements)
@ -9,3 +10,4 @@
* [UPDATE](https://github.com/go-jet/jet/wiki/UPDATE)
* [DELETE](https://github.com/go-jet/jet/wiki/DELETE)
* [LOCK](https://github.com/go-jet/jet/wiki/LOCK)
* [Execution]()