Skip to content

Commit 0ca7eb8

Browse files
author
Crystal Melting Dot
committed
Support named tuples as return values
1 parent 3373c30 commit 0ca7eb8

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/easy_sqlite3/macros.nim

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ proc fillPar(ret, st_ident: NimNode): NimNode =
8989
bindSym "getColumn"
9090
),
9191
newLit idx,
92-
it[1]
92+
nnkBracketExpr.newTree(newIdentNode("typedesc"),it[1])
9393
)
9494
)
9595

@@ -114,7 +114,7 @@ proc genQueryProcedure(sql: string, body, tupdef: NimNode, opt: static bool): Ni
114114
let rettype = when opt:
115115
result[3][0][1]
116116
else:
117-
result[3][0]
117+
tupdef
118118
injectDbDecl(result, db_ident)
119119
result[6] = nnkStmtList.genTree(procbody):
120120
injectDbFetch(procbody, sql, db_ident, st_ident)
@@ -180,16 +180,23 @@ proc genCreateProcedure(sql: string, body: NimNode): NimNode =
180180
nnkCall.newTree(bindSym "newException", ident "SQLiteError", newLit "Invalid statement")
181181
)
182182

183-
macro importdb*(sql: static string, body: untyped) =
183+
macro importdb*(sql: static string, body: typed) =
184184
case body.kind:
185185
of nnkProcDef:
186186
let ret = body[3][0]
187187
case ret.kind:
188188
of nnkEmpty:
189189
result = genCreateProcedure(sql, body)
190-
of nnkIdent:
191-
ret.expectIdent "int"
192-
result = genUpdateProcedure(sql, body)
190+
of nnkSym:
191+
case ret.strVal:
192+
of "int":
193+
result = genUpdateProcedure(sql, body)
194+
else:
195+
let typImpl = ret.getImpl
196+
typImpl.expectKind nnkTypeDef
197+
let tuplImpl = typImpl[2]
198+
tuplImpl.expectKind nnkTupleTy
199+
result = genQueryProcedure(sql, body, tuplImpl, false)
193200
of nnkBracketExpr:
194201
ret[0].expectIdent "Option"
195202
ret[1].expectKind nnkTupleTy
@@ -205,6 +212,14 @@ macro importdb*(sql: static string, body: untyped) =
205212
else:
206213
error("Expected proc or iterator, got " & $body.kind, body)
207214
return
215+
216+
if result[0].isExported:
217+
result[0] = nnkPostfix.newTree(
218+
"*".ident,
219+
result[0].strVal.ident
220+
)
221+
else:
222+
result[0] = result[0].strVal.ident
208223

209224
proc db_begin_deferred() {.importdb: "BEGIN DEFERRED".}
210225
proc db_begin_immediate() {.importdb: "BEGIN IMMEDIATE".}

0 commit comments

Comments
 (0)