Skip to content

Commit 53b1dbd

Browse files
committed
add block wrapper
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent accdd3b commit 53b1dbd

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/ozark/database.nim

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,38 +73,48 @@ macro withDB*(body: untyped) =
7373
##
7474
## This macro will open a connection to the database,
7575
## execute the body, and then close the connection.
76+
##
77+
## For more efficient connection management, consider using `withDBPool` instead.
7678
result = newStmtList()
7779
add result, quote do:
78-
let db = getInstance()
79-
assert db != nil, "Database manager not initialized. Call initDBManager first."
80-
let dbcon {.inject.} =
81-
open(db[].maindb.address, db[].maindb.user,
82-
db[].maindb.password, db[].maindb.name)
83-
defer:
84-
dbcon.close()
8580
block:
86-
`body`
81+
# This block ensures that multiple withDB calls can be nested without
82+
# interfering with each other's connections.
83+
let db = getInstance()
84+
assert db != nil, "Database manager not initialized. Call initDBManager first."
85+
let dbcon {.inject.} =
86+
open(db[].maindb.address, db[].maindb.user,
87+
db[].maindb.password, db[].maindb.name)
88+
defer:
89+
dbcon.close()
90+
block:
91+
`body`
8792

8893
macro withDatabase*(id: static string, body: untyped) =
8994
## Use the specified database context to run database queries.
9095
##
9196
## This macro will open a connection to the database,
9297
## execute the body, and then close the connection.
98+
##
99+
## For more efficient connection management, consider using `withDBPool` instead.
93100
result = newStmtList()
94101
add result, quote do:
95-
let db = getInstance()
96-
assert db != nil, "Database manager not initialized. Call initDBManager first."
97-
assert db.hasKey(id), "Database connection with id `" & id & "` not found."
98-
let dbcon {.inject.} =
99-
open(db[id].address, db[$id].user,
100-
db[id].password, db[$id].name)
101-
defer:
102-
dbcon.close()
103102
block:
104-
`body`
105-
103+
# ensure multiple withDB calls can be nested without interfering with
104+
# each other's connections.
105+
let db = getInstance()
106+
assert db != nil, "Database manager not initialized. Call initDBManager first."
107+
assert db.hasKey(id), "Database connection with id `" & id & "` not found."
108+
let dbcon {.inject.} =
109+
open(db[id].address, db[$id].user,
110+
db[id].password, db[$id].name)
111+
defer:
112+
dbcon.close()
113+
block:
114+
`body`
106115

107116
proc openConn(cfg: DBConnection): DBConn =
117+
# Currently only supports PostgreSQL, but can be extended to support other databases
108118
case cfg.driver
109119
of PostgreSQLDriver:
110120
open(cfg.address, cfg.user, cfg.password, cfg.name)
@@ -180,13 +190,11 @@ macro withDBPool*(body: untyped) =
180190
## Run queries using a pooled connection.
181191
result = newStmtList()
182192
add result, quote do:
183-
let db = getInstance()
184-
assert db != nil, "Database manager not initialized. Call initOzarkDatabase first."
185-
assert db[].mainPool != nil, "DB pool not initialized. Call initOzarkPool first."
186-
187-
let dbcon {.inject.} = acquireConn(db[].mainPool)
188-
defer:
189-
releaseConn(db[].mainPool, dbcon)
190-
191193
block:
194+
let db = getInstance()
195+
assert db != nil, "Database manager not initialized. Call initOzarkDatabase first."
196+
assert db[].mainPool != nil, "DB pool not initialized. Call initOzarkPool first."
197+
let dbcon {.inject.} = acquireConn(db[].mainPool)
198+
defer:
199+
releaseConn(db[].mainPool, dbcon)
192200
`body`

0 commit comments

Comments
 (0)