MSP-8976 Up deps
This commit is contained in:
1 parent
b8ef777339
commit
11d705124d
603 files changed
+47715
-13202
No files matched your search
+62
-5
@@ -57,12 +57,25 @@
|
||||
//
|
||||
// # PostgreSQL Specific Data Types
|
||||
//
|
||||
// The pgtype package provides support for PostgreSQL specific types. *pgtype.Map.SQLScanner is an adapter that makes
|
||||
// these types usable as a sql.Scanner.
|
||||
// As of Go 1.27, database/sql allows drivers to implement their own scanning logic by implementing the
|
||||
// driver.RowsColumnScanner interface. This allows PostgreSQL types such as arrays to be scanned directly into Go
|
||||
// values such as slices.
|
||||
//
|
||||
// var a []int64
|
||||
// err := db.QueryRow("select '{1,2,3}'::bigint[]").Scan(&a)
|
||||
//
|
||||
// In older versions of Go, *pgtype.Map.SQLScanner can be used as an adapter that makes these types usable as a
|
||||
// sql.Scanner.
|
||||
//
|
||||
// m := pgtype.NewMap()
|
||||
// var a []int64
|
||||
// err := db.QueryRow("select '{1,2,3}'::bigint[]").Scan(m.SQLScanner(&a))
|
||||
//
|
||||
// The pgtype package provides support for PostgreSQL specific types. These types can be used directly in Go 1.27 and
|
||||
// with *pgtype.Map.SQLScanner in older Go versions.
|
||||
//
|
||||
// var r pgtype.Range[pgtype.Int4]
|
||||
// err := db.QueryRow("select int4range(1, 5)").Scan(&r)
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
@@ -659,6 +672,10 @@ func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
|
||||
|
||||
const varHeaderSize = 4
|
||||
|
||||
// noTypeModifier is the type modifier PostgreSQL reports for a column whose type
|
||||
// was used without a precision or length specifier.
|
||||
const noTypeModifier = -1
|
||||
|
||||
// ColumnTypeLength returns the length of the column type if the column is a
|
||||
// variable length type. If the column is not a variable length type ok
|
||||
// should return false.
|
||||
@@ -669,25 +686,59 @@ func (r *Rows) ColumnTypeLength(index int) (int64, bool) {
|
||||
case pgtype.TextOID, pgtype.ByteaOID:
|
||||
return math.MaxInt64, true
|
||||
case pgtype.VarcharOID, pgtype.BPCharOID:
|
||||
// An unconstrained varchar or bpchar is not limited, which
|
||||
// database/sql represents as math.MaxInt64.
|
||||
if fd.TypeModifier == -1 {
|
||||
return math.MaxInt64, true
|
||||
}
|
||||
return int64(fd.TypeModifier - varHeaderSize), true
|
||||
case pgtype.VarbitOID:
|
||||
if fd.TypeModifier == -1 {
|
||||
return math.MaxInt64, true
|
||||
}
|
||||
return int64(fd.TypeModifier), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
// ColumnTypePrecisionScale should return the precision and scale for decimal
|
||||
// types. If not applicable, ok should be false.
|
||||
// ColumnTypePrecisionScale returns the precision and scale for numeric types
|
||||
// with a declared precision. For timestamp, timestamptz, time, timetz, and interval,
|
||||
// it returns a precision of 0 and the fractional seconds precision as the scale,
|
||||
// defaulting to 6 when no fractional seconds precision was declared.
|
||||
// For unconstrained numeric and other types, ok is false.
|
||||
func (r *Rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
|
||||
fd := r.rows.FieldDescriptions()[index]
|
||||
|
||||
switch fd.DataTypeOID {
|
||||
case pgtype.NumericOID:
|
||||
// numeric without a precision specifier accepts any precision and scale,
|
||||
// so there is nothing to report.
|
||||
if fd.TypeModifier == noTypeModifier {
|
||||
return 0, 0, false
|
||||
}
|
||||
mod := fd.TypeModifier - varHeaderSize
|
||||
precision = int64((mod >> 16) & 0xffff)
|
||||
scale = int64(mod & 0xffff)
|
||||
return precision, scale, true
|
||||
case pgtype.TimestampOID, pgtype.TimestamptzOID, pgtype.TimeOID, pgtype.TimetzOID:
|
||||
// The type modifier of these types is their fractional seconds precision:
|
||||
// the number of digits kept after the decimal point, which is the scale.
|
||||
// Numeric precision does not apply to these types, so it is reported as 0.
|
||||
// Without a precision specifier they keep microseconds, the maximum
|
||||
// PostgreSQL supports.
|
||||
if fd.TypeModifier == noTypeModifier {
|
||||
return 0, 6, true
|
||||
}
|
||||
return 0, int64(fd.TypeModifier), true
|
||||
case pgtype.IntervalOID:
|
||||
// An interval's type modifier packs the field range it was declared with in
|
||||
// its high bits and its fractional seconds precision in its low 16 bits,
|
||||
// which are all set when only a range was specified.
|
||||
if fd.TypeModifier == noTypeModifier || fd.TypeModifier&0xffff == 0xffff {
|
||||
return 0, 6, true
|
||||
}
|
||||
return 0, int64(fd.TypeModifier & 0xffff), true
|
||||
default:
|
||||
return 0, 0, false
|
||||
}
|
||||
@@ -726,7 +777,9 @@ func (r *Rows) Close() error {
|
||||
return r.rows.Err()
|
||||
}
|
||||
|
||||
func (r *Rows) Next(dest []driver.Value) error {
|
||||
// initValueFuncs prepares the database/sql representation of each column. Both
|
||||
// Next and ScanColumn use these conversions so their driver.Values agree.
|
||||
func (r *Rows) initValueFuncs() {
|
||||
m := r.conn.conn.TypeMap()
|
||||
fieldDescriptions := r.rows.FieldDescriptions()
|
||||
|
||||
@@ -857,6 +910,10 @@ func (r *Rows) Next(dest []driver.Value) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Rows) Next(dest []driver.Value) error {
|
||||
r.initValueFuncs()
|
||||
|
||||
var more bool
|
||||
if r.skipNext {
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
//go:build go1.27
|
||||
|
||||
package stdlib
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"io"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
// Rows implements driver.RowsColumnScanner as of Go 1.27.
|
||||
var _ driver.RowsColumnScanner = (*Rows)(nil)
|
||||
|
||||
// NextRow implements the driver.RowsColumnScanner interface. It advances to the
|
||||
// next row of data and returns io.EOF when there are no more rows.
|
||||
func (r *Rows) NextRow() error {
|
||||
var more bool
|
||||
if r.skipNext {
|
||||
more = r.skipNextMore
|
||||
r.skipNext = false
|
||||
} else {
|
||||
more = r.rows.Next()
|
||||
}
|
||||
|
||||
if !more {
|
||||
if err := r.rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return io.EOF
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ScanColumn implements the driver.RowsColumnScanner interface. It preserves
|
||||
// database/sql conversions for scalar destinations and sql.Scanner implementations.
|
||||
// Other destinations, such as Go slices, pgtype.Array, and pgtype.Range, are
|
||||
// scanned directly using the pgx type map.
|
||||
func (r *Rows) ScanColumn(scanCtx driver.ScanContext, index int, dest any) error {
|
||||
if dest == nil {
|
||||
return errors.New("destination not a pointer")
|
||||
}
|
||||
rv := reflect.ValueOf(dest)
|
||||
if rv.Kind() == reflect.Pointer && rv.IsNil() {
|
||||
return errors.New("destination pointer is nil")
|
||||
}
|
||||
|
||||
src := r.rows.RawValues()[index]
|
||||
if isSQLScanDestination(rv.Type()) {
|
||||
var value driver.Value
|
||||
if src != nil {
|
||||
r.initValueFuncs()
|
||||
var err error
|
||||
value, err = r.valueFuncs[index](src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return sql.ConvertAssign(scanCtx, dest, value)
|
||||
}
|
||||
|
||||
m := r.conn.conn.TypeMap()
|
||||
fd := r.rows.FieldDescriptions()[index]
|
||||
return m.Scan(fd.DataTypeOID, fd.Format, src, dest)
|
||||
}
|
||||
|
||||
// Map from reflect.Type -> bool.
|
||||
var sqlScanDestinationCache sync.Map
|
||||
|
||||
// isSQLScanDestination includes named scalar types and nullable pointers to
|
||||
// database/sql destinations. Select the conversion before scanning: retrying a
|
||||
// failed scan could call user code twice or discard its error.
|
||||
func isSQLScanDestination(t reflect.Type) bool {
|
||||
if cached, ok := sqlScanDestinationCache.Load(t); ok {
|
||||
return cached.(bool)
|
||||
}
|
||||
|
||||
result := isSQLScanDestinationUncached(t)
|
||||
sqlScanDestinationCache.Store(t, result)
|
||||
return result
|
||||
}
|
||||
|
||||
func isSQLScanDestinationUncached(t reflect.Type) bool {
|
||||
var namedPointers map[reflect.Type]bool
|
||||
for {
|
||||
if t.Implements(reflect.TypeFor[sql.Scanner]()) {
|
||||
return true
|
||||
}
|
||||
// These interfaces distinguish native byte slices, such as
|
||||
// FlatArray[byte] and PreallocBytes, from ordinary []byte destinations.
|
||||
if t.Implements(reflect.TypeFor[pgtype.ArraySetter]()) || t.Implements(reflect.TypeFor[pgtype.BytesScanner]()) {
|
||||
return false
|
||||
}
|
||||
if t.Kind() != reflect.Pointer {
|
||||
break
|
||||
}
|
||||
// Only defined pointer types can form a cycle without reaching a
|
||||
// non-pointer type. Leave those to pgx's bounded scan planning.
|
||||
if t.Name() != "" {
|
||||
if namedPointers[t] {
|
||||
return false
|
||||
}
|
||||
if namedPointers == nil {
|
||||
namedPointers = make(map[reflect.Type]bool)
|
||||
}
|
||||
namedPointers[t] = true
|
||||
}
|
||||
t = t.Elem()
|
||||
}
|
||||
|
||||
switch t.Kind() {
|
||||
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64, reflect.String, reflect.Interface:
|
||||
return true
|
||||
case reflect.Slice:
|
||||
return t.Elem().Kind() == reflect.Uint8
|
||||
case reflect.Struct:
|
||||
return t.ConvertibleTo(reflect.TypeFor[time.Time]())
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user