• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..19-Nov-2020-

.travis.ymlH A D19-Nov-2020933 3527

README.mdH A D19-Nov-20204.3 KiB14799

case.goH A D19-Nov-20202.8 KiB11980

delete.goH A D19-Nov-20204.1 KiB165112

delete_ctx.goH A D19-Nov-2020599 2820

expr.goH A D19-Nov-20207.3 KiB352261

go.modH A D19-Nov-2020312 108

go.sumH A D19-Nov-2020953 1110

insert.goH A D19-Nov-20206.6 KiB259185

insert_ctx.goH A D19-Nov-20201.9 KiB7054

part.goH A D19-Nov-2020983 5647

placeholder.goH A D19-Nov-20201.9 KiB8557

row.goH A D19-Nov-2020472 2314

select.goH A D19-Nov-20209.4 KiB349233

select_ctx.goH A D19-Nov-20201.9 KiB7054

squirrel.goH A D19-Nov-20204.6 KiB173116

squirrel_ctx.goH A D19-Nov-20202 KiB6237

statement.goH A D19-Nov-20202.4 KiB8445

stmtcacher.goH A D19-Nov-20201.7 KiB8667

stmtcacher_ctx.goH A D19-Nov-20201.9 KiB7556

stmtcacher_noctx.goH A D19-Nov-2020321 157

update.goH A D19-Nov-20205.7 KiB233171

update_ctx.goH A D19-Nov-20201.9 KiB7054

where.goH A D19-Nov-2020572 3125

README.md

1# Squirrel - fluent SQL generator for Go
2
3```go
4import "gopkg.in/Masterminds/squirrel.v1"
5```
6or if you prefer using `master` (which may be arbitrarily ahead of or behind `v1`):
7
8**NOTE:** as of Go 1.6, `go get` correctly clones the Github default branch (which is `v1` in this repo).
9```go
10import "github.com/Masterminds/squirrel"
11```
12
13[![GoDoc](https://godoc.org/github.com/Masterminds/squirrel?status.png)](https://godoc.org/github.com/Masterminds/squirrel)
14[![Build Status](https://travis-ci.org/Masterminds/squirrel.svg?branch=v1)](https://travis-ci.org/Masterminds/squirrel)
15
16_**Note:** This project has moved from `github.com/lann/squirrel` to
17`github.com/Masterminds/squirrel`. Lann remains the architect of the
18project, but we're helping him curate.
19
20**Squirrel is not an ORM.** For an application of Squirrel, check out
21[structable, a table-struct mapper](https://github.com/Masterminds/structable)
22
23
24Squirrel helps you build SQL queries from composable parts:
25
26```go
27import sq "github.com/Masterminds/squirrel"
28
29users := sq.Select("*").From("users").Join("emails USING (email_id)")
30
31active := users.Where(sq.Eq{"deleted_at": nil})
32
33sql, args, err := active.ToSql()
34
35sql == "SELECT * FROM users JOIN emails USING (email_id) WHERE deleted_at IS NULL"
36```
37
38```go
39sql, args, err := sq.
40    Insert("users").Columns("name", "age").
41    Values("moe", 13).Values("larry", sq.Expr("? + 5", 12)).
42    ToSql()
43
44sql == "INSERT INTO users (name,age) VALUES (?,?),(?,? + 5)"
45```
46
47Squirrel can also execute queries directly:
48
49```go
50stooges := users.Where(sq.Eq{"username": []string{"moe", "larry", "curly", "shemp"}})
51three_stooges := stooges.Limit(3)
52rows, err := three_stooges.RunWith(db).Query()
53
54// Behaves like:
55rows, err := db.Query("SELECT * FROM users WHERE username IN (?,?,?,?) LIMIT 3",
56                      "moe", "larry", "curly", "shemp")
57```
58
59Squirrel makes conditional query building a breeze:
60
61```go
62if len(q) > 0 {
63    users = users.Where("name LIKE ?", fmt.Sprint("%", q, "%"))
64}
65```
66
67Squirrel wants to make your life easier:
68
69```go
70// StmtCache caches Prepared Stmts for you
71dbCache := sq.NewStmtCacher(db)
72
73// StatementBuilder keeps your syntax neat
74mydb := sq.StatementBuilder.RunWith(dbCache)
75select_users := mydb.Select("*").From("users")
76```
77
78Squirrel loves PostgreSQL:
79
80```go
81psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
82
83// You use question marks for placeholders...
84sql, _, _ := psql.Select("*").From("elephants").Where("name IN (?,?)", "Dumbo", "Verna").ToSql()
85
86/// ...squirrel replaces them using PlaceholderFormat.
87sql == "SELECT * FROM elephants WHERE name IN ($1,$2)"
88
89
90/// You can retrieve id ...
91query := sq.Insert("nodes").
92    Columns("uuid", "type", "data").
93    Values(node.Uuid, node.Type, node.Data).
94    Suffix("RETURNING \"id\"").
95    RunWith(m.db).
96    PlaceholderFormat(sq.Dollar)
97
98query.QueryRow().Scan(&node.id)
99```
100
101You can escape question mask by inserting two question marks:
102
103```sql
104SELECT * FROM nodes WHERE meta->'format' ??| array[?,?]
105```
106
107will generate with the Dollar Placeholder:
108
109```sql
110SELECT * FROM nodes WHERE meta->'format' ?| array[$1,$2]
111```
112
113## FAQ
114
115* **How can I build an IN query on composite keys / tuples, e.g. `WHERE (col1, col2) IN ((1,2),(3,4))`? ([#104](https://github.com/Masterminds/squirrel/issues/104))**
116
117    Squirrel does not explicitly support tuples, but you can get the same effect with e.g.:
118
119    ```go
120    sq.Or{
121      sq.Eq{"col1": 1, "col2": 2},
122      sq.Eq{"col1": 3, "col2": 4}}
123    ```
124
125    ```sql
126    WHERE (col1 = 1 AND col2 = 2) OR (col1 = 3 AND col2 = 4)
127    ```
128
129    (which should produce the same query plan as the tuple version)
130
131* **Why doesn't `Eq{"mynumber": []uint8{1,2,3}}` turn into an `IN` query? ([#114](https://github.com/Masterminds/squirrel/issues/114))**
132
133    Values of type `[]byte` are handled specially by `database/sql`. In Go, [`byte` is just an alias of `uint8`](https://golang.org/pkg/builtin/#byte), so there is no way to distinguish `[]uint8` from `[]byte`.
134
135* **Some features are poorly documented!**
136
137    This isn't a frequent complaints section!
138
139* **Some features are poorly documented?**
140
141    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.
142
143## License
144
145Squirrel is released under the
146[MIT License](http://www.opensource.org/licenses/MIT).
147