forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg.go
225 lines (181 loc) · 4.67 KB
/
pg.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package pg
import (
"context"
"io"
"log"
"os"
"strconv"
"github.com/go-pg/pg/internal"
"github.com/go-pg/pg/orm"
"github.com/go-pg/pg/types"
)
// Discard is used with Query and QueryOne to discard rows.
var Discard orm.Discard
type NullTime = types.NullTime
func init() {
SetLogger(log.New(os.Stderr, "pg: ", log.LstdFlags|log.Lshortfile))
}
// Model returns new query for the optional model.
func Model(model ...interface{}) *orm.Query {
return orm.NewQuery(nil, model...)
}
func ModelContext(c context.Context, model ...interface{}) *orm.Query {
return orm.NewQueryContext(c, nil, model...)
}
// Scan returns ColumnScanner that copies the columns in the
// row into the values.
func Scan(values ...interface{}) orm.ColumnScanner {
return orm.Scan(values...)
}
// Q replaces any placeholders found in the query.
func Q(query string, params ...interface{}) types.ValueAppender {
return orm.Q(query, params...)
}
// F quotes a SQL identifier such as a table or column name replacing any
// placeholders found in the field.
func F(field string) types.ValueAppender {
return types.F(field)
}
// In accepts a slice and returns a wrapper that can be used with PostgreSQL
// IN operator:
//
// Where("id IN (?)", pg.In([]int{1, 2, 3, 4}))
//
// produces
//
// WHERE id IN (1, 2, 3, 4)
func In(slice interface{}) types.ValueAppender {
return types.InSlice(slice)
}
// InMulti accepts multiple values and returns a wrapper that can be used
// with PostgreSQL IN operator:
//
// Where("(id1, id2) IN (?)", pg.InMulti([]int{1, 2}, []int{3, 4}))
//
// produces
//
// WHERE (id1, id2) IN ((1, 2), (3, 4))
func InMulti(values ...interface{}) types.ValueAppender {
return types.In(values...)
}
// Array accepts a slice and returns a wrapper for working with PostgreSQL
// array data type.
//
// For struct fields you can use array tag:
//
// Emails []string `sql:",array"`
func Array(v interface{}) *types.Array {
return types.NewArray(v)
}
// Hstore accepts a map and returns a wrapper for working with hstore data type.
// Supported map types are:
// - map[string]string
//
// For struct fields you can use hstore tag:
//
// Attrs map[string]string `sql:",hstore"`
func Hstore(v interface{}) *types.Hstore {
return types.NewHstore(v)
}
func SetLogger(logger *log.Logger) {
internal.Logger = logger
}
//------------------------------------------------------------------------------
type Strings []string
var _ orm.HooklessModel = (*Strings)(nil)
var _ types.ValueAppender = (*Strings)(nil)
func (strings *Strings) Init() error {
if s := *strings; len(s) > 0 {
*strings = s[:0]
}
return nil
}
func (strings *Strings) NewModel() orm.ColumnScanner {
return strings
}
func (Strings) AddModel(_ orm.ColumnScanner) error {
return nil
}
func (strings *Strings) ScanColumn(colIdx int, _ string, rd types.Reader, n int) error {
b := make([]byte, n)
_, err := io.ReadFull(rd, b)
if err != nil {
return err
}
*strings = append(*strings, internal.BytesToString(b))
return nil
}
func (strings Strings) AppendValue(dst []byte, quote int) []byte {
if len(strings) <= 0 {
return dst
}
for _, s := range strings {
dst = types.AppendString(dst, s, 1)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
//------------------------------------------------------------------------------
type Ints []int64
var _ orm.HooklessModel = (*Ints)(nil)
var _ types.ValueAppender = (*Ints)(nil)
func (ints *Ints) Init() error {
if s := *ints; len(s) > 0 {
*ints = s[:0]
}
return nil
}
func (ints *Ints) NewModel() orm.ColumnScanner {
return ints
}
func (Ints) AddModel(_ orm.ColumnScanner) error {
return nil
}
func (ints *Ints) ScanColumn(colIdx int, colName string, rd types.Reader, n int) error {
num, err := types.ScanInt64(rd, n)
if err != nil {
return err
}
*ints = append(*ints, num)
return nil
}
func (ints Ints) AppendValue(dst []byte, quote int) []byte {
if len(ints) <= 0 {
return dst
}
for _, v := range ints {
dst = strconv.AppendInt(dst, v, 10)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst
}
//------------------------------------------------------------------------------
type IntSet map[int64]struct{}
var _ orm.HooklessModel = (*IntSet)(nil)
func (set *IntSet) Init() error {
if len(*set) > 0 {
*set = make(map[int64]struct{})
}
return nil
}
func (set *IntSet) NewModel() orm.ColumnScanner {
return set
}
func (IntSet) AddModel(_ orm.ColumnScanner) error {
return nil
}
func (setptr *IntSet) ScanColumn(colIdx int, colName string, rd types.Reader, n int) error {
num, err := types.ScanInt64(rd, n)
if err != nil {
return err
}
set := *setptr
if set == nil {
*setptr = make(IntSet)
set = *setptr
}
set[num] = struct{}{}
return nil
}