import"github.com/lann/squirrel"Squirrel is not an ORM.
Squirrel helps you build SQL queries from composable parts:
import sq "github.com/lann/squirrel"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", Expr("? + 5", 12)).
ToSql()
sql=="INSERT INTO users (name,age) VALUES (?,?),(?,? + 5)"Squirrel can also 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")Squirrel makes conditional query building a breeze:
iflen(q) >0 {
users=users.Where("name LIKE ?", fmt.Sprint("%", q, "%"))
}Squirrel wants to make your life easier:
// StmtCache caches Prepared Stmts for youdbCache:=sq.NewStmtCacher(db)
// StatementBuilder keeps your syntax neatmydb:=sq.StatementBuilder.RunWith(dbCache)
select_users:=mydb.Select("*").From("users")Squirrel loves PostgreSQL:
psql:=sq.StatementBuilder.PlaceholderFormat(Dollar)
// You use question marks for placeholders...sql, _, _:=psql.Select("*").From("elephants").Where("name IN (?,?)", "Dumbo", "Verna")
/// ...squirrel replaces them using PlaceholderFormat.sql=="SELECT * FROM elephants WHERE name IN ($1,$2)"/// You can retrieve id ...query:=sq.Insert("nodes").
Columns("uuid", "type", "data").
Values(node.Uuid, node.Type, node.Data).
Suffix("RETURNING \"id\"").
RunWith(m.db).
PlaceholderFormat(sq.Dollar)
query.QueryRow().Scan(&node.id)Squirrel is released under the MIT License.

