func (q *DBQuerier) CompositeUserOneTwoCols(ctx context.Context) (CompositeUserOneTwoColsRow, error) {
	ctx = context.WithValue(ctx, "pggen_query_name", "CompositeUserOneTwoCols")
	row := q.conn.QueryRow(ctx, compositeUserOneTwoColsSQL)
	var item CompositeUserOneTwoColsRow
	userRow := q.types.newUser()
	if err := row.Scan(&item.Num, userRow); err != nil {
		return item, fmt.Errorf("query CompositeUserOneTwoCols: %w", err)
	}
	if err := userRow.AssignTo(&item.User); err != nil {
		return item, fmt.Errorf("assign CompositeUserOneTwoCols row: %w", err)
	}
	return item, nil
}
CompositeUserOneTwoCols implements Querier.CompositeUserOneTwoCols.
package device
import (
	"context"
	"fmt"
	"github.com/jackc/pgconn"
	"github.com/jackc/pgtype"
	"github.com/jackc/pgx/v4"
)
type Querier interface {
	FindDevicesByUser(ctx context.Context, id int) ([]FindDevicesByUserRow, error)
	FindDevicesByUserBatch(batch genericBatch, id int)
	FindDevicesByUserScan(results pgx.BatchResults) ([]FindDevicesByUserRow, error)
	CompositeUser(ctx context.Context) ([]CompositeUserRow, error)
	CompositeUserBatch(batch genericBatch)
	CompositeUserScan(results pgx.BatchResults) ([]CompositeUserRow, error)
	CompositeUserOne(ctx context.Context) (User, error)
	CompositeUserOneBatch(batch genericBatch)
	CompositeUserOneScan(results pgx.BatchResults) (User, error)
	CompositeUserOneTwoCols(ctx context.Context) (CompositeUserOneTwoColsRow, error)
	CompositeUserOneTwoColsBatch(batch genericBatch)
	CompositeUserOneTwoColsScan(results pgx.BatchResults) (CompositeUserOneTwoColsRow, error)
	CompositeUserMany(ctx context.Context) ([]User, error)
	CompositeUserManyBatch(batch genericBatch)
	CompositeUserManyScan(results pgx.BatchResults) ([]User, error)
	InsertUser(ctx context.Context, userID int, name string) (pgconn.CommandTag, error)
	InsertUserBatch(batch genericBatch, userID int, name string)
	InsertUserScan(results pgx.BatchResults) (pgconn.CommandTag, error)
	InsertDevice(ctx context.Context, mac pgtype.Macaddr, owner int) (pgconn.CommandTag, error)
	InsertDeviceBatch(batch genericBatch, mac pgtype.Macaddr, owner int)
	InsertDeviceScan(results pgx.BatchResults) (pgconn.CommandTag, error)
}
type DBQuerier struct {
	conn  genericConn    
	types *typeResolver  
}
var _ Querier = &DBQuerier{}
type genericConn interface {
	Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
	Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
}
type genericBatch interface {
	Queue(query string, arguments ...interface{})
}
func NewQuerier(conn genericConn) *DBQuerier {
	return NewQuerierConfig(conn, QuerierConfig{})
}
type QuerierConfig struct {
	DataTypes []pgtype.DataType
}
func NewQuerierConfig(conn genericConn, cfg QuerierConfig) *DBQuerier {
	return &DBQuerier{conn: conn, types: newTypeResolver(cfg.DataTypes)}
}
func (q *DBQuerier) WithTx(tx pgx.Tx) (*DBQuerier, error) {
	return &DBQuerier{conn: tx}, nil
}
type preparer interface {
	Prepare(ctx context.Context, name, sql string) (sd *pgconn.StatementDescription, err error)
}
func PrepareAllQueries(ctx context.Context, p preparer) error {
	if _, err := p.Prepare(ctx, findDevicesByUserSQL, findDevicesByUserSQL); err != nil {
		return fmt.Errorf("prepare query 'FindDevicesByUser': %w", err)
	}
	if _, err := p.Prepare(ctx, compositeUserSQL, compositeUserSQL); err != nil {
		return fmt.Errorf("prepare query 'CompositeUser': %w", err)
	}
	if _, err := p.Prepare(ctx, compositeUserOneSQL, compositeUserOneSQL); err != nil {
		return fmt.Errorf("prepare query 'CompositeUserOne': %w", err)
	}
	if _, err := p.Prepare(ctx, compositeUserOneTwoColsSQL, compositeUserOneTwoColsSQL); err != nil {
		return fmt.Errorf("prepare query 'CompositeUserOneTwoCols': %w", err)
	}
	if _, err := p.Prepare(ctx, compositeUserManySQL, compositeUserManySQL); err != nil {
		return fmt.Errorf("prepare query 'CompositeUserMany': %w", err)
	}
	if _, err := p.Prepare(ctx, insertUserSQL, insertUserSQL); err != nil {
		return fmt.Errorf("prepare query 'InsertUser': %w", err)
	}
	if _, err := p.Prepare(ctx, insertDeviceSQL, insertDeviceSQL); err != nil {
		return fmt.Errorf("prepare query 'InsertDevice': %w", err)
	}
	return nil
}
type User struct {
	ID   *int    `json:"id"`
	Name *string `json:"name"`
}
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) }
type typeResolver struct {
	connInfo *pgtype.ConnInfo  
}
func newTypeResolver(types []pgtype.DataType) *typeResolver {
	ci := pgtype.NewConnInfo()
	for _, typ := range types {
		if txt, ok := typ.Value.(textPreferrer); ok && typ.OID != unknownOID {
			typ.Value = txt.ValueTranscoder
		}
		ci.RegisterDataType(typ)
	}
	return &typeResolver{connInfo: ci}
}
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
}
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
}
type compositeField struct {
	name       string                  
	typeName   string                  
	defaultVal pgtype.ValueTranscoder  
}
func (tr *typeResolver) newCompositeValue(name string, fields ...compositeField) pgtype.ValueTranscoder {
	if _, val, ok := tr.findValue(name); ok {
		return val
	}
	fs := make([]pgtype.CompositeTypeField, len(fields))
	vals := make([]pgtype.ValueTranscoder, len(fields))
	isBinaryOk := true
	for i, field := range fields {
		oid, val, ok := tr.findValue(field.typeName)
		if !ok {
			oid = unknownOID
			val = field.defaultVal
		}
		isBinaryOk = isBinaryOk && oid != unknownOID
		fs[i] = pgtype.CompositeTypeField{Name: field.name, OID: oid}
		vals[i] = val
	}
	typ, _ := pgtype.NewCompositeTypeValues(name, fs, vals)
	if !isBinaryOk {
		return textPreferrer{typ, name}
	}
	return typ
}
func (tr *typeResolver) newArrayValue(name, elemName string, defaultVal func() pgtype.ValueTranscoder) pgtype.ValueTranscoder {
	if _, val, ok := tr.findValue(name); ok {
		return val
	}
	elemOID, elemVal, ok := tr.findValue(elemName)
	elemValFunc := func() pgtype.ValueTranscoder {
		return pgtype.NewValue(elemVal).(pgtype.ValueTranscoder)
	}
	if !ok {
		elemOID = unknownOID
		elemValFunc = defaultVal
	}
	typ := pgtype.NewArrayType(name, elemOID, elemValFunc)
	if elemOID == unknownOID {
		return textPreferrer{typ, name}
	}
	return typ
}
func (tr *typeResolver) newUser() pgtype.ValueTranscoder {
	return tr.newCompositeValue(
		"user",
		compositeField{"id", "int8", &pgtype.Int8{}},
		compositeField{"name", "text", &pgtype.Text{}},
	)
}
const findDevicesByUserSQL = `SELECT
  id,
  name,
  (SELECT array_agg(mac) FROM device WHERE owner = id) AS mac_addrs
FROM "user"
WHERE id = $1;`
type FindDevicesByUserRow struct {
	ID       int                 `json:"id"`
	Name     string              `json:"name"`
	MacAddrs pgtype.MacaddrArray `json:"mac_addrs"`
}
func (q *DBQuerier) FindDevicesByUser(ctx context.Context, id int) ([]FindDevicesByUserRow, error) {
	ctx = context.WithValue(ctx, "pggen_query_name", "FindDevicesByUser")
	rows, err := q.conn.Query(ctx, findDevicesByUserSQL, id)
	if err != nil {
		return nil, fmt.Errorf("query FindDevicesByUser: %w", err)
	}
	defer rows.Close()
	items := []FindDevicesByUserRow{}
	for rows.Next() {
		var item FindDevicesByUserRow
		if err := rows.Scan(&item.ID, &item.Name, &item.MacAddrs); err != nil {
			return nil, fmt.Errorf("scan FindDevicesByUser row: %w", err)
		}
		items = append(items, item)
	}
	if err := rows.Err(); err != nil {
		return nil, fmt.Errorf("close FindDevicesByUser rows: %w", err)
	}
	return items, err
}
func (q *DBQuerier) FindDevicesByUserBatch(batch genericBatch, id int) {
	batch.Queue(findDevicesByUserSQL, id)
}
func (q *DBQuerier) FindDevicesByUserScan(results pgx.BatchResults) ([]FindDevicesByUserRow, error) {
	rows, err := results.Query()
	if err != nil {
		return nil, fmt.Errorf("query FindDevicesByUserBatch: %w", err)
	}
	defer rows.Close()
	items := []FindDevicesByUserRow{}
	for rows.Next() {
		var item FindDevicesByUserRow
		if err := rows.Scan(&item.ID, &item.Name, &item.MacAddrs); err != nil {
			return nil, fmt.Errorf("scan FindDevicesByUserBatch row: %w", err)
		}
		items = append(items, item)
	}
	if err := rows.Err(); err != nil {
		return nil, fmt.Errorf("close FindDevicesByUserBatch rows: %w", err)
	}
	return items, err
}
const compositeUserSQL = `SELECT
  d.mac,
  d.type,
  ROW (u.id, u.name)::"user" AS "user"
FROM device d
  LEFT JOIN "user" u ON u.id = d.owner;`
type CompositeUserRow struct {
	Mac  pgtype.Macaddr `json:"mac"`
	Type DeviceType     `json:"type"`
	User User           `json:"user"`
}
func (q *DBQuerier) CompositeUser(ctx context.Context) ([]CompositeUserRow, error) {
	ctx = context.WithValue(ctx, "pggen_query_name", "CompositeUser")
	rows, err := q.conn.Query(ctx, compositeUserSQL)
	if err != nil {
		return nil, fmt.Errorf("query CompositeUser: %w", err)
	}
	defer rows.Close()
	items := []CompositeUserRow{}
	userRow := q.types.newUser()
	for rows.Next() {
		var item CompositeUserRow
		if err := rows.Scan(&item.Mac, &item.Type, userRow); err != nil {
			return nil, fmt.Errorf("scan CompositeUser row: %w", err)
		}
		if err := userRow.AssignTo(&item.User); err != nil {
			return nil, fmt.Errorf("assign CompositeUser row: %w", err)
		}
		items = append(items, item)
	}
	if err := rows.Err(); err != nil {
		return nil, fmt.Errorf("close CompositeUser rows: %w", err)
	}
	return items, err
}
func (q *DBQuerier) CompositeUserBatch(batch genericBatch) {
	batch.Queue(compositeUserSQL)
}
func (q *DBQuerier) CompositeUserScan(results pgx.BatchResults) ([]CompositeUserRow, error) {
	rows, err := results.Query()
	if err != nil {
		return nil, fmt.Errorf("query CompositeUserBatch: %w", err)
	}
	defer rows.Close()
	items := []CompositeUserRow{}
	userRow := q.types.newUser()
	for rows.Next() {
		var item CompositeUserRow
		if err := rows.Scan(&item.Mac, &item.Type, userRow); err != nil {
			return nil, fmt.Errorf("scan CompositeUserBatch row: %w", err)
		}
		if err := userRow.AssignTo(&item.User); err != nil {
			return nil, fmt.Errorf("assign CompositeUser row: %w", err)
		}
		items = append(items, item)
	}
	if err := rows.Err(); err != nil {
		return nil, fmt.Errorf("close CompositeUserBatch rows: %w", err)
	}
	return items, err
}
const compositeUserOneSQL = `SELECT ROW (15, 'qux')::"user" AS "user";`
func (q *DBQuerier) CompositeUserOne(ctx context.Context) (User, error) {
	ctx = context.WithValue(ctx, "pggen_query_name", "CompositeUserOne")
	row := q.conn.QueryRow(ctx, compositeUserOneSQL)
	var item User
	userRow := q.types.newUser()
	if err := row.Scan(userRow); err != nil {
		return item, fmt.Errorf("query CompositeUserOne: %w", err)
	}
	if err := userRow.AssignTo(&item); err != nil {
		return item, fmt.Errorf("assign CompositeUserOne row: %w", err)
	}
	return item, nil
}
func (q *DBQuerier) CompositeUserOneBatch(batch genericBatch) {
	batch.Queue(compositeUserOneSQL)
}
func (q *DBQuerier) CompositeUserOneScan(results pgx.BatchResults) (User, error) {
	row := results.QueryRow()
	var item User
	userRow := q.types.newUser()
	if err := row.Scan(userRow); err != nil {
		return item, fmt.Errorf("scan CompositeUserOneBatch row: %w", err)
	}
	if err := userRow.AssignTo(&item); err != nil {
		return item, fmt.Errorf("assign CompositeUserOne row: %w", err)
	}
	return item, nil
}
const compositeUserOneTwoColsSQL = `SELECT 1 AS num, ROW (15, 'qux')::"user" AS "user";`
type CompositeUserOneTwoColsRow struct {
	Num  int32 `json:"num"`
	User User  `json:"user"`
}