@@ -31,8 +31,8 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
3131 {{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }}
3232 var {{.Ret.Name}} {{.Ret.Type}}
3333 {{- end}}
34- {{- if emitSchemaName }}
35- err := row.Scan(strings.ReplaceAll( {{.Ret.Scan}}, "%s", schema) )
34+ {{- if emitSchemaName }}
35+ err := row.Scan({{.Ret.Scan}})
3636 {{- else }}
3737 err := row.Scan({{.Ret.Scan}})
3838 {{- end }}
@@ -58,6 +58,8 @@ type {{.MethodName}}FilterParams struct {
5858 LikeParams []{{.MethodName}}Filter
5959 SinceParams []{{.MethodName}}Filter
6060 MaxParams []{{.MethodName}}Filter
61+ SortParam string
62+ SortOrder string
6163}
6264
6365func (q *Queries) {{.MethodName}}(ctx context.Context, schema string, filterParams {{.MethodName}}FilterParams, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {;
@@ -213,14 +215,16 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
213215 {{- if emitSchemaName }}
214216 query := strings.ReplaceAll({{.ConstantName}}, "%s", schema);
215217
216- // Extract the columns from the query;
217- columns := columnRegex.FindString(query);
218-
219218 {{- /**
220219 If the query has a filter, we need to add the filter to the query
221220 and the query params.
222221 */}}
223222
223+ {{- if eq .Cmd ":many"}}
224+
225+ // Extract the columns from the query;
226+ columns := columnRegex.FindString(query);
227+
224228 isFirstFilter := true;
225229 var queryParams []interface{};
226230
@@ -309,10 +313,25 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
309313 queryParams = append(queryParams, filter.Value)
310314 };
311315
312- {{- queryRetval . }} {{ queryMethod . }}(ctx, query, {{.Arg.Params}})
316+ if filterParams.SortParam != "" {
317+ query += " ORDER BY " + filterParams.SortParam + " " + filterParams.SortOrder
318+ }
319+
320+ // If there is not the ; at the end, add it
321+ if !strings.HasSuffix(query, ";") {
322+ query += ";"
323+ };
324+
325+ {{- queryRetval . }} {{ queryMethod . }}(ctx, query, queryParams...)
326+
327+ {{- end }}
313328
314329 {{- else }}
315330 {{- queryRetval . }} {{ queryMethod . }}(ctx, {{.ConstantName}}, {{.Arg.Params}})
316331 {{- end -}}
332+
333+ {{- if ne .Cmd ":many"}}
334+ {{- queryRetval . }} {{ queryMethod . }}(ctx, query, {{.Arg.Params}})
335+ {{- end -}}
317336 {{- end -}}
318337{{end}}
0 commit comments