From b52421341541e98ca5a8c08d8da6ed6c6033720b Mon Sep 17 00:00:00 2001 From: Joe Schafer Date: Mon, 1 Jan 2024 21:37:49 -0800 Subject: [PATCH] Upgrade to pgx v5 - For pggen row structs, implement pgx.RowScanner. - Remove the typeResolver, since we're punting type resolution to the pgx.Conn type map for now. #74 --- example/acceptance_test.go | 8 +- example/author/query.sql.go | 94 ++++-------- example/author/query.sql_test.go | 2 +- example/complex_params/query.sql.go | 6 +- example/composite/codegen_test.go | 2 +- example/composite/query.sql.go | 6 +- example/composite/query.sql_test.go | 2 +- example/custom_types/query.sql.go | 6 +- example/custom_types/query.sql_test.go | 4 +- example/device/query.sql.go | 6 +- example/device/query.sql_test.go | 2 +- example/domain/query.sql.go | 6 +- example/enums/query.sql.go | 6 +- example/enums/query.sql_test.go | 2 +- example/erp/order/customer.sql.go | 6 +- example/erp/order/customer.sql_test.go | 2 +- example/erp/order/price.sql.go | 2 +- example/function/query.sql.go | 6 +- example/go_pointer_types/query.sql.go | 6 +- .../inline_param_count/inline0/query.sql.go | 6 +- .../inline0/query.sql_test.go | 2 +- .../inline_param_count/inline1/query.sql.go | 6 +- .../inline1/query.sql_test.go | 2 +- .../inline_param_count/inline2/query.sql.go | 6 +- .../inline2/query.sql_test.go | 2 +- .../inline_param_count/inline3/query.sql.go | 6 +- .../inline3/query.sql_test.go | 2 +- example/ltree/codegen_test.go | 4 +- example/ltree/query.sql.go | 6 +- example/ltree/query.sql_test.go | 2 +- example/nested/query.sql.go | 10 +- example/pgcrypto/query.sql.go | 6 +- .../separate_out_dir/out/alpha_query.sql.0.go | 6 +- example/slices/query.sql.go | 6 +- example/syntax/query.sql.go | 6 +- example/void/query.sql.go | 6 +- generate.go | 2 +- go.mod | 7 +- go.sum | 21 ++- internal/codegen/golang/declarer_composite.go | 2 +- internal/codegen/golang/gotype/known_types.go | 142 +++++++++--------- internal/codegen/golang/gotype/types.go | 2 +- internal/codegen/golang/query.gotemplate | 31 +--- internal/codegen/golang/templater.go | 8 +- internal/codegen/golang/type_resolver_test.go | 6 +- internal/pg/column.go | 17 +-- internal/pg/column_test.go | 7 +- internal/pg/known_types.go | 4 +- internal/pg/query.sql.go | 128 +++++----------- internal/pg/type_cache.go | 19 ++- internal/pg/type_fetcher.go | 31 ++-- internal/pg/type_fetcher_test.go | 7 +- internal/pg/types.go | 89 ++++++----- internal/pgdocker/pgdocker.go | 2 +- internal/pginfer/pginfer.go | 14 +- internal/pgplan/pgplan.go | 2 +- internal/pgtest/pg_test_db.go | 2 +- 57 files changed, 346 insertions(+), 455 deletions(-) diff --git a/example/acceptance_test.go b/example/acceptance_test.go index c1e231a9..5a8cb469 100644 --- a/example/acceptance_test.go +++ b/example/acceptance_test.go @@ -7,7 +7,7 @@ import ( "context" "flag" "fmt" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/errs" "github.com/jschaf/pggen/internal/pgdocker" "math/rand" @@ -57,7 +57,7 @@ func TestExamples(t *testing.T) { "--go-type", "int8=int", "--go-type", "int4=int", "--go-type", "text=string", - "--go-type", "citext=github.com/jackc/pgtype.Text", + "--go-type", "citext=github.com/jackc/pgx/v5/pgtype.Text", }, }, { @@ -179,8 +179,8 @@ func TestExamples(t *testing.T) { args: []string{ "--schema-glob", "example/ltree/schema.sql", "--query-glob", "example/ltree/query.sql", - "--go-type", "ltree=github.com/jackc/pgtype.Text", - "--go-type", "_ltree=github.com/jackc/pgtype.TextArray", + "--go-type", "ltree=github.com/jackc/pgx/v5/pgtype.Text", + "--go-type", "_ltree=github.com/jackc/pgx/v5/pgtype.TextArray", }, }, { diff --git a/example/author/query.sql.go b/example/author/query.sql.go index e654ea13..2b892bec 100644 --- a/example/author/query.sql.go +++ b/example/author/query.sql.go @@ -5,9 +5,8 @@ package author import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" ) // Querier is a typesafe Go interface backed by SQL queries. @@ -48,8 +47,7 @@ type Querier interface { var _ Querier = &DBQuerier{} type DBQuerier struct { - conn genericConn // underlying Postgres transport to use - types *typeResolver // resolve types by name + conn genericConn // underlying Postgres transport to use } // genericConn is a connection like *pgx.Conn, pgx.Tx, or *pgxpool.Pool. @@ -61,36 +59,7 @@ type genericConn interface { // NewQuerier creates a DBQuerier that implements Querier. func NewQuerier(conn genericConn) *DBQuerier { - return &DBQuerier{conn: conn, types: newTypeResolver()} -} - -// typeResolver looks up the pgtype.ValueTranscoder by Postgres type name. -type typeResolver struct { - connInfo *pgtype.ConnInfo // types by Postgres type name -} - -func newTypeResolver() *typeResolver { - ci := pgtype.NewConnInfo() - return &typeResolver{connInfo: ci} -} - -// findValue find the OID, and pgtype.ValueTranscoder for a Postgres type name. -func (tr *typeResolver) findValue(name string) (uint32, pgtype.ValueTranscoder, bool) { - typ, ok := tr.connInfo.DataTypeForName(name) - if !ok { - return 0, nil, false - } - v := pgtype.NewValue(typ.Value) - return typ.OID, v.(pgtype.ValueTranscoder), true -} - -// setValue sets the value of a ValueTranscoder to a value that should always -// work and panics if it fails. -func (tr *typeResolver) setValue(vt pgtype.ValueTranscoder, val interface{}) pgtype.ValueTranscoder { - if err := vt.Set(val); err != nil { - panic(fmt.Sprintf("set ValueTranscoder %T to %+v: %s", vt, val, err)) - } - return vt + return &DBQuerier{conn: conn} } const findAuthorByIDSQL = `SELECT * FROM author WHERE author_id = $1;` @@ -102,12 +71,17 @@ type FindAuthorByIDRow struct { Suffix *string `json:"suffix"` } +func (r *FindAuthorByIDRow) scanRow(rows pgx.Rows) error { + return rows.Scan(&r.AuthorID, &r.FirstName, &r.LastName, &r.Suffix) +} + // FindAuthorByID implements Querier.FindAuthorByID. func (q *DBQuerier) FindAuthorByID(ctx context.Context, authorID int32) (FindAuthorByIDRow, error) { ctx = context.WithValue(ctx, "pggen_query_name", "FindAuthorByID") row := q.conn.QueryRow(ctx, findAuthorByIDSQL, authorID) var item FindAuthorByIDRow - if err := row.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil { + s := scanner[*FindAuthorByIDRow]{&item} + if err := row.Scan(s); err != nil { return item, fmt.Errorf("query FindAuthorByID: %w", err) } return item, nil @@ -122,6 +96,10 @@ type FindAuthorsRow struct { Suffix *string `json:"suffix"` } +func (r *FindAuthorsRow) scanRow(rows pgx.Rows) error { + return rows.Scan(&r.AuthorID, &r.FirstName, &r.LastName, &r.Suffix) +} + // FindAuthors implements Querier.FindAuthors. func (q *DBQuerier) FindAuthors(ctx context.Context, firstName string) ([]FindAuthorsRow, error) { ctx = context.WithValue(ctx, "pggen_query_name", "FindAuthors") @@ -131,9 +109,11 @@ func (q *DBQuerier) FindAuthors(ctx context.Context, firstName string) ([]FindAu } defer rows.Close() items := []FindAuthorsRow{} + s := scanner[*FindAuthorsRow]{} for rows.Next() { var item FindAuthorsRow - if err := rows.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil { + s.item = &item + if err := rows.Scan(s); err != nil { return nil, fmt.Errorf("scan FindAuthors row: %w", err) } items = append(items, item) @@ -151,6 +131,10 @@ type FindAuthorNamesRow struct { LastName *string `json:"last_name"` } +func (r *FindAuthorNamesRow) scanRow(rows pgx.Rows) error { + return rows.Scan(&r.FirstName, &r.LastName) +} + // FindAuthorNames implements Querier.FindAuthorNames. func (q *DBQuerier) FindAuthorNames(ctx context.Context, authorID int32) ([]FindAuthorNamesRow, error) { ctx = context.WithValue(ctx, "pggen_query_name", "FindAuthorNames") @@ -160,9 +144,11 @@ func (q *DBQuerier) FindAuthorNames(ctx context.Context, authorID int32) ([]Find } defer rows.Close() items := []FindAuthorNamesRow{} + s := scanner[*FindAuthorNamesRow]{} for rows.Next() { var item FindAuthorNamesRow - if err := rows.Scan(&item.FirstName, &item.LastName); err != nil { + s.item = &item + if err := rows.Scan(s); err != nil { return nil, fmt.Errorf("scan FindAuthorNames row: %w", err) } items = append(items, item) @@ -275,12 +261,17 @@ type InsertAuthorSuffixRow struct { Suffix *string `json:"suffix"` } +func (r *InsertAuthorSuffixRow) scanRow(rows pgx.Rows) error { + return rows.Scan(&r.AuthorID, &r.FirstName, &r.LastName, &r.Suffix) +} + // InsertAuthorSuffix implements Querier.InsertAuthorSuffix. func (q *DBQuerier) InsertAuthorSuffix(ctx context.Context, params InsertAuthorSuffixParams) (InsertAuthorSuffixRow, error) { ctx = context.WithValue(ctx, "pggen_query_name", "InsertAuthorSuffix") row := q.conn.QueryRow(ctx, insertAuthorSuffixSQL, params.FirstName, params.LastName, params.Suffix) var item InsertAuthorSuffixRow - if err := row.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil { + s := scanner[*InsertAuthorSuffixRow]{&item} + if err := row.Scan(s); err != nil { return item, fmt.Errorf("query InsertAuthorSuffix: %w", err) } return item, nil @@ -312,27 +303,8 @@ func (q *DBQuerier) ArrayAggFirstName(ctx context.Context, authorID int32) ([]st return item, nil } -// textPreferrer wraps a pgtype.ValueTranscoder and sets the preferred encoding -// format to text instead binary (the default). pggen uses the text format -// when the OID is unknownOID because the binary format requires the OID. -// Typically occurs for unregistered types. -type textPreferrer struct { - pgtype.ValueTranscoder - typeName string -} - -// PreferredParamFormat implements pgtype.ParamFormatPreferrer. -func (t textPreferrer) PreferredParamFormat() int16 { return pgtype.TextFormatCode } +type rowScanner interface{ scanRow(rows pgx.Rows) error } -func (t textPreferrer) NewTypeValue() pgtype.Value { - return textPreferrer{ValueTranscoder: pgtype.NewValue(t.ValueTranscoder).(pgtype.ValueTranscoder), typeName: t.typeName} -} - -func (t textPreferrer) TypeName() string { - return t.typeName -} +type scanner[T rowScanner] struct{ item T } -// unknownOID means we don't know the OID for a type. This is okay for decoding -// because pgx call DecodeText or DecodeBinary without requiring the OID. For -// encoding parameters, pggen uses textPreferrer if the OID is unknown. -const unknownOID = 0 +func (s scanner[T]) ScanRow(rows pgx.Rows) error { return s.item.scanRow(rows) } diff --git a/example/author/query.sql_test.go b/example/author/query.sql_test.go index 9b4f753b..55521e45 100644 --- a/example/author/query.sql_test.go +++ b/example/author/query.sql_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/require" "testing" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" ) diff --git a/example/complex_params/query.sql.go b/example/complex_params/query.sql.go index 4315bdc5..fc0589f8 100644 --- a/example/complex_params/query.sql.go +++ b/example/complex_params/query.sql.go @@ -5,9 +5,9 @@ package complex_params import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/composite/codegen_test.go b/example/composite/codegen_test.go index 68334b1f..3972f024 100644 --- a/example/composite/codegen_test.go +++ b/example/composite/codegen_test.go @@ -28,7 +28,7 @@ func TestGenerate_Go_Example_Composite(t *testing.T) { "int8": "int", "int4": "int", "text": "string", - "citext": "github.com/jackc/pgtype.Text", + "citext": "github.com/jackc/pgx/v5/pgtype.Text", }, }) if err != nil { diff --git a/example/composite/query.sql.go b/example/composite/query.sql.go index c4b9b0b5..a62844af 100644 --- a/example/composite/query.sql.go +++ b/example/composite/query.sql.go @@ -5,9 +5,9 @@ package composite import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/composite/query.sql_test.go b/example/composite/query.sql_test.go index 1a9d0d0f..f8e8e82e 100644 --- a/example/composite/query.sql_test.go +++ b/example/composite/query.sql_test.go @@ -2,7 +2,7 @@ package composite import ( "context" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/difftest" "github.com/jschaf/pggen/internal/pgtest" "github.com/jschaf/pggen/internal/ptrs" diff --git a/example/custom_types/query.sql.go b/example/custom_types/query.sql.go index 133c424c..326d2b2d 100644 --- a/example/custom_types/query.sql.go +++ b/example/custom_types/query.sql.go @@ -5,9 +5,9 @@ package custom_types import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/example/custom_types/mytype" ) diff --git a/example/custom_types/query.sql_test.go b/example/custom_types/query.sql_test.go index 1912fe94..e98d7cd2 100644 --- a/example/custom_types/query.sql_test.go +++ b/example/custom_types/query.sql_test.go @@ -2,7 +2,7 @@ package custom_types import ( "context" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/pgtest" "github.com/jschaf/pggen/internal/texts" "github.com/stretchr/testify/assert" @@ -38,7 +38,7 @@ func TestQuerier_CustomMyInt(t *testing.T) { AND pn.nspname = current_schema() LIMIT 1; `)) - oidVal := pgtype.OIDValue{} + oidVal := uint32Value{} err := row.Scan(&oidVal) require.NoError(t, err) t.Logf("my_int oid: %d", oidVal.Uint) diff --git a/example/device/query.sql.go b/example/device/query.sql.go index d2b6fcd8..cdad2365 100644 --- a/example/device/query.sql.go +++ b/example/device/query.sql.go @@ -5,9 +5,9 @@ package device import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/device/query.sql_test.go b/example/device/query.sql_test.go index 3319f94e..95171f60 100644 --- a/example/device/query.sql_test.go +++ b/example/device/query.sql_test.go @@ -2,7 +2,7 @@ package device import ( "context" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/example/domain/query.sql.go b/example/domain/query.sql.go index 01130f2c..3d1de221 100644 --- a/example/domain/query.sql.go +++ b/example/domain/query.sql.go @@ -5,9 +5,9 @@ package domain import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/enums/query.sql.go b/example/enums/query.sql.go index cbaea2ed..c72c9d9c 100644 --- a/example/enums/query.sql.go +++ b/example/enums/query.sql.go @@ -5,9 +5,9 @@ package enums import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/enums/query.sql_test.go b/example/enums/query.sql_test.go index e4d87bd3..e36f04a5 100644 --- a/example/enums/query.sql_test.go +++ b/example/enums/query.sql_test.go @@ -2,7 +2,7 @@ package enums import ( "context" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/example/erp/order/customer.sql.go b/example/erp/order/customer.sql.go index ca372b20..45aca9c8 100644 --- a/example/erp/order/customer.sql.go +++ b/example/erp/order/customer.sql.go @@ -5,9 +5,9 @@ package order import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/erp/order/customer.sql_test.go b/example/erp/order/customer.sql_test.go index f979954d..83498397 100644 --- a/example/erp/order/customer.sql_test.go +++ b/example/erp/order/customer.sql_test.go @@ -2,7 +2,7 @@ package order import ( "context" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/example/erp/order/price.sql.go b/example/erp/order/price.sql.go index 2395a2ca..c4c0b9ab 100644 --- a/example/erp/order/price.sql.go +++ b/example/erp/order/price.sql.go @@ -5,7 +5,7 @@ package order import ( "context" "fmt" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" ) const findOrdersByPriceSQL = `SELECT * FROM orders WHERE order_total > $1;` diff --git a/example/function/query.sql.go b/example/function/query.sql.go index ebfd5e7c..c7ba5bd0 100644 --- a/example/function/query.sql.go +++ b/example/function/query.sql.go @@ -5,9 +5,9 @@ package function import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/go_pointer_types/query.sql.go b/example/go_pointer_types/query.sql.go index fce3f65d..08276e52 100644 --- a/example/go_pointer_types/query.sql.go +++ b/example/go_pointer_types/query.sql.go @@ -5,9 +5,9 @@ package go_pointer_types import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/inline_param_count/inline0/query.sql.go b/example/inline_param_count/inline0/query.sql.go index 4b4bbace..c63d4b90 100644 --- a/example/inline_param_count/inline0/query.sql.go +++ b/example/inline_param_count/inline0/query.sql.go @@ -5,9 +5,9 @@ package inline0 import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/inline_param_count/inline0/query.sql_test.go b/example/inline_param_count/inline0/query.sql_test.go index 2babf0a2..a7b1bb82 100644 --- a/example/inline_param_count/inline0/query.sql_test.go +++ b/example/inline_param_count/inline0/query.sql_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/require" "testing" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" ) diff --git a/example/inline_param_count/inline1/query.sql.go b/example/inline_param_count/inline1/query.sql.go index 0583f0df..2b55216d 100644 --- a/example/inline_param_count/inline1/query.sql.go +++ b/example/inline_param_count/inline1/query.sql.go @@ -5,9 +5,9 @@ package inline1 import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/inline_param_count/inline1/query.sql_test.go b/example/inline_param_count/inline1/query.sql_test.go index c3bbfc9a..af989259 100644 --- a/example/inline_param_count/inline1/query.sql_test.go +++ b/example/inline_param_count/inline1/query.sql_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/require" "testing" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" ) diff --git a/example/inline_param_count/inline2/query.sql.go b/example/inline_param_count/inline2/query.sql.go index b5624ca2..113ecb35 100644 --- a/example/inline_param_count/inline2/query.sql.go +++ b/example/inline_param_count/inline2/query.sql.go @@ -5,9 +5,9 @@ package inline2 import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/inline_param_count/inline2/query.sql_test.go b/example/inline_param_count/inline2/query.sql_test.go index b119f7fb..a17347f1 100644 --- a/example/inline_param_count/inline2/query.sql_test.go +++ b/example/inline_param_count/inline2/query.sql_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/require" "testing" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" ) diff --git a/example/inline_param_count/inline3/query.sql.go b/example/inline_param_count/inline3/query.sql.go index 8940776a..88f0c874 100644 --- a/example/inline_param_count/inline3/query.sql.go +++ b/example/inline_param_count/inline3/query.sql.go @@ -5,9 +5,9 @@ package inline3 import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/inline_param_count/inline3/query.sql_test.go b/example/inline_param_count/inline3/query.sql_test.go index 527a983b..b4e9121f 100644 --- a/example/inline_param_count/inline3/query.sql_test.go +++ b/example/inline_param_count/inline3/query.sql_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/require" "testing" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" ) diff --git a/example/ltree/codegen_test.go b/example/ltree/codegen_test.go index f5f1fec1..2a05d1f9 100644 --- a/example/ltree/codegen_test.go +++ b/example/ltree/codegen_test.go @@ -23,8 +23,8 @@ func TestGenerate_Go_Example_ltree(t *testing.T) { Language: pggen.LangGo, InlineParamCount: 2, TypeOverrides: map[string]string{ - "ltree": "github.com/jackc/pgtype.Text", - "_ltree": "github.com/jackc/pgtype.TextArray", + "ltree": "github.com/jackc/pgx/v5/pgtype.Text", + "_ltree": "github.com/jackc/pgx/v5/pgtype.TextArray", }, }) if err != nil { diff --git a/example/ltree/query.sql.go b/example/ltree/query.sql.go index aee3db7f..537e2343 100644 --- a/example/ltree/query.sql.go +++ b/example/ltree/query.sql.go @@ -5,9 +5,9 @@ package ltree import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/ltree/query.sql_test.go b/example/ltree/query.sql_test.go index 83d0d8d9..ce591bbc 100644 --- a/example/ltree/query.sql_test.go +++ b/example/ltree/query.sql_test.go @@ -2,7 +2,7 @@ package ltree import ( "context" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/pgtest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/example/nested/query.sql.go b/example/nested/query.sql.go index 1ffb4074..f1bc80bd 100644 --- a/example/nested/query.sql.go +++ b/example/nested/query.sql.go @@ -5,9 +5,9 @@ package nested import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. @@ -57,7 +57,7 @@ type ProductImageType struct { // typeResolver looks up the pgtype.ValueTranscoder by Postgres type name. type typeResolver struct { - connInfo *pgtype.ConnInfo // types by Postgres type name + connInfo *pgtype.Codec // types by Postgres type name } func newTypeResolver() *typeResolver { @@ -148,6 +148,7 @@ func (tr *typeResolver) newDimensions() pgtype.ValueTranscoder { // newProductImageSetType creates a new pgtype.ValueTranscoder for the Postgres // composite type 'product_image_set_type'. func (tr *typeResolver) newProductImageSetType() pgtype.ValueTranscoder { + pgtype.CompositeCodec{} return tr.newCompositeValue( "product_image_set_type", compositeField{name: "name", typeName: "text", defaultVal: &pgtype.Text{}}, @@ -169,6 +170,7 @@ func (tr *typeResolver) newProductImageType() pgtype.ValueTranscoder { // newProductImageTypeArray creates a new pgtype.ValueTranscoder for the Postgres // '_product_image_type' array type. func (tr *typeResolver) newProductImageTypeArray() pgtype.ValueTranscoder { + pgtype.CompositeCodec{} return tr.newArrayValue("_product_image_type", "product_image_type", tr.newProductImageType) } diff --git a/example/pgcrypto/query.sql.go b/example/pgcrypto/query.sql.go index 90be999c..ff478512 100644 --- a/example/pgcrypto/query.sql.go +++ b/example/pgcrypto/query.sql.go @@ -5,9 +5,9 @@ package pgcrypto import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/separate_out_dir/out/alpha_query.sql.0.go b/example/separate_out_dir/out/alpha_query.sql.0.go index bfebba47..f99f15dc 100644 --- a/example/separate_out_dir/out/alpha_query.sql.0.go +++ b/example/separate_out_dir/out/alpha_query.sql.0.go @@ -5,9 +5,9 @@ package out import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/slices/query.sql.go b/example/slices/query.sql.go index 8ef9062c..ed0fad12 100644 --- a/example/slices/query.sql.go +++ b/example/slices/query.sql.go @@ -5,9 +5,9 @@ package slices import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" "time" ) diff --git a/example/syntax/query.sql.go b/example/syntax/query.sql.go index b221822c..85bf0661 100644 --- a/example/syntax/query.sql.go +++ b/example/syntax/query.sql.go @@ -5,9 +5,9 @@ package syntax import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/example/void/query.sql.go b/example/void/query.sql.go index 62fb382e..146b3cf1 100644 --- a/example/void/query.sql.go +++ b/example/void/query.sql.go @@ -5,9 +5,9 @@ package void import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" ) // Querier is a typesafe Go interface backed by SQL queries. diff --git a/generate.go b/generate.go index 94aef72c..d7bf8699 100644 --- a/generate.go +++ b/generate.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/ast" "github.com/jschaf/pggen/internal/codegen" "github.com/jschaf/pggen/internal/codegen/golang" diff --git a/go.mod b/go.mod index a46a9a7f..db08d690 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/jackc/pgproto3/v2 v2.3.2 github.com/jackc/pgtype v1.14.0 github.com/jackc/pgx/v4 v4.18.1 + github.com/jackc/pgx/v5 v5.5.1 github.com/peterbourgon/ff/v3 v3.4.0 github.com/shopspring/decimal v1.3.1 github.com/stretchr/testify v1.8.4 @@ -26,8 +27,7 @@ require ( github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect - github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect - github.com/kr/pretty v0.2.1 // indirect + github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/kr/text v0.2.0 // indirect github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect github.com/morikuni/aec v1.0.0 // indirect @@ -35,7 +35,8 @@ require ( github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/crypto v0.16.0 // indirect + github.com/rogpeppe/go-internal v1.6.1 // indirect + golang.org/x/crypto v0.17.0 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/sync v0.4.0 // indirect golang.org/x/sys v0.15.0 // indirect diff --git a/go.sum b/go.sum index 32ca3a76..28462699 100644 --- a/go.sum +++ b/go.sum @@ -67,8 +67,9 @@ github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwX github.com/jackc/pgproto3/v2 v2.3.2 h1:7eY55bdBeCz1F2fTzSz69QC+pG46jYq9/jtSPiJ5nn0= github.com/jackc/pgproto3/v2 v2.3.2/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= -github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= @@ -81,17 +82,22 @@ github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQ github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= github.com/jackc/pgx/v4 v4.18.1 h1:YP7G1KABtKpB5IHrO9vYwSrCOhs7p3uqhvhhQBptya0= github.com/jackc/pgx/v4 v4.18.1/go.mod h1:FydWkUyadDmdNH/mHnGob881GawxeEm7TcMCzkb+qQE= +github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI= +github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0= github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -123,6 +129,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= @@ -175,8 +183,8 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= @@ -258,8 +266,9 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/codegen/golang/declarer_composite.go b/internal/codegen/golang/declarer_composite.go index 3dc1988f..34625c04 100644 --- a/internal/codegen/golang/declarer_composite.go +++ b/internal/codegen/golang/declarer_composite.go @@ -263,7 +263,7 @@ func (c CompositeInitDeclarer) Declare(string) (string, error) { // of a composite type as a generic array: []interface{}. Necessary because we // can only set pgtype.CompositeType from a []interface{}. // -// Revisit after https://github.com/jackc/pgtype/pull/100 to see if we can +// Revisit after https://github.com/jackc/pgx/v5/pgtype/pull/100 to see if we can // simplify. type CompositeRawDeclarer struct { typ *gotype.CompositeType diff --git a/internal/codegen/golang/gotype/known_types.go b/internal/codegen/golang/gotype/known_types.go index 6d893f52..87ed8b4b 100644 --- a/internal/codegen/golang/gotype/known_types.go +++ b/internal/codegen/golang/gotype/known_types.go @@ -1,14 +1,14 @@ package gotype import ( - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/pg" "github.com/jschaf/pggen/internal/pg/pgoid" ) // FindKnownTypePgx returns the native pgx type, like pgtype.Text, if known, for // a Postgres OID. If there is no known type, returns nil. -func FindKnownTypePgx(oid pgtype.OID) (Type, bool) { +func FindKnownTypePgx(oid uint32) (Type, bool) { typ, ok := knownTypesByOID[oid] return typ.pgNative, ok } @@ -16,7 +16,7 @@ func FindKnownTypePgx(oid pgtype.OID) (Type, bool) { // FindKnownTypeNullable returns the nullable type, like *string, if known, for // a Postgres OID. Falls back to the pgNative type. If there is no known type // for the OID, returns nil. -func FindKnownTypeNullable(oid pgtype.OID) (Type, bool) { +func FindKnownTypeNullable(oid uint32) (Type, bool) { typ, ok := knownTypesByOID[oid] if !ok { return nil, false @@ -30,7 +30,7 @@ func FindKnownTypeNullable(oid pgtype.OID) (Type, bool) { // FindKnownTypeNonNullable returns the non-nullable type like string, if known, // for a Postgres OID. Falls back to the nullable type and pgNative type. If // there is no known type for the OID, returns nil. -func FindKnownTypeNonNullable(oid pgtype.OID) (Type, bool) { +func FindKnownTypeNonNullable(oid uint32) (Type, bool) { typ, ok := knownTypesByOID[oid] if !ok { return nil, false @@ -91,73 +91,73 @@ var ( // pgtype types prefixed with "pg". var ( - PgBool = MustParseKnownType("github.com/jackc/pgtype.Bool", pg.Bool) - PgQChar = MustParseKnownType("github.com/jackc/pgtype.QChar", pg.QChar) - PgName = MustParseKnownType("github.com/jackc/pgtype.Name", pg.Name) - PgInt8 = MustParseKnownType("github.com/jackc/pgtype.Int8", pg.Int8) - PgInt2 = MustParseKnownType("github.com/jackc/pgtype.Int2", pg.Int2) - PgInt4 = MustParseKnownType("github.com/jackc/pgtype.Int4", pg.Int4) - PgText = MustParseKnownType("github.com/jackc/pgtype.Text", pg.Text) - PgBytea = MustParseKnownType("github.com/jackc/pgtype.Bytea", pg.Bytea) - PgOID = MustParseKnownType("github.com/jackc/pgtype.OID", pg.OID) - PgTID = MustParseKnownType("github.com/jackc/pgtype.TID", pg.TID) - PgXID = MustParseKnownType("github.com/jackc/pgtype.XID", pg.XID) - PgCID = MustParseKnownType("github.com/jackc/pgtype.CID", pg.CID) - PgJSON = MustParseKnownType("github.com/jackc/pgtype.JSON", pg.JSON) - PgPoint = MustParseKnownType("github.com/jackc/pgtype.Point", pg.Point) - PgLseg = MustParseKnownType("github.com/jackc/pgtype.Lseg", pg.Lseg) - PgPath = MustParseKnownType("github.com/jackc/pgtype.Path", pg.Path) - PgBox = MustParseKnownType("github.com/jackc/pgtype.Box", pg.Box) - PgPolygon = MustParseKnownType("github.com/jackc/pgtype.Polygon", pg.Polygon) - PgLine = MustParseKnownType("github.com/jackc/pgtype.Line", pg.Line) - PgCIDR = MustParseKnownType("github.com/jackc/pgtype.CIDR", pg.CIDR) - PgCIDRArray = MustParseKnownType("github.com/jackc/pgtype.CIDRArray", pg.CIDRArray) - PgFloat4 = MustParseKnownType("github.com/jackc/pgtype.Float4", pg.Float4) - PgFloat8 = MustParseKnownType("github.com/jackc/pgtype.Float8", pg.Float8) - PgUnknown = MustParseKnownType("github.com/jackc/pgtype.Unknown", pg.Unknown) - PgCircle = MustParseKnownType("github.com/jackc/pgtype.Circle", pg.Circle) - PgMacaddr = MustParseKnownType("github.com/jackc/pgtype.Macaddr", pg.Macaddr) - PgInet = MustParseKnownType("github.com/jackc/pgtype.Inet", pg.Inet) - PgBoolArray = MustParseKnownType("github.com/jackc/pgtype.BoolArray", pg.BoolArray) - PgByteaArray = MustParseKnownType("github.com/jackc/pgtype.ByteaArray", pg.ByteaArray) - PgInt2Array = MustParseKnownType("github.com/jackc/pgtype.Int2Array", pg.Int2Array) - PgInt4Array = MustParseKnownType("github.com/jackc/pgtype.Int4Array", pg.Int4Array) - PgTextArray = MustParseKnownType("github.com/jackc/pgtype.TextArray", pg.TextArray) - PgBPCharArray = MustParseKnownType("github.com/jackc/pgtype.BPCharArray", pg.BPCharArray) - PgVarcharArray = MustParseKnownType("github.com/jackc/pgtype.VarcharArray", pg.VarcharArray) - PgInt8Array = MustParseKnownType("github.com/jackc/pgtype.Int8Array", pg.Int8Array) - PgFloat4Array = MustParseKnownType("github.com/jackc/pgtype.Float4Array", pg.Float4Array) - PgFloat8Array = MustParseKnownType("github.com/jackc/pgtype.Float8Array", pg.Float8Array) - PgACLItem = MustParseKnownType("github.com/jackc/pgtype.ACLItem", pg.ACLItem) - PgACLItemArray = MustParseKnownType("github.com/jackc/pgtype.ACLItemArray", pg.ACLItemArray) - PgInetArray = MustParseKnownType("github.com/jackc/pgtype.InetArray", pg.InetArray) - PgMacaddrArray = MustParseKnownType("github.com/jackc/pgtype.MacaddrArray", pg.MacaddrArray) - PgBPChar = MustParseKnownType("github.com/jackc/pgtype.BPChar", pg.BPChar) - PgVarchar = MustParseKnownType("github.com/jackc/pgtype.Varchar", pg.Varchar) - PgDate = MustParseKnownType("github.com/jackc/pgtype.Date", pg.Date) - PgTime = MustParseKnownType("github.com/jackc/pgtype.Time", pg.Time) - PgTimestamp = MustParseKnownType("github.com/jackc/pgtype.Timestamp", pg.Timestamp) - PgTimestampArray = MustParseKnownType("github.com/jackc/pgtype.TimestampArray", pg.TimestampArray) - PgDateArray = MustParseKnownType("github.com/jackc/pgtype.DateArray", pg.DateArray) - PgTimestamptz = MustParseKnownType("github.com/jackc/pgtype.Timestamptz", pg.Timestamptz) - PgTimestamptzArray = MustParseKnownType("github.com/jackc/pgtype.TimestamptzArray", pg.TimestamptzArray) - PgInterval = MustParseKnownType("github.com/jackc/pgtype.Interval", pg.Interval) - PgNumericArray = MustParseKnownType("github.com/jackc/pgtype.NumericArray", pg.NumericArray) - PgBit = MustParseKnownType("github.com/jackc/pgtype.Bit", pg.Bit) - PgVarbit = MustParseKnownType("github.com/jackc/pgtype.Varbit", pg.Varbit) + PgBool = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Bool", pg.Bool) + PgQChar = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.QChar", pg.QChar) + PgName = MustParseKnownType("string", pg.Name) + PgInt8 = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int8", pg.Int8) + PgInt2 = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int2", pg.Int2) + PgInt4 = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int4", pg.Int4) + PgText = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Text", pg.Text) + PgBytea = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Bytea", pg.Bytea) + PgOID = MustParseKnownType("uint32", pg.OID) + PgTID = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.TID", pg.TID) + PgXID = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.XID", pg.XID) + PgCID = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.CID", pg.CID) + PgJSON = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.JSON", pg.JSON) + PgPoint = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Point", pg.Point) + PgLseg = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Lseg", pg.Lseg) + PgPath = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Path", pg.Path) + PgBox = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Box", pg.Box) + PgPolygon = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Polygon", pg.Polygon) + PgLine = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Line", pg.Line) + PgCIDR = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.CIDR", pg.CIDR) + PgCIDRArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.CIDRArray", pg.CIDRArray) + PgFloat4 = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Float4", pg.Float4) + PgFloat8 = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Float8", pg.Float8) + PgUnknown = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Unknown", pg.Unknown) + PgCircle = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Circle", pg.Circle) + PgMacaddr = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Macaddr", pg.Macaddr) + PgInet = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Inet", pg.Inet) + PgBoolArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.BoolArray", pg.BoolArray) + PgByteaArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.ByteaArray", pg.ByteaArray) + PgInt2Array = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int2Array", pg.Int2Array) + PgInt4Array = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int4Array", pg.Int4Array) + PgTextArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.TextArray", pg.TextArray) + PgBPCharArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.BPCharArray", pg.BPCharArray) + PgVarcharArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.VarcharArray", pg.VarcharArray) + PgInt8Array = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int8Array", pg.Int8Array) + PgFloat4Array = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Float4Array", pg.Float4Array) + PgFloat8Array = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Float8Array", pg.Float8Array) + PgACLItem = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.ACLItem", pg.ACLItem) + PgACLItemArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.ACLItemArray", pg.ACLItemArray) + PgInetArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.InetArray", pg.InetArray) + PgMacaddrArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.MacaddrArray", pg.MacaddrArray) + PgBPChar = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.BPChar", pg.BPChar) + PgVarchar = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Varchar", pg.Varchar) + PgDate = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Date", pg.Date) + PgTime = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Time", pg.Time) + PgTimestamp = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Timestamp", pg.Timestamp) + PgTimestampArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.TimestampArray", pg.TimestampArray) + PgDateArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.DateArray", pg.DateArray) + PgTimestamptz = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Timestamptz", pg.Timestamptz) + PgTimestamptzArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.TimestamptzArray", pg.TimestamptzArray) + PgInterval = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Interval", pg.Interval) + PgNumericArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.NumericArray", pg.NumericArray) + PgBit = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Bit", pg.Bit) + PgVarbit = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Varbit", pg.Varbit) PgVoid = &VoidType{} - PgNumeric = MustParseKnownType("github.com/jackc/pgtype.Numeric", pg.Numeric) - PgRecord = MustParseKnownType("github.com/jackc/pgtype.Record", pg.Record) - PgUUID = MustParseKnownType("github.com/jackc/pgtype.UUID", pg.UUID) - PgUUIDArray = MustParseKnownType("github.com/jackc/pgtype.UUIDArray", pg.UUIDArray) - PgJSONB = MustParseKnownType("github.com/jackc/pgtype.JSONB", pg.JSONB) - PgJSONBArray = MustParseKnownType("github.com/jackc/pgtype.JSONBArray", pg.JSONBArray) - PgInt4range = MustParseKnownType("github.com/jackc/pgtype.Int4range", pg.Int4range) - PgNumrange = MustParseKnownType("github.com/jackc/pgtype.Numrange", pg.Numrange) - PgTsrange = MustParseKnownType("github.com/jackc/pgtype.Tsrange", pg.Tsrange) - PgTstzrange = MustParseKnownType("github.com/jackc/pgtype.Tstzrange", pg.Tstzrange) - PgDaterange = MustParseKnownType("github.com/jackc/pgtype.Daterange", pg.Daterange) - PgInt8range = MustParseKnownType("github.com/jackc/pgtype.Int8range", pg.Int8range) + PgNumeric = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Numeric", pg.Numeric) + PgRecord = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Record", pg.Record) + PgUUID = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.UUID", pg.UUID) + PgUUIDArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.UUIDArray", pg.UUIDArray) + PgJSONB = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.JSONB", pg.JSONB) + PgJSONBArray = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.JSONBArray", pg.JSONBArray) + PgInt4range = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int4range", pg.Int4range) + PgNumrange = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Numrange", pg.Numrange) + PgTsrange = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Tsrange", pg.Tsrange) + PgTstzrange = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Tstzrange", pg.Tstzrange) + PgDaterange = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Daterange", pg.Daterange) + PgInt8range = MustParseKnownType("github.com/jackc/pgx/v5/pgtype.Int8range", pg.Int8range) ) // knownGoType is the native pgtype type, the nullable and non-nullable types @@ -177,7 +177,7 @@ var ( // "string" for a Postgres text type. type knownGoType struct{ pgNative, nullable, nonNullable Type } -var knownTypesByOID = map[pgtype.OID]knownGoType{ +var knownTypesByOID = map[uint32]knownGoType{ pgtype.BoolOID: {PgBool, Boolp, Bool}, pgtype.QCharOID: {PgQChar, nil, nil}, pgtype.NameOID: {PgName, nil, nil}, diff --git a/internal/codegen/golang/gotype/types.go b/internal/codegen/golang/gotype/types.go index 65e8fce6..199d1e86 100644 --- a/internal/codegen/golang/gotype/types.go +++ b/internal/codegen/golang/gotype/types.go @@ -240,7 +240,7 @@ func ParseOpaqueType(qualType string, pgType pg.Type) (Type, error) { } // MustParseKnownType creates a gotype.Type by parsing a fully qualified Go type -// that pgx supports natively like "github.com/jackc/pgtype.Int4Array", or most +// that pgx supports natively like "github.com/jackc/pgx/v5/pgtype.Int4Array", or most // builtin types like "string" and []*int16. func MustParseKnownType(qualType string, pgType pg.Type) Type { typ, err := ParseOpaqueType(qualType, pgType) diff --git a/internal/codegen/golang/query.gotemplate b/internal/codegen/golang/query.gotemplate index 720befbb..d6456671 100644 --- a/internal/codegen/golang/query.gotemplate +++ b/internal/codegen/golang/query.gotemplate @@ -28,7 +28,6 @@ var _ Querier = &DBQuerier{} type DBQuerier struct { conn genericConn // underlying Postgres transport to use - types *typeResolver // resolve types by name } // genericConn is a connection like *pgx.Conn, pgx.Tx, or *pgxpool.Pool. @@ -40,7 +39,7 @@ type genericConn interface { // NewQuerier creates a DBQuerier that implements Querier. func NewQuerier(conn genericConn) *DBQuerier { - return &DBQuerier{conn: conn, types: newTypeResolver()} + return &DBQuerier{conn: conn} } {{- range .Declarers}}{{- "\n\n" -}}{{ .Declare $.PkgPath }}{{ end -}} @@ -93,33 +92,5 @@ func (q *DBQuerier) {{ $q.Name }}(ctx context.Context {{- $q.EmitParams }}) ({{ {{- end }} } {{- end -}} - -{{- if .IsLeader -}} -{{- "\n\n" -}} -// textPreferrer wraps a pgtype.ValueTranscoder and sets the preferred encoding -// format to text instead binary (the default). pggen uses the text format -// when the OID is unknownOID because the binary format requires the OID. -// Typically occurs for unregistered types. -type textPreferrer struct { - pgtype.ValueTranscoder - typeName string -} - -// PreferredParamFormat implements pgtype.ParamFormatPreferrer. -func (t textPreferrer) PreferredParamFormat() int16 { return pgtype.TextFormatCode } - -func (t textPreferrer) NewTypeValue() pgtype.Value { - return textPreferrer{ValueTranscoder: pgtype.NewValue(t.ValueTranscoder).(pgtype.ValueTranscoder), typeName: t.typeName} -} - -func (t textPreferrer) TypeName() string { - return t.typeName -} - -// unknownOID means we don't know the OID for a type. This is okay for decoding -// because pgx call DecodeText or DecodeBinary without requiring the OID. For -// encoding parameters, pggen uses textPreferrer if the OID is unknown. -const unknownOID = 0 -{{- end -}} {{- "\n" -}} {{- end -}} diff --git a/internal/codegen/golang/templater.go b/internal/codegen/golang/templater.go index 150035bf..de73461e 100644 --- a/internal/codegen/golang/templater.go +++ b/internal/codegen/golang/templater.go @@ -73,7 +73,7 @@ func (tm Templater) TemplateAll(files []codegen.QueryFile) ([]TemplatedFile, err pgconnIdx := -1 imports := file.Imports for i, pkg := range imports { - if pkg == "github.com/jackc/pgconn" { + if pkg == "github.com/jackc/pgx/v5/pgconn" { pgconnIdx = i break } @@ -113,10 +113,10 @@ func (tm Templater) templateFile(file codegen.QueryFile, isLeader bool) (Templat imports := NewImportSet() imports.AddPackage("context") imports.AddPackage("fmt") - imports.AddPackage("github.com/jackc/pgconn") + imports.AddPackage("github.com/jackc/pgx/v5/pgconn") if isLeader { - imports.AddPackage("github.com/jackc/pgtype") - imports.AddPackage("github.com/jackc/pgx/v4") + imports.AddPackage("github.com/jackc/pgx/v5/pgtype") + imports.AddPackage("github.com/jackc/pgx/v5") } pkgPath := "" diff --git a/internal/codegen/golang/type_resolver_test.go b/internal/codegen/golang/type_resolver_test.go index e31492e3..bd371049 100644 --- a/internal/codegen/golang/type_resolver_test.go +++ b/internal/codegen/golang/type_resolver_test.go @@ -2,7 +2,7 @@ package golang import ( "github.com/google/go-cmp/cmp" - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/casing" "github.com/jschaf/pggen/internal/codegen/golang/gotype" "github.com/jschaf/pggen/internal/difftest" @@ -88,7 +88,7 @@ func TestTypeResolver_Resolve(t *testing.T) { pgType: pg.BaseType{Name: "point", ID: pgtype.PointOID}, nullable: false, want: &gotype.ImportType{ - PkgPath: "github.com/jackc/pgtype", + PkgPath: "github.com/jackc/pgx/v5/pgtype", Type: &gotype.OpaqueType{ PgType: pg.BaseType{Name: "point", ID: pgtype.PointOID}, Name: "Point", @@ -100,7 +100,7 @@ func TestTypeResolver_Resolve(t *testing.T) { pgType: pg.BaseType{Name: "point", ID: pgtype.PointOID}, nullable: true, want: &gotype.ImportType{ - PkgPath: "github.com/jackc/pgtype", + PkgPath: "github.com/jackc/pgx/v5/pgtype", Type: &gotype.OpaqueType{ PgType: pg.BaseType{Name: "point", ID: pgtype.PointOID}, Name: "Point", diff --git a/internal/pg/column.go b/internal/pg/column.go index e381ec71..9215205d 100644 --- a/internal/pg/column.go +++ b/internal/pg/column.go @@ -3,8 +3,7 @@ package pg import ( "context" "fmt" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/texts" "strconv" "strings" @@ -15,18 +14,18 @@ import ( // Column stores information about a column in a TableOID. // https://www.postgresql.org/docs/13/catalog-pg-attribute.html type Column struct { - Name string // pg_attribute.attname: column name - TableOID pgtype.OID // pg_attribute:attrelid: table the column belongs to - TableName string // pg_class.relname: name of table that owns the column - Number uint16 // pg_attribute.attnum: the number of column starting from 1 - Type Type // pg_attribute.atttypid: data type of the column - Null bool // pg_attribute.attnotnull: represents a not-null constraint + Name string // pg_attribute.attname: column name + TableOID uint32 // pg_attribute:attrelid: table the column belongs to + TableName string // pg_class.relname: name of table that owns the column + Number uint16 // pg_attribute.attnum: the number of column starting from 1 + Type Type // pg_attribute.atttypid: data type of the column + Null bool // pg_attribute.attnotnull: represents a not-null constraint } // ColumnKey is a composite key of a table OID and the number of the column // within the table. type ColumnKey struct { - TableOID pgtype.OID + TableOID uint32 Number uint16 // the number of column starting from 1 } diff --git a/internal/pg/column_test.go b/internal/pg/column_test.go index c437c89f..5465b77f 100644 --- a/internal/pg/column_test.go +++ b/internal/pg/column_test.go @@ -3,8 +3,7 @@ package pg import ( "context" "github.com/google/go-cmp/cmp" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/pgtest" "github.com/jschaf/pggen/internal/texts" "github.com/stretchr/testify/assert" @@ -74,7 +73,7 @@ func TestFetchColumns(t *testing.T) { } } -func findTableOID(t *testing.T, conn *pgx.Conn, table string) pgtype.OID { +func findTableOID(t *testing.T, conn *pgx.Conn, table string) uint32 { sql := texts.Dedent(` SELECT oid AS table_oid FROM pg_class @@ -85,7 +84,7 @@ func findTableOID(t *testing.T, conn *pgx.Conn, table string) pgtype.OID { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() row := conn.QueryRow(ctx, sql, table) - var oid pgtype.OID = 0 + var oid uint32 = 0 if err := row.Scan(&oid); err != nil && err != pgx.ErrNoRows { t.Fatal(err) } diff --git a/internal/pg/known_types.go b/internal/pg/known_types.go index 5540ad30..a78702c8 100644 --- a/internal/pg/known_types.go +++ b/internal/pg/known_types.go @@ -1,7 +1,7 @@ package pg import ( - "github.com/jackc/pgtype" + "github.com/jackc/pgx/v5/pgtype" "github.com/jschaf/pggen/internal/pg/pgoid" ) @@ -81,7 +81,7 @@ var ( ) // All known Postgres types by OID. -var defaultKnownTypes = map[pgtype.OID]Type{ +var defaultKnownTypes = map[uint32]Type{ pgtype.BoolOID: Bool, pgtype.ByteaOID: Bytea, pgtype.QCharOID: QChar, diff --git a/internal/pg/query.sql.go b/internal/pg/query.sql.go index 61f49aa9..af7b16a3 100644 --- a/internal/pg/query.sql.go +++ b/internal/pg/query.sql.go @@ -5,9 +5,8 @@ package pg import ( "context" "fmt" - "github.com/jackc/pgconn" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" ) // Querier is a typesafe Go interface backed by SQL queries. @@ -23,11 +22,11 @@ type Querier interface { // Recursively expands all given OIDs to all descendants through composite // types. - FindDescendantOIDs(ctx context.Context, oids []uint32) ([]pgtype.OID, error) + FindDescendantOIDs(ctx context.Context, oids []uint32) ([]uint32, error) - FindOIDByName(ctx context.Context, name string) (pgtype.OID, error) + FindOIDByName(ctx context.Context, name string) (uint32, error) - FindOIDName(ctx context.Context, oid pgtype.OID) (pgtype.Name, error) + FindOIDName(ctx context.Context, oid uint32) (string, error) FindOIDNames(ctx context.Context, oid []uint32) ([]FindOIDNamesRow, error) } @@ -35,8 +34,7 @@ type Querier interface { var _ Querier = &DBQuerier{} type DBQuerier struct { - conn genericConn // underlying Postgres transport to use - types *typeResolver // resolve types by name + conn genericConn // underlying Postgres transport to use } // genericConn is a connection like *pgx.Conn, pgx.Tx, or *pgxpool.Pool. @@ -48,36 +46,7 @@ type genericConn interface { // NewQuerier creates a DBQuerier that implements Querier. func NewQuerier(conn genericConn) *DBQuerier { - return &DBQuerier{conn: conn, types: newTypeResolver()} -} - -// typeResolver looks up the pgtype.ValueTranscoder by Postgres type name. -type typeResolver struct { - connInfo *pgtype.ConnInfo // types by Postgres type name -} - -func newTypeResolver() *typeResolver { - ci := pgtype.NewConnInfo() - return &typeResolver{connInfo: ci} -} - -// findValue find the OID, and pgtype.ValueTranscoder for a Postgres type name. -func (tr *typeResolver) findValue(name string) (uint32, pgtype.ValueTranscoder, bool) { - typ, ok := tr.connInfo.DataTypeForName(name) - if !ok { - return 0, nil, false - } - v := pgtype.NewValue(typ.Value) - return typ.OID, v.(pgtype.ValueTranscoder), true -} - -// setValue sets the value of a ValueTranscoder to a value that should always -// work and panics if it fails. -func (tr *typeResolver) setValue(vt pgtype.ValueTranscoder, val interface{}) pgtype.ValueTranscoder { - if err := vt.Set(val); err != nil { - panic(fmt.Sprintf("set ValueTranscoder %T to %+v: %s", vt, val, err)) - } - return vt + return &DBQuerier{conn: conn} } const findEnumTypesSQL = `WITH enums AS ( @@ -125,13 +94,13 @@ WHERE typ.typisdefined AND typ.oid = ANY ($1::oid[]);` type FindEnumTypesRow struct { - OID pgtype.OID `json:"oid"` - TypeName string `json:"type_name"` - ChildOIDs []int `json:"child_oids"` - Orders []float32 `json:"orders"` - Labels []string `json:"labels"` - TypeKind pgtype.QChar `json:"type_kind"` - DefaultExpr string `json:"default_expr"` + OID uint32 `json:"oid"` + TypeName string `json:"type_name"` + ChildOIDs []int `json:"child_oids"` + Orders []float32 `json:"orders"` + Labels []string `json:"labels"` + TypeKind byte `json:"type_kind"` + DefaultExpr string `json:"default_expr"` } // FindEnumTypes implements Querier.FindEnumTypes. @@ -186,10 +155,10 @@ WHERE arr_typ.typisdefined AND arr_typ.oid = ANY ($1::oid[]);` type FindArrayTypesRow struct { - OID pgtype.OID `json:"oid"` - TypeName string `json:"type_name"` - ElemOID pgtype.OID `json:"elem_oid"` - TypeKind pgtype.QChar `json:"type_kind"` + OID uint32 `json:"oid"` + TypeName string `json:"type_name"` + ElemOID uint32 `json:"elem_oid"` + TypeKind byte `json:"type_kind"` } // FindArrayTypes implements Querier.FindArrayTypes. @@ -245,14 +214,14 @@ WHERE typ.oid = ANY ($1::oid[]) AND typ.typtype = 'c';` type FindCompositeTypesRow struct { - TableTypeName string `json:"table_type_name"` - TableTypeOID pgtype.OID `json:"table_type_oid"` - TableName pgtype.Name `json:"table_name"` - ColNames []string `json:"col_names"` - ColOIDs []int `json:"col_oids"` - ColOrders []int `json:"col_orders"` - ColNotNulls pgtype.BoolArray `json:"col_not_nulls"` - ColTypeNames []string `json:"col_type_names"` + TableTypeName string `json:"table_type_name"` + TableTypeOID uint32 `json:"table_type_oid"` + TableName string `json:"table_name"` + ColNames []string `json:"col_names"` + ColOIDs []int `json:"col_oids"` + ColOrders []int `json:"col_orders"` + ColNotNulls []bool `json:"col_not_nulls"` + ColTypeNames []string `json:"col_type_names"` } // FindCompositeTypes implements Querier.FindCompositeTypes. @@ -306,16 +275,16 @@ SELECT oid FROM oid_descs;` // FindDescendantOIDs implements Querier.FindDescendantOIDs. -func (q *DBQuerier) FindDescendantOIDs(ctx context.Context, oids []uint32) ([]pgtype.OID, error) { +func (q *DBQuerier) FindDescendantOIDs(ctx context.Context, oids []uint32) ([]uint32, error) { ctx = context.WithValue(ctx, "pggen_query_name", "FindDescendantOIDs") rows, err := q.conn.Query(ctx, findDescendantOIDsSQL, oids) if err != nil { return nil, fmt.Errorf("query FindDescendantOIDs: %w", err) } defer rows.Close() - items := []pgtype.OID{} + items := []uint32{} for rows.Next() { - var item pgtype.OID + var item uint32 if err := rows.Scan(&item); err != nil { return nil, fmt.Errorf("scan FindDescendantOIDs row: %w", err) } @@ -334,10 +303,10 @@ ORDER BY oid DESC LIMIT 1;` // FindOIDByName implements Querier.FindOIDByName. -func (q *DBQuerier) FindOIDByName(ctx context.Context, name string) (pgtype.OID, error) { +func (q *DBQuerier) FindOIDByName(ctx context.Context, name string) (uint32, error) { ctx = context.WithValue(ctx, "pggen_query_name", "FindOIDByName") row := q.conn.QueryRow(ctx, findOIDByNameSQL, name) - var item pgtype.OID + var item uint32 if err := row.Scan(&item); err != nil { return item, fmt.Errorf("query FindOIDByName: %w", err) } @@ -349,10 +318,10 @@ FROM pg_type WHERE oid = $1;` // FindOIDName implements Querier.FindOIDName. -func (q *DBQuerier) FindOIDName(ctx context.Context, oid pgtype.OID) (pgtype.Name, error) { +func (q *DBQuerier) FindOIDName(ctx context.Context, oid uint32) (string, error) { ctx = context.WithValue(ctx, "pggen_query_name", "FindOIDName") row := q.conn.QueryRow(ctx, findOIDNameSQL, oid) - var item pgtype.Name + var item string if err := row.Scan(&item); err != nil { return item, fmt.Errorf("query FindOIDName: %w", err) } @@ -364,9 +333,9 @@ FROM pg_type WHERE oid = ANY ($1::oid[]);` type FindOIDNamesRow struct { - OID pgtype.OID `json:"oid"` - Name pgtype.Name `json:"name"` - Kind pgtype.QChar `json:"kind"` + OID uint32 `json:"oid"` + Name string `json:"name"` + Kind byte `json:"kind"` } // FindOIDNames implements Querier.FindOIDNames. @@ -390,28 +359,3 @@ func (q *DBQuerier) FindOIDNames(ctx context.Context, oid []uint32) ([]FindOIDNa } return items, err } - -// textPreferrer wraps a pgtype.ValueTranscoder and sets the preferred encoding -// format to text instead binary (the default). pggen uses the text format -// when the OID is unknownOID because the binary format requires the OID. -// Typically occurs for unregistered types. -type textPreferrer struct { - pgtype.ValueTranscoder - typeName string -} - -// PreferredParamFormat implements pgtype.ParamFormatPreferrer. -func (t textPreferrer) PreferredParamFormat() int16 { return pgtype.TextFormatCode } - -func (t textPreferrer) NewTypeValue() pgtype.Value { - return textPreferrer{ValueTranscoder: pgtype.NewValue(t.ValueTranscoder).(pgtype.ValueTranscoder), typeName: t.typeName} -} - -func (t textPreferrer) TypeName() string { - return t.typeName -} - -// unknownOID means we don't know the OID for a type. This is okay for decoding -// because pgx call DecodeText or DecodeBinary without requiring the OID. For -// encoding parameters, pggen uses textPreferrer if the OID is unknown. -const unknownOID = 0 diff --git a/internal/pg/type_cache.go b/internal/pg/type_cache.go index d21a060a..53101a37 100644 --- a/internal/pg/type_cache.go +++ b/internal/pg/type_cache.go @@ -1,18 +1,17 @@ package pg import ( - "github.com/jackc/pgtype" "sync" ) // typeCache caches a map from a Postgres pg_type.oid to a Type. type typeCache struct { - types map[pgtype.OID]Type + types map[uint32]Type mu *sync.Mutex } func newTypeCache() *typeCache { - m := make(map[pgtype.OID]Type, len(defaultKnownTypes)) + m := make(map[uint32]Type, len(defaultKnownTypes)) for oid, typ := range defaultKnownTypes { m[oid] = typ } @@ -23,16 +22,16 @@ func newTypeCache() *typeCache { } // getOIDs returns the cached OIDS (with the type) and uncached OIDs. -func (tc *typeCache) getOIDs(oids ...uint32) (map[pgtype.OID]Type, map[pgtype.OID]struct{}) { - cachedTypes := make(map[pgtype.OID]Type, len(oids)) - uncachedTypes := make(map[pgtype.OID]struct{}, len(oids)) +func (tc *typeCache) getOIDs(oids ...uint32) (map[uint32]Type, map[uint32]struct{}) { + cachedTypes := make(map[uint32]Type, len(oids)) + uncachedTypes := make(map[uint32]struct{}, len(oids)) tc.mu.Lock() defer tc.mu.Unlock() for _, oid := range oids { - if t, ok := tc.types[pgtype.OID(oid)]; ok { - cachedTypes[pgtype.OID(oid)] = t + if t, ok := tc.types[uint32(oid)]; ok { + cachedTypes[uint32(oid)] = t } else { - uncachedTypes[pgtype.OID(oid)] = struct{}{} + uncachedTypes[uint32(oid)] = struct{}{} } } return cachedTypes, uncachedTypes @@ -40,7 +39,7 @@ func (tc *typeCache) getOIDs(oids ...uint32) (map[pgtype.OID]Type, map[pgtype.OI func (tc *typeCache) getOID(oid uint32) (Type, bool) { tc.mu.Lock() - typ, ok := tc.types[pgtype.OID(oid)] + typ, ok := tc.types[uint32(oid)] tc.mu.Unlock() return typ, ok } diff --git a/internal/pg/type_fetcher.go b/internal/pg/type_fetcher.go index c0ad0d6c..8eae7034 100644 --- a/internal/pg/type_fetcher.go +++ b/internal/pg/type_fetcher.go @@ -3,8 +3,7 @@ package pg import ( "context" "fmt" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "time" ) @@ -24,7 +23,7 @@ func NewTypeFetcher(conn *pgx.Conn) *TypeFetcher { // FindTypesByOIDs returns a map of a type OID to the Type description. The // returned map contains every unique OID in oids (oids may contain duplicates) // unless there's an error. -func (tf *TypeFetcher) FindTypesByOIDs(oids ...uint32) (map[pgtype.OID]Type, error) { +func (tf *TypeFetcher) FindTypesByOIDs(oids ...uint32) (map[uint32]Type, error) { if types, uncached := tf.cache.getOIDs(oids...); len(uncached) == 0 { return types, nil } @@ -96,7 +95,7 @@ func (tf *TypeFetcher) FindTypesByOIDs(oids ...uint32) (map[pgtype.OID]Type, err return types, nil } -func (tf *TypeFetcher) findEnumTypes(ctx context.Context, uncached map[pgtype.OID]struct{}) ([]EnumType, error) { +func (tf *TypeFetcher) findEnumTypes(ctx context.Context, uncached map[uint32]struct{}) ([]EnumType, error) { oids := oidKeys(uncached) rows, err := tf.querier.FindEnumTypes(ctx, oids) if err != nil { @@ -104,9 +103,9 @@ func (tf *TypeFetcher) findEnumTypes(ctx context.Context, uncached map[pgtype.OI } types := make([]EnumType, len(rows)) for i, enum := range rows { - childOIDs := make([]pgtype.OID, len(enum.ChildOIDs)) + childOIDs := make([]uint32, len(enum.ChildOIDs)) for i, oidUint32 := range enum.ChildOIDs { - childOIDs[i] = pgtype.OID(oidUint32) + childOIDs[i] = uint32(oidUint32) } types[i] = EnumType{ ID: enum.OID, @@ -119,14 +118,14 @@ func (tf *TypeFetcher) findEnumTypes(ctx context.Context, uncached map[pgtype.OI return types, nil } -func (tf *TypeFetcher) findCompositeTypes(ctx context.Context, uncached map[pgtype.OID]struct{}) ([]CompositeType, error) { +func (tf *TypeFetcher) findCompositeTypes(ctx context.Context, uncached map[uint32]struct{}) ([]CompositeType, error) { oids := oidKeys(uncached) rows, err := tf.querier.FindCompositeTypes(ctx, oids) if err != nil { return nil, fmt.Errorf("find composite types: %w", err) } // Record all composite types to fake a topological sort by repeated iteration. - allComposites := make(map[pgtype.OID]struct{}, len(rows)) + allComposites := make(map[uint32]struct{}, len(rows)) for _, row := range rows { allComposites[row.TableTypeOID] = struct{}{} } @@ -148,13 +147,13 @@ func (tf *TypeFetcher) findCompositeTypes(ctx context.Context, uncached map[pgty // We might resolve this type in a future pass like findArrayTypes. At // the end, we'll attempt to to replace the placeholder with the // resolved type. - colTypes[i] = placeholderType{ID: pgtype.OID(colOID)} + colTypes[i] = placeholderType{ID: uint32(colOID)} colNames[i] = row.ColNames[i] } } typ := CompositeType{ ID: row.TableTypeOID, - Name: row.TableName.String, + Name: row.TableName, ColumnNames: colNames, ColumnTypes: colTypes, } @@ -164,7 +163,7 @@ func (tf *TypeFetcher) findCompositeTypes(ctx context.Context, uncached map[pgty return types, nil } -func (tf *TypeFetcher) findUnknownTypes(ctx context.Context, uncached map[pgtype.OID]struct{}) ([]UnknownType, error) { +func (tf *TypeFetcher) findUnknownTypes(ctx context.Context, uncached map[uint32]struct{}) ([]UnknownType, error) { oids := oidKeys(uncached) rows, err := tf.querier.FindOIDNames(ctx, oids) if err != nil { @@ -174,14 +173,14 @@ func (tf *TypeFetcher) findUnknownTypes(ctx context.Context, uncached map[pgtype for i, row := range rows { types[i] = UnknownType{ ID: row.OID, - Name: row.Name.String, - PgKind: TypeKind(row.Kind.Int), + Name: row.Name, + PgKind: TypeKind(row.Kind), } } return types, nil } -func (tf *TypeFetcher) findArrayTypes(ctx context.Context, uncached map[pgtype.OID]struct{}) ([]ArrayType, error) { +func (tf *TypeFetcher) findArrayTypes(ctx context.Context, uncached map[uint32]struct{}) ([]ArrayType, error) { oids := oidKeys(uncached) rows, err := tf.querier.FindArrayTypes(ctx, oids) if err != nil { @@ -204,7 +203,7 @@ func (tf *TypeFetcher) findArrayTypes(ctx context.Context, uncached map[pgtype.O // resolvePlaceholderTypes resolves all placeholder types or errors if we can't // resolve a placeholderType using all known types. -func (tf *TypeFetcher) resolvePlaceholderTypes(knownTypes map[pgtype.OID]Type) error { +func (tf *TypeFetcher) resolvePlaceholderTypes(knownTypes map[uint32]Type) error { // resolveType walks down type, replacing placeholderType with a known type. var resolveType func(typ Type) (Type, error) resolveType = func(typ Type) (Type, error) { @@ -246,7 +245,7 @@ func (tf *TypeFetcher) resolvePlaceholderTypes(knownTypes map[pgtype.OID]Type) e return nil } -func oidKeys(os map[pgtype.OID]struct{}) []uint32 { +func oidKeys(os map[uint32]struct{}) []uint32 { oids := make([]uint32, 0, len(os)) for oid := range os { oids = append(oids, uint32(oid)) diff --git a/internal/pg/type_fetcher_test.go b/internal/pg/type_fetcher_test.go index 08e7456f..43fc0077 100644 --- a/internal/pg/type_fetcher_test.go +++ b/internal/pg/type_fetcher_test.go @@ -4,7 +4,6 @@ import ( "context" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/jackc/pgtype" "github.com/jschaf/pggen/internal/pg/pgoid" "github.com/jschaf/pggen/internal/pgtest" "github.com/jschaf/pggen/internal/texts" @@ -248,7 +247,7 @@ func TestNewTypeFetcher(t *testing.T) { } // Get the OID by name if fetchOID was a string, or just return the OID. -func findOIDVal(t *testing.T, fetchOID interface{}, querier *DBQuerier) pgtype.OID { +func findOIDVal(t *testing.T, fetchOID interface{}, querier *DBQuerier) uint32 { switch rawOID := fetchOID.(type) { case string: oid, err := querier.FindOIDByName(context.Background(), rawOID) @@ -256,10 +255,10 @@ func findOIDVal(t *testing.T, fetchOID interface{}, querier *DBQuerier) pgtype.O t.Fatalf("find oid by name %s: %s", rawOID, err) } return oid - case pgtype.OID: + case uint32: return rawOID case int: - return pgtype.OID(rawOID) + return uint32(rawOID) default: t.Fatalf("unhandled oid test value type %T: %v", rawOID, rawOID) return 0 diff --git a/internal/pg/types.go b/internal/pg/types.go index b5b07af4..b662df8a 100644 --- a/internal/pg/types.go +++ b/internal/pg/types.go @@ -1,15 +1,14 @@ package pg import ( - "github.com/jackc/pgtype" "github.com/jschaf/pggen/internal/pg/pgoid" "strconv" ) // Type is a Postgres type. type Type interface { - OID() pgtype.OID // pg_type.oid: row identifier - String() string // pg_type.typname: data type name + OID() uint32 // pg_type.oid: row identifier + String() string // pg_type.typname: data type name Kind() TypeKind } @@ -49,8 +48,8 @@ type ( // BaseType is a fundamental Postgres type like text and bool. // https://www.postgresql.org/docs/13/catalog-pg-type.html BaseType struct { - ID pgtype.OID // pg_type.oid: row identifier - Name string // pg_type.typname: data type name + ID uint32 // pg_type.oid: row identifier + Name string // pg_type.typname: data type name } // VoidType is an empty type. A void type doesn't appear in output, but it's @@ -60,7 +59,7 @@ type ( // ArrayType is an array type where pg_type.typelem != 0 and the name begins // with an underscore. ArrayType struct { - ID pgtype.OID // pg_type.oid: row identifier + ID uint32 // pg_type.oid: row identifier // The name of the type, like _int4. Array types in Postgres typically // begin with an underscore. From pg_type.typname. Name string @@ -69,7 +68,7 @@ type ( } EnumType struct { - ID pgtype.OID // pg_type.oid: row identifier + ID uint32 // pg_type.oid: row identifier // The name of the enum, like 'device_type' in: // CREATE TYPE device_type AS ENUM ('foo'); // From pg_type.typname. @@ -82,26 +81,26 @@ type ( // values is that they be correctly ordered and unique within each enum // type. Orders []float32 - ChildOIDs []pgtype.OID + ChildOIDs []uint32 } // DomainType is a user-create domain type. DomainType struct { - ID pgtype.OID // pg_type.oid: row identifier - Name string // pg_type.typname: data type name - IsNotNull bool // pg_type.typnotnull: domains only, not null constraint for domains - HasDefault bool // pg_type.typdefault: domains only, if there's a default value - BaseType BaseType // pg_type.typbasetype: domains only, the base type - Dimensions int // pg_type.typndims: domains on array type only, 0 otherwise, number of array dimensions + ID uint32 // pg_type.oid: row identifier + Name string // pg_type.typname: data type name + IsNotNull bool // pg_type.typnotnull: domains only, not null constraint for domains + HasDefault bool // pg_type.typdefault: domains only, if there's a default value + BaseType BaseType // pg_type.typbasetype: domains only, the base type + Dimensions int // pg_type.typndims: domains on array type only, 0 otherwise, number of array dimensions } // CompositeType is a type containing multiple columns and is represented as // a class. https://www.postgresql.org/docs/13/catalog-pg-class.html CompositeType struct { - ID pgtype.OID // pg_class.oid: row identifier - Name string // pg_class.relname: name of the composite type - ColumnNames []string // pg_attribute.attname: names of the column, in order - ColumnTypes []Type // pg_attribute JOIN pg_type: information about columns of the composite type + ID uint32 // pg_class.oid: row identifier + Name string // pg_class.relname: name of the composite type + ColumnNames []string // pg_attribute.attname: names of the column, in order + ColumnTypes []Type // pg_attribute JOIN pg_type: information about columns of the composite type } // UnknownType is a Postgres type that's not a well-known type in @@ -109,8 +108,8 @@ type ( // generator might be able to resolve this type from a user-provided mapping // like --go-type my_int=int. UnknownType struct { - ID pgtype.OID // pg_type.oid: row identifier - Name string // pg_type.typname: data type name + ID uint32 // pg_type.oid: row identifier + Name string // pg_type.typname: data type name PgKind TypeKind } @@ -120,38 +119,38 @@ type ( // requires two passes for cases like when a composite type has a child type // that's an array. placeholderType struct { - ID pgtype.OID // pg_type.oid: row identifier + ID uint32 // pg_type.oid: row identifier } ) -func (b BaseType) OID() pgtype.OID { return b.ID } -func (b BaseType) String() string { return b.Name } -func (b BaseType) Kind() TypeKind { return KindBaseType } +func (b BaseType) OID() uint32 { return b.ID } +func (b BaseType) String() string { return b.Name } +func (b BaseType) Kind() TypeKind { return KindBaseType } -func (b VoidType) OID() pgtype.OID { return pgoid.Void } -func (b VoidType) String() string { return "void" } -func (b VoidType) Kind() TypeKind { return KindPseudoType } +func (b VoidType) OID() uint32 { return pgoid.Void } +func (b VoidType) String() string { return "void" } +func (b VoidType) Kind() TypeKind { return KindPseudoType } -func (b ArrayType) OID() pgtype.OID { return b.ID } -func (b ArrayType) String() string { return b.Name } -func (b ArrayType) Kind() TypeKind { return KindBaseType } +func (b ArrayType) OID() uint32 { return b.ID } +func (b ArrayType) String() string { return b.Name } +func (b ArrayType) Kind() TypeKind { return KindBaseType } -func (e EnumType) OID() pgtype.OID { return e.ID } -func (e EnumType) String() string { return e.Name } -func (e EnumType) Kind() TypeKind { return KindEnumType } +func (e EnumType) OID() uint32 { return e.ID } +func (e EnumType) String() string { return e.Name } +func (e EnumType) Kind() TypeKind { return KindEnumType } -func (e DomainType) OID() pgtype.OID { return e.ID } -func (e DomainType) String() string { return e.Name } -func (e DomainType) Kind() TypeKind { return KindDomainType } +func (e DomainType) OID() uint32 { return e.ID } +func (e DomainType) String() string { return e.Name } +func (e DomainType) Kind() TypeKind { return KindDomainType } -func (e CompositeType) OID() pgtype.OID { return e.ID } -func (e CompositeType) String() string { return e.Name } -func (e CompositeType) Kind() TypeKind { return KindCompositeType } +func (e CompositeType) OID() uint32 { return e.ID } +func (e CompositeType) String() string { return e.Name } +func (e CompositeType) Kind() TypeKind { return KindCompositeType } -func (e UnknownType) OID() pgtype.OID { return e.ID } -func (e UnknownType) String() string { return e.Name } -func (e UnknownType) Kind() TypeKind { return e.PgKind } +func (e UnknownType) OID() uint32 { return e.ID } +func (e UnknownType) String() string { return e.Name } +func (e UnknownType) Kind() TypeKind { return e.PgKind } -func (p placeholderType) OID() pgtype.OID { return p.ID } -func (p placeholderType) String() string { return "placeholder-" + strconv.Itoa(int(p.ID)) } -func (p placeholderType) Kind() TypeKind { return kindPlaceholderType } +func (p placeholderType) OID() uint32 { return p.ID } +func (p placeholderType) String() string { return "placeholder-" + strconv.Itoa(int(p.ID)) } +func (p placeholderType) Kind() TypeKind { return kindPlaceholderType } diff --git a/internal/pgdocker/pgdocker.go b/internal/pgdocker/pgdocker.go index 1f4a1ce6..0d629932 100644 --- a/internal/pgdocker/pgdocker.go +++ b/internal/pgdocker/pgdocker.go @@ -12,7 +12,7 @@ import ( "github.com/docker/docker/api/types/container" dockerClient "github.com/docker/docker/client" "github.com/docker/go-connections/nat" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/errs" "github.com/jschaf/pggen/internal/ports" "io" diff --git a/internal/pginfer/pginfer.go b/internal/pginfer/pginfer.go index 82de7f24..bcb46d70 100644 --- a/internal/pginfer/pginfer.go +++ b/internal/pginfer/pginfer.go @@ -3,13 +3,11 @@ package pginfer import ( "context" "fmt" - "github.com/jackc/pgconn" + "github.com/jackc/pgx/v5/pgconn" "strings" "time" - "github.com/jackc/pgproto3/v2" - "github.com/jackc/pgtype" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "github.com/jschaf/pggen/internal/ast" "github.com/jschaf/pggen/internal/pg" ) @@ -157,7 +155,7 @@ func (inf *Inferrer) prepareTypes(query *ast.SourceQuery) (_a []InputParam, _ [] return nil, nil, fmt.Errorf("fetch oid types: %w", err) } for i, oid := range stmtDesc.ParamOIDs { - inputType, ok := types[pgtype.OID(oid)] + inputType, ok := types[uint32(oid)] if !ok { return nil, nil, fmt.Errorf("no postgres type name found for parameter %s with oid %d", query.ParamNames[i], oid) } @@ -187,7 +185,7 @@ func (inf *Inferrer) prepareTypes(query *ast.SourceQuery) (_a []InputParam, _ [] // Create output columns var outputColumns []OutputColumn for i, desc := range stmtDesc.Fields { - pgType, ok := outputTypes[pgtype.OID(desc.DataTypeOID)] + pgType, ok := outputTypes[uint32(desc.DataTypeOID)] if !ok { return nil, nil, fmt.Errorf("no postgrestype name found for column %s with oid %d", string(desc.Name), desc.DataTypeOID) } @@ -202,7 +200,7 @@ func (inf *Inferrer) prepareTypes(query *ast.SourceQuery) (_a []InputParam, _ [] // inferOutputNullability infers which of the output columns produced by the // query and described by descs can be null. -func (inf *Inferrer) inferOutputNullability(query *ast.SourceQuery, descs []pgproto3.FieldDescription) ([]bool, error) { +func (inf *Inferrer) inferOutputNullability(query *ast.SourceQuery, descs []pgconn.FieldDescription) ([]bool, error) { if len(descs) == 0 { return nil, nil } @@ -215,7 +213,7 @@ func (inf *Inferrer) inferOutputNullability(query *ast.SourceQuery, descs []pgpr for i, desc := range descs { if desc.TableOID > 0 { columnKeys[i] = pg.ColumnKey{ - TableOID: pgtype.OID(desc.TableOID), + TableOID: uint32(desc.TableOID), Number: desc.TableAttributeNumber, } } diff --git a/internal/pgplan/pgplan.go b/internal/pgplan/pgplan.go index 18294505..d7e69db3 100644 --- a/internal/pgplan/pgplan.go +++ b/internal/pgplan/pgplan.go @@ -3,7 +3,7 @@ package pgplan import ( "context" "fmt" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "time" ) diff --git a/internal/pgtest/pg_test_db.go b/internal/pgtest/pg_test_db.go index e0905fd0..62c1a986 100644 --- a/internal/pgtest/pg_test_db.go +++ b/internal/pgtest/pg_test_db.go @@ -2,7 +2,7 @@ package pgtest import ( "context" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v5" "math/rand" "os" "strconv"