feat: Add dbIgnore pragma

dbIgnore allows an object prooperty to be specified on an object without it being assumed to be a column in the database.

When unpacking a row in to the object, the dbIgnore pragma is checked, and if it is specified, the object property's is not initialised from the DB.
This commit is contained in:
emizzle 2020-12-21 13:59:38 +11:00
parent d1ed963f50
commit 72aa518280
No known key found for this signature in database
GPG Key ID: 1FD4BAB3C37EE9BA
1 changed files with 15 additions and 6 deletions

View File

@ -75,6 +75,9 @@ proc hasRows*(rows: seq[ResultRow]): bool = rows.len > 0
template dbColumnName*(name: string) {.pragma.} template dbColumnName*(name: string) {.pragma.}
## Specifies the database column name for the object property ## Specifies the database column name for the object property
template dbIgnore*() {.pragma.}
## Specifies the object property should not be considered a DB column
template dbTableName*(name: string) {.pragma.} template dbTableName*(name: string) {.pragma.}
## Specifies the database table name for the object ## Specifies the database table name for the object
@ -95,9 +98,9 @@ template tableName*(obj: auto | typedesc): string =
template enumInstanceDbColumns*(obj: auto, template enumInstanceDbColumns*(obj: auto,
fieldNameVar, fieldVar, fieldNameVar, fieldVar, fieldIgnore,
body: untyped) = body: untyped) =
## Expands a block over all serialized fields of an object. ## Expands a block over all fields of an object.
## ##
## Inside the block body, the passed `fieldNameVar` identifier ## Inside the block body, the passed `fieldNameVar` identifier
## will refer to the name of each field as a string. `fieldVar` ## will refer to the name of each field as a string. `fieldVar`
@ -108,16 +111,22 @@ template enumInstanceDbColumns*(obj: auto,
type ObjType {.used.} = type(obj) type ObjType {.used.} = type(obj)
for fieldName, fieldVar in fieldPairs(obj): for fieldName, fieldVar in fieldPairs(obj):
when hasCustomPragmaFixed(ObjType, fieldName, dbColumnName): when hasCustomPragmaFixed(ObjType, fieldName, dbIgnore):
const fieldIgnore = true
const fieldNameVar = fieldName
elif hasCustomPragmaFixed(ObjType, fieldName, dbColumnName):
const fieldIgnore = false
const fieldNameVar = getCustomPragmaFixed(ObjType, fieldName, dbColumnName) const fieldNameVar = getCustomPragmaFixed(ObjType, fieldName, dbColumnName)
else: else:
const fieldIgnore = false
const fieldNameVar = fieldName const fieldNameVar = fieldName
body body
proc unpack*(row: ResultRow, obj: var object) = proc unpack*(row: ResultRow, obj: var object) =
obj.enumInstanceDbColumns(dbColName, property): obj.enumInstanceDbColumns(dbColName, property, ignore):
type ColType = type property if not ignore:
property = row[dbColName, ColType] type ColType = type property
property = row[dbColName, ColType]
# #
# Custom.sqlcipher # Custom.sqlcipher