2023-05-22 18:08:14 +03:00
|
|
|
package record
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-08-12 21:54:14 +03:00
|
|
|
"log/slog"
|
2020-08-06 20:58:47 +03:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// FieldMeta is a bit of a strange entity, and in an ideal
|
|
|
|
// world, it wouldn't exist. It's here because:
|
|
|
|
//
|
|
|
|
// - The DB driver impls that sq utilizes (postgres, sqlite, etc)
|
|
|
|
// often have individual quirks (not reporting nullability etc)
|
|
|
|
// that necessitate modifying sql.ColumnType so that there's
|
|
|
|
// consistent behavior across the drivers.
|
|
|
|
//
|
|
|
|
// - We wanted to retain (and supplement) the method set of
|
|
|
|
// sql.ColumnType (basically, use the same "interface", even
|
|
|
|
// though sql.ColumnType is a struct, not interface) so that
|
|
|
|
// devs don't need to learn a whole new thing.
|
|
|
|
//
|
|
|
|
// - For that reason, stdlib sql.ColumnType needs to be
|
2020-08-23 13:42:15 +03:00
|
|
|
// supplemented with kind.Kind, and there needs to
|
2020-08-06 20:58:47 +03:00
|
|
|
// be a mechanism for modifying sql.ColumnType's fields.
|
|
|
|
//
|
|
|
|
// - But sql.ColumnType is sealed (its fields cannot be changed
|
|
|
|
// from outside its package).
|
|
|
|
//
|
|
|
|
// - Hence this construct where we have FieldMeta (which
|
|
|
|
// abstractly is an adapter around sql.ColumnType) and also
|
|
|
|
// a data holder struct (ColumnDataType), which permits the
|
|
|
|
// mutation of the column type fields.
|
|
|
|
//
|
|
|
|
// Likely there's a better design available than this one,
|
|
|
|
// but it suffices.
|
|
|
|
type FieldMeta struct {
|
2023-07-09 04:34:53 +03:00
|
|
|
data *ColumnTypeData
|
|
|
|
mungedName string
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFieldMeta returns a new instance backed by the data arg.
|
2023-07-09 04:34:53 +03:00
|
|
|
// If mungedName is empty, ColumnTypeData.Name is used.
|
|
|
|
func NewFieldMeta(data *ColumnTypeData, mungedName string) *FieldMeta {
|
|
|
|
if mungedName == "" {
|
|
|
|
mungedName = data.Name
|
|
|
|
}
|
|
|
|
return &FieldMeta{data: data, mungedName: mungedName}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
// String returns a log-debug friendly representation.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (fm *FieldMeta) String() string {
|
|
|
|
nullMsg := "?"
|
|
|
|
if fm.data.HasNullable {
|
|
|
|
nullMsg = strconv.FormatBool(fm.data.Nullable)
|
|
|
|
}
|
|
|
|
|
2023-07-27 07:19:11 +03:00
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s|%s|%s|%s|%s|%s",
|
|
|
|
fm.data.Name,
|
|
|
|
fm.mungedName,
|
|
|
|
fm.data.Kind.String(),
|
|
|
|
fm.data.DatabaseTypeName,
|
|
|
|
fm.ScanType().String(),
|
|
|
|
nullMsg,
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name is documented by sql.ColumnType.Name.
|
|
|
|
func (fm *FieldMeta) Name() string {
|
|
|
|
return fm.data.Name
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
// MungedName returns the (possibly-munged) column name.
|
|
|
|
// This value is what should be used for outputting the col name.
|
|
|
|
// This exists largely to handle the case of duplicate col names
|
|
|
|
// in a result set, e.g. when doing a JOIN on tables with
|
|
|
|
// identically-named columns. But typically this value is the same
|
|
|
|
// as that returned by FieldMeta.Name.
|
|
|
|
func (fm *FieldMeta) MungedName() string {
|
|
|
|
return fm.mungedName
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
// Length is documented by sql.ColumnType.Written.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (fm *FieldMeta) Length() (length int64, ok bool) {
|
|
|
|
return fm.data.Length, fm.data.HasLength
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecimalSize is documented by sql.ColumnType.DecimalSize.
|
|
|
|
func (fm *FieldMeta) DecimalSize() (precision, scale int64, ok bool) {
|
|
|
|
return fm.data.Precision, fm.data.Scale, fm.data.HasPrecisionScale
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanType is documented by sql.ColumnType.ScanType.
|
|
|
|
func (fm *FieldMeta) ScanType() reflect.Type {
|
|
|
|
return fm.data.ScanType
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nullable is documented by sql.ColumnType.Nullable.
|
|
|
|
func (fm *FieldMeta) Nullable() (nullable, ok bool) {
|
|
|
|
return fm.data.Nullable, fm.data.HasNullable
|
|
|
|
}
|
|
|
|
|
|
|
|
// DatabaseTypeName is documented by sql.ColumnType.DatabaseTypeName.
|
|
|
|
func (fm *FieldMeta) DatabaseTypeName() string {
|
|
|
|
return fm.data.DatabaseTypeName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kind returns the data kind for the column.
|
2020-08-23 13:42:15 +03:00
|
|
|
func (fm *FieldMeta) Kind() kind.Kind {
|
2020-08-06 20:58:47 +03:00
|
|
|
return fm.data.Kind
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// Meta is a slice of *FieldMeta, encapsulating the metadata
|
2020-08-06 20:58:47 +03:00
|
|
|
// for a record.
|
2023-05-22 18:08:14 +03:00
|
|
|
type Meta []*FieldMeta
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
// Names returns the column names. These are the col names from
|
|
|
|
// the database. See also: MungedNames.
|
2023-05-22 18:08:14 +03:00
|
|
|
func (rm Meta) Names() []string {
|
2020-08-06 20:58:47 +03:00
|
|
|
names := make([]string, len(rm))
|
|
|
|
for i, col := range rm {
|
|
|
|
names[i] = col.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:34:53 +03:00
|
|
|
// MungedNames returns the munged column names, which may be
|
|
|
|
// the same as those returned from Meta.Names.
|
|
|
|
func (rm Meta) MungedNames() []string {
|
|
|
|
names := make([]string, len(rm))
|
|
|
|
for i, col := range rm {
|
|
|
|
names[i] = col.MungedName()
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2022-12-17 02:34:33 +03:00
|
|
|
// NewScanRow returns a new []any that can be scanned
|
2020-08-06 20:58:47 +03:00
|
|
|
// into by sql.Rows.Scan.
|
2023-05-22 18:08:14 +03:00
|
|
|
func (rm Meta) NewScanRow() []any {
|
2022-12-17 02:34:33 +03:00
|
|
|
dests := make([]any, len(rm))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
for i, col := range rm {
|
2022-12-24 07:05:26 +03:00
|
|
|
if col.data.ScanType == nil {
|
|
|
|
// If there's no scan type set, fall back on *any
|
|
|
|
dests[i] = new(any)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
val := reflect.New(col.data.ScanType)
|
|
|
|
dests[i] = val.Interface()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return dests
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kinds returns the data kinds for the record.
|
2023-05-22 18:08:14 +03:00
|
|
|
func (rm Meta) Kinds() []kind.Kind {
|
2020-08-23 13:42:15 +03:00
|
|
|
kinds := make([]kind.Kind, len(rm))
|
2020-08-06 20:58:47 +03:00
|
|
|
for i, col := range rm {
|
|
|
|
kinds[i] = col.Kind()
|
|
|
|
}
|
|
|
|
|
|
|
|
return kinds
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanTypes returns the scan types for the record.
|
2023-05-22 18:08:14 +03:00
|
|
|
func (rm Meta) ScanTypes() []reflect.Type {
|
2020-08-06 20:58:47 +03:00
|
|
|
scanTypes := make([]reflect.Type, len(rm))
|
|
|
|
for i, col := range rm {
|
|
|
|
scanTypes[i] = col.ScanType()
|
|
|
|
}
|
|
|
|
|
|
|
|
return scanTypes
|
|
|
|
}
|
|
|
|
|
2023-07-27 07:19:11 +03:00
|
|
|
// LogValue implements slog.LogValuer.
|
|
|
|
func (rm Meta) LogValue() slog.Value {
|
|
|
|
if len(rm) == 0 {
|
|
|
|
return slog.Value{}
|
|
|
|
}
|
|
|
|
|
|
|
|
a := make([]string, len(rm))
|
|
|
|
for i := range rm {
|
|
|
|
a[i] = rm[i].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return slog.AnyValue(a)
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// ColumnTypeData contains the same data as sql.ColumnType
|
|
|
|
// as well SQ's derived data kind. This type exists with
|
|
|
|
// exported fields instead of methods (as on sql.ColumnType)
|
|
|
|
// due to the need to work with the fields for testing, and
|
|
|
|
// also because for some drivers it's useful to twiddle with
|
|
|
|
// the scan type.
|
|
|
|
//
|
|
|
|
// This is all a bit ugly.
|
|
|
|
type ColumnTypeData struct {
|
2024-01-27 10:11:24 +03:00
|
|
|
ScanType reflect.Type `json:"scan_type"`
|
|
|
|
|
2022-12-24 07:05:26 +03:00
|
|
|
Name string `json:"name"`
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2024-01-27 10:11:24 +03:00
|
|
|
DatabaseTypeName string `json:"database_type_name"`
|
|
|
|
Length int64 `json:"length"`
|
|
|
|
Precision int64 `json:"precision"`
|
|
|
|
Scale int64 `json:"scale"`
|
|
|
|
|
|
|
|
Kind kind.Kind `json:"kind"`
|
|
|
|
|
2022-12-24 07:05:26 +03:00
|
|
|
HasNullable bool `json:"has_nullable"`
|
|
|
|
HasLength bool `json:"has_length"`
|
|
|
|
HasPrecisionScale bool `json:"has_precision_scale"`
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2024-01-27 10:11:24 +03:00
|
|
|
Nullable bool `json:"nullable"`
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewColumnTypeData returns a new instance with field values
|
|
|
|
// taken from col, supplemented with the kind param.
|
2021-09-13 01:14:30 +03:00
|
|
|
func NewColumnTypeData(col *sql.ColumnType, knd kind.Kind) *ColumnTypeData {
|
2020-08-06 20:58:47 +03:00
|
|
|
ct := &ColumnTypeData{
|
|
|
|
Name: col.Name(),
|
|
|
|
DatabaseTypeName: col.DatabaseTypeName(),
|
|
|
|
ScanType: col.ScanType(),
|
2021-09-13 01:14:30 +03:00
|
|
|
Kind: knd,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ct.Nullable, ct.HasNullable = col.Nullable()
|
|
|
|
ct.Length, ct.HasLength = col.Length()
|
|
|
|
ct.Precision, ct.Scale, ct.HasPrecisionScale = col.DecimalSize()
|
|
|
|
|
|
|
|
return ct
|
|
|
|
}
|
2022-12-24 07:05:26 +03:00
|
|
|
|
|
|
|
// SetKindIfUnknown sets meta[i].kind to k, iff the kind is
|
|
|
|
// currently kind.Unknown or kind.Null. This function can be used to set
|
|
|
|
// the kind after-the-fact, which is useful for some databases
|
|
|
|
// that don't always return sufficient type info upfront.
|
2023-05-22 18:08:14 +03:00
|
|
|
func SetKindIfUnknown(meta Meta, i int, k kind.Kind) {
|
2022-12-24 07:05:26 +03:00
|
|
|
if meta[i].data.Kind == kind.Unknown || meta[i].data.Kind == kind.Null {
|
|
|
|
meta[i].data.Kind = k
|
|
|
|
}
|
|
|
|
}
|