pgxhelper

package module
v0.0.0-...-65801d8 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2016 License: Apache-2.0 Imports: 3 Imported by: 0

README

pgxhelper

Build Status GoDoc

Helper for github.com/jackc/pgx (PostgreSQL) package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustPrepare

func MustPrepare(p PgxPreparer, sql string) (stmtName string)

MustPrepare is like pgxConn[Pool].Prepare but panics if the SQL cannot be parsed. It simplifies safe initialization of global variables holding prepared statements. It also assign name to prepared statement (currently name is "stmt<number>").

func MustPrepareAll

func MustPrepareAll(p PgxPreparer, sqls ...string) (stmtNames []string)

MustPrepareAll is like MustPrepare but accept multiple sql to prepare. It return slice of statements names.

func MustPrepareAllInPlace

func MustPrepareAllInPlace(p PgxPreparer, stmts ...*string)

MustPrepareAllInPlace is modification of MustPrepareAll with resulting statements names stored in original strings with sql.

func MustPrepareInPlace

func MustPrepareInPlace(p PgxPreparer, stmt *string)

MustPrepareInPlace is modification of MustPrepare with resulting statement name stored in original string with sql.

func ScanAll

func ScanAll(conn PgxQueryer, sql string, dst sqlhelper.MultiScannable, args ...interface{}) error

ScanAll is adaptation of "github.com/apaxa-io/databasehelper/sqlhelper" StmtScanAll for "github.com/jackc/pgx". ScanAll performs query sql on connection conn with arguments 'args' and stores all result rows in dst. sql passed as-is to conn.Query so it is possible to pass prepared statement name as sql. ScanAll stop working on first error. Example:

 type Label struct {
 	Id       int32
 	Name     string
 }

 func (l *Label) SqlScanInterface() []interface{} {
 	return []interface{}{
 		&l.Id,
 		&l.Name,
 	}
 }

 type Labels []*Label

 func (l *Labels) SqlNewElement() sqlhelper.SingleScannable {
	e := &Label{}
	*l = append(*l, e)
	return e
 }
 ...
 var labels Labels
 if err := pgxhelper.ScanAll(conn, "SELECT id, name FROM LABELS where amount>$1", &labels, someAmount); err != nil {
 	return err
 }

Types

type PgxPreparer

type PgxPreparer interface {
	Prepare(name, sql string) (*pgx.PreparedStatement, error)
}

PgxPreparer interface can hold any object that can prepare SQL statements. Currently (and is primary used for) it can hold pgx.Conn & pgx.ConnPool.

type PgxQueryer

type PgxQueryer interface {
	Query(sql string, args ...interface{}) (*pgx.Rows, error)
}

PgxQueryer interface can hold any object that can query SQL statements. Currently (and is primary used for) it can hold pgx.Conn & pgx.ConnPool.

Jump to

Keyboard shortcuts

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