https://github.com/nzmprlr/sqlpp
A database connection wrapper to cache prepared statements by transforming queries to use with array arguments.
https://github.com/nzmprlr/sqlpp
database go golang mysql postgresql sql wrapper
Last synced: 2 months ago
JSON representation
A database connection wrapper to cache prepared statements by transforming queries to use with array arguments.
- Host: GitHub
- URL: https://github.com/nzmprlr/sqlpp
- Owner: nzmprlr
- License: mit
- Created: 2021-08-12T18:56:16.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-22T20:07:16.000Z (over 4 years ago)
- Last Synced: 2024-06-20T05:29:07.964Z (almost 2 years ago)
- Topics: database, go, golang, mysql, postgresql, sql, wrapper
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlpp [](http://godoc.org/github.com/nzmprlr/sqlpp) [](https://goreportcard.com/report/github.com/nzmprlr/sqlpp) [](http://gocover.io/github.com/nzmprlr/sqlpp)
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
``` go
/* 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](LICENSE) for more information.