3 Commits
0.3.2 ... 0.3.5

Author SHA1 Message Date
f7791b6f60 Add logging statments (behind namespaced logger). 2022-01-13 16:22:15 -06:00
279d9aa7fd Expose a number of useful utility methods and macros. 2021-08-02 05:54:56 -05:00
d90372127b Further fix for ISO8601 date parsing.
Recognize versions of timestamps with 'T' as the date/time separator.
For example, compare:

    '2021-08-01 23:14:00-05:00'
    '2021-08-01T23:14:00-05:00'

This commit adds support for the second flavor (and it's variations).
2021-08-01 23:14:18 -05:00
3 changed files with 69 additions and 37 deletions

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.3.2" version = "0.3.5"
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,3 +11,4 @@ srcDir = "src"
# Dependencies # Dependencies
requires "nim >= 1.4.0" requires "nim >= 1.4.0"
requires "https://git.jdb-software.com/jdb/nim-namespaced-logging.git"

View File

@ -1,11 +1,22 @@
import db_postgres, macros, options, sequtils, strutils, uuids import db_postgres, macros, options, sequtils, strutils, uuids
import namespaced_logging
from unicode import capitalize from unicode import capitalize
import ./fiber_orm/util import ./fiber_orm/util
export
util.columnNamesForModel,
util.dbFormat,
util.dbNameToIdent,
util.identNameToDb,
util.modelName,
util.rowToModel,
util.tableName
type NotFoundError* = object of CatchableError type NotFoundError* = object of CatchableError
let logNs = initLoggingNamespace(name = "fiber_orm", level = lvlNotice)
proc newMutateClauses(): MutateClauses = proc newMutateClauses(): MutateClauses =
return MutateClauses( return MutateClauses(
columns: @[], columns: @[],
@ -18,11 +29,14 @@ proc createRecord*[T](db: DbConn, rec: T): T =
# Confusingly, getRow allows inserts and updates. We use it to get back the ID # Confusingly, getRow allows inserts and updates. We use it to get back the ID
# we want from the row. # we want from the row.
let newRow = db.getRow(sql( let sqlStmt =
"INSERT INTO " & tableName(rec) & "INSERT INTO " & tableName(rec) &
" (" & mc.columns.join(",") & ") " & " (" & mc.columns.join(",") & ") " &
" VALUES (" & mc.placeholders.join(",") & ") " & " VALUES (" & mc.placeholders.join(",") & ") " &
" RETURNING *"), mc.values) " RETURNING *"
logNs.debug "createRecord: [" & sqlStmt & "]"
let newRow = db.getRow(sql(sqlStmt), mc.values)
result = rowToModel(T, newRow) result = rowToModel(T, newRow)
@ -31,24 +45,34 @@ proc updateRecord*[T](db: DbConn, rec: T): bool =
populateMutateClauses(rec, false, mc) populateMutateClauses(rec, false, mc)
let setClause = zip(mc.columns, mc.placeholders).mapIt(it[0] & " = " & it[1]).join(",") let setClause = zip(mc.columns, mc.placeholders).mapIt(it[0] & " = " & it[1]).join(",")
let numRowsUpdated = db.execAffectedRows(sql( let sqlStmt =
"UPDATE " & tableName(rec) & "UPDATE " & tableName(rec) &
" SET " & setClause & " SET " & setClause &
" WHERE id = ? "), mc.values.concat(@[$rec.id])) " WHERE id = ? "
logNs.debug "updateRecord: [" & sqlStmt & "] id: " & $rec.id
let numRowsUpdated = db.execAffectedRows(sql(sqlStmt), mc.values.concat(@[$rec.id]))
return numRowsUpdated > 0; return numRowsUpdated > 0;
template deleteRecord*(db: DbConn, modelType: type, id: typed): untyped = template deleteRecord*(db: DbConn, modelType: type, id: typed): untyped =
db.tryExec(sql("DELETE FROM " & tableName(modelType) & " WHERE id = ?"), $id) let sqlStmt = "DELETE FROM " & tableName(modelType) & " WHERE id = ?"
logNs.debug "deleteRecord: [" & sqlStmt & "] id: " & $id
db.tryExec(sql(sqlStmt), $id)
proc deleteRecord*[T](db: DbConn, rec: T): bool = proc deleteRecord*[T](db: DbConn, rec: T): bool =
return db.tryExec(sql("DELETE FROM " & tableName(rec) & " WHERE id = ?"), $rec.id) let sqlStmt = "DELETE FROM " & tableName(rec) & " WHERE id = ?"
logNs.debug "deleteRecord: [" & sqlStmt & "] id: " & $rec.id
return db.tryExec(sql(sqlStmt), $rec.id)
template getRecord*(db: DbConn, modelType: type, id: typed): untyped = template getRecord*(db: DbConn, modelType: type, id: typed): untyped =
let row = db.getRow(sql( let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") & "SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType) & " FROM " & tableName(modelType) &
" WHERE id = ?"), @[$id]) " WHERE id = ?"
logNs.debug "getRecord: [" & sqlStmt & "] id: " & $id
let row = db.getRow(sql(sqlStmt), @[$id])
if allIt(row, it.len == 0): if allIt(row, it.len == 0):
raise newException(NotFoundError, "no " & modelName(modelType) & " record for id " & $id) raise newException(NotFoundError, "no " & modelName(modelType) & " record for id " & $id)
@ -56,25 +80,31 @@ template getRecord*(db: DbConn, modelType: type, id: typed): untyped =
rowToModel(modelType, row) rowToModel(modelType, row)
template findRecordsWhere*(db: DbConn, modelType: type, whereClause: string, values: varargs[string, dbFormat]): untyped = template findRecordsWhere*(db: DbConn, modelType: type, whereClause: string, values: varargs[string, dbFormat]): untyped =
db.getAllRows(sql( let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") & "SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType) & " FROM " & tableName(modelType) &
" WHERE " & whereClause), values) " WHERE " & whereClause
.mapIt(rowToModel(modelType, it))
logNs.debug "findRecordsWhere: [" & sqlStmt & "] values: (" & values.join(", ") & ")"
db.getAllRows(sql(sqlStmt), values).mapIt(rowToModel(modelType, it))
template getAllRecords*(db: DbConn, modelType: type): untyped = template getAllRecords*(db: DbConn, modelType: type): untyped =
db.getAllRows(sql( let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") & "SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType))) " FROM " & tableName(modelType)
.mapIt(rowToModel(modelType, it))
logNs.debug "getAllRecords: [" & sqlStmt & "]"
db.getAllRows(sql(sqlStmt)).mapIt(rowToModel(modelType, it))
template findRecordsBy*(db: DbConn, modelType: type, lookups: seq[tuple[field: string, value: string]]): untyped = template findRecordsBy*(db: DbConn, modelType: type, lookups: seq[tuple[field: string, value: string]]): untyped =
db.getAllRows(sql( let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") & "SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType) & " FROM " & tableName(modelType) &
" WHERE " & lookups.mapIt(it.field & " = ?").join(" AND ")), " WHERE " & lookups.mapIt(it.field & " = ?").join(" AND ")
lookups.mapIt(it.value)) let values = lookups.mapIt(it.value)
.mapIt(rowToModel(modelType, it))
logNs.debug "findRecordsBy: [" & sqlStmt & "] values (" & values.join(", ") & ")"
db.getAllRows(sql(sqlStmt), values).mapIt(rowToModel(modelType, it))
macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untyped = macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untyped =
result = newStmtList() result = newStmtList()

View File

@ -67,37 +67,38 @@ proc parsePGDatetime*(val: string): DateTime =
const PG_TIMESTAMP_FORMATS = [ const PG_TIMESTAMP_FORMATS = [
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd HH:mm:sszz", "yyyy-MM-dd HH:mm:sszz",
"yyyy-MM-dd'T'HH:mm:sszz",
"yyyy-MM-dd HH:mm:ss'.'fff", "yyyy-MM-dd HH:mm:ss'.'fff",
"yyyy-MM-dd HH:mm:ss'.'fffzz" "yyyy-MM-dd'T'HH:mm:ss'.'fff",
"yyyy-MM-dd HH:mm:ss'.'fffzz",
"yyyy-MM-dd'T'HH:mm:ss'.'fffzz",
"yyyy-MM-dd HH:mm:ss'.'fffzzz",
"yyyy-MM-dd'T'HH:mm:ss'.'fffzzz",
] ]
let PG_PARTIAL_FORMAT_REGEX = re"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.)(\d{1,3})(\S+)?" var correctedVal = val;
var errStr = ""
# Try to parse directly using known format strings.
for df in PG_TIMESTAMP_FORMATS:
try: return val.parse(df)
except: errStr &= "\n\t" & getCurrentExceptionMsg()
# PostgreSQL will truncate any trailing 0's in the millisecond value leading # PostgreSQL will truncate any trailing 0's in the millisecond value leading
# to values like `2020-01-01 16:42.3+00`. This cannot currently be parsed by # to values like `2020-01-01 16:42.3+00`. This cannot currently be parsed by
# the standard times format as it expects exactly three digits for # the standard times format as it expects exactly three digits for
# millisecond values. So we have to detect this and pad out the millisecond # millisecond values. So we have to detect this and pad out the millisecond
# value to 3 digits. # value to 3 digits.
let PG_PARTIAL_FORMAT_REGEX = re"(\d{4}-\d{2}-\d{2}( |'T')\d{2}:\d{2}:\d{2}\.)(\d{1,2})(\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
try: if c.toSeq.len == 2: correctedVal = c[0] & alignLeft(c[2], 3, '0')
if c.toSeq.len == 2: else: correctedVal = c[0] & alignLeft(c[2], 3, '0') & c[3]
let corrected = c[0] & alignLeft(c[1], 3, '0')
return corrected.parse(PG_TIMESTAMP_FORMATS[2]) var errStr = ""
else:
let corrected = c[0] & alignLeft(c[1], 3, '0') & c[2] # Try to parse directly using known format strings.
return corrected.parse(PG_TIMESTAMP_FORMATS[3]) for df in PG_TIMESTAMP_FORMATS:
except: try: return correctedVal.parse(df)
errStr &= "\n\t" & getCurrentExceptionMsg() except: errStr &= "\n\t" & getCurrentExceptionMsg()
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr) raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)