Skip to content

Commit c53ddd3

Browse files
author
Crystal Melting Dot
committed
Named tuple return types for iterator queries
1 parent 0ca7eb8 commit c53ddd3

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/easy_sqlite3/macros.nim

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,21 @@ proc genQueryIterator(sql: string, body: NimNode): NimNode =
9797
result = body.copy()
9898
let db_ident = genSym(nskParam, "db")
9999
let st_ident = genSym(nskVar, "st")
100-
let rettype = result[3][0]
100+
101+
let rettype = block:
102+
case result[3][0].kind
103+
of nnkTupleTy:
104+
result[3][0]
105+
of nnkSym:
106+
let typeDef = result[3][0].getImpl
107+
typeDef.expectKind nnkTypeDef
108+
let tuplDef = typeDef[2]
109+
tuplDef.expectKind nnkTupleTy
110+
tuplDef
111+
else:
112+
error "Expected a tuple type", result[3][0]
113+
nil
114+
101115
injectDbDecl(result, db_ident)
102116
result[6] = nnkStmtList.genTree(procbody):
103117
injectDbFetch(procbody, sql, db_ident, st_ident)
@@ -207,8 +221,12 @@ macro importdb*(sql: static string, body: typed) =
207221
error("Expected int, tuple, Option[tuple]")
208222
return
209223
of nnkIteratorDef:
210-
body[3][0].expectKind nnkTupleTy
211-
result = genQueryIterator(sql, body)
224+
let retType = body[3][0]
225+
case retType.kind
226+
of nnkTupleTy, nnkSym:
227+
result = genQueryIterator(sql, body)
228+
else:
229+
error("Expected a tuple type", retType)
212230
else:
213231
error("Expected proc or iterator, got " & $body.kind, body)
214232
return

0 commit comments

Comments
 (0)