Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fluent SQL generation for golang
Go
branch: v1

Merge pull request #37 from elgris/select_case

Support of CASE operator in SELECT query
latest commit e13dbacee4
@lann authored
Failed to load latest commit information.
.gitignore Add .gitignore
.travis.yml Add 1.3 to travis config; fix order dependence in test
README.md Merge pull request #28 from mjangda/bug/docs-Expr
case.go Made What() method of CaseBuilder non-exported to simplify interface
case_test.go 'Case()' uses all arguments
delete.go Allow RunWith values to not implement QueryRower
delete_test.go Add 1.3 to travis config; fix order dependence in test
expr.go Merge pull request #37 from elgris/select_case
expr_test.go Support null types for expresion
insert.go Change code style
insert_test.go Propose InsertBuilder.SetMap() function.
part.go Made part support Sqlizer, not only string
placeholder.go Small optimization for placeholder generator.
placeholder_test.go Small optimization for placeholder generator.
row.go Prepare for release
row_test.go Use assert in tests
select.go Allow RunWith values to not implement QueryRower
select_test.go Added brackets around expression in resulted SQL of Alias function
squirrel.go Select RunWith(*sql.Tx) supports QueryRow
squirrel_test.go Fix TestWithToSqlErr test
statement.go 'Case()' uses all arguments
statement_test.go Select RunWith(*sql.Tx) supports QueryRow
stmtcacher.go make stmtCacher thread-safe
stmtcacher_test.go Use assert in tests
update.go Allow RunWith values to not implement QueryRower
update_test.go Add Prefix and Suffix methods
where.go Add And/Or helpers
where_test.go expr: raise error when 0-len slice passed

README.md

Squirrel - fluent SQL generator for Go

import "github.com/lann/squirrel"

GoDoc Build Status

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", 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(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.

Something went wrong with that request. Please try again.