Non thread safe fork of squirrel. The same handy fluffy helper, but with extra letters removed :)
import "github.com/elgris/sqrl"
Ask benchmarks about that ;). Squirrel is good, reliable and thread-safe with it's immutable query builder. Although immutability is nice, it's resource consuming and sometimes redundant. As authors of dbr
say: "100% of our application code was written without the need for this".
Although, dbr
's query builder is proven to be much faster than squirrel and even faster than sqrl, it doesn't have all syntax sugar. Especially I miss support of JOINs, subqueries and aliases.
sqrl is not an ORM., it helps you build SQL queries from composable parts. sqrl is non thread safe. SQL builders change their state, so using the same builder in parallel is dangerous.
It's very easy to switch between original squirrel and sqrl, because there is no change in interface:
import sq "github.com/elgris/sqrl" // you can easily use github.com/lann/squirrel here
users := sq.Select("*").From("users").Join("emails USING (email_id)")
active := users.Where(sq.Eq{"deleted_at": nil})
sql, args, err := active.ToSql()
sql == "SELECT * FROM users JOIN emails USING (email_id) WHERE deleted_at IS NULL"
sql, args, err := sq.
Insert("users").Columns("name", "age").
Values("moe", 13).Values("larry", sq.Expr("? + 5", 12)).
ToSql()
sql == "INSERT INTO users (name,age) VALUES (?,?),(?,? + 5)"
Like squirrel, sqrl can execute queries directly:
stooges := users.Where(sq.Eq{"username": []string{"moe", "larry", "curly", "shemp"}})
three_stooges := stooges.Limit(3)
rows, err := three_stooges.RunWith(db).Query()
// Behaves like:
rows, err := db.Query("SELECT * FROM users WHERE username IN (?,?,?,?) LIMIT 3", "moe", "larry", "curly", "shemp")
Build conditional queries with ease:
if len(q) > 0 {
users = users.Where("name LIKE ?", q)
}
Sqrl is released under the MIT License.