forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg.go
138 lines (107 loc) · 2.86 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
package pg
import (
"log"
"strconv"
"gopkg.in/pg.v4/internal"
"gopkg.in/pg.v4/orm"
"gopkg.in/pg.v4/types"
)
// Discard is used with Query and QueryOne to discard rows.
var Discard orm.Discard
// Scan returns ColumnScanner that copies the columns in the
// row into the values.
func Scan(values ...interface{}) orm.ColumnScanner {
return orm.Scan(values...)
}
// Q returns a ValueAppender that represents safe SQL query.
func Q(query string, params ...interface{}) types.Q {
return orm.Q(query, params...)
}
// F returns a ValueAppender that represents SQL identifier,
// e.g. table or column name.
func F(field string, params ...interface{}) types.F {
return orm.F(field, params...)
}
// Array returns an Array type that represents PostgreSQL array of any type.
func Array(v interface{}) *types.Array {
return types.NewArray(v)
}
func SetLogger(logger *log.Logger) {
internal.Logger = logger
}
//------------------------------------------------------------------------------
type Strings []string
var _ orm.Model = (*Strings)(nil)
var _ types.ValueAppender = (*Strings)(nil)
func (strings *Strings) NewModel() orm.ColumnScanner {
return strings
}
func (Strings) AddModel(_ orm.ColumnScanner) error {
return nil
}
func (strings *Strings) ScanColumn(colIdx int, _ string, b []byte) error {
*strings = append(*strings, string(b))
return nil
}
func (strings Strings) AppendValue(dst []byte, quote int) ([]byte, error) {
if len(strings) <= 0 {
return dst, nil
}
for _, s := range strings {
dst = types.AppendString(dst, s, 1)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst, nil
}
//------------------------------------------------------------------------------
type Ints []int64
var _ orm.Model = (*Ints)(nil)
var _ types.ValueAppender = (*Ints)(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, b []byte) error {
n, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
*ints = append(*ints, n)
return nil
}
func (ints Ints) AppendValue(dst []byte, quote int) ([]byte, error) {
if len(ints) <= 0 {
return dst, nil
}
for _, v := range ints {
dst = strconv.AppendInt(dst, v, 10)
dst = append(dst, ',')
}
dst = dst[:len(dst)-1]
return dst, nil
}
//------------------------------------------------------------------------------
type IntSet map[int64]struct{}
var _ orm.Model = (*IntSet)(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, b []byte) error {
set := *setptr
if set == nil {
*setptr = make(IntSet)
set = *setptr
}
n, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
set[n] = struct{}{}
return nil
}