-
Notifications
You must be signed in to change notification settings - Fork 22
/
parameter.go
257 lines (200 loc) · 4.47 KB
/
parameter.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2010 The go-pgsql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pgsql
import (
"errors"
"fmt"
"math/big"
"reflect"
"time"
)
// Parameter is used to set the value of a parameter in a Statement.
type Parameter struct {
name string
stmt *Statement
typ Type
customTypeName string
value interface{}
}
// NewParameter returns a new Parameter with the specified name and type.
func NewParameter(name string, typ Type) *Parameter {
return &Parameter{name: name, typ: typ}
}
// NewCustomTypeParameter returns a new Parameter with the specified name and
// custom data type.
//
// The value of customTypeName will be used to insert a type cast into the
// command text for each occurrence of the parameter.
//
// This constructor can be used for enum type parameters. In that case the value
// provided to SetValue is expected to be a string.
func NewCustomTypeParameter(name, customTypeName string) *Parameter {
return &Parameter{name: name, customTypeName: customTypeName}
}
// CustomTypeName returns the custom type name of the Parameter.
func (p *Parameter) CustomTypeName() string {
return p.customTypeName
}
// Name returns the name of the Parameter.
func (p *Parameter) Name() string {
return p.name
}
// Statement returns the *Statement this Parameter is associated with.
func (p *Parameter) Statement() *Statement {
return p.stmt
}
// Type returns the PostgreSQL data type of the Parameter.
func (p *Parameter) Type() Type {
return p.typ
}
// Value returns the current value of the Parameter.
func (p *Parameter) Value() interface{} {
return p.value
}
func (p *Parameter) panicInvalidValue(v interface{}) {
panic(errors.New(fmt.Sprintf("Parameter %s: Invalid value for PostgreSQL type %s: '%v' (Go type: %T)",
p.name, p.typ, v, v)))
}
func isNilPtr(v interface{}) bool {
ptr := reflect.ValueOf(v)
return ptr.Kind() == reflect.Ptr &&
ptr.IsNil()
}
// SetValue sets the current value of the Parameter.
func (p *Parameter) SetValue(v interface{}) (err error) {
if p.stmt != nil && p.stmt.conn.LogLevel >= LogVerbose {
defer p.stmt.conn.logExit(p.stmt.conn.logEnter("*Parameter.SetValue"))
}
defer func() {
if x := recover(); x != nil {
if p.stmt == nil {
switch ex := x.(type) {
case error:
err = ex
case string:
err = errors.New(ex)
default:
err = errors.New("pgsql.*Parameter.SetValue: D'oh!")
}
} else {
err = p.stmt.conn.logAndConvertPanic(x)
}
}
}()
if v == nil {
p.value = nil
return
}
switch p.typ {
case Bigint:
switch val := v.(type) {
case byte:
p.value = int64(val)
case int:
p.value = int64(val)
case int16:
p.value = int64(val)
case int32:
p.value = int64(val)
case uint:
p.value = int64(val)
case uint16:
p.value = int64(val)
case uint32:
p.value = int64(val)
case uint64:
p.value = int64(val)
case int64:
p.value = val
default:
p.panicInvalidValue(v)
}
case Boolean:
val, ok := v.(bool)
if !ok {
p.panicInvalidValue(v)
}
p.value = val
case Char, Text, Varchar:
val, ok := v.(string)
if !ok {
p.panicInvalidValue(v)
}
p.value = val
case Custom:
p.value = v
case Date, Time, TimeTZ, Timestamp, TimestampTZ:
switch val := v.(type) {
case int64:
p.value = val
case time.Time:
if isNilPtr(v) {
p.value = nil
return
}
p.value = val
case uint64:
p.value = val
default:
p.panicInvalidValue(v)
}
case Double:
switch val := v.(type) {
case float32:
p.value = float64(val)
case float64:
p.value = val
default:
p.panicInvalidValue(v)
}
case Integer:
switch val := v.(type) {
case byte:
p.value = int32(val)
case int:
p.value = int32(val)
case int16:
p.value = int32(val)
case uint:
p.value = int32(val)
case uint16:
p.value = int32(val)
case uint32:
p.value = int32(val)
case int32:
p.value = val
default:
p.panicInvalidValue(v)
}
case Numeric:
val, ok := v.(*big.Rat)
if !ok {
p.panicInvalidValue(v)
}
if isNilPtr(v) {
p.value = nil
return
}
p.value = val
case Real:
switch val := v.(type) {
case float32:
p.value = val
default:
p.panicInvalidValue(v)
}
case Smallint:
switch val := v.(type) {
case byte:
p.value = int16(val)
case uint16:
p.value = int16(val)
case int16:
p.value = val
default:
p.panicInvalidValue(v)
}
}
return
}