lpgx

package module
v0.11.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 5 Imported by: 0

README

litsql-db lpgx

Test Status GitHub go.mod Go version Go Reference GitHub tag (latest SemVer)

Golang pgx DB wrappers for litsql.

Installation

go get -u github.com/rrgmc/litsql-db/lpgx

Examples

Query
func ExampleDB() {
    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)
    }
}
Prepared statement
func ExampleStmt() {
    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)
    }
}

Author

Rangel Reale (rangelreale@gmail.com)

Documentation

Index

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:

func NewDB

func NewDB(db *pgx.Conn, options ...Option) *DB

NewDB wraps a pgx.Conn.

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.

func (*DBT[T]) BeginTx

func (d *DBT[T]) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (*TxT[pgx.Tx], error)

func (DBT) Exec added in v0.11.3

func (d DBT) Exec(ctx context.Context, query litsql.Query, params any) (pgconn.CommandTag, error)

func (DBT) Handler added in v0.11.3

func (d DBT) Handler() T

func (DBT) Prepare added in v0.11.3

func (d DBT) Prepare(ctx context.Context, name string, query litsql.Query) (*Stmt[T], error)

func (DBT) Query added in v0.11.3

func (d DBT) Query(ctx context.Context, query litsql.Query, params any) (pgx.Rows, error)

func (DBT) QueryRow added in v0.11.3

func (d DBT) QueryRow(ctx context.Context, query litsql.Query, params any) (pgx.Row, error)

type Option

type Option func(options *dbOptions)

func WithQueryHandler

func WithQueryHandler(queryHandler sq.Handler) Option

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 PGXQuerierTx interface {
	PGXQuerier
	Begin(ctx context.Context) (pgx.Tx, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type QuerierDB

type QuerierDB = QuerierDBT[*pgx.Conn]

type QuerierDBT

type QuerierDBT[T PGXQuerier] interface {
	QuerierT[T]
	BeginTx(ctx context.Context, opts pgx.TxOptions) (*TxT[pgx.Tx], error)
}

type QuerierStmt

type QuerierStmt = QuerierStmtT

type QuerierStmtT

type QuerierStmtT interface {
	Query(ctx context.Context, params any) (pgx.Rows, error)
	QueryRow(ctx context.Context, params any) (pgx.Row, error)
	Exec(ctx context.Context, params any) (pgconn.CommandTag, error)
}

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 QuerierTxT[T PGXQuerier] interface {
	QuerierT[T]
	Begin(ctx context.Context) (*TxT[pgx.Tx], error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

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.

func (*Stmt[T]) Exec

func (d *Stmt[T]) Exec(ctx context.Context, params any) (pgconn.CommandTag, error)

func (*Stmt[T]) Handler

func (d *Stmt[T]) Handler() T

func (*Stmt[T]) Query

func (d *Stmt[T]) Query(ctx context.Context, params any) (pgx.Rows, error)

func (*Stmt[T]) QueryRow

func (d *Stmt[T]) QueryRow(ctx context.Context, params any) (pgx.Row, error)

type Tx

type Tx = TxT[pgx.Tx]

Tx wraps a pgx.Tx.

func NewTx

func NewTx(tx pgx.Tx, options ...Option) *Tx

NewTx wraps a pgx.Tx.

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.

func (*TxT[T]) Begin

func (d *TxT[T]) Begin(ctx context.Context) (*TxT[pgx.Tx], error)

func (*TxT[T]) Commit

func (d *TxT[T]) Commit(ctx context.Context) error

func (TxT) Exec added in v0.11.3

func (d TxT) Exec(ctx context.Context, query litsql.Query, params any) (pgconn.CommandTag, error)

func (TxT) Handler added in v0.11.3

func (d TxT) Handler() T

func (TxT) Prepare added in v0.11.3

func (d TxT) Prepare(ctx context.Context, name string, query litsql.Query) (*Stmt[T], error)

func (TxT) Query added in v0.11.3

func (d TxT) Query(ctx context.Context, query litsql.Query, params any) (pgx.Rows, error)

func (TxT) QueryRow added in v0.11.3

func (d TxT) QueryRow(ctx context.Context, query litsql.Query, params any) (pgx.Row, error)

func (*TxT[T]) Rollback

func (d *TxT[T]) Rollback(ctx context.Context) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL