snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
134
internal/codegen/golang/templates/pgx/batchCode.tmpl
Normal file
134
internal/codegen/golang/templates/pgx/batchCode.tmpl
Normal file
@@ -0,0 +1,134 @@
|
||||
{{define "batchCodePgx"}}
|
||||
|
||||
var (
|
||||
ErrBatchAlreadyClosed = errors.New("batch already closed")
|
||||
)
|
||||
|
||||
{{range .GoQueries}}
|
||||
{{if eq (hasPrefix .Cmd ":batch") true }}
|
||||
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
|
||||
{{escape .SQL}}
|
||||
{{$.Q}}
|
||||
|
||||
type {{.MethodName}}BatchResults struct {
|
||||
br pgx.BatchResults
|
||||
tot int
|
||||
closed bool
|
||||
}
|
||||
|
||||
{{if .Arg.Struct}}
|
||||
type {{.Arg.Type}} struct { {{- range .Arg.Struct.Fields}}
|
||||
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if .Ret.EmitStruct}}
|
||||
type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
|
||||
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, {{ if $.EmitMethodsWithDBArgument}}db DBTX,{{end}} {{.Arg.SlicePair}}) *{{.MethodName}}BatchResults {
|
||||
batch := &pgx.Batch{}
|
||||
for _, a := range {{index .Arg.Name}} {
|
||||
vals := []any{
|
||||
{{- if .Arg.Struct }}
|
||||
{{- range .Arg.Struct.Fields }}
|
||||
a.{{.Name}},
|
||||
{{- end }}
|
||||
{{- else }}
|
||||
a,
|
||||
{{- end }}
|
||||
}
|
||||
batch.Queue({{.ConstantName}}, vals...)
|
||||
}
|
||||
br := {{if not $.EmitMethodsWithDBArgument}}q.{{end}}db.SendBatch(ctx, batch)
|
||||
return &{{.MethodName}}BatchResults{br,len({{.Arg.Name}}),false}
|
||||
}
|
||||
|
||||
{{if eq .Cmd ":batchexec"}}
|
||||
func (b *{{.MethodName}}BatchResults) Exec(f func(int, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
_, err := b.br.Exec()
|
||||
if f != nil {
|
||||
f(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if eq .Cmd ":batchmany"}}
|
||||
func (b *{{.MethodName}}BatchResults) Query(f func(int, []{{.Ret.DefineType}}, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
{{- if $.EmitEmptySlices}}
|
||||
items := []{{.Ret.DefineType}}{}
|
||||
{{else}}
|
||||
var items []{{.Ret.DefineType}}
|
||||
{{end -}}
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, items, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
err := func() error {
|
||||
rows, err := b.br.Query()
|
||||
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
|
||||
}
|
||||
items = append(items, {{.Ret.ReturnName}})
|
||||
}
|
||||
return rows.Err()
|
||||
}()
|
||||
if f != nil {
|
||||
f(t, items, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if eq .Cmd ":batchone"}}
|
||||
func (b *{{.MethodName}}BatchResults) QueryRow(f func(int, {{.Ret.DefineType}}, error)) {
|
||||
defer b.br.Close()
|
||||
for t := 0; t < b.tot; t++ {
|
||||
var {{.Ret.Name}} {{.Ret.Type}}
|
||||
if b.closed {
|
||||
if f != nil {
|
||||
f(t, {{if .Ret.IsPointer}}nil{{else}}{{.Ret.Name}}{{end}}, ErrBatchAlreadyClosed)
|
||||
}
|
||||
continue
|
||||
}
|
||||
row := b.br.QueryRow()
|
||||
err := row.Scan({{.Ret.Scan}})
|
||||
if f != nil {
|
||||
f(t, {{.Ret.ReturnName}}, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
func (b *{{.MethodName}}BatchResults) Close() error {
|
||||
b.closed = true
|
||||
return b.br.Close()
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
51
internal/codegen/golang/templates/pgx/copyfromCopy.tmpl
Normal file
51
internal/codegen/golang/templates/pgx/copyfromCopy.tmpl
Normal file
@@ -0,0 +1,51 @@
|
||||
{{define "copyfromCodePgx"}}
|
||||
{{range .GoQueries}}
|
||||
{{if eq .Cmd ":copyfrom" }}
|
||||
// iteratorFor{{.MethodName}} implements pgx.CopyFromSource.
|
||||
type iteratorFor{{.MethodName}} struct {
|
||||
rows []{{.Arg.DefineType}}
|
||||
skippedFirstNextCall bool
|
||||
}
|
||||
|
||||
func (r *iteratorFor{{.MethodName}}) Next() bool {
|
||||
if len(r.rows) == 0 {
|
||||
return false
|
||||
}
|
||||
if !r.skippedFirstNextCall {
|
||||
r.skippedFirstNextCall = true
|
||||
return true
|
||||
}
|
||||
r.rows = r.rows[1:]
|
||||
return len(r.rows) > 0
|
||||
}
|
||||
|
||||
func (r iteratorFor{{.MethodName}}) Values() ([]any, error) {
|
||||
return []any{
|
||||
{{- if .Arg.Struct }}
|
||||
{{- range .Arg.Struct.Fields }}
|
||||
r.rows[0].{{.Name}},
|
||||
{{- end }}
|
||||
{{- else }}
|
||||
r.rows[0],
|
||||
{{- end }}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r iteratorFor{{.MethodName}}) Err() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{- if $.EmitMethodsWithDBArgument -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.SlicePair}}) (int64, error) {
|
||||
return db.CopyFrom(ctx, {{.TableIdentifierAsGoSlice}}, {{.Arg.ColumnNamesAsGoSlice}}, &iteratorFor{{.MethodName}}{rows: {{.Arg.Name}}})
|
||||
{{- else -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.SlicePair}}) (int64, error) {
|
||||
return q.db.CopyFrom(ctx, {{.TableIdentifierAsGoSlice}}, {{.Arg.ColumnNamesAsGoSlice}}, &iteratorFor{{.MethodName}}{rows: {{.Arg.Name}}})
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
37
internal/codegen/golang/templates/pgx/dbCode.tmpl
Normal file
37
internal/codegen/golang/templates/pgx/dbCode.tmpl
Normal file
@@ -0,0 +1,37 @@
|
||||
{{define "dbCodeTemplatePgx"}}
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...any) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...any) pgx.Row
|
||||
{{- if .UsesCopyFrom }}
|
||||
CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
|
||||
{{- end }}
|
||||
{{- if .UsesBatch }}
|
||||
SendBatch(context.Context, *pgx.Batch) pgx.BatchResults
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
{{ if .EmitMethodsWithDBArgument}}
|
||||
func New() *Queries {
|
||||
return &Queries{}
|
||||
{{- else -}}
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
{{if not .EmitMethodsWithDBArgument}}
|
||||
db DBTX
|
||||
{{end}}
|
||||
}
|
||||
|
||||
{{if not .EmitMethodsWithDBArgument}}
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
73
internal/codegen/golang/templates/pgx/interfaceCode.tmpl
Normal file
73
internal/codegen/golang/templates/pgx/interfaceCode.tmpl
Normal file
@@ -0,0 +1,73 @@
|
||||
{{define "interfaceCodePgx"}}
|
||||
type Querier interface {
|
||||
{{- $dbtxParam := .EmitMethodsWithDBArgument -}}
|
||||
{{- range .GoQueries}}
|
||||
{{- if and (eq .Cmd ":one") ($dbtxParam) }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error)
|
||||
{{- else if eq .Cmd ":one" }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error)
|
||||
{{- end}}
|
||||
{{- if and (eq .Cmd ":many") ($dbtxParam) }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error)
|
||||
{{- else if eq .Cmd ":many" }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error)
|
||||
{{- end}}
|
||||
{{- if and (eq .Cmd ":exec") ($dbtxParam) }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) error
|
||||
{{- else if eq .Cmd ":exec" }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error
|
||||
{{- end}}
|
||||
{{- if and (eq .Cmd ":execrows") ($dbtxParam) }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error)
|
||||
{{- else if eq .Cmd ":execrows" }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error)
|
||||
{{- end}}
|
||||
{{- if and (eq .Cmd ":execresult") ($dbtxParam) }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (pgconn.CommandTag, error)
|
||||
{{- else if eq .Cmd ":execresult" }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (pgconn.CommandTag, error)
|
||||
{{- end}}
|
||||
{{- if and (eq .Cmd ":copyfrom") ($dbtxParam) }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.SlicePair}}) (int64, error)
|
||||
{{- else if eq .Cmd ":copyfrom" }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, {{.Arg.SlicePair}}) (int64, error)
|
||||
{{- end}}
|
||||
{{- if and (or (eq .Cmd ":batchexec") (eq .Cmd ":batchmany") (eq .Cmd ":batchone")) ($dbtxParam) }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.SlicePair}}) *{{.MethodName}}BatchResults
|
||||
{{- else if or (eq .Cmd ":batchexec") (eq .Cmd ":batchmany") (eq .Cmd ":batchone") }}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{.MethodName}}(ctx context.Context, {{.Arg.SlicePair}}) *{{.MethodName}}BatchResults
|
||||
{{- end}}
|
||||
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
{{end}}
|
||||
142
internal/codegen/golang/templates/pgx/queryCode.tmpl
Normal file
142
internal/codegen/golang/templates/pgx/queryCode.tmpl
Normal file
@@ -0,0 +1,142 @@
|
||||
{{define "queryCodePgx"}}
|
||||
{{range .GoQueries}}
|
||||
{{if $.OutputQuery .SourceName}}
|
||||
{{if and (ne .Cmd ":copyfrom") (ne (hasPrefix .Cmd ":batch") true)}}
|
||||
const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
|
||||
{{escape .SQL}}
|
||||
{{$.Q}}
|
||||
{{end}}
|
||||
|
||||
{{if ne (hasPrefix .Cmd ":batch") true}}
|
||||
{{if .Arg.EmitStruct}}
|
||||
type {{.Arg.Type}} struct { {{- range .Arg.Struct.Fields}}
|
||||
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if .Ret.EmitStruct}}
|
||||
type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
|
||||
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{if eq .Cmd ":one"}}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{- if $.EmitMethodsWithDBArgument -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
|
||||
row := db.QueryRow(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- else -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
|
||||
row := q.db.QueryRow(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- end}}
|
||||
{{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }}
|
||||
var {{.Ret.Name}} {{.Ret.Type}}
|
||||
{{- end}}
|
||||
err := row.Scan({{.Ret.Scan}})
|
||||
{{- if $.WrapErrors}}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("query {{.MethodName}}: %w", err)
|
||||
}
|
||||
{{- end}}
|
||||
return {{.Ret.ReturnName}}, err
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if eq .Cmd ":many"}}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{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}})
|
||||
{{- else -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
|
||||
rows, err := q.db.Query(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- end}}
|
||||
if err != nil {
|
||||
return nil, {{if $.WrapErrors}}fmt.Errorf("query {{.MethodName}}: %w", err){{else}}err{{end}}
|
||||
}
|
||||
defer rows.Close()
|
||||
{{- if $.EmitEmptySlices}}
|
||||
items := []{{.Ret.DefineType}}{}
|
||||
{{else}}
|
||||
var items []{{.Ret.DefineType}}
|
||||
{{end -}}
|
||||
for rows.Next() {
|
||||
var {{.Ret.Name}} {{.Ret.Type}}
|
||||
if err := rows.Scan({{.Ret.Scan}}); err != nil {
|
||||
return nil, {{if $.WrapErrors}}fmt.Errorf("query {{.MethodName}}: %w", err){{else}}err{{end}}
|
||||
}
|
||||
items = append(items, {{.Ret.ReturnName}})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, {{if $.WrapErrors}}fmt.Errorf("query {{.MethodName}}: %w", err){{else}}err{{end}}
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if eq .Cmd ":exec"}}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{- if $.EmitMethodsWithDBArgument -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) error {
|
||||
_, err := db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- else -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error {
|
||||
_, err := q.db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- end}}
|
||||
{{- if $.WrapErrors }}
|
||||
if err != nil {
|
||||
return fmt.Errorf("query {{.MethodName}}: %w", err)
|
||||
}
|
||||
return nil
|
||||
{{- else }}
|
||||
return err
|
||||
{{- end }}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if eq .Cmd ":execrows"}}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{if $.EmitMethodsWithDBArgument -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (int64, error) {
|
||||
result, err := db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- else -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- end}}
|
||||
if err != nil {
|
||||
return 0, {{if $.WrapErrors}}fmt.Errorf("query {{.MethodName}}: %w", err){{else}}err{{end}}
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if eq .Cmd ":execresult"}}
|
||||
{{range .Comments}}//{{.}}
|
||||
{{end -}}
|
||||
{{- if $.EmitMethodsWithDBArgument -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (pgconn.CommandTag, error) {
|
||||
{{queryRetval .}} db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- else -}}
|
||||
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (pgconn.CommandTag, error) {
|
||||
{{queryRetval .}} q.db.Exec(ctx, {{.ConstantName}}, {{.Arg.Params}})
|
||||
{{- end}}
|
||||
{{- if $.WrapErrors}}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("query {{.MethodName}}: %w", err)
|
||||
}
|
||||
return result, err
|
||||
{{- end}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user