Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
1d7c955805 | |||
9625ac6a5e | |||
d4540a1de7 |
@ -1,6 +1,6 @@
|
||||
# Package
|
||||
|
||||
version = "1.0.0"
|
||||
version = "1.0.2"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Lightweight Postgres ORM for Nim."
|
||||
license = "GPL-3.0"
|
||||
|
@ -34,13 +34,11 @@ proc createRecord*[T](db: DbConn, rec: T): T =
|
||||
var mc = newMutateClauses()
|
||||
populateMutateClauses(rec, true, mc)
|
||||
|
||||
# Confusingly, getRow allows inserts and updates. We use it to get back the ID
|
||||
# we want from the row.
|
||||
let sqlStmt =
|
||||
"INSERT INTO " & tableName(rec) &
|
||||
" (" & mc.columns.join(",") & ") " &
|
||||
" VALUES (" & mc.placeholders.join(",") & ") " &
|
||||
" RETURNING *"
|
||||
" RETURNING " & columnNamesForModel(rec).join(",")
|
||||
|
||||
log().debug "createRecord: [" & sqlStmt & "]"
|
||||
let newRow = db.getRow(sql(sqlStmt), mc.values)
|
||||
|
@ -62,14 +62,16 @@ proc maintain(pool: DbConnPool): void =
|
||||
let freeConns = pool.conns.filterIt(it.free)
|
||||
if pool.conns.len > pool.cfg.poolSize and freeConns.len > 0:
|
||||
let numToCull = min(freeConns.len, pool.conns.len - pool.cfg.poolSize)
|
||||
let toCull = freeConns[0..numToCull]
|
||||
pool.conns.keepIf((pc) => toCull.allIt(it.id != pc.id))
|
||||
for culled in toCull:
|
||||
try: culled.conn.close()
|
||||
except: discard ""
|
||||
log().debug(
|
||||
"Trimming pool size. Culled $# free connections. $# connections remaining." %
|
||||
[$toCull.len, $pool.conns.len])
|
||||
|
||||
if numToCull > 0:
|
||||
let toCull = freeConns[0..numToCull]
|
||||
pool.conns.keepIf((pc) => toCull.allIt(it.id != pc.id))
|
||||
for culled in toCull:
|
||||
try: culled.conn.close()
|
||||
except: discard ""
|
||||
log().debug(
|
||||
"Trimming pool size. Culled $# free connections. $# connections remaining." %
|
||||
[$toCull.len, $pool.conns.len])
|
||||
|
||||
proc take*(pool: DbConnPool): tuple[id: int, conn: DbConn] =
|
||||
pool.maintain
|
||||
@ -93,5 +95,5 @@ proc release*(pool: DbConnPool, connId: int): void =
|
||||
|
||||
template withConn*(pool: DbConnPool, stmt: untyped): untyped =
|
||||
let (connId, conn {.inject.}) = take(pool)
|
||||
stmt
|
||||
release(pool, connId)
|
||||
try: stmt
|
||||
finally: release(pool, connId)
|
||||
|
Reference in New Issue
Block a user