-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
62 lines (49 loc) · 1.08 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"fmt"
"github.com/jacobbrewer1/patcher"
)
type Person struct {
ID *int `db:"id"`
Name *string `db:"name"`
}
type PersonWhere struct {
ID *int `db:"id"`
}
func NewPersonWhere(id int) *PersonWhere {
return &PersonWhere{
ID: &id,
}
}
func (p *PersonWhere) Where() (string, []any) {
return "id = ?", []any{*p.ID}
}
func (p *PersonWhere) WhereType() patcher.WhereType {
// Please do not use this in production code. This is just an example.
if *p.ID == 1 {
return patcher.WhereTypeOr
}
return patcher.WhereTypeAnd
}
func main() {
const jsonStr = `{"name": "john"}`
person := new(Person)
if err := json.Unmarshal([]byte(jsonStr), person); err != nil {
panic(err)
}
condition := NewPersonWhere(0)
conditionOr := NewPersonWhere(1)
sqlStr, args, err := patcher.GenerateSQL(
person,
patcher.WithTable("people"),
patcher.WithWhere(condition),
patcher.WithWhere(conditionOr),
)
if err != nil {
panic(err)
}
// The where clause here should be "id = ?" and the where type should be "OR"
fmt.Println(sqlStr)
fmt.Println(args)
}