Skip to content

Commit ed47f42

Browse files
committed
db/lib/sqlbuilder: add missing custom_types.go file
1 parent 92c8aa9 commit ed47f42

File tree

4 files changed

+92
-21
lines changed

4 files changed

+92
-21
lines changed

Diff for: lib/sqlbuilder/custom_types.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining
4+
// a copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to
8+
// permit persons to whom the Software is furnished to do so, subject to
9+
// the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be
12+
// included in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
package sqlbuilder
23+
24+
import (
25+
"database/sql"
26+
"database/sql/driver"
27+
28+
"reflect"
29+
)
30+
31+
var (
32+
// ValuerType is the reflection type for the driver.Valuer interface.
33+
ValuerType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
34+
35+
// ScannerType is the reflection type for the sql.Scanner interface.
36+
ScannerType = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
37+
38+
// ValueWrapperType is the reflection type for the sql.ValueWrapper interface.
39+
ValueWrapperType = reflect.TypeOf((*ValueWrapper)(nil)).Elem()
40+
)
41+
42+
// ValueWrapper defines a method WrapValue that query arguments can use to wrap
43+
// themselves around helper types right before being used in a query.
44+
//
45+
// Example:
46+
//
47+
// func (a MyCustomArray) WrapValue(value interface{}) interface{} {
48+
// // postgresql.Array adds a driver.Valuer and sql.Scanner around
49+
// // custom arrays.
50+
// return postgresql.Array(values)
51+
// }
52+
type ValueWrapper interface {
53+
WrapValue(value interface{}) interface{}
54+
}
55+
56+
// ScannerValuer represents a value that satisfies both driver.Valuer and
57+
// sql.Scanner interfaces.
58+
type ScannerValuer interface {
59+
driver.Valuer
60+
sql.Scanner
61+
}

Diff for: postgresql/adapter_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ type customJSONB struct {
230230
}
231231

232232
func (c customJSONB) Value() (driver.Value, error) {
233-
return ToJSONBValue(c)
233+
return JSONBValue(c)
234234
}
235235

236236
func (c *customJSONB) Scan(src interface{}) error {
237-
return FromJSONBValue(c, src)
237+
return ScanJSONB(c, src)
238238
}
239239

240240
type autoCustomJSONB struct {
@@ -332,8 +332,8 @@ func testPostgreSQLTypes(t *testing.T, sess sqlbuilder.Database) {
332332
Int64Value int64Compat `db:"int64_value"`
333333
Int64ValueArray int64CompatArray `db:"int64_value_array"`
334334

335-
IntegerArray Int64Array `db:"integer_array"`
336-
StringArray StringArray `db:"string_array"`
335+
IntegerArray Int64Array `db:"integer_array,jsonb"`
336+
StringArray StringArray `db:"string_array,stringarray"`
337337
JSONBMap JSONBMap `db:"jsonb_map"`
338338

339339
PGTypeInline `db:",inline"`
@@ -836,10 +836,10 @@ type Settings struct {
836836
}
837837

838838
func (s *Settings) Scan(src interface{}) error {
839-
return FromJSONBValue(s, src)
839+
return ScanJSONB(s, src)
840840
}
841841
func (s Settings) Value() (driver.Value, error) {
842-
return ToJSONBValue(s)
842+
return JSONBValue(s)
843843
}
844844

845845
func TestOptionTypeJsonbStruct(t *testing.T) {

Diff for: postgresql/custom_types.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ type JSONBMap map[string]interface{}
202202

203203
// Value satisfies the driver.Valuer interface.
204204
func (m JSONBMap) Value() (driver.Value, error) {
205-
return ToJSONBValue(m)
205+
return JSONBValue(m)
206206
}
207207

208208
// Scan satisfies the sql.Scanner interface.
209209
func (m *JSONBMap) Scan(src interface{}) error {
210210
*m = map[string]interface{}(nil)
211-
return FromJSONBValue(m, src)
211+
return ScanJSONB(m, src)
212212
}
213213

214214
// JSONBArray represents an array of any type (`[]interface{}`) that is
@@ -218,27 +218,37 @@ type JSONBArray []interface{}
218218

219219
// Value satisfies the driver.Valuer interface.
220220
func (a JSONBArray) Value() (driver.Value, error) {
221-
return ToJSONBValue(a)
221+
return JSONBValue(a)
222222
}
223223

224224
// Scan satisfies the sql.Scanner interface.
225225
func (a *JSONBArray) Scan(src interface{}) error {
226-
return FromJSONBValue(a, src)
226+
return ScanJSONB(a, src)
227227
}
228228

229-
// ToJSONBValue takes an interface and provides a driver.Value that can be
229+
// JSONBValue takes an interface and provides a driver.Value that can be
230230
// stored as a JSONB column.
231-
func ToJSONBValue(i interface{}) (driver.Value, error) {
231+
func JSONBValue(i interface{}) (driver.Value, error) {
232232
v := JSONB{i}
233233
return v.Value()
234234
}
235235

236-
// FromJSONBValue decodes a JSON byte stream into the passed dst value.
237-
func FromJSONBValue(dst interface{}, src interface{}) error {
236+
// ScanJSONB decodes a JSON byte stream into the passed dst value.
237+
func ScanJSONB(dst interface{}, src interface{}) error {
238238
v := JSONB{dst}
239239
return v.Scan(src)
240240
}
241241

242+
// EncodeJSONB is deprecated and going to be removed. Use ScanJSONB instead.
243+
func EncodeJSONB(i interface{}) (driver.Value, error) {
244+
return JSONBValue(i)
245+
}
246+
247+
// DecodeJSONB is deprecated and going to be removed. Use JSONBValue instead.
248+
func DecodeJSONB(dst interface{}, src interface{}) error {
249+
return ScanJSONB(dst, src)
250+
}
251+
242252
// JSONBConverter provides a helper method WrapValue that satisfies
243253
// sqlbuilder.ValueWrapper, can be used to encode Go structs into JSONB
244254
// PostgreSQL types and vice versa.

Diff for: postgresql/custom_types_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,46 @@ type testStruct struct {
1212
V JSONB `json:"v"`
1313
}
1414

15-
func TestFromJSONBValue(t *testing.T) {
15+
func TestScanJSONB(t *testing.T) {
1616
{
1717
a := testStruct{}
18-
err := FromJSONBValue(&a, []byte(`{"x": 5, "z": "Hello", "v": 1}`))
18+
err := ScanJSONB(&a, []byte(`{"x": 5, "z": "Hello", "v": 1}`))
1919
assert.NoError(t, err)
2020
assert.Equal(t, "Hello", a.Z)
2121
assert.Equal(t, float64(1), a.V.V)
2222
assert.Equal(t, 5, a.X)
2323
}
2424
{
2525
a := testStruct{}
26-
err := FromJSONBValue(&a, []byte(`{"x": 5, "z": "Hello", "v": null}`))
26+
err := DecodeJSONB(&a, []byte(`{"x": 5, "z": "Hello", "v": null}`))
2727
assert.NoError(t, err)
2828
assert.Equal(t, "Hello", a.Z)
2929
assert.Equal(t, nil, a.V.V)
3030
assert.Equal(t, 5, a.X)
3131
}
3232
{
3333
a := testStruct{}
34-
err := FromJSONBValue(&a, []byte(`{"x": 5, "z": "Hello"}`))
34+
err := ScanJSONB(&a, []byte(`{"x": 5, "z": "Hello"}`))
3535
assert.NoError(t, err)
3636
assert.Equal(t, "Hello", a.Z)
3737
assert.Equal(t, nil, a.V.V)
3838
assert.Equal(t, 5, a.X)
3939
}
4040
{
4141
a := testStruct{}
42-
err := FromJSONBValue(&a, []byte(`{"v": "Hello"}`))
42+
err := ScanJSONB(&a, []byte(`{"v": "Hello"}`))
4343
assert.NoError(t, err)
4444
assert.Equal(t, "Hello", a.V.V)
4545
}
4646
{
4747
a := testStruct{}
48-
err := FromJSONBValue(&a, []byte(`{"v": true}`))
48+
err := ScanJSONB(&a, []byte(`{"v": true}`))
4949
assert.NoError(t, err)
5050
assert.Equal(t, true, a.V.V)
5151
}
5252
{
5353
a := testStruct{}
54-
err := FromJSONBValue(&a, []byte(`{}`))
54+
err := ScanJSONB(&a, []byte(`{}`))
5555
assert.NoError(t, err)
5656
assert.Equal(t, nil, a.V.V)
5757
}

0 commit comments

Comments
 (0)