Skip to content

Instantly share code, notes, and snippets.

@abijr

abijr/query.sql Secret

Last active April 17, 2021 17:54
Show Gist options
  • Save abijr/6f7883bb089f974dd1f9310b2db62420 to your computer and use it in GitHub Desktop.
Save abijr/6f7883bb089f974dd1f9310b2db62420 to your computer and use it in GitHub Desktop.
Code generates pointer results for non-nullable types
-- name: BugExample :one
SELECT
u.name,
r.some_data
FROM some_reference_tbl r
JOIN user_tbl u ON u.id = r.user_id
;
// Code generated by pggen. DO NOT EDIT.
package queries
import (
"context"
"fmt"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
// Querier is a typesafe Go interface backed by SQL queries.
//
// Methods ending with Batch enqueue a query to run later in a pgx.Batch. After
// calling SendBatch on pgx.Conn, pgxpool.Pool, or pgx.Tx, use the Scan methods
// to parse the results.
type Querier interface {
BugExample(ctx context.Context) (BugExampleRow, error)
// BugExampleBatch enqueues a BugExample query into batch to be executed
// later by the batch.
BugExampleBatch(batch *pgx.Batch)
// BugExampleScan scans the result of an executed BugExampleBatch query.
BugExampleScan(results pgx.BatchResults) (BugExampleRow, error)
}
type DBQuerier struct {
conn genericConn
}
var _ Querier = &DBQuerier{}
// genericConn is a connection to a Postgres database. This is usually backed by
// *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
type genericConn interface {
// Query executes sql with args. If there is an error the returned Rows will
// be returned in an error state. So it is allowed to ignore the error
// returned from Query and handle it in Rows.
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
// QueryRow is a convenience wrapper over Query. Any error that occurs while
// querying is deferred until calling Scan on the returned Row. That Row will
// error with pgx.ErrNoRows if no rows are returned.
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
// Exec executes sql. sql can be either a prepared statement name or an SQL
// string. arguments should be referenced positionally from the sql string
// as $1, $2, etc.
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
}
// NewQuerier creates a DBQuerier that implements Querier. conn is typically
// *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
func NewQuerier(conn genericConn) *DBQuerier {
return &DBQuerier{
conn: conn,
}
}
// WithTx creates a new DBQuerier that uses the transaction to run all queries.
func (q *DBQuerier) WithTx(tx pgx.Tx) (*DBQuerier, error) {
return &DBQuerier{conn: tx}, nil
}
const bugExampleSQL = `SELECT
u.name,
r.some_data
FROM some_reference_tbl r
JOIN user_tbl u ON u.id = r.user_id
;`
type BugExampleRow struct {
Name *string `json:"name"`
SomeData *string `json:"some_data"`
}
// BugExample implements Querier.BugExample.
func (q *DBQuerier) BugExample(ctx context.Context) (BugExampleRow, error) {
row := q.conn.QueryRow(ctx, bugExampleSQL)
var item BugExampleRow
if err := row.Scan(&item.Name, &item.SomeData); err != nil {
return item, fmt.Errorf("query BugExample: %w", err)
}
return item, nil
}
// BugExampleBatch implements Querier.BugExampleBatch.
func (q *DBQuerier) BugExampleBatch(batch *pgx.Batch) {
batch.Queue(bugExampleSQL)
}
// BugExampleScan implements Querier.BugExampleScan.
func (q *DBQuerier) BugExampleScan(results pgx.BatchResults) (BugExampleRow, error) {
row := results.QueryRow()
var item BugExampleRow
if err := row.Scan(&item.Name, &item.SomeData); err != nil {
return item, fmt.Errorf("scan BugExampleBatch row: %w", err)
}
return item, nil
}
CREATE TABLE user_tbl (
id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name TEXT NOT NULL
);
CREATE TABLE some_reference_tbl (
id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
user_id INT NOT NULL REFERENCES user_tbl (id),
some_data TEXT NOT NULL
);
@abijr
Copy link
Author

abijr commented Apr 17, 2021

pggen gen go -schema-glob=schema/schema.sql -query-glob='queries/*.sql'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment