-
Notifications
You must be signed in to change notification settings - Fork 21
Add custom sql functions #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d4b9702
/Users/elcritch/.local/share/grabnim/current/bin/nim c -r /Volumes/pr…
elcritch 4572b77
implement importSql pragma that allows adding custom sql functions to…
elcritch f8c86fe
cleanup
elcritch 5f67872
cleanup
elcritch a13ed66
fix postgres
elcritch 38aff41
fix postgres
elcritch 8ab9b25
Merge branch 'master' into add-custom-sql-funcs-pr
elcritch 50140c2
fix merge
elcritch 2acb87e
add docs
elcritch e2df8c2
add docs
elcritch d6bb334
bump version
elcritch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| * | ||
| !*/ | ||
| !*.* | ||
| *.db | ||
| *.js | ||
| nimcache | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import std/strutils | ||
| import db_connector/db_common | ||
|
|
||
| proc dbTypFromName*(name: string): DbTypeKind = | ||
| var k = dbUnknown | ||
|
|
||
| case name.toLowerAscii | ||
| of "int", "integer", "int8", "smallint", "int16", | ||
| "longint", "int32", "int64", "tinyint", "hugeint": k = dbInt | ||
| of "uint", "uint8", "uint16", "uint32", "uint64": k = dbUInt | ||
| of "serial": k = dbSerial | ||
| of "bit": k = dbBit | ||
| of "bool", "boolean": k = dbBool | ||
| of "blob": k = dbBlob | ||
| of "fixedchar": k = dbFixedChar | ||
| of "varchar", "text", "string": k = dbVarchar | ||
| of "json": k = dbJson | ||
| of "xml": k = dbXml | ||
| of "decimal": k = dbDecimal | ||
| of "float", "double", "longdouble", "real": k = dbFloat | ||
| of "date", "day": k = dbDate | ||
| of "time": k = dbTime | ||
| of "datetime": k = dbDateTime | ||
| of "timestamp", "timestamptz": k = dbTimestamp | ||
| of "timeinterval": k = dbTimeInterval | ||
| of "set": k = dbSet | ||
| of "array": k = dbArray | ||
| of "composite": k = dbComposite | ||
| of "url", "uri": k = dbUrl | ||
| of "uuid": k = dbUuid | ||
| of "inet", "ip", "tcpip": k = dbInet | ||
| of "mac", "macaddress": k = dbMacAddress | ||
| of "geometry": k = dbGeometry | ||
| of "point": k = dbPoint | ||
| of "line": k = dbLine | ||
| of "lseg": k = dbLseg | ||
| of "box": k = dbBox | ||
| of "path": k = dbPath | ||
| of "polygon": k = dbPolygon | ||
| of "circle": k = dbCircle | ||
| else: discard | ||
|
|
||
| return k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ import macros, strutils | |
| import db_connector/db_common | ||
| from os import parentDir, `/` | ||
|
|
||
| import db_types | ||
|
|
||
| # SQL dialect specific things: | ||
| const | ||
| equals = "=" | ||
|
|
@@ -37,6 +39,79 @@ var | |
| Function(name: "replace", arity: 3, typ: dbVarchar) | ||
| ] | ||
|
|
||
| proc isVarargsType(n: NimNode): bool {.compileTime.} = | ||
| ## Checks if the provided type node encodes a varargs parameter. | ||
| case n.kind | ||
| of nnkBracketExpr: | ||
| if n.len > 0: | ||
| let head = n[0] | ||
| if head.kind in {nnkIdent, nnkSym} and head.strVal == "varargs": | ||
| return true | ||
| else: | ||
| discard | ||
| result = false | ||
|
|
||
| proc typeNodeToDbKind(n: NimNode): DbTypeKind {.compileTime.} = | ||
| ## Maps the return type of an imported SQL function to DbTypeKind. | ||
| if n.kind == nnkEmpty: | ||
| return dbUnknown | ||
| if isVarargsType(n): | ||
| if n.len > 1: | ||
| return typeNodeToDbKind(n[1]) | ||
| return dbUnknown | ||
| let name = | ||
| case n.kind | ||
| of nnkSym, nnkIdent: | ||
| n.strVal | ||
| else: | ||
| $n | ||
| return dbTypFromName(name) | ||
|
|
||
| proc registerImportSqlFunction(name: string; arity: int; typ: DbTypeKind) {.compileTime.} = | ||
| ## Adds or updates a Function descriptor for vendor specific SQL routines. | ||
| let lname = name.toLowerAscii() | ||
| for f in mitems(functions): | ||
| if f.name.toLowerAscii() == lname and f.arity == arity: | ||
| f.typ = typ | ||
| return | ||
| let f = Function(name: name, arity: arity, typ: typ) | ||
| when defined(debugOrminDsl): | ||
| echo "registerImportSqlFunction: ", f | ||
| functions.add f | ||
|
|
||
| macro importSql*(n: typed): untyped = | ||
| ## Registers a Nim proc as callable SQL function within the query DSL. | ||
| if n.kind notin {nnkProcDef, nnkFuncDef}: | ||
| macros.error("{.importSql.} can only be applied to proc or func definitions", n) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used GPT5-Codex-High for this. Seems to have done a good job. It checked for conditions I wouldn't have thought to check. I had to cleanup a little bit in the tests. |
||
| let params = n[3] | ||
| expectKind(params, nnkFormalParams) | ||
| var paramCount = 0 | ||
| var hasVarargs = false | ||
| for i in 1..<params.len: | ||
| let identDefs = params[i] | ||
| expectKind(identDefs, nnkIdentDefs) | ||
| if identDefs.len < 3: | ||
| macros.error("unexpected parameter definition in {.importSql.}", identDefs) | ||
| let defaultValue = identDefs[^1] | ||
| if defaultValue.kind != nnkEmpty: | ||
| macros.error("{.importSql.} procs cannot declare default values", defaultValue) | ||
| let typeNode = identDefs[^2] | ||
| if isVarargsType(typeNode): | ||
| if hasVarargs: | ||
| macros.error("{.importSql.} supports only a single varargs parameter", identDefs) | ||
| if params.len > 2: | ||
| macros.error("varargs parameters cannot be combined with fixed parameters", identDefs) | ||
| if identDefs.len - 2 != 1: | ||
| macros.error("varargs parameter must declare exactly one identifier", identDefs) | ||
| hasVarargs = true | ||
| else: | ||
| paramCount += identDefs.len - 2 | ||
| let arity = if hasVarargs: -1 else: paramCount | ||
| let fnName = n[0].strVal | ||
| let retKind = typeNodeToDbKind(params[0]) | ||
| registerImportSqlFunction(fnName, arity, retKind) | ||
| result = newStmtList() | ||
|
|
||
| type | ||
| Env = seq[(int, string)] | ||
| Params = seq[tuple[ex, typ: NimNode; isJson: bool]] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be nice to upgrade these at some point to also support type checking the args. But alas, not today.