Skip to content

Commit 549641f

Browse files
committed
add more transaction types
1 parent ea99239 commit 549641f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/easy_sqlite3/macros.nim

+14-3
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ macro importdb*(sql: static string, body: untyped) =
205205
error("Expected proc or iterator, got " & $body.kind, body)
206206
return
207207

208-
proc db_begin() {.importdb: "BEGIN".}
208+
proc db_begin_deferred() {.importdb: "BEGIN DEFERRED".}
209+
proc db_begin_immediate() {.importdb: "BEGIN IMMEDIATE".}
210+
proc db_begin_exclusive() {.importdb: "BEGIN EXCLUSIVE".}
209211
proc db_commit() {.importdb: "COMMIT".}
210212
proc db_rollback() {.importdb: "ROLLBACK".}
211213

212-
template transaction*(db: var Database, body: untyped): untyped =
213-
db_begin db
214+
template gen_transaction(db: var Database, begin_stmt, body: untyped): untyped =
215+
begin_stmt db
214216
block outer:
215217
template commit() {.inject, used.} =
216218
db_commit db
@@ -225,3 +227,12 @@ template transaction*(db: var Database, body: untyped): untyped =
225227
except:
226228
db_rollback db
227229
raise getCurrentException()
230+
231+
template transaction*(db: var Database, body: untyped): untyped =
232+
gen_transaction db, db_begin_deferred, body
233+
234+
template transactionImmediate*(db: var Database, body: untyped): untyped =
235+
gen_transaction db, db_begin_immediate, body
236+
237+
template transactionExclusive*(db: var Database, body: untyped): untyped =
238+
gen_transaction db, db_begin_exclusive, body

tests/test_thread.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ proc worker_fn() {.thread.} =
5151
var r = initRand(42)
5252
for _ in 0..<(COUNT div GROUP):
5353
retry:
54-
tdb.transaction:
54+
tdb.transactionImmediate:
5555
for _ in 0..<GROUP:
5656
let val = r.rand(1048576)
5757
# increase the chance of collision

0 commit comments

Comments
 (0)