Skip to content

Commit 91ee0b0

Browse files
Fix pgx.Tx use and array support
1 parent 69ccc94 commit 91ee0b0

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

internal/codegen/golang/imports.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ func (i *importer) dbImports() fileImports {
142142
pkg = append(pkg, ImportSpec{Path: "github.com/jackc/pgx/v5"})
143143
if len(i.CompositeTypes) > 0 {
144144
pkg = append(pkg, ImportSpec{Path: "github.com/jackc/pgx/v5/pgtype"})
145+
if !i.Options.EmitMethodsWithDbArgument {
146+
pkg = append(pkg, ImportSpec{Path: "fmt"})
147+
}
145148
}
146149
default:
147150
std = append(std, ImportSpec{Path: "database/sql"})

internal/codegen/golang/templates/pgx/dbCode.tmpl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type DBTX interface {
1010
{{- if .UsesBatch }}
1111
SendBatch(context.Context, *pgx.Batch) pgx.BatchResults
1212
{{- end }}
13+
}
14+
15+
type DB interface {
16+
DBTX
1317
{{- if gt (len .CompositeTypes) 0 }}
1418
LoadTypes(ctx context.Context, typeNames []string) ([]*pgtype.Type, error)
1519
TypeMap() *pgtype.Map
@@ -20,7 +24,7 @@ type DBTX interface {
2024
func New() *Queries {
2125
return &Queries{}
2226
{{- else -}}
23-
func New(db DBTX) *Queries {
27+
func New(db DB) *Queries {
2428
return &Queries{db: db}
2529
{{- end}}
2630
}
@@ -34,13 +38,17 @@ type Queries struct {
3438
{{if gt (len .CompositeTypes) 0}}
3539
{{if not .EmitMethodsWithDBArgument}}
3640
func (q *Queries) RegisterTypes(ctx context.Context) error {
37-
db := q.db
41+
db, ok := q.db.(DB)
42+
if !ok {
43+
return fmt.Errorf("cannot register types with a transaction object")
44+
}
3845
{{else}}
39-
func (q *Queries) RegisterTypes(ctx context.Context, db *Queries) error {
46+
func (q *Queries) RegisterTypes(ctx context.Context, db DB) error {
4047
{{end}}
4148
typeNames := []string{
4249
{{- range .CompositeTypes}}
4350
"{{.SQLName}}",
51+
"_{{.SQLName}}",
4452
{{- end}}
4553
}
4654
dataTypes, err := db.LoadTypes(ctx, typeNames)

0 commit comments

Comments
 (0)