Documentation ¶
Index ¶
- type DB
- type DBT
- func (d *DBT[T]) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (*TxT[pgx.Tx], error)
- func (d DBT) Exec(ctx context.Context, query litsql.Query, params any) (pgconn.CommandTag, error)
- func (d DBT) Handler() T
- func (d DBT) Prepare(ctx context.Context, name string, query litsql.Query) (*Stmt[T], error)
- func (d DBT) Query(ctx context.Context, query litsql.Query, params any) (pgx.Rows, error)
- func (d DBT) QueryRow(ctx context.Context, query litsql.Query, params any) (pgx.Row, error)
- type Option
- type PGXQuerier
- type PGXQuerierDB
- type PGXQuerierTx
- type QuerierDB
- type QuerierDBT
- type QuerierStmt
- type QuerierStmtT
- type QuerierT
- type QuerierTx
- type QuerierTxT
- type Stmt
- type Tx
- type TxT
- func (d *TxT[T]) Begin(ctx context.Context) (*TxT[pgx.Tx], error)
- func (d *TxT[T]) Commit(ctx context.Context) error
- func (d TxT) Exec(ctx context.Context, query litsql.Query, params any) (pgconn.CommandTag, error)
- func (d TxT) Handler() T
- func (d TxT) Prepare(ctx context.Context, name string, query litsql.Query) (*Stmt[T], error)
- func (d TxT) Query(ctx context.Context, query litsql.Query, params any) (pgx.Rows, error)
- func (d TxT) QueryRow(ctx context.Context, query litsql.Query, params any) (pgx.Row, error)
- func (d *TxT[T]) Rollback(ctx context.Context) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB = DBT[*pgx.Conn]
DB wraps a pgx.Conn.
Example ¶
ctx := context.Background() conn, err := pgx.Connect(ctx, "test") if err != nil { panic(err) } // wrap *pgx.Conn instance ddb := lpgx.NewDB(conn) query := psql.Select( sm.Columns("film_id", "title", "length"), sm.From("film"), sm.WhereClause("length > ?", sq.NamedArg("length")), sm.Limit(10), ) // generate SQL string from litsql and execute it, replacing named parameters. rows, err := ddb.Query(ctx, query, map[string]any{ "length": 90, }) if err != nil { panic(err) } defer rows.Close() for rows.Next() { var film_id, length int var title string err = rows.Scan(&film_id, &title, &length) if err != nil { panic(err) } } if rows.Err() != nil { panic(err) }
Output:
type DBT ¶
type DBT[T PGXQuerierDB] struct { // contains filtered or unexported fields }
DBT wraps any implementation of PGXQuerierDB.
func NewDBT ¶
func NewDBT[T PGXQuerierDB](querier T, options ...Option) *DBT[T]
NewDBT wraps any implementation of PGXQuerierDB.
type PGXQuerier ¶
type PGXQuerier 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, args ...any) (commandTag pgconn.CommandTag, err error) Prepare(ctx context.Context, name, sql string) (sd *pgconn.StatementDescription, err error) }
PGXQuerier is something that lpgx can query and get the pgx.Rows from. For example, it can be: pgx.Conn or pgx.Tx.
type PGXQuerierDB ¶
type PGXQuerierDB interface { PGXQuerier BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) }
type PGXQuerierTx ¶
type QuerierDB ¶
type QuerierDB = QuerierDBT[*pgx.Conn]
type QuerierDBT ¶
type QuerierStmt ¶
type QuerierStmt = QuerierStmtT
type QuerierStmtT ¶
type QuerierT ¶
type QuerierT[T PGXQuerier] interface { Query(ctx context.Context, query litsql.Query, params any) (pgx.Rows, error) QueryRow(ctx context.Context, query litsql.Query, params any) (pgx.Row, error) Exec(ctx context.Context, query litsql.Query, params any) (pgconn.CommandTag, error) Prepare(ctx context.Context, name string, query litsql.Query) (*Stmt[T], error) }
type QuerierTx ¶
type QuerierTx = QuerierTxT[pgx.Tx]
type QuerierTxT ¶
type Stmt ¶
type Stmt[T PGXQuerier] struct { // contains filtered or unexported fields }
Stmt wraps a PGXQuerier with a statement description from a Prepare call.
Example ¶
ctx := context.Background() conn, err := pgx.Connect(ctx, "test") if err != nil { panic(err) } // wrap *pgx.Conn instance ddb := lpgx.NewDB(conn) query := psql.Select( sm.Columns("film_id", "title", "length"), sm.From("film"), sm.WhereClause("length > ?", sq.NamedArg("length")), sm.Limit(10), ) queryName := "query1" // generate SQL string from litsql and prepare it, storing the named parameters to be replaced later dstmt, err := ddb.Prepare(ctx, queryName, query) if err != nil { panic(err) } // execute prepared query, replacing named parameters rows, err := dstmt.Query(ctx, map[string]any{ "length": 90, }) if err != nil { panic(err) } defer rows.Close() for rows.Next() { var film_id, length int var title string err = rows.Scan(&film_id, &title, &length) if err != nil { panic(err) } } if rows.Err() != nil { panic(err) }
Output:
func NewStmt ¶
func NewStmt[T PGXQuerier](querier T, desc *pgconn.StatementDescription, args []any, options ...Option) *Stmt[T]
NewStmt wraps a PGXQuerier with a statement description from a Prepare call.
type TxT ¶
type TxT[T PGXQuerierTx] struct { // contains filtered or unexported fields }
TxT wraps any implementation of PGXQuerierTx.
func NewTxT ¶
func NewTxT[T PGXQuerierTx](querier T, options ...Option) *TxT[T]
NewTxT wraps any implementation of PGXQuerierTx.
Click to show internal directories.
Click to hide internal directories.