-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_mapper_test.go
174 lines (131 loc) · 3.36 KB
/
example_mapper_test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package sqluct_test
import (
"fmt"
"time"
"github.com/Masterminds/squirrel"
"github.com/bool64/sqluct"
)
func ExampleMapper_Col() {
sm := sqluct.Mapper{}
type Order struct {
ID int `db:"order_id,omitempty"`
CreatedAt time.Time `db:"created_at,omitempty"`
}
o := Order{
ID: 123,
}
q := sm.
Select(squirrel.Select(), o).
From("orders").
Where(squirrel.Eq{
sm.Col(&o, &o.ID): o.ID, // Col returns "order_id" defined in field tag.
})
query, args, err := q.ToSql()
fmt.Println(query, args, err)
// Output: SELECT order_id, created_at FROM orders WHERE order_id = ? [123] <nil>
}
func ExampleMapper_Insert() {
sm := sqluct.Mapper{}
type Order struct {
ID int `db:"order_id,omitempty"`
Amount int `db:"amount"`
UserID int `db:"user_id"`
}
o := Order{}
o.Amount = 100
o.UserID = 123
q := sm.Insert(squirrel.Insert("orders"), o)
query, args, err := q.ToSql()
fmt.Println(query, args, err)
// Output: INSERT INTO orders (amount,user_id) VALUES (?,?) [100 123] <nil>
}
func ExampleMapper_Update() {
sm := sqluct.Mapper{}
type OrderData struct {
Amount int `db:"amount"`
UserID int `db:"user_id,omitempty"`
}
type Order struct {
ID int `db:"order_id,omitempty"`
OrderData
}
o := Order{}
o.ID = 321
o.Amount = 100
o.UserID = 123
q := sm.
Update(squirrel.Update("orders"), o.OrderData).
Where(squirrel.Eq{sm.Col(&o, &o.ID): o.ID})
query, args, err := q.ToSql()
fmt.Println(query, args, err)
// Output: UPDATE orders SET amount = ?, user_id = ? WHERE order_id = ? [100 123 321] <nil>
}
func ExampleMapper_Select() {
sm := sqluct.Mapper{}
type OrderData struct {
Amount int `db:"amount"`
UserID int `db:"user_id,omitempty"`
}
type Order struct {
ID int `db:"order_id,omitempty"`
OrderData
}
o := Order{}
o.ID = 321
q := sm.
Select(squirrel.Select(), o).
Where(squirrel.Eq{sm.Col(&o, &o.ID): o.ID})
query, args, err := q.ToSql()
fmt.Println(query, args, err)
// Output: SELECT order_id, amount, user_id WHERE order_id = ? [321] <nil>
}
func ExampleMapper_WhereEq() {
sm := sqluct.Mapper{}
type OrderData struct {
Amount int `db:"amount"`
UserID int `db:"user_id,omitempty"`
}
type Order struct {
ID int `db:"order_id"`
OrderData
}
o := Order{}
o.Amount = 100
o.UserID = 123
q := sm.
Select(squirrel.Select().From("orders"), o).
Where(sm.WhereEq(o.OrderData))
query, args, err := q.ToSql()
fmt.Println(query, args, err)
// Output: SELECT order_id, amount, user_id FROM orders WHERE amount = ? AND user_id = ? [100 123] <nil>
}
func ExampleMapper_WhereEq_columnsOf() {
sm := sqluct.Mapper{}
type OrderData struct {
Amount int `db:"amount"`
UserID int `db:"user_id,omitempty"`
}
type Order struct {
ID int `db:"id"`
OrderData
}
type User struct {
ID int `db:"id"`
Name string `db:"name"`
}
rf := sqluct.Referencer{}
o := &Order{}
u := &User{}
rf.AddTableAlias(o, "orders")
rf.AddTableAlias(u, "users")
q := sm.
Select(squirrel.Select().From(rf.Ref(o)), o, rf.ColumnsOf(o)).
Join(rf.Fmt("%s ON %s = %s", u, &o.UserID, &u.ID)).
Where(sm.WhereEq(OrderData{
Amount: 100,
UserID: 123,
}, rf.ColumnsOf(o)))
query, args, err := q.ToSql()
fmt.Println(query, args, err)
// Output: SELECT orders.id, orders.amount, orders.user_id FROM orders JOIN users ON orders.user_id = users.id WHERE orders.amount = ? AND orders.user_id = ? [100 123] <nil>
}