Skip to content

Commit fdd7b3e

Browse files
Nicholas Santikyleconroy
authored andcommitted
TODO: add flag for custom params
1 parent 7f49775 commit fdd7b3e

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

internal/codegen/golang/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ApplySchema(query string) string {
4646
// Helper function to check if a word is a relevant SQL keyword
4747
func isSQLKeyword(word string) bool {
4848
switch word {
49-
case "FROM", "JOIN", "LEFT JOIN", "RIGHT JOIN", "FULL JOIN", "INNER JOIN", "CROSS JOIN", "UPDATE", "DELETE FROM", "INSERT INTO":
49+
case "FROM", "JOIN", "UPDATE", "INTO":
5050
return true
5151
}
5252
return false

internal/codegen/golang/templates/stdlib/queryCode.tmpl

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6365
func (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

Comments
 (0)