-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.go
219 lines (192 loc) · 4.6 KB
/
access.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
package pgtalk
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"github.com/emicklei/pgtalk/convert"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
var EmptyColumnAccessor = []ColumnAccessor{}
type valuePrinter struct {
unimplementedBooleanExpression
v any
}
func makeValuePrinter(v any) valuePrinter { return valuePrinter{v: v} }
func (p valuePrinter) SQLOn(w WriteContext) {
if e, ok := p.v.(SQLWriter); ok {
e.SQLOn(w)
return
}
// TODO rewrite using type switch
if e, ok := p.v.(string); ok {
fmt.Fprintf(w, "'%s'", e)
return
}
if e, ok := p.v.(pgtype.UUID); ok {
fmt.Fprintf(w, "'%s'::uuid", encodeUUID(e.Bytes))
return
}
if e, ok := p.v.(pgtype.Date); ok {
fmt.Fprintf(w, "'%s'::date", toJSON(e))
return
}
if e, ok := p.v.(pgtype.Text); ok {
fmt.Fprintf(w, "'%s'", e.String)
return
}
if e, ok := p.v.(pgtype.Timestamptz); ok {
// '2015-07-16 00:00:00'
// RFC3339 = "2006-01-02T15:04:05Z07:00"
fmt.Fprintf(w, "timestamp '%s'", e.Time.Format("2006-01-02 15:04:05"))
return
}
if e, ok := p.v.(pgtype.Timestamp); ok {
fmt.Fprintf(w, "timestamp '%s'", e.Time.Format("2006-01-02 15:04:05"))
return
}
if e, ok := p.v.(uuid.UUID); ok {
fmt.Fprintf(w, "'%s'::uuid", e.String())
return
}
if e, ok := p.v.(pgtype.Int4); ok {
fmt.Fprintf(w, "%d", e.Int32)
return
}
if e, ok := p.v.(pgtype.Int8); ok {
fmt.Fprintf(w, "%d", e.Int64)
return
}
if e, ok := p.v.(pgtype.Float4); ok {
fmt.Fprintf(w, "%f", e.Float32)
return
}
if e, ok := p.v.(pgtype.Float8); ok {
fmt.Fprintf(w, "%f", e.Float64)
return
}
fmt.Fprintf(w, "%v", p.v)
}
type unimplementedBooleanExpression struct{}
func (unimplementedBooleanExpression) And(e SQLExpression) SQLExpression { panic("unsupported And") }
func (unimplementedBooleanExpression) Or(e SQLExpression) SQLExpression { panic("unsupported Or") }
// hack
func toJSON(m json.Marshaler) string {
data, _ := m.MarshalJSON()
return strings.Trim(string(data), "\"")
}
// encodeUUID converts a uuid byte array to UUID standard string form.
func encodeUUID(src [16]byte) string {
return fmt.Sprintf("%x-%x-%x-%x-%x", src[0:4], src[4:6], src[6:8], src[8:10], src[10:16])
}
type valuesPrinter struct {
unimplementedBooleanExpression
vs []any
}
func (p valuesPrinter) SQLOn(w WriteContext) {
fmt.Fprintf(w, "(")
for i, each := range p.vs {
if i > 0 {
fmt.Fprintf(w, ",")
}
valuePrinter{v: each}.SQLOn(w)
}
fmt.Fprintf(w, ")")
}
type literalString struct {
unimplementedBooleanExpression
value string
}
func newLiteralString(s string) literalString {
return literalString{value: s}
}
func (l literalString) SQLOn(w WriteContext) {
io.WriteString(w, "'")
io.WriteString(w, l.value)
io.WriteString(w, "'")
}
type noCondition struct{}
var EmptyCondition SQLExpression = noCondition{}
func (n noCondition) SQLOn(w WriteContext) {}
// And returns the argument as the receiver is a no operation
func (n noCondition) And(ex SQLExpression) SQLExpression {
return ex
}
// And returns the argument as the receiver is a no operation
func (n noCondition) Or(ex SQLExpression) SQLExpression {
return ex
}
const (
IsPrimary = true
NotPrimary = false
NotNull = true
Nullable = false
)
func writeAccessOn(list []ColumnAccessor, w WriteContext) {
for i, each := range list {
if i > 0 {
io.WriteString(w, ",\n")
}
io.WriteString(w, "\t")
each.SQLOn(w)
}
}
func writeListOn(list []SQLWriter, w WriteContext) {
for i, each := range list {
if i > 0 {
io.WriteString(w, ",\n")
}
io.WriteString(w, "\t")
each.SQLOn(w)
}
}
const HideNilValues = true
func StringWithFields(v any, includePresent bool) string {
vt := reflect.TypeOf(v)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
b := new(bytes.Buffer)
fmt.Fprint(b, vt.PkgPath())
fmt.Fprint(b, ".")
fmt.Fprint(b, vt.Name())
fmt.Fprint(b, "{")
for i := 0; i < vt.NumField(); i++ {
f := vt.Field(i)
if !f.IsExported() {
continue
}
fv := rv.Field(i)
if fv.IsZero() {
continue
}
var fi any
// check fields that have pointer type
if fv.Kind() == reflect.Pointer {
fi = fv.Elem().Interface()
} else {
fi = fv.Interface()
}
// StringWithFields is for debugging purposes so we try to display the xxx-xxx-xxx representation of a UUID
if uid, ok := fi.(pgtype.UUID); ok {
fmt.Fprintf(b, "%s:%s ", f.Name, convert.UUIDToString(uid))
} else {
fmt.Fprintf(b, "%s:%v ", f.Name, fi)
}
}
fmt.Fprint(b, "}")
return b.String()
}
type SQLLiteral struct {
Literal string
}
func (l SQLLiteral) SQLOn(w WriteContext) {
w.Write([]byte(l.Literal))
}