Use namespaced_logging 1.x for logging (optionally).
This commit is contained in:
parent
0599d41061
commit
2030fd4490
@ -11,4 +11,4 @@ srcDir = "src"
|
|||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
||||||
requires @["nim >= 1.4.0", "uuids"]
|
requires @["nim >= 1.4.0", "uuids"]
|
||||||
requires "namespaced_logging >= 0.3.0"
|
requires "namespaced_logging >= 1.0.0"
|
||||||
|
@ -284,7 +284,7 @@
|
|||||||
##
|
##
|
||||||
## .. _pool.DbConnPool: fiber_orm/pool.html#DbConnPool
|
## .. _pool.DbConnPool: fiber_orm/pool.html#DbConnPool
|
||||||
##
|
##
|
||||||
import std/[logging, macros, options, sequtils, strutils]
|
import std/[json, macros, options, sequtils, strutils]
|
||||||
import db_connector/db_common
|
import db_connector/db_common
|
||||||
import namespaced_logging, uuids
|
import namespaced_logging, uuids
|
||||||
|
|
||||||
@ -321,11 +321,18 @@ type
|
|||||||
NotFoundError* = object of CatchableError ##\
|
NotFoundError* = object of CatchableError ##\
|
||||||
## Error type raised when no record matches a given ID
|
## Error type raised when no record matches a given ID
|
||||||
|
|
||||||
var logNs {.threadvar.}: LoggingNamespace
|
var logService {.threadvar.}: Option[LogService]
|
||||||
|
|
||||||
template log(): untyped =
|
proc logQuery(methodName: string, sqlStmt: string, args: openArray[(string, string)] = []) =
|
||||||
if logNs.isNil: logNs = getLoggerForNamespace(namespace = "fiber_orm", level = lvlNotice)
|
# namespaced_logging would do this check for us, but we don't want to even
|
||||||
logNs
|
# build the log object if we're not actually logging
|
||||||
|
if logService.isNone: return
|
||||||
|
var log = %*{ "method": methodName, "sql": sqlStmt }
|
||||||
|
for (k, v) in args: log[k] = %v
|
||||||
|
logService.getLogger("fiber_orm/query").debug(log)
|
||||||
|
|
||||||
|
proc enableDbLogging*(svc: LogService) =
|
||||||
|
logService = some(svc)
|
||||||
|
|
||||||
proc newMutateClauses(): MutateClauses =
|
proc newMutateClauses(): MutateClauses =
|
||||||
return MutateClauses(
|
return MutateClauses(
|
||||||
@ -351,7 +358,7 @@ proc createRecord*[D: DbConnType, T](db: D, rec: T): T =
|
|||||||
" VALUES (" & mc.placeholders.join(",") & ") " &
|
" VALUES (" & mc.placeholders.join(",") & ") " &
|
||||||
" RETURNING " & columnNamesForModel(rec).join(",")
|
" RETURNING " & columnNamesForModel(rec).join(",")
|
||||||
|
|
||||||
log().debug "createRecord: [" & sqlStmt & "]"
|
logQuery("createRecord", sqlStmt)
|
||||||
let newRow = db.getRow(sql(sqlStmt), mc.values)
|
let newRow = db.getRow(sql(sqlStmt), mc.values)
|
||||||
|
|
||||||
result = rowToModel(T, newRow)
|
result = rowToModel(T, newRow)
|
||||||
@ -367,7 +374,7 @@ proc updateRecord*[D: DbConnType, T](db: D, rec: T): bool =
|
|||||||
" SET " & setClause &
|
" SET " & setClause &
|
||||||
" WHERE id = ? "
|
" WHERE id = ? "
|
||||||
|
|
||||||
log().debug "updateRecord: [" & sqlStmt & "] id: " & $rec.id
|
logQuery("updateRecord", sqlStmt, [("id", $rec.id)])
|
||||||
let numRowsUpdated = db.execAffectedRows(sql(sqlStmt), mc.values.concat(@[$rec.id]))
|
let numRowsUpdated = db.execAffectedRows(sql(sqlStmt), mc.values.concat(@[$rec.id]))
|
||||||
|
|
||||||
return numRowsUpdated > 0;
|
return numRowsUpdated > 0;
|
||||||
@ -380,7 +387,7 @@ proc createOrUpdateRecord*[D: DbConnType, T](db: D, rec: T): T =
|
|||||||
## Note that this does not perform partial updates, all fields are updated.
|
## Note that this does not perform partial updates, all fields are updated.
|
||||||
|
|
||||||
let findRecordStmt = "SELECT id FROM " & tableName(rec) & " WHERE id = ?"
|
let findRecordStmt = "SELECT id FROM " & tableName(rec) & " WHERE id = ?"
|
||||||
log().debug "createOrUpdateRecord: [" & findRecordStmt & "] id: " & $rec.id
|
logQuery("createOrUpdateRecord", findRecordStmt, [("id", $rec.id)])
|
||||||
let rows = db.getAllRows(sql(findRecordStmt), [$rec.id])
|
let rows = db.getAllRows(sql(findRecordStmt), [$rec.id])
|
||||||
|
|
||||||
if rows.len == 0: result = createRecord(db, rec)
|
if rows.len == 0: result = createRecord(db, rec)
|
||||||
@ -393,7 +400,7 @@ proc createOrUpdateRecord*[D: DbConnType, T](db: D, rec: T): T =
|
|||||||
template deleteRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
|
template deleteRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
|
||||||
## Delete a record by id.
|
## Delete a record by id.
|
||||||
let sqlStmt = "DELETE FROM " & tableName(modelType) & " WHERE id = ?"
|
let sqlStmt = "DELETE FROM " & tableName(modelType) & " WHERE id = ?"
|
||||||
log().debug "deleteRecord: [" & sqlStmt & "] id: " & $id
|
logQuery("deleteRecord", sqlStmt, [("id", $id)])
|
||||||
db.tryExec(sql(sqlStmt), $id)
|
db.tryExec(sql(sqlStmt), $id)
|
||||||
|
|
||||||
proc deleteRecord*[D: DbConnType, T](db: D, rec: T): bool =
|
proc deleteRecord*[D: DbConnType, T](db: D, rec: T): bool =
|
||||||
@ -401,7 +408,7 @@ proc deleteRecord*[D: DbConnType, T](db: D, rec: T): bool =
|
|||||||
##
|
##
|
||||||
## .. _id: #model-class-id-field
|
## .. _id: #model-class-id-field
|
||||||
let sqlStmt = "DELETE FROM " & tableName(rec) & " WHERE id = ?"
|
let sqlStmt = "DELETE FROM " & tableName(rec) & " WHERE id = ?"
|
||||||
log().debug "deleteRecord: [" & sqlStmt & "] id: " & $rec.id
|
logQuery("deleteRecord", sqlStmt, [("id", $rec.id)])
|
||||||
return db.tryExec(sql(sqlStmt), $rec.id)
|
return db.tryExec(sql(sqlStmt), $rec.id)
|
||||||
|
|
||||||
template getRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
|
template getRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
|
||||||
@ -411,7 +418,7 @@ template getRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
|
|||||||
" FROM " & tableName(modelType) &
|
" FROM " & tableName(modelType) &
|
||||||
" WHERE id = ?"
|
" WHERE id = ?"
|
||||||
|
|
||||||
log().debug "getRecord: [" & sqlStmt & "] id: " & $id
|
logQuery("getRecord", sqlStmt, [("id", $id)])
|
||||||
let row = db.getRow(sql(sqlStmt), @[$id])
|
let row = db.getRow(sql(sqlStmt), @[$id])
|
||||||
|
|
||||||
if allIt(row, it.len == 0):
|
if allIt(row, it.len == 0):
|
||||||
@ -448,7 +455,7 @@ template findRecordsWhere*[D: DbConnType](
|
|||||||
fetchStmt &= " LIMIT " & $p.pageSize &
|
fetchStmt &= " LIMIT " & $p.pageSize &
|
||||||
" OFFSET " & $p.offset
|
" OFFSET " & $p.offset
|
||||||
|
|
||||||
log().debug "findRecordsWhere: [" & fetchStmt & "] values: (" & values.join(", ") & ")"
|
logQuery("findRecordsWhere", fetchStmt, [("values", values.join(", "))])
|
||||||
let records = db.getAllRows(sql(fetchStmt), values).mapIt(rowToModel(modelType, it))
|
let records = db.getAllRows(sql(fetchStmt), values).mapIt(rowToModel(modelType, it))
|
||||||
|
|
||||||
PagedRecords[modelType](
|
PagedRecords[modelType](
|
||||||
@ -480,7 +487,7 @@ template getAllRecords*[D: DbConnType](
|
|||||||
fetchStmt &= " LIMIT " & $p.pageSize &
|
fetchStmt &= " LIMIT " & $p.pageSize &
|
||||||
" OFFSET " & $p.offset
|
" OFFSET " & $p.offset
|
||||||
|
|
||||||
log().debug "getAllRecords: [" & fetchStmt & "]"
|
logQuery("getAllRecords", fetchStmt)
|
||||||
let records = db.getAllRows(sql(fetchStmt)).mapIt(rowToModel(modelType, it))
|
let records = db.getAllRows(sql(fetchStmt)).mapIt(rowToModel(modelType, it))
|
||||||
|
|
||||||
PagedRecords[modelType](
|
PagedRecords[modelType](
|
||||||
@ -520,7 +527,7 @@ template findRecordsBy*[D: DbConnType](
|
|||||||
fetchStmt &= " LIMIT " & $p.pageSize &
|
fetchStmt &= " LIMIT " & $p.pageSize &
|
||||||
" OFFSET " & $p.offset
|
" OFFSET " & $p.offset
|
||||||
|
|
||||||
log().debug "findRecordsBy: [" & fetchStmt & "] values (" & values.join(", ") & ")"
|
logQuery("findRecordsBy", fetchStmt, [("values", values.join(", "))])
|
||||||
let records = db.getAllRows(sql(fetchStmt), values).mapIt(rowToModel(modelType, it))
|
let records = db.getAllRows(sql(fetchStmt), values).mapIt(rowToModel(modelType, it))
|
||||||
|
|
||||||
PagedRecords[modelType](
|
PagedRecords[modelType](
|
||||||
|
@ -4,13 +4,12 @@
|
|||||||
|
|
||||||
## Simple database connection pooling implementation compatible with Fiber ORM.
|
## Simple database connection pooling implementation compatible with Fiber ORM.
|
||||||
|
|
||||||
import std/[logging, sequtils, strutils, sugar]
|
import std/[sequtils, strutils, sugar]
|
||||||
import db_connector/db_common
|
import db_connector/db_common
|
||||||
|
|
||||||
from db_connector/db_sqlite import getRow, close
|
from db_connector/db_sqlite import getRow, close
|
||||||
from db_connector/db_postgres import getRow, close
|
from db_connector/db_postgres import getRow, close
|
||||||
|
|
||||||
import namespaced_logging
|
|
||||||
import ./db_common as fiber_db_common
|
import ./db_common as fiber_db_common
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -44,20 +43,12 @@ type
|
|||||||
cfg: DbConnPoolConfig[D]
|
cfg: DbConnPoolConfig[D]
|
||||||
lastId: int
|
lastId: int
|
||||||
|
|
||||||
var logNs {.threadvar.}: LoggingNamespace
|
|
||||||
|
|
||||||
template log(): untyped =
|
|
||||||
if logNs.isNil: logNs = getLoggerForNamespace(namespace = "fiber_orm/pool", level = lvlNotice)
|
|
||||||
logNs
|
|
||||||
|
|
||||||
proc initDbConnPool*[D: DbConnType](cfg: DbConnPoolConfig[D]): DbConnPool[D] =
|
proc initDbConnPool*[D: DbConnType](cfg: DbConnPoolConfig[D]): DbConnPool[D] =
|
||||||
log().debug("Initializing new pool (size: " & $cfg.poolSize)
|
|
||||||
result = DbConnPool[D](
|
result = DbConnPool[D](
|
||||||
conns: @[],
|
conns: @[],
|
||||||
cfg: cfg)
|
cfg: cfg)
|
||||||
|
|
||||||
proc newConn[D: DbConnType](pool: DbConnPool[D]): PooledDbConn[D] =
|
proc newConn[D: DbConnType](pool: DbConnPool[D]): PooledDbConn[D] =
|
||||||
log().debug("Creating a new connection to add to the pool.")
|
|
||||||
pool.lastId += 1
|
pool.lastId += 1
|
||||||
{.gcsafe.}:
|
{.gcsafe.}:
|
||||||
let conn = pool.cfg.connect()
|
let conn = pool.cfg.connect()
|
||||||
@ -68,7 +59,6 @@ proc newConn[D: DbConnType](pool: DbConnPool[D]): PooledDbConn[D] =
|
|||||||
pool.conns.add(result)
|
pool.conns.add(result)
|
||||||
|
|
||||||
proc maintain[D: DbConnType](pool: DbConnPool[D]): void =
|
proc maintain[D: DbConnType](pool: DbConnPool[D]): void =
|
||||||
log().debug("Maintaining pool. $# connections." % [$pool.conns.len])
|
|
||||||
pool.conns.keepIf(proc (pc: PooledDbConn[D]): bool =
|
pool.conns.keepIf(proc (pc: PooledDbConn[D]): bool =
|
||||||
if not pc.free: return true
|
if not pc.free: return true
|
||||||
|
|
||||||
@ -80,9 +70,6 @@ proc maintain[D: DbConnType](pool: DbConnPool[D]): void =
|
|||||||
except: discard ""
|
except: discard ""
|
||||||
return false
|
return false
|
||||||
)
|
)
|
||||||
log().debug(
|
|
||||||
"Pruned dead connections. $# connections remaining." %
|
|
||||||
[$pool.conns.len])
|
|
||||||
|
|
||||||
let freeConns = pool.conns.filterIt(it.free)
|
let freeConns = pool.conns.filterIt(it.free)
|
||||||
if pool.conns.len > pool.cfg.poolSize and freeConns.len > 0:
|
if pool.conns.len > pool.cfg.poolSize and freeConns.len > 0:
|
||||||
@ -94,9 +81,6 @@ proc maintain[D: DbConnType](pool: DbConnPool[D]): void =
|
|||||||
for culled in toCull:
|
for culled in toCull:
|
||||||
try: culled.conn.close()
|
try: culled.conn.close()
|
||||||
except: discard ""
|
except: discard ""
|
||||||
log().debug(
|
|
||||||
"Trimming pool size. Culled $# free connections. $# connections remaining." %
|
|
||||||
[$toCull.len, $pool.conns.len])
|
|
||||||
|
|
||||||
proc take*[D: DbConnType](pool: DbConnPool[D]): tuple[id: int, conn: D] =
|
proc take*[D: DbConnType](pool: DbConnPool[D]): tuple[id: int, conn: D] =
|
||||||
## Request a connection from the pool. Returns a DbConn if the pool has free
|
## Request a connection from the pool. Returns a DbConn if the pool has free
|
||||||
@ -109,20 +93,15 @@ proc take*[D: DbConnType](pool: DbConnPool[D]): tuple[id: int, conn: D] =
|
|||||||
pool.maintain
|
pool.maintain
|
||||||
let freeConns = pool.conns.filterIt(it.free)
|
let freeConns = pool.conns.filterIt(it.free)
|
||||||
|
|
||||||
log().debug(
|
|
||||||
"Providing a new connection ($# currently free)." % [$freeConns.len])
|
|
||||||
|
|
||||||
let reserved =
|
let reserved =
|
||||||
if freeConns.len > 0: freeConns[0]
|
if freeConns.len > 0: freeConns[0]
|
||||||
else: pool.newConn()
|
else: pool.newConn()
|
||||||
|
|
||||||
reserved.free = false
|
reserved.free = false
|
||||||
log().debug("Reserve connection $#" % [$reserved.id])
|
|
||||||
return (id: reserved.id, conn: reserved.conn)
|
return (id: reserved.id, conn: reserved.conn)
|
||||||
|
|
||||||
proc release*[D: DbConnType](pool: DbConnPool[D], connId: int): void =
|
proc release*[D: DbConnType](pool: DbConnPool[D], connId: int): void =
|
||||||
## Release a connection back to the pool.
|
## Release a connection back to the pool.
|
||||||
log().debug("Reclaiming released connaction $#" % [$connId])
|
|
||||||
let foundConn = pool.conns.filterIt(it.id == connId)
|
let foundConn = pool.conns.filterIt(it.id == connId)
|
||||||
if foundConn.len > 0: foundConn[0].free = true
|
if foundConn.len > 0: foundConn[0].free = true
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user