lpgx

package module
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 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 BaseQuerier

type BaseQuerier[T PGXQuerier] struct {
	// contains filtered or unexported fields
}

func NewBaseQuerier

func NewBaseQuerier[T PGXQuerier](querier T, options ...Option) *BaseQuerier[T]

func (*BaseQuerier[T]) Exec

func (d *BaseQuerier[T]) Exec(ctx context.Context, query litsql.Query, params any) (pgconn.CommandTag, error)

func (*BaseQuerier[T]) Handler

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

func (*BaseQuerier[T]) Prepare

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

func (*BaseQuerier[T]) Query

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

func (*BaseQuerier[T]) QueryRow

func (d *BaseQuerier[T]) QueryRow(ctx context.Context, query litsql.Query, params any) (pgx.Row, error)

type DB

type DB = DBT[*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

type DBT

type DBT[T PGXQuerierDB] struct {
	*BaseQuerier[T]
}

func NewDBT

func NewDBT[T PGXQuerierDB](querier T, options ...Option) *DBT[T]

func (*DBT[T]) BeginTx

func (d *DBT[T]) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (*TxT[pgx.Tx], 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, pgx.Tx]

type QuerierDBT

type QuerierDBT[ST PGXQuerier, TT PGXQuerierTx] interface {
	QuerierT[ST]
	BeginTx(ctx context.Context, opts pgx.TxOptions) (*TxT[TT], 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[ST 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[ST], error)
}

type QuerierTx

type QuerierTx = QuerierTxT[pgx.Tx]

type QuerierTxT

type QuerierTxT[ST PGXQuerierTx] interface {
	QuerierT[ST]
	Begin(ctx context.Context) (*TxT[ST], error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type Stmt

type Stmt[T PGXQuerier] struct {
	// contains filtered or unexported fields
}
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]

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() PGXQuerier

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]

func NewTx

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

type TxT

type TxT[T PGXQuerierTx] struct {
	*BaseQuerier[T]
}

func NewTxT

func NewTxT[T PGXQuerierTx](querier T, options ...Option) *TxT[T]

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[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