Documentation ¶
Overview ¶
Package pig – simple pgx wrapper to execute and scan query results.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn interface { BeginFunc(context.Context, func(pgx.Tx) error) error Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) Query(context.Context, string, ...interface{}) (pgx.Rows, error) }
Conn connection interface.
type Ex ¶
type Ex struct {
// contains filtered or unexported fields
}
Ex to execute queries.
type Option ¶
type Option func(*Options)
Option func.
func StatementTimeout ¶
StatementTimeout sets transaction statement timeout (ignored with queries).
func TransactionTimeout ¶
TransactionTimeout sets transaction timeout (ignored with queries).
type Pig ¶
type Pig struct {
// contains filtered or unexported fields
}
Pig pgx wrapper.
func (*Pig) Query ¶
Query returns new query executor.
Example ¶
conn, err := pgx.Connect(context.Background(), "") if err != nil { log.Fatalln(err) } p := pig.New(conn) // Execute query affectedRows, err := p.Query().Exec("DELETE FROM things WHERE id = $1", 123) if err != nil { log.Fatalln(err) } log.Println("affected", affectedRows, "rows") // Get single record from database var cnt int64 err = p.Query().Get(&cnt, "SELECT count(*) FROM things") if err != nil { log.Fatalln(err) } type Thing struct { ID int64 `db:"id"` Name string `db:"name"` Quantity int64 `db:"quantity"` } // Select multiple records var things []Thing err = p.Query().Select(&things, "SELECT * FROM things") if err != nil { log.Fatalln(err) } log.Println(things)
Output:
func (*Pig) Tx ¶
Tx returns new transaction.
Example ¶
conn, err := pgx.Connect(context.Background(), "") if err != nil { log.Fatalln(err) } p := pig.New(conn) var affectedRows int64 err = p.Tx().Exec(func(ex *pig.Ex) error { affectedRows, err = p.Query().Exec("DELETE FROM things WHERE id = $1", 123) if err != nil { return err } return nil }) if err != nil { log.Fatalln(err) } log.Println("affected", affectedRows, "rows")
Output:
Click to show internal directories.
Click to hide internal directories.