From 461911889a3cea08c731e48bf1aa3a5b572993f7 Mon Sep 17 00:00:00 2001 From: go-jet Date: Mon, 1 Jul 2019 13:18:39 +0200 Subject: [PATCH] Add LOCK statement wiki. --- wiki/LOCK.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ wiki/SELECT.md | 18 +++++++++++++++--- wiki/_Sidebar.md | 4 +++- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 wiki/LOCK.md diff --git a/wiki/LOCK.md b/wiki/LOCK.md new file mode 100644 index 0000000..b86e849 --- /dev/null +++ b/wiki/LOCK.md @@ -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. + + \ No newline at end of file diff --git a/wiki/SELECT.md b/wiki/SELECT.md index e903190..0b99e6b 100644 --- a/wiki/SELECT.md +++ b/wiki/SELECT.md @@ -123,15 +123,27 @@ SQL: OFFSET 11 ``` -##### 9. FOR clause +##### 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 diff --git a/wiki/_Sidebar.md b/wiki/_Sidebar.md index ef6ac47..cc97acd 100644 --- a/wiki/_Sidebar.md +++ b/wiki/_Sidebar.md @@ -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) @@ -8,4 +9,5 @@ * [INSERT](https://github.com/go-jet/jet/wiki/INSERT) * [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) \ No newline at end of file + * [LOCK](https://github.com/go-jet/jet/wiki/LOCK) +* [Execution]() \ No newline at end of file