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:pass[email protected](127.0.0.1:3306)/foo")
db := sqlpp.NewMysql(conn) */
conn, _ := sql.Open("postgres", "postgres://username:[email protected]:5432/foo?sslmode=disable")
db := sqlpp.NewPostgres(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.

GitHub

https://github.com/nzmprlr/sqlpp