2022-08-31 07:52:43 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
2023-05-05 15:34:53 +00:00
|
|
|
"encoding/json"
|
2023-09-15 14:58:45 +00:00
|
|
|
"time"
|
2022-08-31 07:52:43 +00:00
|
|
|
|
|
|
|
"github.com/jackc/pgtype"
|
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type TextArray[t ~string] []t
|
2022-08-31 07:52:43 +00:00
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
// Scan implements the [database/sql.Scanner] interface.
|
2023-10-19 10:19:10 +00:00
|
|
|
func (s *TextArray[t]) Scan(src any) error {
|
2022-08-31 07:52:43 +00:00
|
|
|
array := new(pgtype.TextArray)
|
|
|
|
if err := array.Scan(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return array.AssignTo(s)
|
2022-08-31 07:52:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
// Value implements the [database/sql/driver.Valuer] interface.
|
2023-10-19 10:19:10 +00:00
|
|
|
func (s TextArray[t]) Value() (driver.Value, error) {
|
2022-08-31 07:52:43 +00:00
|
|
|
if len(s) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
array := pgtype.TextArray{}
|
|
|
|
if err := array.Set(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return array.Value()
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type arrayField interface {
|
2022-08-31 07:52:43 +00:00
|
|
|
~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type Array[F arrayField] []F
|
2022-08-31 07:52:43 +00:00
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
// Scan implements the [database/sql.Scanner] interface.
|
2023-10-19 10:19:10 +00:00
|
|
|
func (a *Array[F]) Scan(src any) error {
|
|
|
|
array := new(pgtype.Int8Array)
|
2022-08-31 07:52:43 +00:00
|
|
|
if err := array.Scan(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
elements := make([]int64, len(array.Elements))
|
|
|
|
if err := array.AssignTo(&elements); err != nil {
|
2022-08-31 07:52:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
*a = make([]F, len(elements))
|
|
|
|
for i, element := range elements {
|
|
|
|
(*a)[i] = F(element)
|
2022-08-31 07:52:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
// Value implements the [database/sql/driver.Valuer] interface.
|
2023-10-19 10:19:10 +00:00
|
|
|
func (a Array[F]) Value() (driver.Value, error) {
|
|
|
|
if len(a) == 0 {
|
2022-08-31 07:52:43 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
array := pgtype.Int8Array{}
|
|
|
|
if err := array.Set(a); err != nil {
|
2022-08-31 07:52:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return array.Value()
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
|
|
|
|
type Map[V any] map[string]V
|
|
|
|
|
|
|
|
// Scan implements the [database/sql.Scanner] interface.
|
|
|
|
func (m *Map[V]) Scan(src any) error {
|
|
|
|
bytea := new(pgtype.Bytea)
|
|
|
|
if err := bytea.Scan(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(bytea.Bytes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(bytea.Bytes, &m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements the [database/sql/driver.Valuer] interface.
|
|
|
|
func (m Map[V]) Value() (driver.Value, error) {
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
2023-09-15 14:58:45 +00:00
|
|
|
|
|
|
|
type Duration time.Duration
|
|
|
|
|
|
|
|
// Scan implements the [database/sql.Scanner] interface.
|
|
|
|
func (d *Duration) Scan(src any) error {
|
|
|
|
interval := new(pgtype.Interval)
|
|
|
|
if err := interval.Scan(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*d = Duration(time.Duration(interval.Microseconds*1000) + time.Duration(interval.Days)*24*time.Hour + time.Duration(interval.Months)*30*24*time.Hour)
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-25 11:42:00 +00:00
|
|
|
|
|
|
|
// NullDuration can be used for NULL intervals.
|
|
|
|
// If Valid is false, the scanned value was NULL
|
|
|
|
// This behavior is similar to [database/sql.NullString]
|
|
|
|
type NullDuration struct {
|
|
|
|
Valid bool
|
|
|
|
Duration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan implements the [database/sql.Scanner] interface.
|
|
|
|
func (d *NullDuration) Scan(src any) error {
|
|
|
|
if src == nil {
|
|
|
|
d.Duration, d.Valid = 0, false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
duration := new(Duration)
|
|
|
|
if err := duration.Scan(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Duration, d.Valid = time.Duration(*duration), true
|
|
|
|
return nil
|
|
|
|
}
|