2022-02-07 10:48:10 -06:00
|
|
|
import std/db_postgres, std/macros, std/options, std/sequtils, std/strutils
|
|
|
|
import namespaced_logging, uuids
|
2019-12-25 18:07:23 -06:00
|
|
|
|
2022-02-07 10:48:10 -06:00
|
|
|
from std/unicode import capitalize
|
2019-12-25 18:07:23 -06:00
|
|
|
|
2022-02-07 10:48:10 -06:00
|
|
|
import ./fiber_orm/pool
|
2019-12-25 18:51:00 -06:00
|
|
|
import ./fiber_orm/util
|
2022-02-07 10:48:10 -06:00
|
|
|
|
2021-08-02 05:54:56 -05:00
|
|
|
export
|
2022-02-07 10:48:10 -06:00
|
|
|
pool,
|
2021-08-02 05:54:56 -05:00
|
|
|
util.columnNamesForModel,
|
|
|
|
util.dbFormat,
|
|
|
|
util.dbNameToIdent,
|
|
|
|
util.identNameToDb,
|
|
|
|
util.modelName,
|
|
|
|
util.rowToModel,
|
|
|
|
util.tableName
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
type NotFoundError* = object of CatchableError
|
|
|
|
|
2022-01-22 20:20:02 -06:00
|
|
|
var logNs {.threadvar.}: LoggingNamespace
|
|
|
|
|
|
|
|
template log(): untyped =
|
2022-02-07 10:48:10 -06:00
|
|
|
if logNs.isNil: logNs = initLoggingNamespace(name = "fiber_orm", level = lvlNotice)
|
2022-01-22 20:20:02 -06:00
|
|
|
logNs
|
2022-01-13 14:45:16 -06:00
|
|
|
|
2019-12-25 18:07:23 -06:00
|
|
|
proc newMutateClauses(): MutateClauses =
|
|
|
|
return MutateClauses(
|
|
|
|
columns: @[],
|
|
|
|
placeholders: @[],
|
|
|
|
values: @[])
|
|
|
|
|
|
|
|
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.
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt =
|
2019-12-25 18:07:23 -06:00
|
|
|
"INSERT INTO " & tableName(rec) &
|
|
|
|
" (" & mc.columns.join(",") & ") " &
|
|
|
|
" VALUES (" & mc.placeholders.join(",") & ") " &
|
2022-01-13 14:45:16 -06:00
|
|
|
" RETURNING *"
|
|
|
|
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "createRecord: [" & sqlStmt & "]"
|
2022-01-13 14:45:16 -06:00
|
|
|
let newRow = db.getRow(sql(sqlStmt), mc.values)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
result = rowToModel(T, newRow)
|
2021-04-02 13:58:08 -05:00
|
|
|
|
2019-12-25 18:07:23 -06:00
|
|
|
proc updateRecord*[T](db: DbConn, rec: T): bool =
|
|
|
|
var mc = newMutateClauses()
|
|
|
|
populateMutateClauses(rec, false, mc)
|
|
|
|
|
2021-04-02 13:58:08 -05:00
|
|
|
let setClause = zip(mc.columns, mc.placeholders).mapIt(it[0] & " = " & it[1]).join(",")
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt =
|
2019-12-25 18:07:23 -06:00
|
|
|
"UPDATE " & tableName(rec) &
|
|
|
|
" SET " & setClause &
|
2022-01-13 14:45:16 -06:00
|
|
|
" WHERE id = ? "
|
|
|
|
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "updateRecord: [" & sqlStmt & "] id: " & $rec.id
|
2022-01-13 14:45:16 -06:00
|
|
|
let numRowsUpdated = db.execAffectedRows(sql(sqlStmt), mc.values.concat(@[$rec.id]))
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
return numRowsUpdated > 0;
|
|
|
|
|
|
|
|
template deleteRecord*(db: DbConn, modelType: type, id: typed): untyped =
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt = "DELETE FROM " & tableName(modelType) & " WHERE id = ?"
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "deleteRecord: [" & sqlStmt & "] id: " & $id
|
2022-01-13 14:45:16 -06:00
|
|
|
db.tryExec(sql(sqlStmt), $id)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
proc deleteRecord*[T](db: DbConn, rec: T): bool =
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt = "DELETE FROM " & tableName(rec) & " WHERE id = ?"
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "deleteRecord: [" & sqlStmt & "] id: " & $rec.id
|
2022-01-13 14:45:16 -06:00
|
|
|
return db.tryExec(sql(sqlStmt), $rec.id)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
template getRecord*(db: DbConn, modelType: type, id: typed): untyped =
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt =
|
2019-12-25 18:07:23 -06:00
|
|
|
"SELECT " & columnNamesForModel(modelType).join(",") &
|
|
|
|
" FROM " & tableName(modelType) &
|
2022-01-13 14:45:16 -06:00
|
|
|
" WHERE id = ?"
|
|
|
|
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "getRecord: [" & sqlStmt & "] id: " & $id
|
2022-01-13 14:45:16 -06:00
|
|
|
let row = db.getRow(sql(sqlStmt), @[$id])
|
2019-12-25 18:07:23 -06:00
|
|
|
|
2020-01-02 18:42:48 -06:00
|
|
|
if allIt(row, it.len == 0):
|
|
|
|
raise newException(NotFoundError, "no " & modelName(modelType) & " record for id " & $id)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
rowToModel(modelType, row)
|
|
|
|
|
|
|
|
template findRecordsWhere*(db: DbConn, modelType: type, whereClause: string, values: varargs[string, dbFormat]): untyped =
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt =
|
2019-12-25 18:07:23 -06:00
|
|
|
"SELECT " & columnNamesForModel(modelType).join(",") &
|
|
|
|
" FROM " & tableName(modelType) &
|
2022-01-13 14:45:16 -06:00
|
|
|
" WHERE " & whereClause
|
|
|
|
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "findRecordsWhere: [" & sqlStmt & "] values: (" & values.join(", ") & ")"
|
2022-01-13 14:45:16 -06:00
|
|
|
db.getAllRows(sql(sqlStmt), values).mapIt(rowToModel(modelType, it))
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
template getAllRecords*(db: DbConn, modelType: type): untyped =
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt =
|
2019-12-25 18:07:23 -06:00
|
|
|
"SELECT " & columnNamesForModel(modelType).join(",") &
|
2022-01-13 14:45:16 -06:00
|
|
|
" FROM " & tableName(modelType)
|
|
|
|
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "getAllRecords: [" & sqlStmt & "]"
|
2022-01-13 14:45:16 -06:00
|
|
|
db.getAllRows(sql(sqlStmt)).mapIt(rowToModel(modelType, it))
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
template findRecordsBy*(db: DbConn, modelType: type, lookups: seq[tuple[field: string, value: string]]): untyped =
|
2022-01-13 14:45:16 -06:00
|
|
|
let sqlStmt =
|
2019-12-25 18:07:23 -06:00
|
|
|
"SELECT " & columnNamesForModel(modelType).join(",") &
|
|
|
|
" FROM " & tableName(modelType) &
|
2022-01-13 14:45:16 -06:00
|
|
|
" WHERE " & lookups.mapIt(it.field & " = ?").join(" AND ")
|
|
|
|
let values = lookups.mapIt(it.value)
|
|
|
|
|
2022-01-22 20:20:02 -06:00
|
|
|
log().debug "findRecordsBy: [" & sqlStmt & "] values (" & values.join(", ") & ")"
|
2022-01-13 14:45:16 -06:00
|
|
|
db.getAllRows(sql(sqlStmt), values).mapIt(rowToModel(modelType, it))
|
2019-12-25 18:07:23 -06:00
|
|
|
|
2020-01-02 18:44:44 -06:00
|
|
|
macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untyped =
|
2019-12-25 18:07:23 -06:00
|
|
|
result = newStmtList()
|
|
|
|
|
|
|
|
for t in modelTypes:
|
|
|
|
let modelName = $(t.getType[1])
|
|
|
|
let getName = ident("get" & modelName)
|
|
|
|
let getAllName = ident("getAll" & modelName & "s")
|
|
|
|
let findWhereName = ident("find" & modelName & "sWhere")
|
|
|
|
let createName = ident("create" & modelName)
|
|
|
|
let updateName = ident("update" & modelName)
|
|
|
|
let deleteName = ident("delete" & modelName)
|
|
|
|
let idType = typeOfColumn(t, "id")
|
|
|
|
result.add quote do:
|
2022-02-07 10:48:10 -06:00
|
|
|
proc `getName`*(db: `dbType`, id: `idType`): `t` =
|
|
|
|
db.withConn: result = getRecord(conn, `t`, id)
|
|
|
|
|
|
|
|
proc `getAllName`*(db: `dbType`): seq[`t`] =
|
|
|
|
db.withConn: result = getAllRecords(conn, `t`)
|
|
|
|
|
2020-01-02 18:44:44 -06:00
|
|
|
proc `findWhereName`*(db: `dbType`, whereClause: string, values: varargs[string, dbFormat]): seq[`t`] =
|
2022-02-07 10:48:10 -06:00
|
|
|
db.withConn:
|
|
|
|
result = findRecordsWhere(conn, `t`, whereClause, values)
|
|
|
|
|
|
|
|
proc `createName`*(db: `dbType`, rec: `t`): `t` =
|
|
|
|
db.withConn: result = createRecord(conn, rec)
|
|
|
|
|
|
|
|
proc `updateName`*(db: `dbType`, rec: `t`): bool =
|
|
|
|
db.withConn: result = updateRecord(conn, rec)
|
|
|
|
|
|
|
|
proc `deleteName`*(db: `dbType`, rec: `t`): bool =
|
|
|
|
db.withConn: result = deleteRecord(conn, rec)
|
|
|
|
|
|
|
|
proc `deleteName`*(db: `dbType`, id: `idType`): bool =
|
|
|
|
db.withConn: result = deleteRecord(conn, `t`, id)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
2020-01-02 18:44:44 -06:00
|
|
|
macro generateLookup*(dbType: type, modelType: type, fields: seq[string]): untyped =
|
2019-12-25 18:07:23 -06:00
|
|
|
let fieldNames = fields[1].mapIt($it)
|
2020-01-02 18:46:03 -06:00
|
|
|
let procName = ident("find" & pluralize($modelType.getType[1]) & "By" & fieldNames.mapIt(it.capitalize).join("And"))
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
# Create proc skeleton
|
|
|
|
result = quote do:
|
2020-01-02 18:44:44 -06:00
|
|
|
proc `procName`*(db: `dbType`): seq[`modelType`] =
|
2022-02-07 10:48:10 -06:00
|
|
|
db.withConn: result = findRecordsBy(conn, `modelType`)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
var callParams = quote do: @[]
|
|
|
|
|
|
|
|
# Add dynamic parameters for the proc definition and inner proc call
|
|
|
|
for n in fieldNames:
|
|
|
|
let paramTuple = newNimNode(nnkPar)
|
|
|
|
paramTuple.add(newColonExpr(ident("field"), newLit(identNameToDb(n))))
|
|
|
|
paramTuple.add(newColonExpr(ident("value"), ident(n)))
|
|
|
|
|
2022-02-07 10:48:10 -06:00
|
|
|
# Add the parameter to the outer call (the generated proc)
|
|
|
|
# result[3] is ProcDef -> [3]: FormalParams
|
2019-12-25 18:07:23 -06:00
|
|
|
result[3].add(newIdentDefs(ident(n), ident("string")))
|
2022-02-07 10:48:10 -06:00
|
|
|
|
|
|
|
# Build up the AST for the inner procedure call
|
2019-12-25 18:07:23 -06:00
|
|
|
callParams[1].add(paramTuple)
|
|
|
|
|
2022-02-07 10:48:10 -06:00
|
|
|
# Add the call params to the inner procedure call
|
|
|
|
# result[6][0][1][0][1] is
|
|
|
|
# ProcDef -> [6]: StmtList (body) -> [0]: Call ->
|
|
|
|
# [1]: StmtList (withConn body) -> [0]: Asgn (result =) ->
|
|
|
|
# [1]: Call (inner findRecords invocation)
|
|
|
|
result[6][0][1][0][1].add(callParams)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
2020-01-02 18:44:44 -06:00
|
|
|
macro generateProcsForFieldLookups*(dbType: type, modelsAndFields: openarray[tuple[t: type, fields: seq[string]]]): untyped =
|
2019-12-25 18:07:23 -06:00
|
|
|
result = newStmtList()
|
|
|
|
|
|
|
|
for i in modelsAndFields:
|
|
|
|
var modelType = i[1][0]
|
|
|
|
let fieldNames = i[1][1][1].mapIt($it)
|
|
|
|
|
|
|
|
let procName = ident("find" & $modelType & "sBy" & fieldNames.mapIt(it.capitalize).join("And"))
|
|
|
|
|
|
|
|
# Create proc skeleton
|
|
|
|
let procDefAST = quote do:
|
2020-01-02 18:44:44 -06:00
|
|
|
proc `procName`*(db: `dbType`): seq[`modelType`] =
|
2022-02-07 10:48:10 -06:00
|
|
|
db.withConn: result = findRecordsBy(conn, `modelType`)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
var callParams = quote do: @[]
|
|
|
|
|
|
|
|
# Add dynamic parameters for the proc definition and inner proc call
|
|
|
|
for n in fieldNames:
|
|
|
|
let paramTuple = newNimNode(nnkPar)
|
2021-07-02 20:16:49 -05:00
|
|
|
paramTuple.add(newColonExpr(ident("field"), newLit(identNameToDb(n))))
|
2019-12-25 18:07:23 -06:00
|
|
|
paramTuple.add(newColonExpr(ident("value"), ident(n)))
|
|
|
|
|
|
|
|
procDefAST[3].add(newIdentDefs(ident(n), ident("string")))
|
|
|
|
callParams[1].add(paramTuple)
|
|
|
|
|
2022-02-07 10:48:10 -06:00
|
|
|
procDefAST[6][0][1][0][1].add(callParams)
|
2019-12-25 18:07:23 -06:00
|
|
|
|
|
|
|
result.add procDefAST
|
2022-02-07 10:48:10 -06:00
|
|
|
|
|
|
|
proc initPool*(
|
|
|
|
connect: proc(): DbConn,
|
|
|
|
poolSize = 10,
|
|
|
|
hardCap = false,
|
|
|
|
healthCheckQuery = "SELECT 'true' AS alive"): DbConnPool =
|
|
|
|
|
|
|
|
initDbConnPool(DbConnPoolConfig(
|
|
|
|
connect: connect,
|
|
|
|
poolSize: poolSize,
|
|
|
|
hardCap: hardCap,
|
|
|
|
healthCheckQuery: healthCheckQuery))
|
|
|
|
|
|
|
|
template inTransaction*(db: DbConnPool, body: untyped) =
|
|
|
|
pool.withConn(db):
|
|
|
|
conn.exec(sql"BEGIN TRANSACTION")
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
conn.exec(sql"COMMIT")
|
|
|
|
except:
|
|
|
|
conn.exec(sql"ROLLBACK")
|
|
|
|
raise getCurrentException()
|