__ _ ___ __ _ _ _
/ _` |/ _ \ / _` | | | |
| (_| | (_) | (_| | |_| |
\__, |\___/ \__, |\__,_|
|___/ |_|
goqu
is an expressive SQL builder and executor
If you are upgrading from an older version please read the Migrating Between Versions docs.
If using go modules.
go get -u github.com/doug-martin/goqu/v9
If you are not using go modules...
NOTE You should still be able to use this package if you are using go version >v1.10
but, you will need to drop the version from the package. import "github.com/doug-martin/goqu/v9
-> import "github.com/doug-martin/goqu"
go get -u github.com/doug-martin/goqu
goqu
comes with many features but here are a few of the more notable ones
- Query Builder
- Parameter interpolation (e.g
SELECT * FROM "items" WHERE "id" = ?
->SELECT * FROM "items" WHERE "id" = 1
) - Built from the ground up with multiple dialects in mind
- Insert, Multi Insert, Update, and Delete support
- Scanning of rows to struct[s] or primitive value[s]
While goqu may support the scanning of rows into structs it is not intended to be used as an ORM if you are looking for common ORM features like associations, or hooks I would recommend looking at some of the great ORM libraries such as:
We tried a few other sql builders but each was a thin wrapper around sql fragments that we found error prone. goqu
was built with the following goals in mind:
- Make the generation of SQL easy and enjoyable
- Create an expressive DSL that would find common errors with SQL at compile time.
- Provide a DSL that accounts for the common SQL expressions, NOT every nuance for each database.
- Provide developers the ability to:
- Use SQL when desired
- Easily scan results into primitive values and structs
- Use the native sql.Db methods when desired
- Dialect - Introduction to different dialects (
mysql
,postgres
,sqlite3
,sqlserver
etc) - Expressions - Introduction to
goqu
expressions and common examples. - Select Dataset - Docs and examples about creating and executing SELECT sql statements.
- Insert Dataset - Docs and examples about creating and executing INSERT sql statements.
- Update Dataset - Docs and examples about creating and executing UPDATE sql statements.
- Delete Dataset - Docs and examples about creating and executing DELETE sql statements.
- Prepared Statements - Docs about interpolation and prepared statements in
goqu
. - Database - Docs and examples of using a Database to execute queries in
goqu
- Working with time.Time - Docs on how to use alternate time locations.
See the select dataset docs for more in depth examples
sql, _, _ := goqu.From("test").ToSQL()
fmt.Println(sql)
Output:
SELECT * FROM "test"
sql, _, _ := goqu.From("test").Where(goqu.Ex{
"d": []string{"a", "b", "c"},
}).ToSQL()
fmt.Println(sql)
Output:
SELECT * FROM "test" WHERE ("d" IN ('a', 'b', 'c'))
See the insert dataset docs for more in depth examples
ds := goqu.Insert("user").
Cols("first_name", "last_name").
Vals(
goqu.Vals{"Greg", "Farley"},
goqu.Vals{"Jimmy", "Stewart"},
goqu.Vals{"Jeff", "Jeffers"},
)
insertSQL, args, _ := ds.ToSQL()
fmt.Println(insertSQL, args)
Output:
INSERT INTO "user" ("first_name", "last_name") VALUES ('Greg', 'Farley'), ('Jimmy', 'Stewart'), ('Jeff', 'Jeffers') []
ds := goqu.Insert("user").Rows(
goqu.Record{"first_name": "Greg", "last_name": "Farley"},
goqu.Record{"first_name": "Jimmy", "last_name": "Stewart"},
goqu.Record{"first_name": "Jeff", "last_name": "Jeffers"},
)
insertSQL, args, _ := ds.ToSQL()
fmt.Println(insertSQL, args)
Output:
INSERT INTO "user" ("first_name", "last_name") VALUES ('Greg', 'Farley'), ('Jimmy', 'Stewart'), ('Jeff', 'Jeffers') []
type User struct {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
}
ds := goqu.Insert("user").Rows(
User{FirstName: "Greg", LastName: "Farley"},
User{FirstName: "Jimmy", LastName: "Stewart"},
User{FirstName: "Jeff", LastName: "Jeffers"},
)
insertSQL, args, _ := ds.ToSQL()
fmt.Println(insertSQL, args)
Output:
INSERT INTO "user" ("first_name", "last_name") VALUES ('Greg', 'Farley'), ('Jimmy', 'Stewart'), ('Jeff', 'Jeffers') []
ds := goqu.Insert("user").Prepared(true).
FromQuery(goqu.From("other_table"))
insertSQL, args, _ := ds.ToSQL()
fmt.Println(insertSQL, args)
Output:
INSERT INTO "user" SELECT * FROM "other_table" []
ds := goqu.Insert("user").Prepared(true).
Cols("first_name", "last_name").
FromQuery(goqu.From("other_table").Select("fn", "ln"))
insertSQL, args, _ := ds.ToSQL()
fmt.Println(insertSQL, args)
Output:
INSERT INTO "user" ("first_name", "last_name") SELECT "fn", "ln" FROM "other_table" []
See the update dataset docs for more in depth examples