Created
March 25, 2021 12:35
-
-
Save abijr/ea58ff718b99a830c79772c803b86743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- name: QueryOne :one | |
SELECT 1 AS one; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by pggen. DO NOT EDIT. | |
package pggen_test | |
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 { | |
QueryOne(ctx context.Context) (int32, error) | |
// QueryOneBatch enqueues a QueryOne query into batch to be executed | |
// later by the batch. | |
QueryOneBatch(batch *pgx.Batch) | |
// QueryOneScan scans the result of an executed QueryOneBatch query. | |
QueryOneScan(results pgx.BatchResults) (int32, 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 queryOneSQL = `SELECT 1 AS one;` | |
// QueryOne implements Querier.QueryOne. | |
func (q *DBQuerier) QueryOne(ctx context.Context) (int32, error) { | |
row := q.conn.QueryRow(ctx, queryOneSQL) | |
var item int32 | |
if err := row.Scan(&item); err != nil { | |
return item, fmt.Errorf("query QueryOne: %w", err) | |
} | |
return item, nil | |
} | |
// QueryOneBatch implements Querier.QueryOneBatch. | |
func (q *DBQuerier) QueryOneBatch(batch *pgx.Batch) { | |
batch.Queue(queryOneSQL) | |
} | |
// QueryOneScan implements Querier.QueryOneScan. | |
func (q *DBQuerier) QueryOneScan(results pgx.BatchResults) (int32, error) { | |
row := results.QueryRow() | |
var item int32 | |
if err := row.Scan(&item); err != nil { | |
return item, fmt.Errorf("scan QueryOneBatch row: %w", err) | |
} | |
return item, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE test_tbl (test_column text); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- name: QueryTwo :one | |
SELECT 2 AS two; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by pggen. DO NOT EDIT. | |
package pggen_test | |
import ( | |
"context" | |
"fmt" | |
"github.com/jackc/pgx/v4" | |
) | |
const queryTwoSQL = `SELECT 2 AS two;` | |
// QueryTwo implements Querier.QueryTwo. | |
func (q *DBQuerier) QueryTwo(ctx context.Context) (int32, error) { | |
row := q.conn.QueryRow(ctx, queryTwoSQL) | |
var item int32 | |
if err := row.Scan(&item); err != nil { | |
return item, fmt.Errorf("query QueryTwo: %w", err) | |
} | |
return item, nil | |
} | |
// QueryTwoBatch implements Querier.QueryTwoBatch. | |
func (q *DBQuerier) QueryTwoBatch(batch *pgx.Batch) { | |
batch.Queue(queryTwoSQL) | |
} | |
// QueryTwoScan implements Querier.QueryTwoScan. | |
func (q *DBQuerier) QueryTwoScan(results pgx.BatchResults) (int32, error) { | |
row := results.QueryRow() | |
var item int32 | |
if err := row.Scan(&item); err != nil { | |
return item, fmt.Errorf("scan QueryTwoBatch row: %w", err) | |
} | |
return item, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment