Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions adapter/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const Adapter = `cockroachdb`
var registeredAdapter = sqladapter.RegisterAdapter(Adapter, &database{})

// Open establishes a connection to the database server and returns a
// db.Session instance (which is compatible with db.Session).
// [sqladapter.Session] instance (which is compatible with [db.Session]).
func Open(connURL db.ConnectionURL) (db.Session, error) {
return registeredAdapter.OpenDSN(connURL)
}

// NewTx creates a sqlbuilder.Tx instance by wrapping a *sql.Tx value.
// NewTx creates a [sqlbuilder.Tx] instance by wrapping a *[sql.Tx] value.
func NewTx(sqlTx *sql.Tx) (sqlbuilder.Tx, error) {
return registeredAdapter.NewTx(sqlTx)
}

// New creates a sqlbuilder.Sesion instance by wrapping a *sql.DB value.
// New creates a [sqlbuilder.Sesion] instance by wrapping a *[sql.DB] value.
func New(sqlDB *sql.DB) (db.Session, error) {
return registeredAdapter.New(sqlDB)
}
2 changes: 1 addition & 1 deletion adapter/cockroachdb/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type collectionAdapter struct {
}

func (*collectionAdapter) Insert(col sqladapter.Collection, item interface{}) (interface{}, error) {
func (*collectionAdapter) Insert(col sqladapter.Collection, item any) (any, error) {
pKey, err := col.PrimaryKeys()
if err != nil {
return nil, err
Expand Down
70 changes: 44 additions & 26 deletions adapter/cockroachdb/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,61 +32,79 @@ import (
)

// JSONBMap represents a map of interfaces with string keys
// (`map[string]interface{}`) that is compatible with PostgreSQL's JSONB type.
// JSONBMap satisfies sqlbuilder.ScannerValuer.
type JSONBMap map[string]interface{}
// (`map[string]any`) that is compatible with PostgreSQL's JSONB type.
// JSONBMap satisfies [sqlbuilder.ScannerValuer].
//
// Deprecated: This type is simply an alias of [JSONBMapOf][any].
//
//go:fix inline
type JSONBMap = JSONBMapOf[any]

// JSONBMapOf represents a map of any type T with string keys (`map[string]T`)
// that is compatible with PostgreSQL's JSONB type. JSONBMapOf satisfies
// [sqlbuilder.ScannerValuer].
type JSONBMapOf[T any] map[string]T

// Value satisfies the driver.Valuer interface.
func (m JSONBMap) Value() (driver.Value, error) {
// Value satisfies the [driver.Valuer] interface.
func (m JSONBMapOf[T]) Value() (driver.Value, error) {
return JSONBValue(m)
}

// Scan satisfies the sql.Scanner interface.
func (m *JSONBMap) Scan(src interface{}) error {
*m = map[string]interface{}(nil)
// Scan satisfies the [sql.Scanner] interface.
func (m *JSONBMapOf[T]) Scan(src any) error {
*m = map[string]T(nil)
return ScanJSONB(m, src)
}

// JSONBArray represents an array of any type (`[]interface{}`) that is
// JSONBArray represents an array of any type (`[]any`) that is
// compatible with PostgreSQL's JSONB type. JSONBArray satisfies
// sqlbuilder.ScannerValuer.
type JSONBArray []interface{}
// [sqlbuilder.ScannerValuer].
//
// Deprecated: This type is simply an alias of [JSONBArrayOf][any].
//
//go:fix inline
type JSONBArray = JSONBArrayOf[any]

// JSONBArrayOf represents an array of any type (`[]any`) that is
// compatible with PostgreSQL's JSONB type. JSONBArrayOf satisfies
// [sqlbuilder.ScannerValuer].
type JSONBArrayOf[T any] []T

// Value satisfies the driver.Valuer interface.
func (a JSONBArray) Value() (driver.Value, error) {
// Value satisfies the [driver.Valuer] interface.
func (a JSONBArrayOf[T]) Value() (driver.Value, error) {
return JSONBValue(a)
}

// Scan satisfies the sql.Scanner interface.
func (a *JSONBArray) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (a *JSONBArrayOf[T]) Scan(src any) error {
return ScanJSONB(a, src)
}

// JSONBValue takes an interface and provides a driver.Value that can be
// JSONBValue takes an interface and provides a [driver.Value] that can be
// stored as a JSONB column.
func JSONBValue(i interface{}) (driver.Value, error) {
func JSONBValue(i any) (driver.Value, error) {
v := JSONB{i}
return v.Value()
}

// ScanJSONB decodes a JSON byte stream into the passed dst value.
func ScanJSONB(dst interface{}, src interface{}) error {
func ScanJSONB(dst any, src any) error {
v := JSONB{dst}
return v.Scan(src)
}

// EncodeJSONB is deprecated and going to be removed. Use ScanJSONB instead.
func EncodeJSONB(i interface{}) (driver.Value, error) {
func EncodeJSONB(i any) (driver.Value, error) {
return JSONBValue(i)
}

// DecodeJSONB is deprecated and going to be removed. Use JSONBValue instead.
func DecodeJSONB(dst interface{}, src interface{}) error {
func DecodeJSONB(dst any, src any) error {
return ScanJSONB(dst, src)
}

// JSONBConverter provides a helper method WrapValue that satisfies
// sqlbuilder.ValueWrapper, can be used to encode Go structs into JSONB
// [sqlbuilder.ValueWrapper], can be used to encode Go structs into JSONB
// PostgreSQL types and vice versa.
//
// Example:
Expand All @@ -100,7 +118,7 @@ func DecodeJSONB(dst interface{}, src interface{}) error {
type JSONBConverter struct {
}

func (*JSONBConverter) ConvertValue(src interface{}) interface {
func (*JSONBConverter) ConvertValue(src any) interface {
sql.Scanner
driver.Valuer
} {
Expand All @@ -119,7 +137,7 @@ func (t timeWrapper) Value() (driver.Value, error) {
return nil, nil
}

func (t *timeWrapper) Scan(src interface{}) error {
func (t *timeWrapper) Scan(src any) error {
if src == nil {
nilTime := (*time.Time)(nil)
if t.v == nil {
Expand All @@ -144,7 +162,7 @@ func (t *timeWrapper) Scan(src interface{}) error {
return nil
}

func (d *database) ConvertValueContext(ctx context.Context, in interface{}) interface{} {
func (d *database) ConvertValueContext(ctx context.Context, in any) any {
tz, _ := ctx.Value(db.ContextKey("timezone")).(*time.Location)

switch v := in.(type) {
Expand All @@ -163,6 +181,6 @@ var (
_ sqlbuilder.ScannerValuer = &Int64Array{}
_ sqlbuilder.ScannerValuer = &Float64Array{}
_ sqlbuilder.ScannerValuer = &BoolArray{}
_ sqlbuilder.ScannerValuer = &JSONBMap{}
_ sqlbuilder.ScannerValuer = &JSONBArray{}
_ sqlbuilder.ScannerValuer = &JSONBMapOf[struct{}]{}
_ sqlbuilder.ScannerValuer = &JSONBArrayOf[struct{}]{}
)
73 changes: 37 additions & 36 deletions adapter/cockroachdb/custom_types_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ import (
"github.com/jackc/pgtype"
)

// JSONB represents a PostgreSQL's JSONB value:
// https://www.postgresql.org/docs/9.6/static/datatype-json.html. JSONB
// satisfies sqlbuilder.ScannerValuer.
// JSONB represents a [PostgreSQL's JSONB] value. JSONB satisfies
// [sqlbuilder.ScannerValuer].
//
// [PostgreSQL's JSONB]: https://www.postgresql.org/docs/current/static/datatype-json.html
type JSONB struct {
Data interface{}
Data any
}

// MarshalJSON encodes the wrapper value as JSON.
Expand Down Expand Up @@ -62,8 +63,8 @@ func (j *JSONB) UnmarshalJSON(b []byte) error {
return nil
}

// Scan satisfies the sql.Scanner interface.
func (j *JSONB) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (j *JSONB) Scan(src any) error {
t := &pgtype.JSONB{}
if err := t.Scan(src); err != nil {
return err
Expand All @@ -78,7 +79,7 @@ func (j *JSONB) Scan(src interface{}) error {
return nil
}

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (j JSONB) Value() (driver.Value, error) {
t := &pgtype.JSONB{}
if err := t.Set(j.Data); err != nil {
Expand All @@ -89,10 +90,10 @@ func (j JSONB) Value() (driver.Value, error) {

// StringArray represents a one-dimensional array of strings (`[]string{}`)
// that is compatible with PostgreSQL's text array (`text[]`). StringArray
// satisfies sqlbuilder.ScannerValuer.
// satisfies [sqlbuilder.ScannerValuer].
type StringArray []string

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (a StringArray) Value() (driver.Value, error) {
t := pgtype.TextArray{}
if err := t.Set(a); err != nil {
Expand All @@ -101,8 +102,8 @@ func (a StringArray) Value() (driver.Value, error) {
return t.Value()
}

// Scan satisfies the sql.Scanner interface.
func (sa *StringArray) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (sa *StringArray) Scan(src any) error {
d := []string{}
t := pgtype.TextArray{}
if err := t.Scan(src); err != nil {
Expand All @@ -125,7 +126,7 @@ func (b Bytea) Value() (driver.Value, error) {
return t.Value()
}

func (b *Bytea) Scan(src interface{}) error {
func (b *Bytea) Scan(src any) error {
d := []byte{}
t := pgtype.Bytea{}
if err := t.Scan(src); err != nil {
Expand All @@ -140,10 +141,10 @@ func (b *Bytea) Scan(src interface{}) error {

// ByteaArray represents a one-dimensional array of strings (`[]string{}`)
// that is compatible with PostgreSQL's text array (`text[]`). ByteaArray
// satisfies sqlbuilder.ScannerValuer.
// satisfies [sqlbuilder.ScannerValuer].
type ByteaArray [][]byte

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (a ByteaArray) Value() (driver.Value, error) {
t := pgtype.ByteaArray{}
if err := t.Set(a); err != nil {
Expand All @@ -152,8 +153,8 @@ func (a ByteaArray) Value() (driver.Value, error) {
return t.Value()
}

// Scan satisfies the sql.Scanner interface.
func (ba *ByteaArray) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (ba *ByteaArray) Scan(src any) error {
d := [][]byte{}
t := pgtype.ByteaArray{}
if err := t.Scan(src); err != nil {
Expand All @@ -168,10 +169,10 @@ func (ba *ByteaArray) Scan(src interface{}) error {

// Int64Array represents a one-dimensional array of int64s (`[]int64{}`) that
// is compatible with PostgreSQL's integer array (`integer[]`). Int64Array
// satisfies sqlbuilder.ScannerValuer.
// satisfies [sqlbuilder.ScannerValuer].
type Int64Array []int64

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (i64a Int64Array) Value() (driver.Value, error) {
t := pgtype.Int8Array{}
if err := t.Set(i64a); err != nil {
Expand All @@ -180,8 +181,8 @@ func (i64a Int64Array) Value() (driver.Value, error) {
return t.Value()
}

// Scan satisfies the sql.Scanner interface.
func (i64a *Int64Array) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (i64a *Int64Array) Scan(src any) error {
d := []int64{}
t := pgtype.Int8Array{}
if err := t.Scan(src); err != nil {
Expand All @@ -196,10 +197,10 @@ func (i64a *Int64Array) Scan(src interface{}) error {

// Int32Array represents a one-dimensional array of int32s (`[]int32{}`) that
// is compatible with PostgreSQL's integer array (`integer[]`). Int32Array
// satisfies sqlbuilder.ScannerValuer.
// satisfies [sqlbuilder.ScannerValuer].
type Int32Array []int32

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (i32a Int32Array) Value() (driver.Value, error) {
t := pgtype.Int4Array{}
if err := t.Set(i32a); err != nil {
Expand All @@ -208,8 +209,8 @@ func (i32a Int32Array) Value() (driver.Value, error) {
return t.Value()
}

// Scan satisfies the sql.Scanner interface.
func (i32a *Int32Array) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (i32a *Int32Array) Scan(src any) error {
d := []int32{}
t := pgtype.Int4Array{}
if err := t.Scan(src); err != nil {
Expand All @@ -224,10 +225,10 @@ func (i32a *Int32Array) Scan(src interface{}) error {

// Float64Array represents a one-dimensional array of float64s (`[]float64{}`)
// that is compatible with PostgreSQL's double precision array (`double
// precision[]`). Float64Array satisfies sqlbuilder.ScannerValuer.
// precision[]`). Float64Array satisfies [sqlbuilder.ScannerValuer].
type Float64Array []float64

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (f64a Float64Array) Value() (driver.Value, error) {
t := pgtype.Float8Array{}
if err := t.Set(f64a); err != nil {
Expand All @@ -236,8 +237,8 @@ func (f64a Float64Array) Value() (driver.Value, error) {
return t.Value()
}

// Scan satisfies the sql.Scanner interface.
func (f64a *Float64Array) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (f64a *Float64Array) Scan(src any) error {
d := []float64{}
t := pgtype.Float8Array{}
if err := t.Scan(src); err != nil {
Expand All @@ -252,10 +253,10 @@ func (f64a *Float64Array) Scan(src interface{}) error {

// Float32Array represents a one-dimensional array of float32s (`[]float32{}`)
// that is compatible with PostgreSQL's double precision array (`double
// precision[]`). Float32Array satisfies sqlbuilder.ScannerValuer.
// precision[]`). Float32Array satisfies [sqlbuilder.ScannerValuer].
type Float32Array []float32

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (f32a Float32Array) Value() (driver.Value, error) {
t := pgtype.Float8Array{}
if err := t.Set(f32a); err != nil {
Expand All @@ -264,8 +265,8 @@ func (f32a Float32Array) Value() (driver.Value, error) {
return t.Value()
}

// Scan satisfies the sql.Scanner interface.
func (f32a *Float32Array) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (f32a *Float32Array) Scan(src any) error {
d := []float32{}
t := pgtype.Float8Array{}
if err := t.Scan(src); err != nil {
Expand All @@ -280,10 +281,10 @@ func (f32a *Float32Array) Scan(src interface{}) error {

// BoolArray represents a one-dimensional array of int64s (`[]bool{}`) that
// is compatible with PostgreSQL's boolean type (`boolean[]`). BoolArray
// satisfies sqlbuilder.ScannerValuer.
// satisfies [sqlbuilder.ScannerValuer].
type BoolArray []bool

// Value satisfies the driver.Valuer interface.
// Value satisfies the [driver.Valuer] interface.
func (ba BoolArray) Value() (driver.Value, error) {
t := pgtype.BoolArray{}
if err := t.Set(ba); err != nil {
Expand All @@ -292,8 +293,8 @@ func (ba BoolArray) Value() (driver.Value, error) {
return t.Value()
}

// Scan satisfies the sql.Scanner interface.
func (ba *BoolArray) Scan(src interface{}) error {
// Scan satisfies the [sql.Scanner] interface.
func (ba *BoolArray) Scan(src any) error {
d := []bool{}
t := pgtype.BoolArray{}
if err := t.Scan(src); err != nil {
Expand Down
Loading