sqlpp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: MIT Imports: 7 Imported by: 0

README

sqlpp GoDoc Go Report Card Coverage

sqlpp is a sql(MySQL and PostgreSQL) database connection wrapper to cache prepared statements by transforming queries ("...in (?)...", []) to use with array arguments.

Query Transformation

Given query:

select * from bar where b = ? or a in (?) or b = ? or b in (?)

With args:

db.Args(1, []int{2,3}, 4, []string{"5", "6", "7"})

Will transform to:

MySQL => select * from bar where b = ? or a in (?,?) or b = ? or b in (?,?,?)
PostgreSQL => select * from bar where b = $1 or a in ($2,$3) or b = $4 or b in ($5,$6,$7)

With args:

[]interface{}{1, 2, 3, 4, "5", "6", "7"}

Usage

/* conn, _ := sql.Open("mysql", "username:password@tcp(host:port)/database")
db := sqlpp.NewMySQL(conn) */
conn, _ := sql.Open("postgres", "postgres://username:password@host:port/database?sslmode=disable")
db := sqlpp.NewPostgreSQL(conn)

defer db.Close()

err = db.Ping()
if err != nil {
    panic(err)
}

r, _ := db.Query("select * from foo", nil, func(r *sql.Rows) (interface{}, error) {
    var a int
    err := r.Scan(&a)
    return a, err
})

fmt.Println(r)
// output: [1,2,3,4]

r, _ = db.Query("select * from foo where id = ?", db.Args(1), func(r *sql.Rows) (interface{}, error) {
    var a int
    err := r.Scan(&a)
    return a, err
})

fmt.Println(r)
// output: [1]


r, _ = db.Query("select * from foo where id in (?)", db.Args([]int{2,3}), func(r *sql.Rows) (interface{}, error) {
    var a int
    err := r.Scan(&a)
    return a, err
})

fmt.Println(r)
// output: [2,3]

License

The MIT License (MIT). See License File for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilRows    = errors.New("sqlpp: nil rows")
	ErrNilScanner = errors.New("sqlpp: nil scanner")
)

Functions

This section is empty.

Types

type DB

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

func NewMySQL

func NewMySQL(db *sql.DB) *DB

func NewPostgreSQL

func NewPostgreSQL(db *sql.DB) *DB

func (*DB) Args

func (sqlpp *DB) Args(args ...interface{}) []interface{}

func (*DB) Close

func (sqlpp *DB) Close() error

func (*DB) Exec

func (sqlpp *DB) Exec(query string, args ...interface{}) (sql.Result, error)

func (*DB) ExecContext

func (sqlpp *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DB) Query

func (sqlpp *DB) Query(query string, args []interface{}, scan Scanner) ([]interface{}, error)

func (*DB) QueryContext

func (sqlpp *DB) QueryContext(ctx context.Context, query string, args []interface{}, scan Scanner) ([]interface{}, error)

func (*DB) QueryRow

func (sqlpp *DB) QueryRow(query string, args []interface{}, dest ...interface{}) error

func (*DB) QueryRowContext

func (sqlpp *DB) QueryRowContext(ctx context.Context, query string, args []interface{}, dest ...interface{}) error

type Scanner

type Scanner func(*sql.Rows) (interface{}, error)

Jump to

Keyboard shortcuts

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