|
| 1 | +package pgtype |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql/driver" |
| 5 | + "encoding/binary" |
| 6 | + "fmt" |
| 7 | + "math" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/jackc/pgx/v5/internal/pgio" |
| 11 | +) |
| 12 | + |
| 13 | +type Uint64Scanner interface { |
| 14 | + ScanUint64(v Uint64) error |
| 15 | +} |
| 16 | + |
| 17 | +type Uint64Valuer interface { |
| 18 | + Uint64Value() (Uint64, error) |
| 19 | +} |
| 20 | + |
| 21 | +// Uint64 is the core type that is used to represent PostgreSQL types such as XID8. |
| 22 | +type Uint64 struct { |
| 23 | + Uint64 uint64 |
| 24 | + Valid bool |
| 25 | +} |
| 26 | + |
| 27 | +func (n *Uint64) ScanUint64(v Uint64) error { |
| 28 | + *n = v |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +func (n Uint64) Uint64Value() (Uint64, error) { |
| 33 | + return n, nil |
| 34 | +} |
| 35 | + |
| 36 | +// Scan implements the database/sql Scanner interface. |
| 37 | +func (dst *Uint64) Scan(src any) error { |
| 38 | + if src == nil { |
| 39 | + *dst = Uint64{} |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + var n uint64 |
| 44 | + |
| 45 | + switch src := src.(type) { |
| 46 | + case int64: |
| 47 | + if src < 0 { |
| 48 | + return fmt.Errorf("%d is less than the minimum value for Uint64", src) |
| 49 | + } |
| 50 | + n = uint64(src) |
| 51 | + case string: |
| 52 | + un, err := strconv.ParseUint(src, 10, 64) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + n = un |
| 57 | + default: |
| 58 | + return fmt.Errorf("cannot scan %T", src) |
| 59 | + } |
| 60 | + |
| 61 | + *dst = Uint64{Uint64: n, Valid: true} |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// Value implements the database/sql/driver Valuer interface. |
| 67 | +func (src Uint64) Value() (driver.Value, error) { |
| 68 | + if !src.Valid { |
| 69 | + return nil, nil |
| 70 | + } |
| 71 | + |
| 72 | + // If the value is greater than the maximum value for int64, return it as a string instead of losing data or returning |
| 73 | + // an error. |
| 74 | + if src.Uint64 > math.MaxInt64 { |
| 75 | + return strconv.FormatUint(src.Uint64, 10), nil |
| 76 | + } |
| 77 | + |
| 78 | + return int64(src.Uint64), nil |
| 79 | +} |
| 80 | + |
| 81 | +type Uint64Codec struct{} |
| 82 | + |
| 83 | +func (Uint64Codec) FormatSupported(format int16) bool { |
| 84 | + return format == TextFormatCode || format == BinaryFormatCode |
| 85 | +} |
| 86 | + |
| 87 | +func (Uint64Codec) PreferredFormat() int16 { |
| 88 | + return BinaryFormatCode |
| 89 | +} |
| 90 | + |
| 91 | +func (Uint64Codec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan { |
| 92 | + switch format { |
| 93 | + case BinaryFormatCode: |
| 94 | + switch value.(type) { |
| 95 | + case uint64: |
| 96 | + return encodePlanUint64CodecBinaryUint64{} |
| 97 | + case Uint64Valuer: |
| 98 | + return encodePlanUint64CodecBinaryUint64Valuer{} |
| 99 | + case Int64Valuer: |
| 100 | + return encodePlanUint64CodecBinaryInt64Valuer{} |
| 101 | + } |
| 102 | + case TextFormatCode: |
| 103 | + switch value.(type) { |
| 104 | + case uint64: |
| 105 | + return encodePlanUint64CodecTextUint64{} |
| 106 | + case Int64Valuer: |
| 107 | + return encodePlanUint64CodecTextInt64Valuer{} |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +type encodePlanUint64CodecBinaryUint64 struct{} |
| 115 | + |
| 116 | +func (encodePlanUint64CodecBinaryUint64) Encode(value any, buf []byte) (newBuf []byte, err error) { |
| 117 | + v := value.(uint64) |
| 118 | + return pgio.AppendUint64(buf, v), nil |
| 119 | +} |
| 120 | + |
| 121 | +type encodePlanUint64CodecBinaryUint64Valuer struct{} |
| 122 | + |
| 123 | +func (encodePlanUint64CodecBinaryUint64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { |
| 124 | + v, err := value.(Uint64Valuer).Uint64Value() |
| 125 | + if err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + |
| 129 | + if !v.Valid { |
| 130 | + return nil, nil |
| 131 | + } |
| 132 | + |
| 133 | + return pgio.AppendUint64(buf, v.Uint64), nil |
| 134 | +} |
| 135 | + |
| 136 | +type encodePlanUint64CodecBinaryInt64Valuer struct{} |
| 137 | + |
| 138 | +func (encodePlanUint64CodecBinaryInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { |
| 139 | + v, err := value.(Int64Valuer).Int64Value() |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + |
| 144 | + if !v.Valid { |
| 145 | + return nil, nil |
| 146 | + } |
| 147 | + |
| 148 | + if v.Int64 < 0 { |
| 149 | + return nil, fmt.Errorf("%d is less than minimum value for uint64", v.Int64) |
| 150 | + } |
| 151 | + |
| 152 | + return pgio.AppendUint64(buf, uint64(v.Int64)), nil |
| 153 | +} |
| 154 | + |
| 155 | +type encodePlanUint64CodecTextUint64 struct{} |
| 156 | + |
| 157 | +func (encodePlanUint64CodecTextUint64) Encode(value any, buf []byte) (newBuf []byte, err error) { |
| 158 | + v := value.(uint64) |
| 159 | + return append(buf, strconv.FormatUint(uint64(v), 10)...), nil |
| 160 | +} |
| 161 | + |
| 162 | +type encodePlanUint64CodecTextUint64Valuer struct{} |
| 163 | + |
| 164 | +func (encodePlanUint64CodecTextUint64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { |
| 165 | + v, err := value.(Uint64Valuer).Uint64Value() |
| 166 | + if err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + |
| 170 | + if !v.Valid { |
| 171 | + return nil, nil |
| 172 | + } |
| 173 | + |
| 174 | + return append(buf, strconv.FormatUint(v.Uint64, 10)...), nil |
| 175 | +} |
| 176 | + |
| 177 | +type encodePlanUint64CodecTextInt64Valuer struct{} |
| 178 | + |
| 179 | +func (encodePlanUint64CodecTextInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { |
| 180 | + v, err := value.(Int64Valuer).Int64Value() |
| 181 | + if err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + |
| 185 | + if !v.Valid { |
| 186 | + return nil, nil |
| 187 | + } |
| 188 | + |
| 189 | + if v.Int64 < 0 { |
| 190 | + return nil, fmt.Errorf("%d is less than minimum value for uint64", v.Int64) |
| 191 | + } |
| 192 | + |
| 193 | + return append(buf, strconv.FormatInt(v.Int64, 10)...), nil |
| 194 | +} |
| 195 | + |
| 196 | +func (Uint64Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan { |
| 197 | + |
| 198 | + switch format { |
| 199 | + case BinaryFormatCode: |
| 200 | + switch target.(type) { |
| 201 | + case *uint64: |
| 202 | + return scanPlanBinaryUint64ToUint64{} |
| 203 | + case Uint64Scanner: |
| 204 | + return scanPlanBinaryUint64ToUint64Scanner{} |
| 205 | + case TextScanner: |
| 206 | + return scanPlanBinaryUint64ToTextScanner{} |
| 207 | + } |
| 208 | + case TextFormatCode: |
| 209 | + switch target.(type) { |
| 210 | + case *uint64: |
| 211 | + return scanPlanTextAnyToUint64{} |
| 212 | + case Uint64Scanner: |
| 213 | + return scanPlanTextAnyToUint64Scanner{} |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return nil |
| 218 | +} |
| 219 | + |
| 220 | +func (c Uint64Codec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) { |
| 221 | + if src == nil { |
| 222 | + return nil, nil |
| 223 | + } |
| 224 | + |
| 225 | + var n uint64 |
| 226 | + err := codecScan(c, m, oid, format, src, &n) |
| 227 | + if err != nil { |
| 228 | + return nil, err |
| 229 | + } |
| 230 | + return int64(n), nil |
| 231 | +} |
| 232 | + |
| 233 | +func (c Uint64Codec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) { |
| 234 | + if src == nil { |
| 235 | + return nil, nil |
| 236 | + } |
| 237 | + |
| 238 | + var n uint64 |
| 239 | + err := codecScan(c, m, oid, format, src, &n) |
| 240 | + if err != nil { |
| 241 | + return nil, err |
| 242 | + } |
| 243 | + return n, nil |
| 244 | +} |
| 245 | + |
| 246 | +type scanPlanBinaryUint64ToUint64 struct{} |
| 247 | + |
| 248 | +func (scanPlanBinaryUint64ToUint64) Scan(src []byte, dst any) error { |
| 249 | + if src == nil { |
| 250 | + return fmt.Errorf("cannot scan NULL into %T", dst) |
| 251 | + } |
| 252 | + |
| 253 | + if len(src) != 8 { |
| 254 | + return fmt.Errorf("invalid length for uint64: %v", len(src)) |
| 255 | + } |
| 256 | + |
| 257 | + p := (dst).(*uint64) |
| 258 | + *p = binary.BigEndian.Uint64(src) |
| 259 | + |
| 260 | + return nil |
| 261 | +} |
| 262 | + |
| 263 | +type scanPlanBinaryUint64ToUint64Scanner struct{} |
| 264 | + |
| 265 | +func (scanPlanBinaryUint64ToUint64Scanner) Scan(src []byte, dst any) error { |
| 266 | + s, ok := (dst).(Uint64Scanner) |
| 267 | + if !ok { |
| 268 | + return ErrScanTargetTypeChanged |
| 269 | + } |
| 270 | + |
| 271 | + if src == nil { |
| 272 | + return s.ScanUint64(Uint64{}) |
| 273 | + } |
| 274 | + |
| 275 | + if len(src) != 8 { |
| 276 | + return fmt.Errorf("invalid length for uint64: %v", len(src)) |
| 277 | + } |
| 278 | + |
| 279 | + n := binary.BigEndian.Uint64(src) |
| 280 | + |
| 281 | + return s.ScanUint64(Uint64{Uint64: n, Valid: true}) |
| 282 | +} |
| 283 | + |
| 284 | +type scanPlanBinaryUint64ToTextScanner struct{} |
| 285 | + |
| 286 | +func (scanPlanBinaryUint64ToTextScanner) Scan(src []byte, dst any) error { |
| 287 | + s, ok := (dst).(TextScanner) |
| 288 | + if !ok { |
| 289 | + return ErrScanTargetTypeChanged |
| 290 | + } |
| 291 | + |
| 292 | + if src == nil { |
| 293 | + return s.ScanText(Text{}) |
| 294 | + } |
| 295 | + |
| 296 | + if len(src) != 8 { |
| 297 | + return fmt.Errorf("invalid length for uint64: %v", len(src)) |
| 298 | + } |
| 299 | + |
| 300 | + n := uint64(binary.BigEndian.Uint64(src)) |
| 301 | + return s.ScanText(Text{String: strconv.FormatUint(n, 10), Valid: true}) |
| 302 | +} |
| 303 | + |
| 304 | +type scanPlanTextAnyToUint64Scanner struct{} |
| 305 | + |
| 306 | +func (scanPlanTextAnyToUint64Scanner) Scan(src []byte, dst any) error { |
| 307 | + s, ok := (dst).(Uint64Scanner) |
| 308 | + if !ok { |
| 309 | + return ErrScanTargetTypeChanged |
| 310 | + } |
| 311 | + |
| 312 | + if src == nil { |
| 313 | + return s.ScanUint64(Uint64{}) |
| 314 | + } |
| 315 | + |
| 316 | + n, err := strconv.ParseUint(string(src), 10, 64) |
| 317 | + if err != nil { |
| 318 | + return err |
| 319 | + } |
| 320 | + |
| 321 | + return s.ScanUint64(Uint64{Uint64: n, Valid: true}) |
| 322 | +} |
0 commit comments