Skip to content
Open
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
6 changes: 4 additions & 2 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"strings"
"text/template"

"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"github.com/sqlc-dev/plugin-sdk-go/sdk"
"github.com/sqlc-dev/plugin-sdk-go/metadata"
"github.com/sqlc-dev/plugin-sdk-go/plugin"
"github.com/sqlc-dev/plugin-sdk-go/sdk"
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
)

type tmplCtx struct {
Expand All @@ -34,6 +34,7 @@ type tmplCtx struct {
EmitPreparedQueries bool
EmitInterface bool
EmitEmptySlices bool
EmitResultSlicePointer bool
EmitMethodsWithDBArgument bool
EmitEnumValidMethod bool
EmitAllEnumValues bool
Expand Down Expand Up @@ -174,6 +175,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
EmitDBTags: options.EmitDbTags,
EmitPreparedQueries: options.EmitPreparedQueries,
EmitEmptySlices: options.EmitEmptySlices,
EmitResultSlicePointer: options.EmitResultSlicePointer,
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
EmitEnumValidMethod: options.EmitEnumValidMethod,
EmitAllEnumValues: options.EmitAllEnumValues,
Expand Down
1 change: 1 addition & 0 deletions internal/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Options struct {
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
EmitExportedQueries bool `json:"emit_exported_queries" yaml:"emit_exported_queries"`
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
EmitResultSlicePointer bool `json:"emit_result_slice_pointer" yaml:"emit_result_slice_pointer"`
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
EmitMethodsWithDbArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
Expand Down
23 changes: 23 additions & 0 deletions internal/templates/pgx/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.De
{{if eq .Cmd ":many"}}
{{range .Comments}}//{{.}}
{{end -}}
{{- if $.EmitResultSlicePointer -}}
{{- if $.EmitMethodsWithDBArgument -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{if .Arg.Pair}}{{.Arg.Pair}}, {{end}}dest *[]{{.Ret.DefineType}}) error {
rows, err := db.Query(ctx, {{.ConstantName}}{{if .Arg.Params}}, {{.Arg.Params}}{{end}})
{{- else -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{if .Arg.Pair}}{{.Arg.Pair}}, {{end}}dest *[]{{.Ret.DefineType}}) error {
rows, err := q.db.Query(ctx, {{.ConstantName}}{{if .Arg.Params}}, {{.Arg.Params}}{{end}})
{{- end}}
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var {{.Ret.Name}} {{.Ret.Type}}
if err := rows.Scan({{.Ret.Scan}}); err != nil {
return err
}
*dest = append(*dest, {{.Ret.ReturnName}})
}
return rows.Err()
}
{{- else -}}
{{- if $.EmitMethodsWithDBArgument -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
rows, err := db.Query(ctx, {{.ConstantName}}, {{.Arg.Params}})
Expand Down Expand Up @@ -72,6 +94,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.
}
return items, nil
}
{{- end}}
{{end}}

{{if eq .Cmd ":exec"}}
Expand Down