Skip to content

Dynamic query #2343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ func (v QueryValue) Params() string {
} else if !v.EmitStruct() && v.IsStruct() {
out = append(out, toLowerCase(f.Name))
} else {
out = append(out, v.Name+"."+f.Name)
if !f.Column.IsClause {
out = append(out, v.Name+"."+f.Name)
}
}
}
}
Expand Down Expand Up @@ -189,17 +191,24 @@ func (v QueryValue) Scan() string {
return "\n" + strings.Join(out, ",\n")
}

type QueryClause struct {
Position int
Name string
}

// A struct used to generate methods and fields on the Queries struct
type Query struct {
Cmd string
Comments []string
MethodName string
FieldName string
ConstantName string
SQL string
SourceName string
Ret QueryValue
Arg QueryValue
Cmd string
Comments []string
MethodName string
FieldName string
ConstantName string
SQL string
SourceName string
Ret QueryValue
Arg QueryValue
WhereClause *QueryClause
OrderbyClause *QueryClause
// Used for :copyfrom
Table *plugin.Identifier
}
Expand Down
41 changes: 39 additions & 2 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
}

type goColumn struct {
id int
id int
FilterNumber int
OrderByNumber int
*plugin.Column
embed *goEmbed
}
Expand Down Expand Up @@ -214,21 +216,55 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)

if len(query.Params) == 1 && qpl != 0 {
p := query.Params[0]

if p.Column.Name == "filter" && p.Column.Table == nil {
p.Column.IsClause = true
gq.WhereClause = &QueryClause{
Position: 1,
Name: "filter",
}
}
if p.Column.Name == "orderby" && p.Column.Table == nil {
p.Column.IsClause = true
gq.OrderbyClause = &QueryClause{
Position: 1,
Name: "orderby",
}
}
gq.Arg = QueryValue{
Name: paramName(p),
DBName: p.Column.GetName(),
Typ: goType(req, p.Column),
SQLDriver: sqlpkg,
Column: p.Column,
}

} else if len(query.Params) >= 1 {
var cols []goColumn

for _, p := range query.Params {
number := int(p.Number)

if p.Column.Name == "filter" && p.Column.Table == nil {
p.Column.IsClause = true
gq.WhereClause = &QueryClause{
Position: number,
Name: "arg.Filter",
}
}
if p.Column.Name == "orderby" && p.Column.Table == nil {
p.Column.IsClause = true
gq.OrderbyClause = &QueryClause{
Position: number,
Name: "arg.Orderby",
}
}
cols = append(cols, goColumn{
id: int(p.Number),
id: int(number),
Column: p.Column,
})
}

s, err := columnsToStruct(req, gq.MethodName+"Params", cols, false)
if err != nil {
return nil, err
Expand Down Expand Up @@ -378,6 +414,7 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []goColumn
Tags: tags,
Column: c.Column,
}
//f.Column.IsClause = c.IsClause // TODO: Is this needed?
if c.embed == nil {
f.Type = goType(req, c.Column)
} else {
Expand Down
22 changes: 18 additions & 4 deletions internal/codegen/golang/templates/pgx/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
{{end -}}
{{- if $.EmitMethodsWithDBArgument -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
row := db.QueryRow(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- template "makeSql" . -}}
row := db.QueryRow(ctx, sql, {{.Arg.Params}})
{{- else -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
row := q.db.QueryRow(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- template "makeSql" . -}}
row := q.db.QueryRow(ctx, sql, {{.Arg.Params}})
{{- end}}
{{- if ne .Arg.Pair .Ret.Pair }}
var {{.Ret.Name}} {{.Ret.Type}}
Expand All @@ -46,10 +48,12 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.De
{{end -}}
{{- if $.EmitMethodsWithDBArgument -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
rows, err := db.Query(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- template "makeSql" . -}}
rows, err := db.Query(ctx, sql, {{.Arg.Params}})
{{- else -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
rows, err := q.db.Query(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- template "makeSql" . -}}
rows, err := q.db.Query(ctx, sql, {{.Arg.Params}})
{{- end}}
if err != nil {
return nil, err
Expand Down Expand Up @@ -122,3 +126,13 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (pgconn.Co
{{end}}
{{end}}
{{end}}

{{define "makeSql"}}
sql := {{.ConstantName}}
{{- if .WhereClause }}
sql = strings.Replace(sql, "${{.WhereClause.Position}}::text", {{.WhereClause.Name}}, -1)
{{- end }}
{{- if .OrderbyClause }}
sql = strings.Replace(sql, "${{.OrderbyClause.Position}}::text", {{.OrderbyClause.Name}}, -1)
{{- end }}
{{end}}
1 change: 1 addition & 0 deletions internal/plugin/codegen.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.