@@ -15,6 +15,7 @@ import {
1515 createPrinter ,
1616 createSourceFile ,
1717 factory ,
18+ Statement ,
1819} from "typescript" ;
1920
2021import {
@@ -52,30 +53,34 @@ interface Driver {
5253 name : string ,
5354 text : string ,
5455 iface : string | undefined ,
55- params : Parameter [ ]
56- ) => Node ;
56+ params : Parameter [ ] ,
57+ namespace ?: string ,
58+ ) => Statement ;
5759 execlastidDecl : (
5860 name : string ,
5961 text : string ,
6062 iface : string | undefined ,
61- params : Parameter [ ]
62- ) => Node ;
63+ params : Parameter [ ] ,
64+ namespace ?: string ,
65+ ) => Statement ;
6366 manyDecl : (
6467 name : string ,
6568 text : string ,
6669 argIface : string | undefined ,
6770 returnIface : string ,
6871 params : Parameter [ ] ,
69- columns : Column [ ]
70- ) => Node ;
72+ columns : Column [ ] ,
73+ namespace ?: string ,
74+ ) => Statement ;
7175 oneDecl : (
7276 name : string ,
7377 text : string ,
7478 argIface : string | undefined ,
7579 returnIface : string ,
7680 params : Parameter [ ] ,
77- columns : Column [ ]
78- ) => Node ;
81+ columns : Column [ ] ,
82+ namespace ?: string ,
83+ ) => Statement ;
7984}
8085
8186function createNodeGenerator ( options : Options ) : Driver {
@@ -135,10 +140,15 @@ function codegen(input: GenerateRequest): GenerateResponse {
135140 colmap . set ( column . name , count + 1 ) ;
136141 }
137142
138- const lowerName = query . name [ 0 ] . toLowerCase ( ) + query . name . slice ( 1 ) ;
143+ const [ namespace , name ] = query . name . indexOf ( '_' ) > - 1
144+ ? query . name . split ( "_" )
145+ : [ undefined , query . name ] ;
146+ const lowerName = name [ 0 ] . toLowerCase ( ) + name . slice ( 1 ) ;
139147 const textName = `${ lowerName } Query` ;
140148
141- nodes . push (
149+ const nodesToPush : Statement [ ] = [ ] ;
150+
151+ nodesToPush . push (
142152 queryDecl (
143153 textName ,
144154 `-- name: ${ query . name } ${ query . cmd }
@@ -150,54 +160,67 @@ ${query.text}`
150160 let returnIface = undefined ;
151161 if ( query . params . length > 0 ) {
152162 argIface = `${ query . name } Args` ;
153- nodes . push ( argsDecl ( argIface , driver , query . params ) ) ;
163+ nodesToPush . push ( argsDecl ( argIface , driver , query . params ) ) ;
154164 }
155165 if ( query . columns . length > 0 ) {
156166 returnIface = `${ query . name } Row` ;
157- nodes . push ( rowDecl ( returnIface , driver , query . columns ) ) ;
167+ nodesToPush . push ( rowDecl ( returnIface , driver , query . columns ) ) ;
158168 }
159169
160170 switch ( query . cmd ) {
161171 case ":exec" : {
162- nodes . push (
163- driver . execDecl ( lowerName , textName , argIface , query . params )
172+ nodesToPush . push (
173+ driver . execDecl ( lowerName , textName , argIface , query . params , namespace )
164174 ) ;
165175 break ;
166176 }
167177 case ":execlastid" : {
168- nodes . push (
169- driver . execlastidDecl ( lowerName , textName , argIface , query . params )
178+ nodesToPush . push (
179+ driver . execlastidDecl ( lowerName , textName , argIface , query . params , namespace )
170180 ) ;
171181 break ;
172182 }
173183 case ":one" : {
174- nodes . push (
184+ nodesToPush . push (
175185 driver . oneDecl (
176186 lowerName ,
177187 textName ,
178188 argIface ,
179189 returnIface ?? "void" ,
180190 query . params ,
181- query . columns
191+ query . columns ,
192+ namespace
182193 )
183194 ) ;
184195 break ;
185196 }
186197 case ":many" : {
187- nodes . push (
198+ nodesToPush . push (
188199 driver . manyDecl (
189200 lowerName ,
190201 textName ,
191202 argIface ,
192203 returnIface ?? "void" ,
193204 query . params ,
194- query . columns
205+ query . columns ,
206+ namespace
195207 )
196208 ) ;
197209 break ;
198210 }
199211 }
200- if ( nodes ) {
212+ if ( nodesToPush ) {
213+ if ( namespace ) {
214+ nodes . push (
215+ factory . createModuleDeclaration (
216+ [ factory . createToken ( SyntaxKind . ExportKeyword ) , ] ,
217+ factory . createIdentifier ( namespace ) ,
218+ factory . createModuleBlock ( nodesToPush )
219+ ) )
220+ } else {
221+ nodes . push ( ...nodesToPush ) ;
222+ }
223+
201224 files . push (
202225 new File ( {
203226 name : `${ filename . replace ( "." , "_" ) } .ts` ,
0 commit comments