lpgx

package module
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 4 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 ExampleConn() {
    ctx := context.Background()

    conn, err := pgx.Connect(ctx, "test")
    if err != nil {
        panic(err)
    }

    // wrap *pgx.Conn instance
    dconn := lpgx.NewConn(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 := dconn.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
    dconn := lpgx.NewConn(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 := dconn.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 Conn added in v0.11.4

type Conn = lpgxt.Conn[*pgx.Conn]

Conn wraps a pgx.Conn.

Example
ctx := context.Background()

conn, err := pgx.Connect(ctx, "test")
if err != nil {
	panic(err)
}

// wrap *pgx.Conn instance
dconn := lpgx.NewConn(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 := dconn.Query(ctx, query, sq.MapArgValues{
	"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 NewConn added in v0.11.4

func NewConn(conn *pgx.Conn, options ...Option) *Conn

NewConn wraps a pgx.Conn.

type Option

type Option = lpgxt.Option

func WithQueryHandler

func WithQueryHandler(queryHandler sq.Handler) Option

type Pool added in v0.11.4

type Pool = lpgxt.Pool[*pgxpool.Pool]

Pool wraps a pgxpool.Pool.

func NewPool added in v0.11.4

func NewPool(conn *pgxpool.Pool, options ...Option) *Pool

NewPool wraps a pgxpool.Pool.

type PoolConn added in v0.11.5

type PoolConn = lpgxt.PoolConn[*pgxpool.Conn]

PoolConn wraps a pgxpool.Conn.

func NewPoolConn added in v0.11.5

func NewPoolConn(conn *pgxpool.Conn, options ...Option) *PoolConn

NewPoolConn wraps a pgxpool.Conn.

type Querier added in v0.11.8

type Querier = lpgxt.Querier

type QuerierConn added in v0.11.4

type QuerierConn = lpgxt.QuerierConn[*pgx.Conn]

type QuerierPool added in v0.11.4

type QuerierPool = lpgxt.QuerierPool

type QuerierPoolConn added in v0.11.5

type QuerierPoolConn = lpgxt.QuerierPoolConn

type QuerierPoolTx added in v0.11.5

type QuerierPoolTx = lpgxt.QuerierTx[*pgxpool.Tx]

type QuerierStmt

type QuerierStmt = lpgxt.QuerierStmt

type QuerierTx

type QuerierTx = lpgxt.QuerierTx[pgx.Tx]

type StmtConn added in v0.11.6

type StmtConn = lpgxt.Stmt[*pgx.Conn]

type StmtPoolTx added in v0.11.6

type StmtPoolTx = lpgxt.Stmt[*pgxpool.Tx]

type StmtTx added in v0.11.6

type StmtTx = lpgxt.Stmt[pgx.Tx]

type Tx

type Tx = lpgxt.Tx[pgx.Tx]

Tx wraps a pgx.Tx.

func NewTx

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

NewTx wraps a pgx.Tx.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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