-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect.go
50 lines (41 loc) · 904 Bytes
/
select.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"log"
"github.com/rembosk8/query-builder-go/builder/pg"
)
func main() {
fmt.Println("Select example:")
sel()
fmt.Println("Update example:")
update()
fmt.Println("Delete example:")
del()
fmt.Println("Insert example:")
insert()
}
func sel() {
qb := pg.NewQueryBuilder()
// generate select query
prepQuery := qb.Select("id", "name", "year").
From("example_table").
Where("year").Between(1990, 2023).
Where("name").Equal("Max").
Limit(100).
Offset(200).
OrderBy("year").Desc()
sql, err := prepQuery.ToSQL()
if err != nil {
log.Fatal(err)
}
fmt.Println("Generated SQL plain request: ")
fmt.Println(sql)
sql, args, err := prepQuery.ToSQLWithStmts()
if err != nil {
log.Fatal(err)
}
fmt.Println("Generated SQL with placeholders request: ")
fmt.Println(sql)
fmt.Println("List of arguments for placeholders: ")
fmt.Println(args...)
}