...
1# Prepared Statements
2
3By default the `goqu` will interpolate all parameters, if you do not want to have values interpolated you can use the [`Prepared`](http://godoc.org/github.com/doug-martin/goqu#SelectDataset.Prepared) method to prevent this.
4
5**Note** For the examples all placeholders are `?` this will be dialect specific when using other examples (e.g. Postgres `$1, $2...`)
6
7```go
8
9preparedDs := db.From("items").Prepared(true)
10
11sql, args, _ := preparedDs.Where(goqu.Ex{
12 "col1": "a",
13 "col2": 1,
14 "col3": true,
15 "col4": false,
16 "col5": []string{"a", "b", "c"},
17}).ToSQL()
18fmt.Println(sql, args)
19
20sql, args, _ = preparedDs.Insert().Rows(
21 goqu.Record{"name": "Test1", "address": "111 Test Addr"},
22 goqu.Record{"name": "Test2", "address": "112 Test Addr"},
23).ToSQL()
24fmt.Println(sql, args)
25
26sql, args, _ = preparedDs.Update().Set(
27 goqu.Record{"name": "Test", "address": "111 Test Addr"},
28).ToSQL()
29fmt.Println(sql, args)
30
31sql, args, _ = preparedDs.
32 Delete().
33 Where(goqu.Ex{"id": goqu.Op{"gt": 10}}).
34 ToSQL()
35fmt.Println(sql, args)
36
37// Output:
38// SELECT * FROM "items" WHERE (("col1" = ?) AND ("col2" = ?) AND ("col3" IS TRUE) AND ("col4" IS FALSE) AND ("col5" IN (?, ?, ?))) [a 1 a b c]
39// INSERT INTO "items" ("address", "name") VALUES (?, ?), (?, ?) [111 Test Addr Test1 112 Test Addr Test2]
40// UPDATE "items" SET "address"=?,"name"=? [111 Test Addr Test]
41// DELETE FROM "items" WHERE ("id" > ?) [10]
42```
43
44When setting prepared to true executing the SQL using the different querying methods will also use the non-interpolated SQL also.
45
46```go
47var items []Item
48sql, args, _ := db.From("items").Prepared(true).Where(goqu.Ex{
49 "col1": "a",
50 "col2": 1,
51}).ScanStructs(&items)
52
53//Is the same as
54db.ScanStructs(&items, `SELECT * FROM "items" WHERE (("col1" = ?) AND ("col2" = ?))`, "a", 1)
55```
56
View as plain text