Skip to content
Fluent SQL generation for golang
Go
Branch: master
Clone or download

Latest commit

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
integration Move integration tests to their own module Nov 19, 2019
.gitignore Add .gitignore Jan 28, 2014
.travis.yml Move integration tests to their own module Nov 19, 2019
LICENSE.txt Convert empty array/slice in expr to "IN (NULL)" rather than erroring Feb 26, 2016
README.md Update README.md Feb 5, 2020
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 add PrefixExpr and SuffixExpr methods to all builders Dec 6, 2019
delete_ctx.go Add forgotten ctx methods for DeleteBuilder (#246) Apr 27, 2020
delete_ctx_test.go Add forgotten ctx methods for DeleteBuilder (#246) Apr 27, 2020
delete_test.go Added query to delete builder Nov 28, 2018
expr.go Update documentation and examples for some SelectBuilder methods and … Apr 27, 2020
expr_test.go Update documentation and examples for some SelectBuilder methods and … Apr 27, 2020
go.mod Add go-module support Dec 19, 2018
go.sum Add go-module support Dec 19, 2018
insert.go Expand Sqlizer arguments in Expr Dec 6, 2019
insert_ctx.go Update Context support Aug 11, 2017
insert_ctx_test.go Go fmt Aug 23, 2017
insert_test.go Add Replace statement builder Oct 3, 2019
part.go Made part support Sqlizer, not only string Jan 20, 2015
placeholder.go MS SQL Server compatible placeholders (#244) Apr 12, 2020
placeholder_test.go MS SQL Server compatible placeholders (#244) Apr 12, 2020
row.go Prepare for release Jan 28, 2014
row_test.go Use assert in tests Feb 9, 2014
select.go Update documentation and examples for some SelectBuilder methods and … Apr 27, 2020
select_ctx.go Update Context support Aug 11, 2017
select_ctx_test.go Go fmt Aug 23, 2017
select_test.go Fix ExampleSelectBuilder_FromSelect output May 9, 2020
squirrel.go prioritize db with context over non-context db Dec 6, 2019
squirrel_ctx.go add methods to wrap standard sql runners for use with QueryRowWith me… Dec 6, 2019
squirrel_ctx_test.go Go fmt Aug 23, 2017
squirrel_test.go MS SQL Server compatible placeholders (#244) Apr 12, 2020
statement.go Add StatementBuilder.Where method May 9, 2020
statement_test.go Add StatementBuilder.Where method May 9, 2020
stmtcacher.go Revert rename of NewStmtCacherProxy to NewStmtCacheProxy Feb 26, 2019
stmtcacher_ctx.go Rename StmtCacher to StmtCache and deprecate NewStmtCacher Feb 26, 2019
stmtcacher_ctx_test.go Rename StmtCacher to StmtCache and deprecate NewStmtCacher Feb 26, 2019
stmtcacher_noctx.go Rename StmtCacher to StmtCache and deprecate NewStmtCacher Feb 26, 2019
stmtcacher_test.go Rename StmtCacher to StmtCache and deprecate NewStmtCacher Feb 26, 2019
update.go Expand Sqlizer arguments in Expr Dec 6, 2019
update_ctx.go Update Context support Aug 11, 2017
update_ctx_test.go Go fmt Aug 23, 2017
update_test.go feat: update case support Jul 30, 2019
where.go fix: nested select placeholders numbering Jun 12, 2018
where_test.go Convert empty array/slice in expr to "IN (NULL)" rather than erroring Feb 26, 2016

README.md

Project Status: Inactive – The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows.

Squirrel is "complete".

Bug fixes will still be merged (slowly). Bug reports are welcome, but I will not necessarily respond to them. If another fork (or substantially similar project) actively improves on what Squirrel does, let me know and I may link to it here.

Squirrel - fluent SQL generator for Go

import "github.com/Masterminds/squirrel"

GoDoc Build Status

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").ToSql()

/// ...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 marks 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]

FAQ

  • How can I build an IN query on composite keys / tuples, e.g. WHERE (col1, col2) IN ((1,2),(3,4))? (#104)

    Squirrel does not explicitly support tuples, but you can get the same effect with e.g.:

    sq.Or{
      sq.Eq{"col1": 1, "col2": 2},
      sq.Eq{"col1": 3, "col2": 4}}
    WHERE (col1 = 1 AND col2 = 2) OR (col1 = 3 AND col2 = 4)

    (which should produce the same query plan as the tuple version)

  • Why doesn't Eq{"mynumber": []uint8{1,2,3}} turn into an IN query? (#114)

    Values of type []byte are handled specially by database/sql. In Go, byte is just an alias of uint8, so there is no way to distinguish []uint8 from []byte.

  • Some features are poorly documented!

    This isn't a frequent complaints section!

  • Some features are poorly documented?

    Yes. The tests should be considered a part of the documentation; take a look at those for ideas on how to express more complex queries.

License

Squirrel is released under the MIT License.

You can’t perform that action at this time.