Compare commits

..

No commits in common. "main" and "2.1.0" have entirely different histories.
main ... 2.1.0

6 changed files with 129 additions and 141 deletions

View File

@ -57,7 +57,7 @@ Models may be defined as:
.. code-block:: Nim .. code-block:: Nim
# models.nim # models.nim
import std/[options, times] import std/options, std/times
import uuids import uuids
type type
@ -82,8 +82,6 @@ Using Fiber ORM we can generate a data access layer with:
.. code-block:: Nim .. code-block:: Nim
# db.nim # db.nim
import std/[options]
import db_connectors/db_postgres
import fiber_orm import fiber_orm
import ./models.nim import ./models.nim
@ -104,7 +102,6 @@ This will generate the following procedures:
.. code-block:: Nim .. code-block:: Nim
proc getTodoItem*(db: TodoDB, id: UUID): TodoItem; proc getTodoItem*(db: TodoDB, id: UUID): TodoItem;
proc getTodoItemIfItExists*(db: TodoDB, id: UUID): Option[TodoItem];
proc getAllTodoItems*(db: TodoDB): seq[TodoItem]; proc getAllTodoItems*(db: TodoDB): seq[TodoItem];
proc createTodoItem*(db: TodoDB, rec: TodoItem): TodoItem; proc createTodoItem*(db: TodoDB, rec: TodoItem): TodoItem;
proc updateTodoItem*(db: TodoDB, rec: TodoItem): bool; proc updateTodoItem*(db: TodoDB, rec: TodoItem): bool;
@ -115,7 +112,6 @@ This will generate the following procedures:
values: varargs[string, dbFormat]): seq[TodoItem]; values: varargs[string, dbFormat]): seq[TodoItem];
proc getTimeEntry*(db: TodoDB, id: UUID): TimeEntry; proc getTimeEntry*(db: TodoDB, id: UUID): TimeEntry;
proc getTimeEntryIfItExists*(db: TodoDB, id: UUID): Option[TimeEntry];
proc getAllTimeEntries*(db: TodoDB): seq[TimeEntry]; proc getAllTimeEntries*(db: TodoDB): seq[TimeEntry];
proc createTimeEntry*(db: TodoDB, rec: TimeEntry): TimeEntry; proc createTimeEntry*(db: TodoDB, rec: TimeEntry): TimeEntry;
proc updateTimeEntry*(db: TodoDB, rec: TimeEntry): bool; proc updateTimeEntry*(db: TodoDB, rec: TimeEntry): bool;

View File

@ -1,6 +1,6 @@
# Package # Package
version = "3.1.1" version = "2.1.0"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Lightweight Postgres ORM for Nim." description = "Lightweight Postgres ORM for Nim."
license = "GPL-3.0" license = "GPL-3.0"
@ -11,4 +11,4 @@ srcDir = "src"
# Dependencies # Dependencies
requires @["nim >= 1.4.0", "uuids"] requires @["nim >= 1.4.0", "uuids"]
requires "namespaced_logging >= 1.0.0" requires "https://git.jdb-software.com/jdb/nim-namespaced-logging.git"

View File

@ -1,6 +1,6 @@
# Fiber ORM # Fiber ORM
# #
# Copyright 2019-2024 Jonathan Bernard <jonathan@jdbernard.com> # Copyright 2019 Jonathan Bernard <jonathan@jdbernard.com>
## Lightweight ORM supporting the `Postgres`_ and `SQLite`_ databases in Nim. ## Lightweight ORM supporting the `Postgres`_ and `SQLite`_ databases in Nim.
## It supports a simple, opinionated model mapper to generate SQL queries based ## It supports a simple, opinionated model mapper to generate SQL queries based
@ -107,7 +107,6 @@
## ##
## proc createTodoItem*(db: TodoDB, rec: TodoItem): TodoItem; ## proc createTodoItem*(db: TodoDB, rec: TodoItem): TodoItem;
## proc updateTodoItem*(db: TodoDB, rec: TodoItem): bool; ## proc updateTodoItem*(db: TodoDB, rec: TodoItem): bool;
## proc createOrUpdateTodoItem*(db: TodoDB, rec: TodoItem): bool;
## proc deleteTodoItem*(db: TodoDB, rec: TodoItem): bool; ## proc deleteTodoItem*(db: TodoDB, rec: TodoItem): bool;
## proc deleteTodoItem*(db: TodoDB, id: UUID): bool; ## proc deleteTodoItem*(db: TodoDB, id: UUID): bool;
## ##
@ -264,28 +263,26 @@
## In the example above the `pool.DbConnPool`_ object is used as database ## In the example above the `pool.DbConnPool`_ object is used as database
## object type (aliased as `TodoDB`). This is the intended usage pattern, but ## object type (aliased as `TodoDB`). This is the intended usage pattern, but
## anything can be passed as the database object type so long as there is a ## anything can be passed as the database object type so long as there is a
## defined `withConnection` template that provides a `conn: DbConn` object ## defined `withConn` template that provides an injected `conn: DbConn` object
## to the provided statement body. ## to the provided statement body.
## ##
## For example, a valid database object implementation that opens a new ## For example, a valid database object implementation that opens a new
## connection for every request might look like this: ## connection for every request might look like this:
## ##
## .. code-block:: Nim ## .. code-block:: Nim
## import db_connector/db_postgres ## import std/db_postgres
## ##
## type TodoDB* = object ## type TodoDB* = object
## connString: string ## connString: string
## ##
## template withConnection*(db: TodoDB, stmt: untyped): untyped = ## template withConn*(db: TodoDB, stmt: untyped): untyped =
## block: ## let conn {.inject.} = open("", "", "", db.connString)
## let conn = open("", "", "", db.connString)
## try: stmt ## try: stmt
## finally: close(conn) ## finally: close(conn)
## ##
## .. _pool.DbConnPool: fiber_orm/pool.html#DbConnPool ## .. _pool.DbConnPool: fiber_orm/pool.html#DbConnPool
## ##
import std/[json, macros, options, sequtils, strutils] import std/[db_common, logging, macros, options, sequtils, strutils]
import db_connector/db_common
import namespaced_logging, uuids import namespaced_logging, uuids
from std/unicode import capitalize from std/unicode import capitalize
@ -294,34 +291,35 @@ import ./fiber_orm/db_common as fiber_db_common
import ./fiber_orm/pool import ./fiber_orm/pool
import ./fiber_orm/util import ./fiber_orm/util
export pool, util export
pool,
util.columnNamesForModel,
util.dbFormat,
util.dbNameToIdent,
util.identNameToDb,
util.modelName,
util.rowToModel,
util.tableName
type type
PaginationParams* = object
pageSize*: int
offset*: int
orderBy*: Option[string]
PagedRecords*[T] = object PagedRecords*[T] = object
pagination*: Option[PaginationParams] pagination*: Option[PaginationParams]
records*: seq[T] records*: seq[T]
totalRecords*: int totalRecords*: int
DbUpdateError* = object of CatchableError ##\
## Error types raised when a DB modification fails.
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 logService {.threadvar.}: Option[LogService] var logNs {.threadvar.}: LoggingNamespace
var logger {.threadvar.}: Option[Logger]
proc logQuery*(methodName: string, sqlStmt: string, args: openArray[(string, string)] = []) = template log(): untyped =
# namespaced_logging would do this check for us, but we don't want to even if logNs.isNil: logNs = getLoggerForNamespace(namespace = "fiber_orm", level = lvlNotice)
# build the log object if we're not actually logging logNs
if logService.isNone: return
if logger.isNone: logger = logService.getLogger("fiber_orm/query")
var log = %*{ "method": methodName, "sql": sqlStmt }
for (k, v) in args: log[k] = %v
logger.debug(log)
proc enableDbLogging*(svc: LogService) =
logService = some(svc)
proc newMutateClauses(): MutateClauses = proc newMutateClauses(): MutateClauses =
return MutateClauses( return MutateClauses(
@ -347,7 +345,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(",")
logQuery("createRecord", sqlStmt) log().debug "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)
@ -363,33 +361,15 @@ proc updateRecord*[D: DbConnType, T](db: D, rec: T): bool =
" SET " & setClause & " SET " & setClause &
" WHERE id = ? " " WHERE id = ? "
logQuery("updateRecord", sqlStmt, [("id", $rec.id)]) log().debug "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;
proc createOrUpdateRecord*[D: DbConnType, T](db: D, rec: T): T =
## Create or update a record. `rec` is expected to be a `model class`_. If
## the `id` field is unset, or if there is no existing record with the given
## id, a new record is inserted. Otherwise, the existing record is updated.
##
## Note that this does not perform partial updates, all fields are updated.
let findRecordStmt = "SELECT id FROM " & tableName(rec) & " WHERE id = ?"
logQuery("createOrUpdateRecord", findRecordStmt, [("id", $rec.id)])
let rows = db.getAllRows(sql(findRecordStmt), [$rec.id])
if rows.len == 0: result = createRecord(db, rec)
else:
result = rec
if not updateRecord(db, rec):
raise newException(DbUpdateError,
"unable to update " & modelName(rec) & " for id " & $rec.id)
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 = ?"
logQuery("deleteRecord", sqlStmt, [("id", $id)]) log().debug "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 =
@ -397,7 +377,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 = ?"
logQuery("deleteRecord", sqlStmt, [("id", $rec.id)]) log().debug "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 =
@ -407,7 +387,7 @@ template getRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
" FROM " & tableName(modelType) & " FROM " & tableName(modelType) &
" WHERE id = ?" " WHERE id = ?"
logQuery("getRecord", sqlStmt, [("id", $id)]) log().debug "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):
@ -433,9 +413,17 @@ template findRecordsWhere*[D: DbConnType](
"SELECT COUNT(*) FROM " & tableName(modelType) & "SELECT COUNT(*) FROM " & tableName(modelType) &
" WHERE " & whereClause " WHERE " & whereClause
if page.isSome: fetchStmt &= getPagingClause(page.get) if page.isSome:
let p = page.get
if p.orderBy.isSome:
fetchStmt &= " ORDER BY " & p.orderBy.get
else:
fetchStmt &= " ORDER BY id"
logQuery("findRecordsWhere", fetchStmt, [("values", values.join(", "))]) fetchStmt &= " LIMIT " & $p.pageSize &
" OFFSET " & $p.offset
log().debug "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](
@ -456,9 +444,17 @@ template getAllRecords*[D: DbConnType](
var countStmt = "SELECT COUNT(*) FROM " & tableName(modelType) var countStmt = "SELECT COUNT(*) FROM " & tableName(modelType)
if page.isSome: fetchStmt &= getPagingClause(page.get) if page.isSome:
let p = page.get
if p.orderBy.isSome:
fetchStmt &= " ORDER BY " & p.orderBy.get
else:
fetchStmt &= " ORDER BY id"
logQuery("getAllRecords", fetchStmt) fetchStmt &= " LIMIT " & $p.pageSize &
" OFFSET " & $p.offset
log().debug "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](
@ -487,9 +483,17 @@ template findRecordsBy*[D: DbConnType](
"SELECT COUNT(*) FROM " & tableName(modelType) & "SELECT COUNT(*) FROM " & tableName(modelType) &
" WHERE " & whereClause " WHERE " & whereClause
if page.isSome: fetchStmt &= getPagingClause(page.get) if page.isSome:
let p = page.get
if p.orderBy.isSome:
fetchStmt &= " ORDER BY " & p.orderBy.get
else:
fetchStmt &= " ORDER BY id"
logQuery("findRecordsBy", fetchStmt, [("values", values.join(", "))]) fetchStmt &= " LIMIT " & $p.pageSize &
" OFFSET " & $p.offset
log().debug "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](
@ -512,12 +516,11 @@ macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untype
## proc deleteTodoItem*(db: TodoDB, rec: TodoItem): bool; ## proc deleteTodoItem*(db: TodoDB, rec: TodoItem): bool;
## proc deleteTodoItem*(db: TodoDB, id: idType): bool; ## proc deleteTodoItem*(db: TodoDB, id: idType): bool;
## proc updateTodoItem*(db: TodoDB, rec: TodoItem): bool; ## proc updateTodoItem*(db: TodoDB, rec: TodoItem): bool;
## proc createOrUpdateTodoItem*(db: TodoDB, rec: TodoItem): bool;
## ##
## proc findTodoItemsWhere*( ## proc findTodoItemsWhere*(
## db: TodoDB, whereClause: string, values: varargs[string]): TodoItem; ## db: TodoDB, whereClause: string, values: varargs[string]): TodoItem;
## ##
## `dbType` is expected to be some type that has a defined `withConnection` ## `dbType` is expected to be some type that has a defined `withConn`
## procedure (see `Database Object`_ for details). ## procedure (see `Database Object`_ for details).
## ##
## .. _Database Object: #database-object ## .. _Database Object: #database-object
@ -530,48 +533,38 @@ macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untype
let modelName = $(t.getType[1]) let modelName = $(t.getType[1])
let getName = ident("get" & modelName) let getName = ident("get" & modelName)
let getIfExistsName = ident("get" & modelName & "IfItExists")
let getAllName = ident("getAll" & pluralize(modelName)) let getAllName = ident("getAll" & pluralize(modelName))
let findWhereName = ident("find" & pluralize(modelName) & "Where") let findWhereName = ident("find" & pluralize(modelName) & "Where")
let createName = ident("create" & modelName) let createName = ident("create" & modelName)
let updateName = ident("update" & modelName) let updateName = ident("update" & modelName)
let createOrUpdateName = ident("createOrUpdate" & modelName)
let deleteName = ident("delete" & modelName) let deleteName = ident("delete" & modelName)
let idType = typeOfColumn(t, "id") let idType = typeOfColumn(t, "id")
result.add quote do: result.add quote do:
proc `getName`*(db: `dbType`, id: `idType`): `t` = proc `getName`*(db: `dbType`, id: `idType`): `t` =
db.withConnection conn: result = getRecord(conn, `t`, id) db.withConn: result = getRecord(conn, `t`, id)
proc `getIfExistsName`*(db: `dbType`, id: `idType`): Option[`t`] =
db.withConnection conn:
try: result = some(getRecord(conn, `t`, id))
except NotFoundError: result = none[`t`]()
proc `getAllName`*(db: `dbType`, pagination = none[PaginationParams]()): PagedRecords[`t`] = proc `getAllName`*(db: `dbType`, pagination = none[PaginationParams]()): PagedRecords[`t`] =
db.withConnection conn: result = getAllRecords(conn, `t`, pagination) db.withConn: result = getAllRecords(conn, `t`, pagination)
proc `findWhereName`*( proc `findWhereName`*(
db: `dbType`, db: `dbType`,
whereClause: string, whereClause: string,
values: varargs[string, dbFormat], values: varargs[string, dbFormat],
pagination = none[PaginationParams]()): PagedRecords[`t`] = pagination = none[PaginationParams]()): PagedRecords[`t`] =
db.withConnection conn: db.withConn:
result = findRecordsWhere(conn, `t`, whereClause, values, pagination) result = findRecordsWhere(conn, `t`, whereClause, values, pagination)
proc `createName`*(db: `dbType`, rec: `t`): `t` = proc `createName`*(db: `dbType`, rec: `t`): `t` =
db.withConnection conn: result = createRecord(conn, rec) db.withConn: result = createRecord(conn, rec)
proc `updateName`*(db: `dbType`, rec: `t`): bool = proc `updateName`*(db: `dbType`, rec: `t`): bool =
db.withConnection conn: result = updateRecord(conn, rec) db.withConn: result = updateRecord(conn, rec)
proc `createOrUpdateName`*(db: `dbType`, rec: `t`): `t` =
db.inTransaction: result = createOrUpdateRecord(conn, rec)
proc `deleteName`*(db: `dbType`, rec: `t`): bool = proc `deleteName`*(db: `dbType`, rec: `t`): bool =
db.withConnection conn: result = deleteRecord(conn, rec) db.withConn: result = deleteRecord(conn, rec)
proc `deleteName`*(db: `dbType`, id: `idType`): bool = proc `deleteName`*(db: `dbType`, id: `idType`): bool =
db.withConnection conn: result = deleteRecord(conn, `t`, id) db.withConn: result = deleteRecord(conn, `t`, id)
macro generateLookup*(dbType: type, modelType: type, fields: seq[string]): untyped = macro generateLookup*(dbType: type, modelType: type, fields: seq[string]): untyped =
## Create a lookup procedure for a given set of field names. For example, ## Create a lookup procedure for a given set of field names. For example,
@ -591,7 +584,7 @@ macro generateLookup*(dbType: type, modelType: type, fields: seq[string]): untyp
# Create proc skeleton # Create proc skeleton
result = quote do: result = quote do:
proc `procName`*(db: `dbType`): PagedRecords[`modelType`] = proc `procName`*(db: `dbType`): PagedRecords[`modelType`] =
db.withConnection conn: result = findRecordsBy(conn, `modelType`) db.withConn: result = findRecordsBy(conn, `modelType`)
var callParams = quote do: @[] var callParams = quote do: @[]
@ -615,11 +608,11 @@ macro generateLookup*(dbType: type, modelType: type, fields: seq[string]): untyp
# Add the call params to the inner procedure call # Add the call params to the inner procedure call
# result[6][0][1][0][1] is # result[6][0][1][0][1] is
# ProcDef -> [6]: StmtList (body) -> [0]: Command -> # ProcDef -> [6]: StmtList (body) -> [0]: Call ->
# [2]: StmtList (withConnection body) -> [0]: Asgn (result =) -> # [1]: StmtList (withConn body) -> [0]: Asgn (result =) ->
# [1]: Call (inner findRecords invocation) # [1]: Call (inner findRecords invocation)
result[6][0][2][0][1].add(callParams) result[6][0][1][0][1].add(callParams)
result[6][0][2][0][1].add(quote do: pagination) result[6][0][1][0][1].add(quote do: pagination)
macro generateProcsForFieldLookups*(dbType: type, modelsAndFields: openarray[tuple[t: type, fields: seq[string]]]): untyped = macro generateProcsForFieldLookups*(dbType: type, modelsAndFields: openarray[tuple[t: type, fields: seq[string]]]): untyped =
result = newStmtList() result = newStmtList()
@ -633,7 +626,7 @@ macro generateProcsForFieldLookups*(dbType: type, modelsAndFields: openarray[tup
# Create proc skeleton # Create proc skeleton
let procDefAST = quote do: let procDefAST = quote do:
proc `procName`*(db: `dbType`): PagedRecords[`modelType`] = proc `procName`*(db: `dbType`): PagedRecords[`modelType`] =
db.withConnection conn: result = findRecordsBy(conn, `modelType`) db.withConn: result = findRecordsBy(conn, `modelType`)
var callParams = quote do: @[] var callParams = quote do: @[]
@ -685,8 +678,8 @@ proc initPool*[D: DbConnType](
hardCap: hardCap, hardCap: hardCap,
healthCheckQuery: healthCheckQuery)) healthCheckQuery: healthCheckQuery))
template inTransaction*(db, body: untyped) = template inTransaction*[D: DbConnType](db: DbConnPool[D], body: untyped) =
db.withConnection conn: pool.withConn(db):
conn.exec(sql"BEGIN TRANSACTION") conn.exec(sql"BEGIN TRANSACTION")
try: try:
body body

View File

@ -1,3 +1,3 @@
import db_connector/[db_postgres, db_sqlite] import std/[db_postgres, db_sqlite]
type DbConnType* = db_postgres.DbConn or db_sqlite.DbConn type DbConnType* = db_postgres.DbConn or db_sqlite.DbConn

View File

@ -4,12 +4,12 @@
## Simple database connection pooling implementation compatible with Fiber ORM. ## Simple database connection pooling implementation compatible with Fiber ORM.
import std/[sequtils, strutils, sugar] import std/[db_common, logging, sequtils, strutils, sugar]
import db_connector/db_common
from db_connector/db_sqlite import getRow, close from std/db_sqlite import getRow
from db_connector/db_postgres import getRow, close from std/db_postgres import getRow
import namespaced_logging
import ./db_common as fiber_db_common import ./db_common as fiber_db_common
type type
@ -43,14 +43,21 @@ 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.}:
let conn = pool.cfg.connect() let conn = pool.cfg.connect()
result = PooledDbConn[D]( result = PooledDbConn[D](
conn: conn, conn: conn,
@ -59,6 +66,7 @@ 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
@ -70,6 +78,9 @@ 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:
@ -81,6 +92,9 @@ 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
@ -93,22 +107,29 @@ 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
template withConnection*[D: DbConnType](pool: DbConnPool[D], conn, stmt: untyped): untyped = template withConn*[D: DbConnType](pool: DbConnPool[D], stmt: untyped): untyped =
## Convenience template to provide a connection from the pool for use in a ## Convenience template to provide a connection from the pool for use in a
## statement block, automatically releasing that connnection when done. ## statement block, automatically releasing that connnection when done.
block: ##
let (connId, conn) = take(pool) ## The provided connection is injected as the variable `conn` in the
## statement body.
let (connId, conn {.inject.}) = take(pool)
try: stmt try: stmt
finally: release(pool, connId) finally: release(pool, connId)

View File

@ -9,11 +9,6 @@ import uuids
import std/nre except toSeq import std/nre except toSeq
type type
PaginationParams* = object
pageSize*: int
offset*: int
orderBy*: Option[seq[string]]
MutateClauses* = object MutateClauses* = object
## Data structure to hold information about the clauses that should be ## Data structure to hold information about the clauses that should be
## added to a query. How these clauses are used will depend on the query. ## added to a query. How these clauses are used will depend on the query.
@ -27,11 +22,9 @@ const ISO_8601_FORMATS = @[
"yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ssz",
"yyyy-MM-dd'T'HH:mm:sszzz", "yyyy-MM-dd'T'HH:mm:sszzz",
"yyyy-MM-dd'T'HH:mm:ss'.'fffzzz", "yyyy-MM-dd'T'HH:mm:ss'.'fffzzz",
"yyyy-MM-dd'T'HH:mm:ss'.'ffffzzz",
"yyyy-MM-dd HH:mm:ssz", "yyyy-MM-dd HH:mm:ssz",
"yyyy-MM-dd HH:mm:sszzz", "yyyy-MM-dd HH:mm:sszzz",
"yyyy-MM-dd HH:mm:ss'.'fffzzz", "yyyy-MM-dd HH:mm:ss'.'fffzzz"
"yyyy-MM-dd HH:mm:ss'.'ffffzzz"
] ]
proc parseIso8601(val: string): DateTime = proc parseIso8601(val: string): DateTime =
@ -109,7 +102,7 @@ proc dbFormat*[T](list: seq[T]): string =
proc dbFormat*[T](item: T): string = proc dbFormat*[T](item: T): string =
## For all other types, fall back on a defined `$` function to create a ## For all other types, fall back on a defined `$` function to create a
## string version of the value we can include in an SQL query. ## string version of the value we can include in an SQL query>
return $item return $item
type DbArrayParseState = enum type DbArrayParseState = enum
@ -133,20 +126,18 @@ proc parsePGDatetime*(val: string): DateTime =
var correctedVal = val; var correctedVal = val;
# The Nim `times#format` function only recognizes 3-digit millisecond values # PostgreSQL will truncate any trailing 0's in the millisecond value leading
# but PostgreSQL will sometimes send 1-2 digits, truncating any trailing 0's, # to values like `2020-01-01 16:42.3+00`. This cannot currently be parsed by
# or sometimes provide more than three digits of preceision in the millisecond value leading # the standard times format as it expects exactly three digits for
# to values like `2020-01-01 16:42.3+00` or `2025-01-06 00:56:00.9007+00`. # millisecond values. So we have to detect this and pad out the millisecond
# This cannot currently be parsed by the standard times format as it expects # value to 3 digits.
# exactly three digits for millisecond values. So we have to detect this and let PG_PARTIAL_FORMAT_REGEX = re"(\d{4}-\d{2}-\d{2}( |'T')\d{2}:\d{2}:\d{2}\.)(\d{1,2})(\S+)?"
# coerce the millisecond value to exactly 3 digits.
let PG_PARTIAL_FORMAT_REGEX = re"(\d{4}-\d{2}-\d{2}( |'T')\d{2}:\d{2}:\d{2}\.)(\d+)(\S+)?"
let match = val.match(PG_PARTIAL_FORMAT_REGEX) let match = val.match(PG_PARTIAL_FORMAT_REGEX)
if match.isSome: if match.isSome:
let c = match.get.captures let c = match.get.captures
if c.toSeq.len == 2: correctedVal = c[0] & alignLeft(c[2], 3, '0')[0..2] if c.toSeq.len == 2: correctedVal = c[0] & alignLeft(c[2], 3, '0')
else: correctedVal = c[0] & alignLeft(c[2], 3, '0')[0..2] & c[3] else: correctedVal = c[0] & alignLeft(c[2], 3, '0') & c[3]
var errStr = "" var errStr = ""
@ -155,7 +146,7 @@ proc parsePGDatetime*(val: string): DateTime =
try: return correctedVal.parse(df) try: return correctedVal.parse(df)
except: errStr &= "\n\t" & getCurrentExceptionMsg() except: errStr &= "\n\t" & getCurrentExceptionMsg()
raise newException(ValueError, "Cannot parse PG date '" & correctedVal & "'. Tried:" & errStr) raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
proc parseDbArray*(val: string): seq[string] = proc parseDbArray*(val: string): seq[string] =
## Parse a Postgres array column into a Nim seq[string] ## Parse a Postgres array column into a Nim seq[string]
@ -456,19 +447,6 @@ macro populateMutateClauses*(t: typed, newRecord: bool, mc: var MutateClauses):
`mc`.placeholders.add("?") `mc`.placeholders.add("?")
`mc`.values.add(dbFormat(`t`.`fieldIdent`)) `mc`.values.add(dbFormat(`t`.`fieldIdent`))
proc getPagingClause*(page: PaginationParams): string =
## Given a `PaginationParams` object, return the SQL clause necessary to
## limit the number of records returned by a query.
result = ""
if page.orderBy.isSome:
let orderByClause = page.orderBy.get.map(identNameToDb).join(",")
result &= " ORDER BY " & orderByClause
else:
result &= " ORDER BY id"
result &= " LIMIT " & $page.pageSize & " OFFSET " & $page.offset
## .. _model class: ../fiber_orm.html#objectminusrelational-modeling-model-class ## .. _model class: ../fiber_orm.html#objectminusrelational-modeling-model-class
## .. _rules for name mapping: ../fiber_orm.html ## .. _rules for name mapping: ../fiber_orm.html
## .. _table name: ../fiber_orm.html ## .. _table name: ../fiber_orm.html