Fluent SQL generation for golang
Go
Latest commit 20f1922 Nov 15, 2016 @lann lann committed on GitHub Merge pull request #76 from Masterminds/debug-sqlizer
Add DebugSqlizer function
Permalink
Failed to load latest commit information.
.gitignore Add .gitignore Jan 28, 2014
.travis.yml Telling Travis CI to explicitly test 1.5. Sep 4, 2015
LICENSE.txt Convert empty array/slice in expr to "IN (NULL)" rather than erroring Feb 26, 2016
README.md fix tiny curate typo in README.md Aug 27, 2016
case.go Made What() method of CaseBuilder non-exported to simplify interface Feb 3, 2015
case_test.go 'Case()' uses all arguments Mar 1, 2015
delete.go Allow RunWith values to not implement QueryRower Aug 29, 2014
delete_test.go Add 1.3 to travis config; fix order dependence in test Jun 26, 2014
expr.go Convert empty array/slice in expr to "IN (NULL)" rather than erroring Feb 26, 2016
expr_test.go Convert empty array/slice in expr to "IN (NULL)" rather than erroring Feb 26, 2016
insert.go Change code style Feb 26, 2015
insert_test.go Propose InsertBuilder.SetMap() function. Feb 25, 2015
part.go Made part support Sqlizer, not only string Jan 20, 2015
placeholder.go Small optimization for placeholder generator. Jan 26, 2015
placeholder_test.go Small optimization for placeholder generator. Jan 26, 2015
row.go Prepare for release Jan 28, 2014
row_test.go Use assert in tests Feb 9, 2014
select.go Support select options for select query builder. Jun 8, 2016
select_test.go Support select options for select query builder. Jun 8, 2016
squirrel.go Add DebugSqlizer function Nov 15, 2016
squirrel_test.go Add DebugSqlizer function Nov 15, 2016
statement.go 'Case()' uses all arguments Mar 1, 2015
statement_test.go Select RunWith(*sql.Tx) supports QueryRow Aug 29, 2014
stmtcacher.go make stmtCacher thread-safe May 8, 2014
stmtcacher_test.go Use assert in tests Feb 9, 2014
update.go Add query api to update. Mar 24, 2016
update_test.go Add Prefix and Suffix methods Jun 18, 2014
where.go Add And/Or helpers Sep 11, 2014
where_test.go Convert empty array/slice in expr to "IN (NULL)" rather than erroring Feb 26, 2016

README.md

Squirrel - fluent SQL generator for Go

import "gopkg.in/Masterminds/squirrel.v1"

or if you prefer using master (which may be arbitrarily ahead of or behind v1):

NOTE: as of Go 1.6, go get correctly clones the Github default branch (which is v1 in this repo).

import "github.com/Masterminds/squirrel"

GoDoc Build Status

_Note: This project has moved from github.com/lann/squirrel to github.com/Masterminds/squirrel. Lann remains the architect of the project, but we're helping him curate.

Squirrel is not an ORM. For an application of Squirrel, check out structable, a table-struct mapper

Squirrel helps you build SQL queries from composable parts:

import sq "github.com/Masterminds/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", sq.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:

if len(q) > 0 {
    users = users.Where("name LIKE ?", fmt.Sprint("%", q, "%"))
}

Squirrel wants to make your life easier:

// StmtCache caches Prepared Stmts for you
dbCache := sq.NewStmtCacher(db)

// StatementBuilder keeps your syntax neat
mydb := sq.StatementBuilder.RunWith(dbCache)
select_users := mydb.Select("*").From("users")

Squirrel loves PostgreSQL:

psql := sq.StatementBuilder.PlaceholderFormat(sq.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)

You can escape question mask by inserting two question marks:

SELECT * FROM nodes WHERE meta->'format' ??| array[?,?]

will generate with the Dollar Placeholder:

SELECT * FROM nodes WHERE meta->'format' ?| array[$1,$2]

License

Squirrel is released under the MIT License.