Skip to content

Commit

Permalink
cockroachdb: add support for pgx
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Jul 13, 2021
1 parent f995814 commit 38a1aec
Show file tree
Hide file tree
Showing 14 changed files with 1,101 additions and 341 deletions.
173 changes: 97 additions & 76 deletions adapter/cockroachdb/cockroachdb_test.go
Expand Up @@ -40,31 +40,32 @@ import (
"github.com/upper/db/v4/internal/testsuite"
)

type customJSONB struct {
N string `json:"name"`
V float64 `json:"value"`
type customJSONBObjectArray []customJSONB

func (customJSONBObjectArray) ConvertValue(in interface{}) interface {
sql.Scanner
driver.Valuer
} {
return &JSONB{in}
}

func (c customJSONB) Value() (driver.Value, error) {
type customJSONBObjectMap map[string]customJSONB

func (c customJSONBObjectMap) Value() (driver.Value, error) {
return JSONBValue(c)
}

func (c *customJSONB) Scan(src interface{}) error {
func (c *customJSONBObjectMap) Scan(src interface{}) error {
return ScanJSONB(c, src)
}

type autoCustomJSONB struct {
type customJSONB struct {
N string `json:"name"`
V float64 `json:"value"`

*JSONBConverter
}

var (
_ = driver.Valuer(&customJSONB{})
_ = sql.Scanner(&customJSONB{})
)

type int64Compat int64

type uintCompat uint
Expand All @@ -73,35 +74,30 @@ type stringCompat string

type uint8Compat uint8

type int64CompatArray []int64Compat

type uint8CompatArray []uint8Compat

type uintCompatArray []uintCompat

func (u *uint8Compat) Scan(src interface{}) error {
if src != nil {
switch v := src.(type) {
case int64:
*u = uint8Compat((src).(int64))
case []byte:
i, err := strconv.ParseInt(string(v), 10, 64)
if err != nil {
return err
}
*u = uint8Compat(i)
default:
panic(fmt.Sprintf("expected type %T", src))
}
func (ua uint8CompatArray) Value() (driver.Value, error) {
v := make([]byte, len(ua))
for i := range ua {
v[i] = byte(ua[i])
}
return nil
return v, nil
}

func (uint8CompatArray) ConvertValue(in interface{}) interface {
sql.Scanner
driver.Valuer
} {
return Array(in)
func (ua *uint8CompatArray) Scan(src interface{}) error {
decoded := Bytea{}
if err := decoded.Scan(src); err != nil {
return nil
}
if len(decoded) < 1 {
*ua = nil
return nil
}
*ua = make([]uint8Compat, len(decoded))
for i := range decoded {
(*ua)[i] = uint8Compat(decoded[i])
}
return nil
}

func (u *int64Compat) Scan(src interface{}) error {
Expand All @@ -122,11 +118,30 @@ func (u *int64Compat) Scan(src interface{}) error {
return nil
}

func (int64CompatArray) ConvertValue(in interface{}) interface {
sql.Scanner
driver.Valuer
} {
return Array(in)
type int64CompatArray []int64Compat

func (i64a int64CompatArray) Value() (driver.Value, error) {
v := make(Int64Array, len(i64a))
for i := range i64a {
v[i] = int64(i64a[i])
}
return v.Value()
}

func (i64a *int64CompatArray) Scan(src interface{}) error {
s := Int64Array{}
if err := s.Scan(src); err != nil {
return err
}
dst := make([]int64Compat, len(s))
for i := range s {
dst[i] = int64Compat(s[i])
}
if len(dst) < 1 {
return nil
}
*i64a = dst
return nil
}

type AdapterTests struct {
Expand Down Expand Up @@ -216,8 +231,8 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
AutoIntegerArray []int64 `db:"auto_integer_array"`
AutoStringArray []string `db:"auto_string_array"`
AutoJSONBMap map[string]interface{} `db:"auto_jsonb_map"`
AutoJSONBMapString map[string]string `db:"auto_jsonb_map_string"`
AutoJSONBMapInteger map[string]int64 `db:"auto_jsonb_map_integer"`
AutoJSONBMapString map[string]interface{} `db:"auto_jsonb_map_string"`
AutoJSONBMapInteger map[string]interface{} `db:"auto_jsonb_map_integer"`
}

type PGType struct {
Expand All @@ -226,8 +241,8 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
UInt8Value uint8Compat `db:"uint8_value"`
UInt8ValueArray uint8CompatArray `db:"uint8_value_array"`

Int64Value int64Compat `db:"int64_value"`
Int64ValueArray int64CompatArray `db:"int64_value_array"`
Int64Value int64Compat `db:"int64_value"`
Int64ValueArray *int64CompatArray `db:"int64_value_array"`

IntegerArray Int64Array `db:"integer_array"`
StringArray StringArray `db:"string_array,stringarray"`
Expand All @@ -240,14 +255,14 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
JSONBObject JSONB `db:"jsonb_object"`
JSONBArray JSONBArray `db:"jsonb_array"`

CustomJSONBObject customJSONB `db:"custom_jsonb_object"`
AutoCustomJSONBObject autoCustomJSONB `db:"auto_custom_jsonb_object"`
CustomJSONBObject customJSONB `db:"custom_jsonb_object"`
AutoCustomJSONBObject customJSONB `db:"auto_custom_jsonb_object"`

CustomJSONBObjectPtr *customJSONB `db:"custom_jsonb_object_ptr,omitempty"`
AutoCustomJSONBObjectPtr *autoCustomJSONB `db:"auto_custom_jsonb_object_ptr,omitempty"`
CustomJSONBObjectPtr *customJSONB `db:"custom_jsonb_object_ptr,omitempty"`
AutoCustomJSONBObjectPtr *customJSONB `db:"auto_custom_jsonb_object_ptr,omitempty"`

AutoCustomJSONBObjectArray []autoCustomJSONB `db:"auto_custom_jsonb_object_array"`
AutoCustomJSONBObjectMap map[string]autoCustomJSONB `db:"auto_custom_jsonb_object_map"`
AutoCustomJSONBObjectArray *customJSONBObjectArray `db:"auto_custom_jsonb_object_array"`
AutoCustomJSONBObjectMap *customJSONBObjectMap `db:"auto_custom_jsonb_object_map"`

StringValue string `db:"string_value"`
IntegerValue int64 `db:"integer_value"`
Expand All @@ -258,46 +273,52 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
UIntCompatValue uintCompat `db:"uinteger_compat_value"`
StringCompatValue stringCompat `db:"string_compat_value"`

Int64CompatValueJSONBArray []int64Compat `db:"integer_compat_value_jsonb_array"`
UIntCompatValueJSONBArray uintCompatArray `db:"uinteger_compat_value_jsonb_array"`
StringCompatValueJSONBArray []stringCompat `db:"string_compat_value_jsonb_array"`
Int64CompatValueJSONBArray JSONBArray `db:"integer_compat_value_jsonb_array"`
UIntCompatValueJSONBArray JSONBArray `db:"uinteger_compat_value_jsonb_array"`
StringCompatValueJSONBArray JSONBArray `db:"string_compat_value_jsonb_array"`

StringValuePtr *string `db:"string_value_ptr,omitempty"`
IntegerValuePtr *int64 `db:"integer_value_ptr,omitempty"`
VarcharValuePtr *string `db:"varchar_value_ptr,omitempty"`
DecimalValuePtr *float64 `db:"decimal_value_ptr,omitempty"`

UUIDValueString *string `db:"uuid_value_string,omitempty"`
}

integerValue := int64(10)
stringValue := string("ten")
decimalValue := float64(10.0)

uuidStringValue := "52356d08-6a16-4839-9224-75f0a547e13c"

integerArrayValue := Int64Array{1, 2, 3, 4}
stringArrayValue := StringArray{"a", "b", "c"}
jsonbMapValue := JSONBMap{"Hello": "World"}

testValue := "Hello world!"

origPgTypeTests := []PGType{
PGType{
UUIDValueString: &uuidStringValue,
},
PGType{
UInt8Value: 7,
UInt8ValueArray: uint8CompatArray{1, 2, 3, 4, 5, 6},
},
PGType{
Int64Value: -1,
Int64ValueArray: int64CompatArray{1, 2, 3, -4, 5, 6},
Int64ValueArray: &int64CompatArray{1, 2, 3, -4, 5, 6},
},
PGType{
UInt8Value: 1,
UInt8ValueArray: uint8CompatArray{1, 2, 3, 4, 5, 6},
},
PGType{
Int64Value: 1,
Int64ValueArray: int64CompatArray{7, 7, 7},
Int64ValueArray: &int64CompatArray{7, 7, 7},
},
PGType{
Int64Value: 1,
Int64ValueArray: int64CompatArray{},
Int64Value: 1,
},
PGType{
Int64Value: 99,
Expand All @@ -309,22 +330,22 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
StringCompatValue: "abc",
},
PGType{
Int64CompatValueJSONBArray: []int64Compat{1, -2, 3, -4},
UIntCompatValueJSONBArray: []uintCompat{1, 2, 3, 4},
StringCompatValueJSONBArray: []stringCompat{"a", "b", "", "c"},
Int64CompatValueJSONBArray: JSONBArray{1.0, -2.0, 3.0, -4.0},
UIntCompatValueJSONBArray: JSONBArray{1.0, 2.0, 3.0, 4.0},
StringCompatValueJSONBArray: JSONBArray{"a", "b", "", "c"},
},
PGType{
Int64CompatValueJSONBArray: []int64Compat(nil),
UIntCompatValueJSONBArray: []uintCompat(nil),
StringCompatValueJSONBArray: []stringCompat(nil),
Int64CompatValueJSONBArray: JSONBArray(nil),
UIntCompatValueJSONBArray: JSONBArray(nil),
StringCompatValueJSONBArray: JSONBArray(nil),
},
PGType{
IntegerValuePtr: &integerValue,
StringValuePtr: &stringValue,
DecimalValuePtr: &decimalValue,
PGTypeAutoInline: PGTypeAutoInline{
AutoJSONBMapString: map[string]string{"a": "x", "b": "67"},
AutoJSONBMapInteger: map[string]int64{"a": 12, "b": 13},
AutoJSONBMapString: map[string]interface{}{"a": "x", "b": "67"},
AutoJSONBMapInteger: map[string]interface{}{"a": 12.0, "b": 13.0},
},
},
PGType{
Expand All @@ -342,19 +363,19 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
},
},
PGType{
AutoCustomJSONBObjectArray: []autoCustomJSONB{
autoCustomJSONB{
AutoCustomJSONBObjectArray: &customJSONBObjectArray{
customJSONB{
N: "Hello",
},
autoCustomJSONB{
customJSONB{
N: "World",
},
},
AutoCustomJSONBObjectMap: map[string]autoCustomJSONB{
"a": autoCustomJSONB{
AutoCustomJSONBObjectMap: &customJSONBObjectMap{
"a": customJSONB{
N: "Hello",
},
"b": autoCustomJSONB{
"b": customJSONB{
N: "World",
},
},
Expand Down Expand Up @@ -434,20 +455,20 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
CustomJSONBObject: customJSONB{
N: "Hello",
},
AutoCustomJSONBObject: autoCustomJSONB{
AutoCustomJSONBObject: customJSONB{
N: "World",
},
StringArray: []string{"", "", "", ``, `""`},
},
PGType{
CustomJSONBObject: customJSONB{},
AutoCustomJSONBObject: autoCustomJSONB{},
AutoCustomJSONBObject: customJSONB{},
},
PGType{
CustomJSONBObject: customJSONB{
N: "Hello 1",
},
AutoCustomJSONBObject: autoCustomJSONB{
AutoCustomJSONBObject: customJSONB{
N: "World 2",
},
},
Expand All @@ -457,13 +478,13 @@ func testPostgreSQLTypes(t *testing.T, sess db.Session) {
},
PGType{
CustomJSONBObjectPtr: &customJSONB{},
AutoCustomJSONBObjectPtr: &autoCustomJSONB{},
AutoCustomJSONBObjectPtr: &customJSONB{},
},
PGType{
CustomJSONBObjectPtr: &customJSONB{
N: "Hello 3",
},
AutoCustomJSONBObjectPtr: &autoCustomJSONB{
AutoCustomJSONBObjectPtr: &customJSONB{
N: "World 4",
},
},
Expand Down

0 comments on commit 38a1aec

Please sign in to comment.