sqb - SQL Query Builder
⚡ Blazing fast, Flexible, SQL Query Builder for Go
Features
- High performance.
- Easy to use.
- Powerful, Flexible. You can define stmt for yourself.
- Supported MySQL, PostgreSQL, Spanner statement.
Synopsis
When used normally
const sqlstr = "SELECT * FROM tables WHERE ?"
builder := sqb.New(sqlstr).Bind(sqb.Eq("category", 1))
query, args, err := builder.Build()
// query => "SELECT * FROM tables WHERE category = ?",
// args => []interface{}{1}
When you want to use build cache
const sqlstr = "SELECT * FROM tables WHERE ? AND ?"
cached := sqb.New(sqlstr).Bind(sqb.Eq("category", 1))
for _, col := range columns {
builder := cached.Bind(sqb.Eq(col, "value"))
query, args, err := builder.Build()
// query => "SELECT * FROM tables WHERE category = ? AND " + col + " = ?",
// args => []interface{}{1, "value"}
}
Error case
const sqlstr = "SELECT * FROM tables WHERE ? OR ?"
builder := sqb.New(sqlstr).Bind(sqb.Eq("category", 1))
query, args, err := builder.Build()
// query => "",
// args => nil
// err => "number of bindVars exceeds replaceable statements"
Install
Use go get
to install this package.
go get -u github.com/Code-Hex/sqb
Performance
sqb is the fastest and least-memory used among currently known SQL Query builder in the benchmark. The data of chart using simple benchmark.