@@ -66,6 +66,8 @@ type GoQueryValue struct {
66
66
Name string
67
67
Struct * GoStruct
68
68
Typ string
69
+ // LocalSQLQuery 函数内部SQL
70
+ LocalSQLQuery string
69
71
}
70
72
71
73
func (v GoQueryValue ) EmitStruct () bool {
@@ -513,6 +515,7 @@ func queryImports(r Generateable, settings config.CombinedSettings, filename str
513
515
}
514
516
515
517
if sliceScan () {
518
+ std ["strings" ] = struct {}{}
516
519
pkg ["github.com/lib/pq" ] = struct {}{}
517
520
}
518
521
_ , overrideNullTime := overrideTypes ["pq.NullTime" ]
@@ -1233,9 +1236,10 @@ import (
1233
1236
{{define "queryCode"}}
1234
1237
{{range .GoQueries}}
1235
1238
{{if $.OutputQuery .SourceName}}
1239
+ {{if not .Arg.LocalSQLQuery}}
1236
1240
// {{.ConstantName}} -- name: {{.MethodName}} {{.Cmd}}
1237
1241
const {{.ConstantName}} = "{{.SQL}}"
1238
-
1242
+ {{end}}
1239
1243
{{if .Arg.EmitStruct}}
1240
1244
type {{.Arg.Type}} struct { {{- range .Arg.Struct.Fields}}
1241
1245
{{.Name}} {{.Type}} {{if $.EmitJSONTags}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
@@ -1269,16 +1273,20 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Ty
1269
1273
{{range .Comments}}//{{.}}
1270
1274
{{end -}}
1271
1275
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.Type}}, error) {
1276
+ var items []{{.Ret.Type}}
1277
+ {{if .Arg.LocalSQLQuery}}
1278
+ querySQL := "{{.Arg.LocalSQLQuery}}"
1279
+ {{end}}
1272
1280
{{- if $.EmitPreparedQueries}}
1273
- rows, err := q.query(ctx, q.{{.FieldName}}, {{ .ConstantName}}, {{.Arg.Params}})
1281
+ rows, err := q.query(ctx, q.{{.FieldName}},{{if .Arg.LocalSQLQuery}} querySQL {{else}} {{ .ConstantName}} {{end }}, {{.Arg.Params}})
1274
1282
{{- else}}
1275
- rows, err := q.db.QueryContext(ctx, {{. ConstantName}}, {{.Arg.Params}})
1283
+ rows, err := q.db.QueryContext(ctx, {{if .Arg.LocalSQLQuery}} querySQL {{else}} {{. ConstantName}} {{end }}, {{.Arg.Params}})
1276
1284
{{- end}}
1277
1285
if err != nil {
1278
1286
return nil, err
1279
1287
}
1280
1288
defer rows.Close()
1281
- var items []{{.Ret.Type}}
1289
+
1282
1290
for rows.Next() {
1283
1291
var {{.Ret.Name}} {{.Ret.Type}}
1284
1292
if err := rows.Scan({{.Ret.Scan}}); err != nil {
0 commit comments