-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathquery.sql.go
149 lines (124 loc) · 4.62 KB
/
query.sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Code generated by pggen. DO NOT EDIT.
package custom_types
import (
"context"
"fmt"
"github.com/jackc/pgconn"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jschaf/pggen/example/custom_types/mytype"
)
// Querier is a typesafe Go interface backed by SQL queries.
type Querier interface {
CustomTypes(ctx context.Context) (CustomTypesRow, error)
CustomMyInt(ctx context.Context) (int, error)
IntArray(ctx context.Context) ([][]int32, error)
}
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.
type genericConn interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
}
// 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
}
const customTypesSQL = `SELECT 'some_text', 1::bigint;`
type CustomTypesRow struct {
Column mytype.String `json:"?column?"`
Int8 CustomInt `json:"int8"`
}
// CustomTypes implements Querier.CustomTypes.
func (q *DBQuerier) CustomTypes(ctx context.Context) (CustomTypesRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "CustomTypes")
row := q.conn.QueryRow(ctx, customTypesSQL)
var item CustomTypesRow
if err := row.Scan(&item.Column, &item.Int8); err != nil {
return item, fmt.Errorf("query CustomTypes: %w", err)
}
return item, nil
}
const customMyIntSQL = `SELECT '5'::my_int as int5;`
// CustomMyInt implements Querier.CustomMyInt.
func (q *DBQuerier) CustomMyInt(ctx context.Context) (int, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "CustomMyInt")
row := q.conn.QueryRow(ctx, customMyIntSQL)
var item int
if err := row.Scan(&item); err != nil {
return item, fmt.Errorf("query CustomMyInt: %w", err)
}
return item, nil
}
const intArraySQL = `SELECT ARRAY ['5', '6', '7']::int[] as ints;`
// IntArray implements Querier.IntArray.
func (q *DBQuerier) IntArray(ctx context.Context) ([][]int32, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "IntArray")
rows, err := q.conn.Query(ctx, intArraySQL)
if err != nil {
return nil, fmt.Errorf("query IntArray: %w", err)
}
defer rows.Close()
items := [][]int32{}
for rows.Next() {
var item []int32
if err := rows.Scan(&item); err != nil {
return nil, fmt.Errorf("scan IntArray row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close IntArray rows: %w", err)
}
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