@@ -44,7 +44,21 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
44
44
{{range .Comments}}//{{.}}
45
45
{{end -}}
46
46
{{- if emitSchemaName }}
47
- func (q *Queries) {{.MethodName}}(ctx context.Context, schema string, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
47
+
48
+ type {{.MethodName}}Filter struct {
49
+ FieldName string
50
+ Value string
51
+ }
52
+
53
+ type {{.MethodName}}FilterParams struct {
54
+ ExactParams []{{.MethodName}}Filter
55
+ InParams []{{.MethodName}}Filter
56
+ LikeParams []{{.MethodName}}Filter
57
+ SinceParams []{{.MethodName}}Filter
58
+ MaxParams []{{.MethodName}}Filter
59
+ }
60
+
61
+ func (q *Queries) {{.MethodName}}(ctx context.Context, schema string, filterParams {{.MethodName}}FilterParams, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
48
62
{{- else }}
49
63
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
50
64
{{- end }}
@@ -140,6 +154,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
140
154
{{- if .Arg.HasSqlcSlices }}
141
155
query := strings.ReplaceAll({{.ConstantName}}, "%s", schema)
142
156
var queryParams []interface{}
157
+
143
158
{{- if .Arg.Struct }}
144
159
{{- $arg := .Arg }}
145
160
{{- range .Arg.Struct.Fields }}
@@ -170,15 +185,18 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
170
185
query = strings.ReplaceAll(query, "/*SLICE:{{.Arg.Column.Name}}*/?", "NULL")
171
186
}
172
187
{{- end }}
188
+
173
189
{{- if emitPreparedQueries }}
174
190
{{- if emitSchemaName }}
175
- {{ queryRetval . }} {{ queryMethod . }}(ctx, nil, strings.ReplaceAll(query, "%s", schema), queryParams...)
191
+ replacedQuery := strings.ReplaceAll(query, "%s", schema)
192
+ {{ queryRetval . }} {{ queryMethod . }}(ctx, nil, replacedQuery, queryParams...)
176
193
{{- else }}
177
194
{{ queryRetval . }} {{ queryMethod . }}(ctx, nil, query, queryParams...)
178
195
{{- end }}
179
196
{{- else}}
180
197
{{- if emitSchemaName }}
181
- {{ queryRetval . }} {{ queryMethod . }}(ctx, strings.ReplaceAll(query, "%s", schema), queryParams...)
198
+ replacedQuery := strings.ReplaceAll(query, "%s", schema)
199
+ {{ queryRetval . }} {{ queryMethod . }}(ctx, replacedQuery, queryParams...)
182
200
{{- else }}
183
201
{{ queryRetval . }} {{ queryMethod . }}(ctx, query, queryParams...)
184
202
{{- end -}}
@@ -187,7 +205,79 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
187
205
{{- queryRetval . }} {{ queryMethod . }}(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
188
206
{{- else}}
189
207
{{- if emitSchemaName }}
190
- {{- queryRetval . }} {{ queryMethod . }}(ctx, strings.ReplaceAll({{.ConstantName}}, "%s", schema), {{.Arg.Params}})
208
+ query := strings.ReplaceAll({{.ConstantName}}, "%s", schema);
209
+
210
+ {{- /**
211
+ If the query has a filter, we need to add the filter to the query
212
+ and the query params.
213
+ */}}
214
+
215
+ isFirstFilter := true;
216
+ var queryParams []interface{};
217
+
218
+ for _, filter := range filterParams.ExactParams {
219
+ if isFirstFilter {
220
+ query += " WHERE "
221
+ isFirstFilter = false
222
+ } else {
223
+ query += " AND "
224
+ }
225
+
226
+ query += filter.FieldName + " = ?"
227
+ queryParams = append(queryParams, filter.Value)
228
+ };
229
+
230
+ for _, filter := range filterParams.InParams {
231
+ if isFirstFilter {
232
+ query += " WHERE "
233
+ isFirstFilter = false
234
+ } else {
235
+ query += " AND "
236
+ }
237
+
238
+ query += filter.FieldName + " IN (?)"
239
+ queryParams = append(queryParams, filter.Value)
240
+ };
241
+
242
+ for _, filter := range filterParams.LikeParams {
243
+ if isFirstFilter {
244
+ query += " WHERE "
245
+ isFirstFilter = false
246
+ } else {
247
+ query += " AND "
248
+ }
249
+
250
+ query += filter.FieldName + " LIKE ?"
251
+ queryParams = append(queryParams, filter.Value)
252
+ };
253
+
254
+ for _, filter := range filterParams.SinceParams {
255
+ if isFirstFilter {
256
+ query += " WHERE "
257
+ isFirstFilter = false
258
+ } else {
259
+ query += " AND "
260
+ }
261
+
262
+ query += filter.FieldName + " > ?"
263
+ queryParams = append(queryParams, filter.Value)
264
+ };
265
+
266
+ for _, filter := range filterParams.MaxParams {
267
+ if isFirstFilter {
268
+ query += " WHERE "
269
+ isFirstFilter = false
270
+ } else {
271
+ query += " AND "
272
+ }
273
+
274
+ query += filter.FieldName + " < ?"
275
+ queryParams = append(queryParams, filter.Value)
276
+ };
277
+
278
+
279
+ {{- queryRetval . }} {{ queryMethod . }}(ctx, query, {{.Arg.Params}})
280
+
191
281
{{- else }}
192
282
{{- queryRetval . }} {{ queryMethod . }}(ctx, {{.ConstantName}}, {{.Arg.Params}})
193
283
{{- end -}}
0 commit comments