Skip to content

Commit

Permalink
TODO: add flag for custom params
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Santi authored and kyleconroy committed Nov 25, 2024
1 parent 7f49775 commit fdd7b3e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/codegen/golang/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ApplySchema(query string) string {
// Helper function to check if a word is a relevant SQL keyword
func isSQLKeyword(word string) bool {
switch word {
case "FROM", "JOIN", "LEFT JOIN", "RIGHT JOIN", "FULL JOIN", "INNER JOIN", "CROSS JOIN", "UPDATE", "DELETE FROM", "INSERT INTO":
case "FROM", "JOIN", "UPDATE", "INTO":
return true
}
return false
Expand Down
31 changes: 25 additions & 6 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }}
var {{.Ret.Name}} {{.Ret.Type}}
{{- end}}
{{- if emitSchemaName }}
err := row.Scan(strings.ReplaceAll({{.Ret.Scan}}, "%s", schema))
{{- if emitSchemaName }}
err := row.Scan({{.Ret.Scan}})
{{- else }}
err := row.Scan({{.Ret.Scan}})
{{- end }}
Expand All @@ -58,6 +58,8 @@ type {{.MethodName}}FilterParams struct {
LikeParams []{{.MethodName}}Filter
SinceParams []{{.MethodName}}Filter
MaxParams []{{.MethodName}}Filter
SortParam string
SortOrder string
}

func (q *Queries) {{.MethodName}}(ctx context.Context, schema string, filterParams {{.MethodName}}FilterParams, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {;
Expand Down Expand Up @@ -213,14 +215,16 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{- if emitSchemaName }}
query := strings.ReplaceAll({{.ConstantName}}, "%s", schema);

// Extract the columns from the query;
columns := columnRegex.FindString(query);

{{- /**
If the query has a filter, we need to add the filter to the query
and the query params.
*/}}

{{- if eq .Cmd ":many"}}

// Extract the columns from the query;
columns := columnRegex.FindString(query);

isFirstFilter := true;
var queryParams []interface{};

Expand Down Expand Up @@ -309,10 +313,25 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
queryParams = append(queryParams, filter.Value)
};

{{- queryRetval . }} {{ queryMethod . }}(ctx, query, {{.Arg.Params}})
if filterParams.SortParam != "" {
query += " ORDER BY " + filterParams.SortParam + " " + filterParams.SortOrder
}

// If there is not the ; at the end, add it
if !strings.HasSuffix(query, ";") {
query += ";"
};

{{- queryRetval . }} {{ queryMethod . }}(ctx, query, queryParams...)

{{- end }}

{{- else }}
{{- queryRetval . }} {{ queryMethod . }}(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end -}}

{{- if ne .Cmd ":many"}}
{{- queryRetval . }} {{ queryMethod . }}(ctx, query, {{.Arg.Params}})
{{- end -}}
{{- end -}}
{{end}}

0 comments on commit fdd7b3e

Please sign in to comment.