forked from jschaf/pggen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.sql.go
237 lines (191 loc) · 7.36 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Code generated by pggen. DO NOT EDIT.
package enums
import (
"context"
"fmt"
"net"
"sync"
pgxdecimal "github.com/jackc/pgx-shopspring-decimal"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgtype"
)
type QueryName struct{}
// Querier is a typesafe Go interface backed by SQL queries.
type Querier interface {
FindAllDevices(ctx context.Context) ([]FindAllDevicesRow, error)
InsertDevice(ctx context.Context, mac net.HardwareAddr, typePg DeviceType) (pgconn.CommandTag, error)
// Select an array of all device_type enum values.
FindOneDeviceArray(ctx context.Context) ([]DeviceType, error)
// Select many rows of device_type enum values.
FindManyDeviceArray(ctx context.Context) ([][]DeviceType, error)
// Select many rows of device_type enum values with multiple output columns.
FindManyDeviceArrayWithNum(ctx context.Context) ([]FindManyDeviceArrayWithNumRow, error)
// Regression test for https://github.com/jschaf/pggen/issues/23.
EnumInsideComposite(ctx context.Context) (Device, error)
}
var _ Querier = &DBQuerier{}
type DBQuerier struct {
conn genericConn
errWrap func(err error) error
}
// 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)
TypeMap() *pgtype.Map
LoadType(ctx context.Context, typeName string) (*pgtype.Type, error)
}
// NewQuerier creates a DBQuerier that implements Querier.
func NewQuerier(conn genericConn) *DBQuerier {
return &DBQuerier{
conn: conn,
errWrap: func(err error) error {
return err
},
}
}
// Device represents the Postgres composite type "device".
type Device struct {
Mac net.HardwareAddr `json:"mac"`
Type DeviceType `json:"type"`
}
// DeviceType represents the Postgres enum "device_type".
type DeviceType string
const (
DeviceTypeUndefined DeviceType = "undefined"
DeviceTypePhone DeviceType = "phone"
DeviceTypeLaptop DeviceType = "laptop"
DeviceTypeIpad DeviceType = "ipad"
DeviceTypeDesktop DeviceType = "desktop"
DeviceTypeIot DeviceType = "iot"
)
func (d DeviceType) String() string { return string(d) }
var registerOnce sync.Once
var registerErr error
func registerTypes(ctx context.Context, conn genericConn) error {
registerOnce.Do(func() {
typeMap := conn.TypeMap()
pgxdecimal.Register(typeMap)
for _, typ := range typesToRegister {
dt, err := conn.LoadType(ctx, typ)
if err != nil {
registerErr = err
return
}
typeMap.RegisterType(dt)
}
})
return registerErr
}
var typesToRegister = []string{}
func addTypeToRegister(typ string) struct{} {
typesToRegister = append(typesToRegister, typ)
return struct{}{}
}
var _ = addTypeToRegister("public.device")
var _ = addTypeToRegister("public._device_type")
const findAllDevicesSQL = `SELECT mac, type
FROM device;`
type FindAllDevicesRow struct {
Mac net.HardwareAddr `json:"mac"`
Type DeviceType `json:"type"`
}
// FindAllDevices implements Querier.FindAllDevices.
func (q *DBQuerier) FindAllDevices(ctx context.Context) ([]FindAllDevicesRow, error) {
err := registerTypes(ctx, q.conn)
if err != nil {
return nil, fmt.Errorf("registering types failed: %w", q.errWrap(err))
}
ctx = context.WithValue(ctx, QueryName{}, "FindAllDevices")
rows, err := q.conn.Query(ctx, findAllDevicesSQL)
if err != nil {
return nil, fmt.Errorf("query FindAllDevices: %w", q.errWrap(err))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByName[FindAllDevicesRow])
return res, q.errWrap(err)
}
const insertDeviceSQL = `INSERT INTO device (mac, type)
VALUES ($1, $2);`
// InsertDevice implements Querier.InsertDevice.
func (q *DBQuerier) InsertDevice(ctx context.Context, mac net.HardwareAddr, typePg DeviceType) (pgconn.CommandTag, error) {
err := registerTypes(ctx, q.conn)
if err != nil {
return pgconn.CommandTag{}, fmt.Errorf("registering types failed: %w", q.errWrap(err))
}
ctx = context.WithValue(ctx, QueryName{}, "InsertDevice")
cmdTag, err := q.conn.Exec(ctx, insertDeviceSQL, mac, typePg)
if err != nil {
return pgconn.CommandTag{}, fmt.Errorf("exec query InsertDevice: %w", q.errWrap(err))
}
return cmdTag, q.errWrap(err)
}
const findOneDeviceArraySQL = `SELECT enum_range(NULL::device_type) AS device_types;`
// FindOneDeviceArray implements Querier.FindOneDeviceArray.
func (q *DBQuerier) FindOneDeviceArray(ctx context.Context) ([]DeviceType, error) {
err := registerTypes(ctx, q.conn)
if err != nil {
return nil, fmt.Errorf("registering types failed: %w", q.errWrap(err))
}
ctx = context.WithValue(ctx, QueryName{}, "FindOneDeviceArray")
rows, err := q.conn.Query(ctx, findOneDeviceArraySQL)
if err != nil {
return nil, fmt.Errorf("query FindOneDeviceArray: %w", q.errWrap(err))
}
res, err := pgx.CollectExactlyOneRow(rows, pgx.RowTo[[]DeviceType])
return res, q.errWrap(err)
}
const findManyDeviceArraySQL = `SELECT enum_range('ipad'::device_type, 'iot'::device_type) AS device_types
UNION ALL
SELECT enum_range(NULL::device_type) AS device_types;`
// FindManyDeviceArray implements Querier.FindManyDeviceArray.
func (q *DBQuerier) FindManyDeviceArray(ctx context.Context) ([][]DeviceType, error) {
err := registerTypes(ctx, q.conn)
if err != nil {
return nil, fmt.Errorf("registering types failed: %w", q.errWrap(err))
}
ctx = context.WithValue(ctx, QueryName{}, "FindManyDeviceArray")
rows, err := q.conn.Query(ctx, findManyDeviceArraySQL)
if err != nil {
return nil, fmt.Errorf("query FindManyDeviceArray: %w", q.errWrap(err))
}
res, err := pgx.CollectRows(rows, pgx.RowTo[[]DeviceType])
return res, q.errWrap(err)
}
const findManyDeviceArrayWithNumSQL = `SELECT 1 AS num, enum_range('ipad'::device_type, 'iot'::device_type) AS device_types
UNION ALL
SELECT 2 as num, enum_range(NULL::device_type) AS device_types;`
type FindManyDeviceArrayWithNumRow struct {
Num *int32 `json:"num"`
DeviceTypes []DeviceType `json:"device_types"`
}
// FindManyDeviceArrayWithNum implements Querier.FindManyDeviceArrayWithNum.
func (q *DBQuerier) FindManyDeviceArrayWithNum(ctx context.Context) ([]FindManyDeviceArrayWithNumRow, error) {
err := registerTypes(ctx, q.conn)
if err != nil {
return nil, fmt.Errorf("registering types failed: %w", q.errWrap(err))
}
ctx = context.WithValue(ctx, QueryName{}, "FindManyDeviceArrayWithNum")
rows, err := q.conn.Query(ctx, findManyDeviceArrayWithNumSQL)
if err != nil {
return nil, fmt.Errorf("query FindManyDeviceArrayWithNum: %w", q.errWrap(err))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByName[FindManyDeviceArrayWithNumRow])
return res, q.errWrap(err)
}
const enumInsideCompositeSQL = `SELECT ROW('08:00:2b:01:02:03'::macaddr, 'phone'::device_type) ::device;`
// EnumInsideComposite implements Querier.EnumInsideComposite.
func (q *DBQuerier) EnumInsideComposite(ctx context.Context) (Device, error) {
err := registerTypes(ctx, q.conn)
if err != nil {
return Device{}, fmt.Errorf("registering types failed: %w", q.errWrap(err))
}
ctx = context.WithValue(ctx, QueryName{}, "EnumInsideComposite")
rows, err := q.conn.Query(ctx, enumInsideCompositeSQL)
if err != nil {
return Device{}, fmt.Errorf("query EnumInsideComposite: %w", q.errWrap(err))
}
res, err := pgx.CollectExactlyOneRow(rows, pgx.RowTo[Device])
return res, q.errWrap(err)
}