Skip to content

Commit b1447ed

Browse files
committed
kotlin :iter
1 parent 8e809ae commit b1447ed

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

internal/tmpl/ktiface.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ func ktIfaceTmpl(w io.Writer, dot core.KtTmplCtx) error {
6262
_, _ = io.WriteString(w, dot.Ret.Type())
6363
_, _ = io.WriteString(w, ">")
6464
}
65+
if dot.Cmd == ":iter" {
66+
_, _ = io.WriteString(w, "\n fun ")
67+
_, _ = io.WriteString(w, dot.MethodName)
68+
_, _ = io.WriteString(w, "(iter: (")
69+
_, _ = io.WriteString(w, dot.Ret.Name)
70+
_, _ = io.WriteString(w, ": ")
71+
_, _ = io.WriteString(w, dot.Ret.Type())
72+
_, _ = io.WriteString(w, ") -> Unit")
73+
if dot.Arg.Args() != "" {
74+
_, _ = io.WriteString(w, ", ")
75+
_, _ = io.WriteString(w, dot.Arg.Args())
76+
}
77+
_, _ = io.WriteString(w, ")")
78+
}
6579
if dot.Cmd == ":exec" {
6680
_, _ = io.WriteString(w, "\n fun ")
6781
_, _ = io.WriteString(w, dot.MethodName)

internal/tmpl/ktiface.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ interface Queries {
1818
{{- if eq .Cmd ":many"}}
1919
fun {{.MethodName}}({{.Arg.Args}}): List<{{.Ret.Type}}>
2020
{{- end}}
21+
{{- if eq .Cmd ":iter"}}
22+
fun {{.MethodName}}(iter: ({{.Ret.Name}}: {{.Ret.Type}}) -> Unit{{ if .Arg.Args }}, {{ .Arg.Args}}{{end}})
23+
{{- end}}
2124
{{- if eq .Cmd ":exec"}}
2225
fun {{.MethodName}}({{.Arg.Args}})
2326
{{- end}}

internal/tmpl/ktsql.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,50 @@ func ktSQLTmpl(w io.Writer, dot core.KtTmplCtx) error {
160160
_, _ = io.WriteString(w, "\n }")
161161
_, _ = io.WriteString(w, "\n }")
162162
}
163+
if dot.Cmd == ":iter" {
164+
if eval := dot.Comments; len(eval) != 0 {
165+
_, _ = io.WriteString(w, "\n")
166+
for _, dot := range eval {
167+
_ = dot
168+
_, _ = io.WriteString(w, "\n//")
169+
_, _ = io.WriteString(w, dot)
170+
}
171+
}
172+
_, _ = io.WriteString(w, "\n\n @Throws(SQLException::class)")
173+
_, _ = io.WriteString(w, "\n override fun ")
174+
_, _ = io.WriteString(w, dot.MethodName)
175+
_, _ = io.WriteString(w, "(iter: (")
176+
_, _ = io.WriteString(w, dot.Ret.Name)
177+
_, _ = io.WriteString(w, ": ")
178+
_, _ = io.WriteString(w, dot.Ret.Type())
179+
_, _ = io.WriteString(w, ") -> Unit")
180+
if dot.Arg.Args() != "" {
181+
_, _ = io.WriteString(w, dot.Arg.Args())
182+
}
183+
_, _ = io.WriteString(w, ") {")
184+
185+
_, _ = io.WriteString(w, "\n return conn.prepareStatement(")
186+
_, _ = io.WriteString(w, dot.ConstantName)
187+
_, _ = io.WriteString(w, ").use { stmt ->")
188+
189+
_, _ = io.WriteString(w, "\n ")
190+
_, _ = io.WriteString(w, dot.Arg.Bindings())
191+
192+
_, _ = io.WriteString(w, "\n\n val results = stmt.executeQuery()")
193+
194+
_, _ = io.WriteString(w, "\n while (results.next()) {")
195+
_, _ = io.WriteString(w, "\n val ret = ")
196+
_, _ = io.WriteString(w, dot.Ret.ResultSet())
197+
_, _ = io.WriteString(w, "\n try {")
198+
_, _ = io.WriteString(w, "\n iter(ret)")
199+
_, _ = io.WriteString(w, "\n } catch (e: Exception) {")
200+
_, _ = io.WriteString(w, "\n throw SQLException(\"error calling iter function at row %d\".format(results.row), e)")
201+
_, _ = io.WriteString(w, "\n }")
202+
_, _ = io.WriteString(w, "\n }")
203+
204+
_, _ = io.WriteString(w, "\n }")
205+
_, _ = io.WriteString(w, "\n }")
206+
}
163207
if dot.Cmd == ":exec" {
164208
if eval := dot.Comments; len(eval) != 0 {
165209
_, _ = io.WriteString(w, "\n")

internal/tmpl/ktsql.tmpl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ class QueriesImpl(private val conn: Connection) : Queries {
6464
}
6565
{{end}}
6666

67+
{{if eq .Cmd ":iter"}}
68+
{{range .Comments}}//{{.}}
69+
{{end}}
70+
@Throws(SQLException::class)
71+
override fun {{.MethodName}}(iter: ({{.Ret.Name}}: {{.Ret.Type}}) -> Unit{{ if .Arg.Args }}, {{ .Arg.Args}}{{end}}) {
72+
return conn.prepareStatement({{.ConstantName}}).use { stmt ->
73+
{{.Arg.Bindings}}
74+
75+
val results = stmt.executeQuery()
76+
while (results.next()) {
77+
val ret = {{.Ret.ResultSet}}
78+
try {
79+
iter(ret)
80+
} catch (e: Exception) {
81+
throw SQLException("error calling iter function at row %d".format(results.row), e)
82+
}
83+
}
84+
}
85+
}
86+
{{end}}
87+
6788
{{if eq .Cmd ":exec"}}
6889
{{range .Comments}}//{{.}}
6990
{{end}}

0 commit comments

Comments
 (0)