github.com/jackc/pgx/v5@v5.5.5/pgtype/int.go (about) 1 // Do not edit. Generated from pgtype/int.go.erb 2 package pgtype 3 4 import ( 5 "database/sql/driver" 6 "encoding/binary" 7 "encoding/json" 8 "fmt" 9 "math" 10 "strconv" 11 12 "github.com/jackc/pgx/v5/internal/pgio" 13 ) 14 15 type Int64Scanner interface { 16 ScanInt64(Int8) error 17 } 18 19 type Int64Valuer interface { 20 Int64Value() (Int8, error) 21 } 22 23 type Int2 struct { 24 Int16 int16 25 Valid bool 26 } 27 28 // ScanInt64 implements the Int64Scanner interface. 29 func (dst *Int2) ScanInt64(n Int8) error { 30 if !n.Valid { 31 *dst = Int2{} 32 return nil 33 } 34 35 if n.Int64 < math.MinInt16 { 36 return fmt.Errorf("%d is less than minimum value for Int2", n.Int64) 37 } 38 if n.Int64 > math.MaxInt16 { 39 return fmt.Errorf("%d is greater than maximum value for Int2", n.Int64) 40 } 41 *dst = Int2{Int16: int16(n.Int64), Valid: true} 42 43 return nil 44 } 45 46 func (n Int2) Int64Value() (Int8, error) { 47 return Int8{Int64: int64(n.Int16), Valid: n.Valid}, nil 48 } 49 50 // Scan implements the database/sql Scanner interface. 51 func (dst *Int2) Scan(src any) error { 52 if src == nil { 53 *dst = Int2{} 54 return nil 55 } 56 57 var n int64 58 59 switch src := src.(type) { 60 case int64: 61 n = src 62 case string: 63 var err error 64 n, err = strconv.ParseInt(src, 10, 16) 65 if err != nil { 66 return err 67 } 68 case []byte: 69 var err error 70 n, err = strconv.ParseInt(string(src), 10, 16) 71 if err != nil { 72 return err 73 } 74 default: 75 return fmt.Errorf("cannot scan %T", src) 76 } 77 78 if n < math.MinInt16 { 79 return fmt.Errorf("%d is greater than maximum value for Int2", n) 80 } 81 if n > math.MaxInt16 { 82 return fmt.Errorf("%d is greater than maximum value for Int2", n) 83 } 84 *dst = Int2{Int16: int16(n), Valid: true} 85 86 return nil 87 } 88 89 // Value implements the database/sql/driver Valuer interface. 90 func (src Int2) Value() (driver.Value, error) { 91 if !src.Valid { 92 return nil, nil 93 } 94 return int64(src.Int16), nil 95 } 96 97 func (src Int2) MarshalJSON() ([]byte, error) { 98 if !src.Valid { 99 return []byte("null"), nil 100 } 101 return []byte(strconv.FormatInt(int64(src.Int16), 10)), nil 102 } 103 104 func (dst *Int2) UnmarshalJSON(b []byte) error { 105 var n *int16 106 err := json.Unmarshal(b, &n) 107 if err != nil { 108 return err 109 } 110 111 if n == nil { 112 *dst = Int2{} 113 } else { 114 *dst = Int2{Int16: *n, Valid: true} 115 } 116 117 return nil 118 } 119 120 type Int2Codec struct{} 121 122 func (Int2Codec) FormatSupported(format int16) bool { 123 return format == TextFormatCode || format == BinaryFormatCode 124 } 125 126 func (Int2Codec) PreferredFormat() int16 { 127 return BinaryFormatCode 128 } 129 130 func (Int2Codec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan { 131 switch format { 132 case BinaryFormatCode: 133 switch value.(type) { 134 case int16: 135 return encodePlanInt2CodecBinaryInt16{} 136 case Int64Valuer: 137 return encodePlanInt2CodecBinaryInt64Valuer{} 138 } 139 case TextFormatCode: 140 switch value.(type) { 141 case int16: 142 return encodePlanInt2CodecTextInt16{} 143 case Int64Valuer: 144 return encodePlanInt2CodecTextInt64Valuer{} 145 } 146 } 147 148 return nil 149 } 150 151 type encodePlanInt2CodecBinaryInt16 struct{} 152 153 func (encodePlanInt2CodecBinaryInt16) Encode(value any, buf []byte) (newBuf []byte, err error) { 154 n := value.(int16) 155 return pgio.AppendInt16(buf, int16(n)), nil 156 } 157 158 type encodePlanInt2CodecTextInt16 struct{} 159 160 func (encodePlanInt2CodecTextInt16) Encode(value any, buf []byte) (newBuf []byte, err error) { 161 n := value.(int16) 162 return append(buf, strconv.FormatInt(int64(n), 10)...), nil 163 } 164 165 type encodePlanInt2CodecBinaryInt64Valuer struct{} 166 167 func (encodePlanInt2CodecBinaryInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { 168 n, err := value.(Int64Valuer).Int64Value() 169 if err != nil { 170 return nil, err 171 } 172 173 if !n.Valid { 174 return nil, nil 175 } 176 177 if n.Int64 > math.MaxInt16 { 178 return nil, fmt.Errorf("%d is greater than maximum value for int2", n.Int64) 179 } 180 if n.Int64 < math.MinInt16 { 181 return nil, fmt.Errorf("%d is less than minimum value for int2", n.Int64) 182 } 183 184 return pgio.AppendInt16(buf, int16(n.Int64)), nil 185 } 186 187 type encodePlanInt2CodecTextInt64Valuer struct{} 188 189 func (encodePlanInt2CodecTextInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { 190 n, err := value.(Int64Valuer).Int64Value() 191 if err != nil { 192 return nil, err 193 } 194 195 if !n.Valid { 196 return nil, nil 197 } 198 199 if n.Int64 > math.MaxInt16 { 200 return nil, fmt.Errorf("%d is greater than maximum value for int2", n.Int64) 201 } 202 if n.Int64 < math.MinInt16 { 203 return nil, fmt.Errorf("%d is less than minimum value for int2", n.Int64) 204 } 205 206 return append(buf, strconv.FormatInt(n.Int64, 10)...), nil 207 } 208 209 func (Int2Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan { 210 211 switch format { 212 case BinaryFormatCode: 213 switch target.(type) { 214 case *int8: 215 return scanPlanBinaryInt2ToInt8{} 216 case *int16: 217 return scanPlanBinaryInt2ToInt16{} 218 case *int32: 219 return scanPlanBinaryInt2ToInt32{} 220 case *int64: 221 return scanPlanBinaryInt2ToInt64{} 222 case *int: 223 return scanPlanBinaryInt2ToInt{} 224 case *uint8: 225 return scanPlanBinaryInt2ToUint8{} 226 case *uint16: 227 return scanPlanBinaryInt2ToUint16{} 228 case *uint32: 229 return scanPlanBinaryInt2ToUint32{} 230 case *uint64: 231 return scanPlanBinaryInt2ToUint64{} 232 case *uint: 233 return scanPlanBinaryInt2ToUint{} 234 case Int64Scanner: 235 return scanPlanBinaryInt2ToInt64Scanner{} 236 case TextScanner: 237 return scanPlanBinaryInt2ToTextScanner{} 238 } 239 case TextFormatCode: 240 switch target.(type) { 241 case *int8: 242 return scanPlanTextAnyToInt8{} 243 case *int16: 244 return scanPlanTextAnyToInt16{} 245 case *int32: 246 return scanPlanTextAnyToInt32{} 247 case *int64: 248 return scanPlanTextAnyToInt64{} 249 case *int: 250 return scanPlanTextAnyToInt{} 251 case *uint8: 252 return scanPlanTextAnyToUint8{} 253 case *uint16: 254 return scanPlanTextAnyToUint16{} 255 case *uint32: 256 return scanPlanTextAnyToUint32{} 257 case *uint64: 258 return scanPlanTextAnyToUint64{} 259 case *uint: 260 return scanPlanTextAnyToUint{} 261 case Int64Scanner: 262 return scanPlanTextAnyToInt64Scanner{} 263 } 264 } 265 266 return nil 267 } 268 269 func (c Int2Codec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) { 270 if src == nil { 271 return nil, nil 272 } 273 274 var n int64 275 err := codecScan(c, m, oid, format, src, &n) 276 if err != nil { 277 return nil, err 278 } 279 return n, nil 280 } 281 282 func (c Int2Codec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) { 283 if src == nil { 284 return nil, nil 285 } 286 287 var n int16 288 err := codecScan(c, m, oid, format, src, &n) 289 if err != nil { 290 return nil, err 291 } 292 return n, nil 293 } 294 295 type scanPlanBinaryInt2ToInt8 struct{} 296 297 func (scanPlanBinaryInt2ToInt8) Scan(src []byte, dst any) error { 298 if src == nil { 299 return fmt.Errorf("cannot scan NULL into %T", dst) 300 } 301 302 if len(src) != 2 { 303 return fmt.Errorf("invalid length for int2: %v", len(src)) 304 } 305 306 p, ok := (dst).(*int8) 307 if !ok { 308 return ErrScanTargetTypeChanged 309 } 310 311 n := int16(binary.BigEndian.Uint16(src)) 312 if n < math.MinInt8 { 313 return fmt.Errorf("%d is less than minimum value for int8", n) 314 } else if n > math.MaxInt8 { 315 return fmt.Errorf("%d is greater than maximum value for int8", n) 316 } 317 318 *p = int8(n) 319 320 return nil 321 } 322 323 type scanPlanBinaryInt2ToUint8 struct{} 324 325 func (scanPlanBinaryInt2ToUint8) Scan(src []byte, dst any) error { 326 if src == nil { 327 return fmt.Errorf("cannot scan NULL into %T", dst) 328 } 329 330 if len(src) != 2 { 331 return fmt.Errorf("invalid length for uint2: %v", len(src)) 332 } 333 334 p, ok := (dst).(*uint8) 335 if !ok { 336 return ErrScanTargetTypeChanged 337 } 338 339 n := int16(binary.BigEndian.Uint16(src)) 340 if n < 0 { 341 return fmt.Errorf("%d is less than minimum value for uint8", n) 342 } 343 344 if n > math.MaxUint8 { 345 return fmt.Errorf("%d is greater than maximum value for uint8", n) 346 } 347 348 *p = uint8(n) 349 350 return nil 351 } 352 353 type scanPlanBinaryInt2ToInt16 struct{} 354 355 func (scanPlanBinaryInt2ToInt16) Scan(src []byte, dst any) error { 356 if src == nil { 357 return fmt.Errorf("cannot scan NULL into %T", dst) 358 } 359 360 if len(src) != 2 { 361 return fmt.Errorf("invalid length for int2: %v", len(src)) 362 } 363 364 p, ok := (dst).(*int16) 365 if !ok { 366 return ErrScanTargetTypeChanged 367 } 368 369 *p = int16(binary.BigEndian.Uint16(src)) 370 371 return nil 372 } 373 374 type scanPlanBinaryInt2ToUint16 struct{} 375 376 func (scanPlanBinaryInt2ToUint16) Scan(src []byte, dst any) error { 377 if src == nil { 378 return fmt.Errorf("cannot scan NULL into %T", dst) 379 } 380 381 if len(src) != 2 { 382 return fmt.Errorf("invalid length for uint2: %v", len(src)) 383 } 384 385 p, ok := (dst).(*uint16) 386 if !ok { 387 return ErrScanTargetTypeChanged 388 } 389 390 n := int16(binary.BigEndian.Uint16(src)) 391 if n < 0 { 392 return fmt.Errorf("%d is less than minimum value for uint16", n) 393 } 394 395 *p = uint16(n) 396 397 return nil 398 } 399 400 type scanPlanBinaryInt2ToInt32 struct{} 401 402 func (scanPlanBinaryInt2ToInt32) Scan(src []byte, dst any) error { 403 if src == nil { 404 return fmt.Errorf("cannot scan NULL into %T", dst) 405 } 406 407 if len(src) != 2 { 408 return fmt.Errorf("invalid length for int2: %v", len(src)) 409 } 410 411 p, ok := (dst).(*int32) 412 if !ok { 413 return ErrScanTargetTypeChanged 414 } 415 416 *p = int32(int16(binary.BigEndian.Uint16(src))) 417 418 return nil 419 } 420 421 type scanPlanBinaryInt2ToUint32 struct{} 422 423 func (scanPlanBinaryInt2ToUint32) Scan(src []byte, dst any) error { 424 if src == nil { 425 return fmt.Errorf("cannot scan NULL into %T", dst) 426 } 427 428 if len(src) != 2 { 429 return fmt.Errorf("invalid length for uint2: %v", len(src)) 430 } 431 432 p, ok := (dst).(*uint32) 433 if !ok { 434 return ErrScanTargetTypeChanged 435 } 436 437 n := int16(binary.BigEndian.Uint16(src)) 438 if n < 0 { 439 return fmt.Errorf("%d is less than minimum value for uint32", n) 440 } 441 442 *p = uint32(n) 443 444 return nil 445 } 446 447 type scanPlanBinaryInt2ToInt64 struct{} 448 449 func (scanPlanBinaryInt2ToInt64) Scan(src []byte, dst any) error { 450 if src == nil { 451 return fmt.Errorf("cannot scan NULL into %T", dst) 452 } 453 454 if len(src) != 2 { 455 return fmt.Errorf("invalid length for int2: %v", len(src)) 456 } 457 458 p, ok := (dst).(*int64) 459 if !ok { 460 return ErrScanTargetTypeChanged 461 } 462 463 *p = int64(int16(binary.BigEndian.Uint16(src))) 464 465 return nil 466 } 467 468 type scanPlanBinaryInt2ToUint64 struct{} 469 470 func (scanPlanBinaryInt2ToUint64) Scan(src []byte, dst any) error { 471 if src == nil { 472 return fmt.Errorf("cannot scan NULL into %T", dst) 473 } 474 475 if len(src) != 2 { 476 return fmt.Errorf("invalid length for uint2: %v", len(src)) 477 } 478 479 p, ok := (dst).(*uint64) 480 if !ok { 481 return ErrScanTargetTypeChanged 482 } 483 484 n := int16(binary.BigEndian.Uint16(src)) 485 if n < 0 { 486 return fmt.Errorf("%d is less than minimum value for uint64", n) 487 } 488 489 *p = uint64(n) 490 491 return nil 492 } 493 494 type scanPlanBinaryInt2ToInt struct{} 495 496 func (scanPlanBinaryInt2ToInt) Scan(src []byte, dst any) error { 497 if src == nil { 498 return fmt.Errorf("cannot scan NULL into %T", dst) 499 } 500 501 if len(src) != 2 { 502 return fmt.Errorf("invalid length for int2: %v", len(src)) 503 } 504 505 p, ok := (dst).(*int) 506 if !ok { 507 return ErrScanTargetTypeChanged 508 } 509 510 *p = int(int16(binary.BigEndian.Uint16(src))) 511 512 return nil 513 } 514 515 type scanPlanBinaryInt2ToUint struct{} 516 517 func (scanPlanBinaryInt2ToUint) Scan(src []byte, dst any) error { 518 if src == nil { 519 return fmt.Errorf("cannot scan NULL into %T", dst) 520 } 521 522 if len(src) != 2 { 523 return fmt.Errorf("invalid length for uint2: %v", len(src)) 524 } 525 526 p, ok := (dst).(*uint) 527 if !ok { 528 return ErrScanTargetTypeChanged 529 } 530 531 n := int64(int16(binary.BigEndian.Uint16(src))) 532 if n < 0 { 533 return fmt.Errorf("%d is less than minimum value for uint", n) 534 } 535 536 *p = uint(n) 537 538 return nil 539 } 540 541 type scanPlanBinaryInt2ToInt64Scanner struct{} 542 543 func (scanPlanBinaryInt2ToInt64Scanner) Scan(src []byte, dst any) error { 544 s, ok := (dst).(Int64Scanner) 545 if !ok { 546 return ErrScanTargetTypeChanged 547 } 548 549 if src == nil { 550 return s.ScanInt64(Int8{}) 551 } 552 553 if len(src) != 2 { 554 return fmt.Errorf("invalid length for int2: %v", len(src)) 555 } 556 557 n := int64(int16(binary.BigEndian.Uint16(src))) 558 559 return s.ScanInt64(Int8{Int64: n, Valid: true}) 560 } 561 562 type scanPlanBinaryInt2ToTextScanner struct{} 563 564 func (scanPlanBinaryInt2ToTextScanner) Scan(src []byte, dst any) error { 565 s, ok := (dst).(TextScanner) 566 if !ok { 567 return ErrScanTargetTypeChanged 568 } 569 570 if src == nil { 571 return s.ScanText(Text{}) 572 } 573 574 if len(src) != 2 { 575 return fmt.Errorf("invalid length for int2: %v", len(src)) 576 } 577 578 n := int64(int16(binary.BigEndian.Uint16(src))) 579 580 return s.ScanText(Text{String: strconv.FormatInt(n, 10), Valid: true}) 581 } 582 583 type Int4 struct { 584 Int32 int32 585 Valid bool 586 } 587 588 // ScanInt64 implements the Int64Scanner interface. 589 func (dst *Int4) ScanInt64(n Int8) error { 590 if !n.Valid { 591 *dst = Int4{} 592 return nil 593 } 594 595 if n.Int64 < math.MinInt32 { 596 return fmt.Errorf("%d is less than minimum value for Int4", n.Int64) 597 } 598 if n.Int64 > math.MaxInt32 { 599 return fmt.Errorf("%d is greater than maximum value for Int4", n.Int64) 600 } 601 *dst = Int4{Int32: int32(n.Int64), Valid: true} 602 603 return nil 604 } 605 606 func (n Int4) Int64Value() (Int8, error) { 607 return Int8{Int64: int64(n.Int32), Valid: n.Valid}, nil 608 } 609 610 // Scan implements the database/sql Scanner interface. 611 func (dst *Int4) Scan(src any) error { 612 if src == nil { 613 *dst = Int4{} 614 return nil 615 } 616 617 var n int64 618 619 switch src := src.(type) { 620 case int64: 621 n = src 622 case string: 623 var err error 624 n, err = strconv.ParseInt(src, 10, 32) 625 if err != nil { 626 return err 627 } 628 case []byte: 629 var err error 630 n, err = strconv.ParseInt(string(src), 10, 32) 631 if err != nil { 632 return err 633 } 634 default: 635 return fmt.Errorf("cannot scan %T", src) 636 } 637 638 if n < math.MinInt32 { 639 return fmt.Errorf("%d is greater than maximum value for Int4", n) 640 } 641 if n > math.MaxInt32 { 642 return fmt.Errorf("%d is greater than maximum value for Int4", n) 643 } 644 *dst = Int4{Int32: int32(n), Valid: true} 645 646 return nil 647 } 648 649 // Value implements the database/sql/driver Valuer interface. 650 func (src Int4) Value() (driver.Value, error) { 651 if !src.Valid { 652 return nil, nil 653 } 654 return int64(src.Int32), nil 655 } 656 657 func (src Int4) MarshalJSON() ([]byte, error) { 658 if !src.Valid { 659 return []byte("null"), nil 660 } 661 return []byte(strconv.FormatInt(int64(src.Int32), 10)), nil 662 } 663 664 func (dst *Int4) UnmarshalJSON(b []byte) error { 665 var n *int32 666 err := json.Unmarshal(b, &n) 667 if err != nil { 668 return err 669 } 670 671 if n == nil { 672 *dst = Int4{} 673 } else { 674 *dst = Int4{Int32: *n, Valid: true} 675 } 676 677 return nil 678 } 679 680 type Int4Codec struct{} 681 682 func (Int4Codec) FormatSupported(format int16) bool { 683 return format == TextFormatCode || format == BinaryFormatCode 684 } 685 686 func (Int4Codec) PreferredFormat() int16 { 687 return BinaryFormatCode 688 } 689 690 func (Int4Codec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan { 691 switch format { 692 case BinaryFormatCode: 693 switch value.(type) { 694 case int32: 695 return encodePlanInt4CodecBinaryInt32{} 696 case Int64Valuer: 697 return encodePlanInt4CodecBinaryInt64Valuer{} 698 } 699 case TextFormatCode: 700 switch value.(type) { 701 case int32: 702 return encodePlanInt4CodecTextInt32{} 703 case Int64Valuer: 704 return encodePlanInt4CodecTextInt64Valuer{} 705 } 706 } 707 708 return nil 709 } 710 711 type encodePlanInt4CodecBinaryInt32 struct{} 712 713 func (encodePlanInt4CodecBinaryInt32) Encode(value any, buf []byte) (newBuf []byte, err error) { 714 n := value.(int32) 715 return pgio.AppendInt32(buf, int32(n)), nil 716 } 717 718 type encodePlanInt4CodecTextInt32 struct{} 719 720 func (encodePlanInt4CodecTextInt32) Encode(value any, buf []byte) (newBuf []byte, err error) { 721 n := value.(int32) 722 return append(buf, strconv.FormatInt(int64(n), 10)...), nil 723 } 724 725 type encodePlanInt4CodecBinaryInt64Valuer struct{} 726 727 func (encodePlanInt4CodecBinaryInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { 728 n, err := value.(Int64Valuer).Int64Value() 729 if err != nil { 730 return nil, err 731 } 732 733 if !n.Valid { 734 return nil, nil 735 } 736 737 if n.Int64 > math.MaxInt32 { 738 return nil, fmt.Errorf("%d is greater than maximum value for int4", n.Int64) 739 } 740 if n.Int64 < math.MinInt32 { 741 return nil, fmt.Errorf("%d is less than minimum value for int4", n.Int64) 742 } 743 744 return pgio.AppendInt32(buf, int32(n.Int64)), nil 745 } 746 747 type encodePlanInt4CodecTextInt64Valuer struct{} 748 749 func (encodePlanInt4CodecTextInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { 750 n, err := value.(Int64Valuer).Int64Value() 751 if err != nil { 752 return nil, err 753 } 754 755 if !n.Valid { 756 return nil, nil 757 } 758 759 if n.Int64 > math.MaxInt32 { 760 return nil, fmt.Errorf("%d is greater than maximum value for int4", n.Int64) 761 } 762 if n.Int64 < math.MinInt32 { 763 return nil, fmt.Errorf("%d is less than minimum value for int4", n.Int64) 764 } 765 766 return append(buf, strconv.FormatInt(n.Int64, 10)...), nil 767 } 768 769 func (Int4Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan { 770 771 switch format { 772 case BinaryFormatCode: 773 switch target.(type) { 774 case *int8: 775 return scanPlanBinaryInt4ToInt8{} 776 case *int16: 777 return scanPlanBinaryInt4ToInt16{} 778 case *int32: 779 return scanPlanBinaryInt4ToInt32{} 780 case *int64: 781 return scanPlanBinaryInt4ToInt64{} 782 case *int: 783 return scanPlanBinaryInt4ToInt{} 784 case *uint8: 785 return scanPlanBinaryInt4ToUint8{} 786 case *uint16: 787 return scanPlanBinaryInt4ToUint16{} 788 case *uint32: 789 return scanPlanBinaryInt4ToUint32{} 790 case *uint64: 791 return scanPlanBinaryInt4ToUint64{} 792 case *uint: 793 return scanPlanBinaryInt4ToUint{} 794 case Int64Scanner: 795 return scanPlanBinaryInt4ToInt64Scanner{} 796 case TextScanner: 797 return scanPlanBinaryInt4ToTextScanner{} 798 } 799 case TextFormatCode: 800 switch target.(type) { 801 case *int8: 802 return scanPlanTextAnyToInt8{} 803 case *int16: 804 return scanPlanTextAnyToInt16{} 805 case *int32: 806 return scanPlanTextAnyToInt32{} 807 case *int64: 808 return scanPlanTextAnyToInt64{} 809 case *int: 810 return scanPlanTextAnyToInt{} 811 case *uint8: 812 return scanPlanTextAnyToUint8{} 813 case *uint16: 814 return scanPlanTextAnyToUint16{} 815 case *uint32: 816 return scanPlanTextAnyToUint32{} 817 case *uint64: 818 return scanPlanTextAnyToUint64{} 819 case *uint: 820 return scanPlanTextAnyToUint{} 821 case Int64Scanner: 822 return scanPlanTextAnyToInt64Scanner{} 823 } 824 } 825 826 return nil 827 } 828 829 func (c Int4Codec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) { 830 if src == nil { 831 return nil, nil 832 } 833 834 var n int64 835 err := codecScan(c, m, oid, format, src, &n) 836 if err != nil { 837 return nil, err 838 } 839 return n, nil 840 } 841 842 func (c Int4Codec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) { 843 if src == nil { 844 return nil, nil 845 } 846 847 var n int32 848 err := codecScan(c, m, oid, format, src, &n) 849 if err != nil { 850 return nil, err 851 } 852 return n, nil 853 } 854 855 type scanPlanBinaryInt4ToInt8 struct{} 856 857 func (scanPlanBinaryInt4ToInt8) Scan(src []byte, dst any) error { 858 if src == nil { 859 return fmt.Errorf("cannot scan NULL into %T", dst) 860 } 861 862 if len(src) != 4 { 863 return fmt.Errorf("invalid length for int4: %v", len(src)) 864 } 865 866 p, ok := (dst).(*int8) 867 if !ok { 868 return ErrScanTargetTypeChanged 869 } 870 871 n := int32(binary.BigEndian.Uint32(src)) 872 if n < math.MinInt8 { 873 return fmt.Errorf("%d is less than minimum value for int8", n) 874 } else if n > math.MaxInt8 { 875 return fmt.Errorf("%d is greater than maximum value for int8", n) 876 } 877 878 *p = int8(n) 879 880 return nil 881 } 882 883 type scanPlanBinaryInt4ToUint8 struct{} 884 885 func (scanPlanBinaryInt4ToUint8) Scan(src []byte, dst any) error { 886 if src == nil { 887 return fmt.Errorf("cannot scan NULL into %T", dst) 888 } 889 890 if len(src) != 4 { 891 return fmt.Errorf("invalid length for uint4: %v", len(src)) 892 } 893 894 p, ok := (dst).(*uint8) 895 if !ok { 896 return ErrScanTargetTypeChanged 897 } 898 899 n := int32(binary.BigEndian.Uint32(src)) 900 if n < 0 { 901 return fmt.Errorf("%d is less than minimum value for uint8", n) 902 } 903 904 if n > math.MaxUint8 { 905 return fmt.Errorf("%d is greater than maximum value for uint8", n) 906 } 907 908 *p = uint8(n) 909 910 return nil 911 } 912 913 type scanPlanBinaryInt4ToInt16 struct{} 914 915 func (scanPlanBinaryInt4ToInt16) Scan(src []byte, dst any) error { 916 if src == nil { 917 return fmt.Errorf("cannot scan NULL into %T", dst) 918 } 919 920 if len(src) != 4 { 921 return fmt.Errorf("invalid length for int4: %v", len(src)) 922 } 923 924 p, ok := (dst).(*int16) 925 if !ok { 926 return ErrScanTargetTypeChanged 927 } 928 929 n := int32(binary.BigEndian.Uint32(src)) 930 if n < math.MinInt16 { 931 return fmt.Errorf("%d is less than minimum value for int16", n) 932 } else if n > math.MaxInt16 { 933 return fmt.Errorf("%d is greater than maximum value for int16", n) 934 } 935 936 *p = int16(n) 937 938 return nil 939 } 940 941 type scanPlanBinaryInt4ToUint16 struct{} 942 943 func (scanPlanBinaryInt4ToUint16) Scan(src []byte, dst any) error { 944 if src == nil { 945 return fmt.Errorf("cannot scan NULL into %T", dst) 946 } 947 948 if len(src) != 4 { 949 return fmt.Errorf("invalid length for uint4: %v", len(src)) 950 } 951 952 p, ok := (dst).(*uint16) 953 if !ok { 954 return ErrScanTargetTypeChanged 955 } 956 957 n := int32(binary.BigEndian.Uint32(src)) 958 if n < 0 { 959 return fmt.Errorf("%d is less than minimum value for uint16", n) 960 } 961 962 if n > math.MaxUint16 { 963 return fmt.Errorf("%d is greater than maximum value for uint16", n) 964 } 965 966 *p = uint16(n) 967 968 return nil 969 } 970 971 type scanPlanBinaryInt4ToInt32 struct{} 972 973 func (scanPlanBinaryInt4ToInt32) Scan(src []byte, dst any) error { 974 if src == nil { 975 return fmt.Errorf("cannot scan NULL into %T", dst) 976 } 977 978 if len(src) != 4 { 979 return fmt.Errorf("invalid length for int4: %v", len(src)) 980 } 981 982 p, ok := (dst).(*int32) 983 if !ok { 984 return ErrScanTargetTypeChanged 985 } 986 987 *p = int32(binary.BigEndian.Uint32(src)) 988 989 return nil 990 } 991 992 type scanPlanBinaryInt4ToUint32 struct{} 993 994 func (scanPlanBinaryInt4ToUint32) Scan(src []byte, dst any) error { 995 if src == nil { 996 return fmt.Errorf("cannot scan NULL into %T", dst) 997 } 998 999 if len(src) != 4 { 1000 return fmt.Errorf("invalid length for uint4: %v", len(src)) 1001 } 1002 1003 p, ok := (dst).(*uint32) 1004 if !ok { 1005 return ErrScanTargetTypeChanged 1006 } 1007 1008 n := int32(binary.BigEndian.Uint32(src)) 1009 if n < 0 { 1010 return fmt.Errorf("%d is less than minimum value for uint32", n) 1011 } 1012 1013 *p = uint32(n) 1014 1015 return nil 1016 } 1017 1018 type scanPlanBinaryInt4ToInt64 struct{} 1019 1020 func (scanPlanBinaryInt4ToInt64) Scan(src []byte, dst any) error { 1021 if src == nil { 1022 return fmt.Errorf("cannot scan NULL into %T", dst) 1023 } 1024 1025 if len(src) != 4 { 1026 return fmt.Errorf("invalid length for int4: %v", len(src)) 1027 } 1028 1029 p, ok := (dst).(*int64) 1030 if !ok { 1031 return ErrScanTargetTypeChanged 1032 } 1033 1034 *p = int64(int32(binary.BigEndian.Uint32(src))) 1035 1036 return nil 1037 } 1038 1039 type scanPlanBinaryInt4ToUint64 struct{} 1040 1041 func (scanPlanBinaryInt4ToUint64) Scan(src []byte, dst any) error { 1042 if src == nil { 1043 return fmt.Errorf("cannot scan NULL into %T", dst) 1044 } 1045 1046 if len(src) != 4 { 1047 return fmt.Errorf("invalid length for uint4: %v", len(src)) 1048 } 1049 1050 p, ok := (dst).(*uint64) 1051 if !ok { 1052 return ErrScanTargetTypeChanged 1053 } 1054 1055 n := int32(binary.BigEndian.Uint32(src)) 1056 if n < 0 { 1057 return fmt.Errorf("%d is less than minimum value for uint64", n) 1058 } 1059 1060 *p = uint64(n) 1061 1062 return nil 1063 } 1064 1065 type scanPlanBinaryInt4ToInt struct{} 1066 1067 func (scanPlanBinaryInt4ToInt) Scan(src []byte, dst any) error { 1068 if src == nil { 1069 return fmt.Errorf("cannot scan NULL into %T", dst) 1070 } 1071 1072 if len(src) != 4 { 1073 return fmt.Errorf("invalid length for int4: %v", len(src)) 1074 } 1075 1076 p, ok := (dst).(*int) 1077 if !ok { 1078 return ErrScanTargetTypeChanged 1079 } 1080 1081 *p = int(int32(binary.BigEndian.Uint32(src))) 1082 1083 return nil 1084 } 1085 1086 type scanPlanBinaryInt4ToUint struct{} 1087 1088 func (scanPlanBinaryInt4ToUint) Scan(src []byte, dst any) error { 1089 if src == nil { 1090 return fmt.Errorf("cannot scan NULL into %T", dst) 1091 } 1092 1093 if len(src) != 4 { 1094 return fmt.Errorf("invalid length for uint4: %v", len(src)) 1095 } 1096 1097 p, ok := (dst).(*uint) 1098 if !ok { 1099 return ErrScanTargetTypeChanged 1100 } 1101 1102 n := int64(int32(binary.BigEndian.Uint32(src))) 1103 if n < 0 { 1104 return fmt.Errorf("%d is less than minimum value for uint", n) 1105 } 1106 1107 *p = uint(n) 1108 1109 return nil 1110 } 1111 1112 type scanPlanBinaryInt4ToInt64Scanner struct{} 1113 1114 func (scanPlanBinaryInt4ToInt64Scanner) Scan(src []byte, dst any) error { 1115 s, ok := (dst).(Int64Scanner) 1116 if !ok { 1117 return ErrScanTargetTypeChanged 1118 } 1119 1120 if src == nil { 1121 return s.ScanInt64(Int8{}) 1122 } 1123 1124 if len(src) != 4 { 1125 return fmt.Errorf("invalid length for int4: %v", len(src)) 1126 } 1127 1128 n := int64(int32(binary.BigEndian.Uint32(src))) 1129 1130 return s.ScanInt64(Int8{Int64: n, Valid: true}) 1131 } 1132 1133 type scanPlanBinaryInt4ToTextScanner struct{} 1134 1135 func (scanPlanBinaryInt4ToTextScanner) Scan(src []byte, dst any) error { 1136 s, ok := (dst).(TextScanner) 1137 if !ok { 1138 return ErrScanTargetTypeChanged 1139 } 1140 1141 if src == nil { 1142 return s.ScanText(Text{}) 1143 } 1144 1145 if len(src) != 4 { 1146 return fmt.Errorf("invalid length for int4: %v", len(src)) 1147 } 1148 1149 n := int64(int32(binary.BigEndian.Uint32(src))) 1150 1151 return s.ScanText(Text{String: strconv.FormatInt(n, 10), Valid: true}) 1152 } 1153 1154 type Int8 struct { 1155 Int64 int64 1156 Valid bool 1157 } 1158 1159 // ScanInt64 implements the Int64Scanner interface. 1160 func (dst *Int8) ScanInt64(n Int8) error { 1161 if !n.Valid { 1162 *dst = Int8{} 1163 return nil 1164 } 1165 1166 if n.Int64 < math.MinInt64 { 1167 return fmt.Errorf("%d is less than minimum value for Int8", n.Int64) 1168 } 1169 if n.Int64 > math.MaxInt64 { 1170 return fmt.Errorf("%d is greater than maximum value for Int8", n.Int64) 1171 } 1172 *dst = Int8{Int64: int64(n.Int64), Valid: true} 1173 1174 return nil 1175 } 1176 1177 func (n Int8) Int64Value() (Int8, error) { 1178 return Int8{Int64: int64(n.Int64), Valid: n.Valid}, nil 1179 } 1180 1181 // Scan implements the database/sql Scanner interface. 1182 func (dst *Int8) Scan(src any) error { 1183 if src == nil { 1184 *dst = Int8{} 1185 return nil 1186 } 1187 1188 var n int64 1189 1190 switch src := src.(type) { 1191 case int64: 1192 n = src 1193 case string: 1194 var err error 1195 n, err = strconv.ParseInt(src, 10, 64) 1196 if err != nil { 1197 return err 1198 } 1199 case []byte: 1200 var err error 1201 n, err = strconv.ParseInt(string(src), 10, 64) 1202 if err != nil { 1203 return err 1204 } 1205 default: 1206 return fmt.Errorf("cannot scan %T", src) 1207 } 1208 1209 if n < math.MinInt64 { 1210 return fmt.Errorf("%d is greater than maximum value for Int8", n) 1211 } 1212 if n > math.MaxInt64 { 1213 return fmt.Errorf("%d is greater than maximum value for Int8", n) 1214 } 1215 *dst = Int8{Int64: int64(n), Valid: true} 1216 1217 return nil 1218 } 1219 1220 // Value implements the database/sql/driver Valuer interface. 1221 func (src Int8) Value() (driver.Value, error) { 1222 if !src.Valid { 1223 return nil, nil 1224 } 1225 return int64(src.Int64), nil 1226 } 1227 1228 func (src Int8) MarshalJSON() ([]byte, error) { 1229 if !src.Valid { 1230 return []byte("null"), nil 1231 } 1232 return []byte(strconv.FormatInt(int64(src.Int64), 10)), nil 1233 } 1234 1235 func (dst *Int8) UnmarshalJSON(b []byte) error { 1236 var n *int64 1237 err := json.Unmarshal(b, &n) 1238 if err != nil { 1239 return err 1240 } 1241 1242 if n == nil { 1243 *dst = Int8{} 1244 } else { 1245 *dst = Int8{Int64: *n, Valid: true} 1246 } 1247 1248 return nil 1249 } 1250 1251 type Int8Codec struct{} 1252 1253 func (Int8Codec) FormatSupported(format int16) bool { 1254 return format == TextFormatCode || format == BinaryFormatCode 1255 } 1256 1257 func (Int8Codec) PreferredFormat() int16 { 1258 return BinaryFormatCode 1259 } 1260 1261 func (Int8Codec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan { 1262 switch format { 1263 case BinaryFormatCode: 1264 switch value.(type) { 1265 case int64: 1266 return encodePlanInt8CodecBinaryInt64{} 1267 case Int64Valuer: 1268 return encodePlanInt8CodecBinaryInt64Valuer{} 1269 } 1270 case TextFormatCode: 1271 switch value.(type) { 1272 case int64: 1273 return encodePlanInt8CodecTextInt64{} 1274 case Int64Valuer: 1275 return encodePlanInt8CodecTextInt64Valuer{} 1276 } 1277 } 1278 1279 return nil 1280 } 1281 1282 type encodePlanInt8CodecBinaryInt64 struct{} 1283 1284 func (encodePlanInt8CodecBinaryInt64) Encode(value any, buf []byte) (newBuf []byte, err error) { 1285 n := value.(int64) 1286 return pgio.AppendInt64(buf, int64(n)), nil 1287 } 1288 1289 type encodePlanInt8CodecTextInt64 struct{} 1290 1291 func (encodePlanInt8CodecTextInt64) Encode(value any, buf []byte) (newBuf []byte, err error) { 1292 n := value.(int64) 1293 return append(buf, strconv.FormatInt(int64(n), 10)...), nil 1294 } 1295 1296 type encodePlanInt8CodecBinaryInt64Valuer struct{} 1297 1298 func (encodePlanInt8CodecBinaryInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { 1299 n, err := value.(Int64Valuer).Int64Value() 1300 if err != nil { 1301 return nil, err 1302 } 1303 1304 if !n.Valid { 1305 return nil, nil 1306 } 1307 1308 if n.Int64 > math.MaxInt64 { 1309 return nil, fmt.Errorf("%d is greater than maximum value for int8", n.Int64) 1310 } 1311 if n.Int64 < math.MinInt64 { 1312 return nil, fmt.Errorf("%d is less than minimum value for int8", n.Int64) 1313 } 1314 1315 return pgio.AppendInt64(buf, int64(n.Int64)), nil 1316 } 1317 1318 type encodePlanInt8CodecTextInt64Valuer struct{} 1319 1320 func (encodePlanInt8CodecTextInt64Valuer) Encode(value any, buf []byte) (newBuf []byte, err error) { 1321 n, err := value.(Int64Valuer).Int64Value() 1322 if err != nil { 1323 return nil, err 1324 } 1325 1326 if !n.Valid { 1327 return nil, nil 1328 } 1329 1330 if n.Int64 > math.MaxInt64 { 1331 return nil, fmt.Errorf("%d is greater than maximum value for int8", n.Int64) 1332 } 1333 if n.Int64 < math.MinInt64 { 1334 return nil, fmt.Errorf("%d is less than minimum value for int8", n.Int64) 1335 } 1336 1337 return append(buf, strconv.FormatInt(n.Int64, 10)...), nil 1338 } 1339 1340 func (Int8Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan { 1341 1342 switch format { 1343 case BinaryFormatCode: 1344 switch target.(type) { 1345 case *int8: 1346 return scanPlanBinaryInt8ToInt8{} 1347 case *int16: 1348 return scanPlanBinaryInt8ToInt16{} 1349 case *int32: 1350 return scanPlanBinaryInt8ToInt32{} 1351 case *int64: 1352 return scanPlanBinaryInt8ToInt64{} 1353 case *int: 1354 return scanPlanBinaryInt8ToInt{} 1355 case *uint8: 1356 return scanPlanBinaryInt8ToUint8{} 1357 case *uint16: 1358 return scanPlanBinaryInt8ToUint16{} 1359 case *uint32: 1360 return scanPlanBinaryInt8ToUint32{} 1361 case *uint64: 1362 return scanPlanBinaryInt8ToUint64{} 1363 case *uint: 1364 return scanPlanBinaryInt8ToUint{} 1365 case Int64Scanner: 1366 return scanPlanBinaryInt8ToInt64Scanner{} 1367 case TextScanner: 1368 return scanPlanBinaryInt8ToTextScanner{} 1369 } 1370 case TextFormatCode: 1371 switch target.(type) { 1372 case *int8: 1373 return scanPlanTextAnyToInt8{} 1374 case *int16: 1375 return scanPlanTextAnyToInt16{} 1376 case *int32: 1377 return scanPlanTextAnyToInt32{} 1378 case *int64: 1379 return scanPlanTextAnyToInt64{} 1380 case *int: 1381 return scanPlanTextAnyToInt{} 1382 case *uint8: 1383 return scanPlanTextAnyToUint8{} 1384 case *uint16: 1385 return scanPlanTextAnyToUint16{} 1386 case *uint32: 1387 return scanPlanTextAnyToUint32{} 1388 case *uint64: 1389 return scanPlanTextAnyToUint64{} 1390 case *uint: 1391 return scanPlanTextAnyToUint{} 1392 case Int64Scanner: 1393 return scanPlanTextAnyToInt64Scanner{} 1394 } 1395 } 1396 1397 return nil 1398 } 1399 1400 func (c Int8Codec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) { 1401 if src == nil { 1402 return nil, nil 1403 } 1404 1405 var n int64 1406 err := codecScan(c, m, oid, format, src, &n) 1407 if err != nil { 1408 return nil, err 1409 } 1410 return n, nil 1411 } 1412 1413 func (c Int8Codec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) { 1414 if src == nil { 1415 return nil, nil 1416 } 1417 1418 var n int64 1419 err := codecScan(c, m, oid, format, src, &n) 1420 if err != nil { 1421 return nil, err 1422 } 1423 return n, nil 1424 } 1425 1426 type scanPlanBinaryInt8ToInt8 struct{} 1427 1428 func (scanPlanBinaryInt8ToInt8) Scan(src []byte, dst any) error { 1429 if src == nil { 1430 return fmt.Errorf("cannot scan NULL into %T", dst) 1431 } 1432 1433 if len(src) != 8 { 1434 return fmt.Errorf("invalid length for int8: %v", len(src)) 1435 } 1436 1437 p, ok := (dst).(*int8) 1438 if !ok { 1439 return ErrScanTargetTypeChanged 1440 } 1441 1442 n := int64(binary.BigEndian.Uint64(src)) 1443 if n < math.MinInt8 { 1444 return fmt.Errorf("%d is less than minimum value for int8", n) 1445 } else if n > math.MaxInt8 { 1446 return fmt.Errorf("%d is greater than maximum value for int8", n) 1447 } 1448 1449 *p = int8(n) 1450 1451 return nil 1452 } 1453 1454 type scanPlanBinaryInt8ToUint8 struct{} 1455 1456 func (scanPlanBinaryInt8ToUint8) Scan(src []byte, dst any) error { 1457 if src == nil { 1458 return fmt.Errorf("cannot scan NULL into %T", dst) 1459 } 1460 1461 if len(src) != 8 { 1462 return fmt.Errorf("invalid length for uint8: %v", len(src)) 1463 } 1464 1465 p, ok := (dst).(*uint8) 1466 if !ok { 1467 return ErrScanTargetTypeChanged 1468 } 1469 1470 n := int64(binary.BigEndian.Uint64(src)) 1471 if n < 0 { 1472 return fmt.Errorf("%d is less than minimum value for uint8", n) 1473 } 1474 1475 if n > math.MaxUint8 { 1476 return fmt.Errorf("%d is greater than maximum value for uint8", n) 1477 } 1478 1479 *p = uint8(n) 1480 1481 return nil 1482 } 1483 1484 type scanPlanBinaryInt8ToInt16 struct{} 1485 1486 func (scanPlanBinaryInt8ToInt16) Scan(src []byte, dst any) error { 1487 if src == nil { 1488 return fmt.Errorf("cannot scan NULL into %T", dst) 1489 } 1490 1491 if len(src) != 8 { 1492 return fmt.Errorf("invalid length for int8: %v", len(src)) 1493 } 1494 1495 p, ok := (dst).(*int16) 1496 if !ok { 1497 return ErrScanTargetTypeChanged 1498 } 1499 1500 n := int64(binary.BigEndian.Uint64(src)) 1501 if n < math.MinInt16 { 1502 return fmt.Errorf("%d is less than minimum value for int16", n) 1503 } else if n > math.MaxInt16 { 1504 return fmt.Errorf("%d is greater than maximum value for int16", n) 1505 } 1506 1507 *p = int16(n) 1508 1509 return nil 1510 } 1511 1512 type scanPlanBinaryInt8ToUint16 struct{} 1513 1514 func (scanPlanBinaryInt8ToUint16) Scan(src []byte, dst any) error { 1515 if src == nil { 1516 return fmt.Errorf("cannot scan NULL into %T", dst) 1517 } 1518 1519 if len(src) != 8 { 1520 return fmt.Errorf("invalid length for uint8: %v", len(src)) 1521 } 1522 1523 p, ok := (dst).(*uint16) 1524 if !ok { 1525 return ErrScanTargetTypeChanged 1526 } 1527 1528 n := int64(binary.BigEndian.Uint64(src)) 1529 if n < 0 { 1530 return fmt.Errorf("%d is less than minimum value for uint16", n) 1531 } 1532 1533 if n > math.MaxUint16 { 1534 return fmt.Errorf("%d is greater than maximum value for uint16", n) 1535 } 1536 1537 *p = uint16(n) 1538 1539 return nil 1540 } 1541 1542 type scanPlanBinaryInt8ToInt32 struct{} 1543 1544 func (scanPlanBinaryInt8ToInt32) Scan(src []byte, dst any) error { 1545 if src == nil { 1546 return fmt.Errorf("cannot scan NULL into %T", dst) 1547 } 1548 1549 if len(src) != 8 { 1550 return fmt.Errorf("invalid length for int8: %v", len(src)) 1551 } 1552 1553 p, ok := (dst).(*int32) 1554 if !ok { 1555 return ErrScanTargetTypeChanged 1556 } 1557 1558 n := int64(binary.BigEndian.Uint64(src)) 1559 if n < math.MinInt32 { 1560 return fmt.Errorf("%d is less than minimum value for int32", n) 1561 } else if n > math.MaxInt32 { 1562 return fmt.Errorf("%d is greater than maximum value for int32", n) 1563 } 1564 1565 *p = int32(n) 1566 1567 return nil 1568 } 1569 1570 type scanPlanBinaryInt8ToUint32 struct{} 1571 1572 func (scanPlanBinaryInt8ToUint32) Scan(src []byte, dst any) error { 1573 if src == nil { 1574 return fmt.Errorf("cannot scan NULL into %T", dst) 1575 } 1576 1577 if len(src) != 8 { 1578 return fmt.Errorf("invalid length for uint8: %v", len(src)) 1579 } 1580 1581 p, ok := (dst).(*uint32) 1582 if !ok { 1583 return ErrScanTargetTypeChanged 1584 } 1585 1586 n := int64(binary.BigEndian.Uint64(src)) 1587 if n < 0 { 1588 return fmt.Errorf("%d is less than minimum value for uint32", n) 1589 } 1590 1591 if n > math.MaxUint32 { 1592 return fmt.Errorf("%d is greater than maximum value for uint32", n) 1593 } 1594 1595 *p = uint32(n) 1596 1597 return nil 1598 } 1599 1600 type scanPlanBinaryInt8ToInt64 struct{} 1601 1602 func (scanPlanBinaryInt8ToInt64) Scan(src []byte, dst any) error { 1603 if src == nil { 1604 return fmt.Errorf("cannot scan NULL into %T", dst) 1605 } 1606 1607 if len(src) != 8 { 1608 return fmt.Errorf("invalid length for int8: %v", len(src)) 1609 } 1610 1611 p, ok := (dst).(*int64) 1612 if !ok { 1613 return ErrScanTargetTypeChanged 1614 } 1615 1616 *p = int64(binary.BigEndian.Uint64(src)) 1617 1618 return nil 1619 } 1620 1621 type scanPlanBinaryInt8ToUint64 struct{} 1622 1623 func (scanPlanBinaryInt8ToUint64) Scan(src []byte, dst any) error { 1624 if src == nil { 1625 return fmt.Errorf("cannot scan NULL into %T", dst) 1626 } 1627 1628 if len(src) != 8 { 1629 return fmt.Errorf("invalid length for uint8: %v", len(src)) 1630 } 1631 1632 p, ok := (dst).(*uint64) 1633 if !ok { 1634 return ErrScanTargetTypeChanged 1635 } 1636 1637 n := int64(binary.BigEndian.Uint64(src)) 1638 if n < 0 { 1639 return fmt.Errorf("%d is less than minimum value for uint64", n) 1640 } 1641 1642 *p = uint64(n) 1643 1644 return nil 1645 } 1646 1647 type scanPlanBinaryInt8ToInt struct{} 1648 1649 func (scanPlanBinaryInt8ToInt) Scan(src []byte, dst any) error { 1650 if src == nil { 1651 return fmt.Errorf("cannot scan NULL into %T", dst) 1652 } 1653 1654 if len(src) != 8 { 1655 return fmt.Errorf("invalid length for int8: %v", len(src)) 1656 } 1657 1658 p, ok := (dst).(*int) 1659 if !ok { 1660 return ErrScanTargetTypeChanged 1661 } 1662 1663 n := int64(binary.BigEndian.Uint64(src)) 1664 if n < math.MinInt { 1665 return fmt.Errorf("%d is less than minimum value for int", n) 1666 } else if n > math.MaxInt { 1667 return fmt.Errorf("%d is greater than maximum value for int", n) 1668 } 1669 1670 *p = int(n) 1671 1672 return nil 1673 } 1674 1675 type scanPlanBinaryInt8ToUint struct{} 1676 1677 func (scanPlanBinaryInt8ToUint) Scan(src []byte, dst any) error { 1678 if src == nil { 1679 return fmt.Errorf("cannot scan NULL into %T", dst) 1680 } 1681 1682 if len(src) != 8 { 1683 return fmt.Errorf("invalid length for uint8: %v", len(src)) 1684 } 1685 1686 p, ok := (dst).(*uint) 1687 if !ok { 1688 return ErrScanTargetTypeChanged 1689 } 1690 1691 n := int64(int64(binary.BigEndian.Uint64(src))) 1692 if n < 0 { 1693 return fmt.Errorf("%d is less than minimum value for uint", n) 1694 } 1695 1696 if uint64(n) > math.MaxUint { 1697 return fmt.Errorf("%d is greater than maximum value for uint", n) 1698 } 1699 1700 *p = uint(n) 1701 1702 return nil 1703 } 1704 1705 type scanPlanBinaryInt8ToInt64Scanner struct{} 1706 1707 func (scanPlanBinaryInt8ToInt64Scanner) Scan(src []byte, dst any) error { 1708 s, ok := (dst).(Int64Scanner) 1709 if !ok { 1710 return ErrScanTargetTypeChanged 1711 } 1712 1713 if src == nil { 1714 return s.ScanInt64(Int8{}) 1715 } 1716 1717 if len(src) != 8 { 1718 return fmt.Errorf("invalid length for int8: %v", len(src)) 1719 } 1720 1721 n := int64(int64(binary.BigEndian.Uint64(src))) 1722 1723 return s.ScanInt64(Int8{Int64: n, Valid: true}) 1724 } 1725 1726 type scanPlanBinaryInt8ToTextScanner struct{} 1727 1728 func (scanPlanBinaryInt8ToTextScanner) Scan(src []byte, dst any) error { 1729 s, ok := (dst).(TextScanner) 1730 if !ok { 1731 return ErrScanTargetTypeChanged 1732 } 1733 1734 if src == nil { 1735 return s.ScanText(Text{}) 1736 } 1737 1738 if len(src) != 8 { 1739 return fmt.Errorf("invalid length for int8: %v", len(src)) 1740 } 1741 1742 n := int64(int64(binary.BigEndian.Uint64(src))) 1743 1744 return s.ScanText(Text{String: strconv.FormatInt(n, 10), Valid: true}) 1745 } 1746 1747 type scanPlanTextAnyToInt8 struct{} 1748 1749 func (scanPlanTextAnyToInt8) Scan(src []byte, dst any) error { 1750 if src == nil { 1751 return fmt.Errorf("cannot scan NULL into %T", dst) 1752 } 1753 1754 p, ok := (dst).(*int8) 1755 if !ok { 1756 return ErrScanTargetTypeChanged 1757 } 1758 1759 n, err := strconv.ParseInt(string(src), 10, 8) 1760 if err != nil { 1761 return err 1762 } 1763 1764 *p = int8(n) 1765 return nil 1766 } 1767 1768 type scanPlanTextAnyToUint8 struct{} 1769 1770 func (scanPlanTextAnyToUint8) Scan(src []byte, dst any) error { 1771 if src == nil { 1772 return fmt.Errorf("cannot scan NULL into %T", dst) 1773 } 1774 1775 p, ok := (dst).(*uint8) 1776 if !ok { 1777 return ErrScanTargetTypeChanged 1778 } 1779 1780 n, err := strconv.ParseUint(string(src), 10, 8) 1781 if err != nil { 1782 return err 1783 } 1784 1785 *p = uint8(n) 1786 return nil 1787 } 1788 1789 type scanPlanTextAnyToInt16 struct{} 1790 1791 func (scanPlanTextAnyToInt16) Scan(src []byte, dst any) error { 1792 if src == nil { 1793 return fmt.Errorf("cannot scan NULL into %T", dst) 1794 } 1795 1796 p, ok := (dst).(*int16) 1797 if !ok { 1798 return ErrScanTargetTypeChanged 1799 } 1800 1801 n, err := strconv.ParseInt(string(src), 10, 16) 1802 if err != nil { 1803 return err 1804 } 1805 1806 *p = int16(n) 1807 return nil 1808 } 1809 1810 type scanPlanTextAnyToUint16 struct{} 1811 1812 func (scanPlanTextAnyToUint16) Scan(src []byte, dst any) error { 1813 if src == nil { 1814 return fmt.Errorf("cannot scan NULL into %T", dst) 1815 } 1816 1817 p, ok := (dst).(*uint16) 1818 if !ok { 1819 return ErrScanTargetTypeChanged 1820 } 1821 1822 n, err := strconv.ParseUint(string(src), 10, 16) 1823 if err != nil { 1824 return err 1825 } 1826 1827 *p = uint16(n) 1828 return nil 1829 } 1830 1831 type scanPlanTextAnyToInt32 struct{} 1832 1833 func (scanPlanTextAnyToInt32) Scan(src []byte, dst any) error { 1834 if src == nil { 1835 return fmt.Errorf("cannot scan NULL into %T", dst) 1836 } 1837 1838 p, ok := (dst).(*int32) 1839 if !ok { 1840 return ErrScanTargetTypeChanged 1841 } 1842 1843 n, err := strconv.ParseInt(string(src), 10, 32) 1844 if err != nil { 1845 return err 1846 } 1847 1848 *p = int32(n) 1849 return nil 1850 } 1851 1852 type scanPlanTextAnyToUint32 struct{} 1853 1854 func (scanPlanTextAnyToUint32) Scan(src []byte, dst any) error { 1855 if src == nil { 1856 return fmt.Errorf("cannot scan NULL into %T", dst) 1857 } 1858 1859 p, ok := (dst).(*uint32) 1860 if !ok { 1861 return ErrScanTargetTypeChanged 1862 } 1863 1864 n, err := strconv.ParseUint(string(src), 10, 32) 1865 if err != nil { 1866 return err 1867 } 1868 1869 *p = uint32(n) 1870 return nil 1871 } 1872 1873 type scanPlanTextAnyToInt64 struct{} 1874 1875 func (scanPlanTextAnyToInt64) Scan(src []byte, dst any) error { 1876 if src == nil { 1877 return fmt.Errorf("cannot scan NULL into %T", dst) 1878 } 1879 1880 p, ok := (dst).(*int64) 1881 if !ok { 1882 return ErrScanTargetTypeChanged 1883 } 1884 1885 n, err := strconv.ParseInt(string(src), 10, 64) 1886 if err != nil { 1887 return err 1888 } 1889 1890 *p = int64(n) 1891 return nil 1892 } 1893 1894 type scanPlanTextAnyToUint64 struct{} 1895 1896 func (scanPlanTextAnyToUint64) Scan(src []byte, dst any) error { 1897 if src == nil { 1898 return fmt.Errorf("cannot scan NULL into %T", dst) 1899 } 1900 1901 p, ok := (dst).(*uint64) 1902 if !ok { 1903 return ErrScanTargetTypeChanged 1904 } 1905 1906 n, err := strconv.ParseUint(string(src), 10, 64) 1907 if err != nil { 1908 return err 1909 } 1910 1911 *p = uint64(n) 1912 return nil 1913 } 1914 1915 type scanPlanTextAnyToInt struct{} 1916 1917 func (scanPlanTextAnyToInt) Scan(src []byte, dst any) error { 1918 if src == nil { 1919 return fmt.Errorf("cannot scan NULL into %T", dst) 1920 } 1921 1922 p, ok := (dst).(*int) 1923 if !ok { 1924 return ErrScanTargetTypeChanged 1925 } 1926 1927 n, err := strconv.ParseInt(string(src), 10, 0) 1928 if err != nil { 1929 return err 1930 } 1931 1932 *p = int(n) 1933 return nil 1934 } 1935 1936 type scanPlanTextAnyToUint struct{} 1937 1938 func (scanPlanTextAnyToUint) Scan(src []byte, dst any) error { 1939 if src == nil { 1940 return fmt.Errorf("cannot scan NULL into %T", dst) 1941 } 1942 1943 p, ok := (dst).(*uint) 1944 if !ok { 1945 return ErrScanTargetTypeChanged 1946 } 1947 1948 n, err := strconv.ParseUint(string(src), 10, 0) 1949 if err != nil { 1950 return err 1951 } 1952 1953 *p = uint(n) 1954 return nil 1955 } 1956 1957 type scanPlanTextAnyToInt64Scanner struct{} 1958 1959 func (scanPlanTextAnyToInt64Scanner) Scan(src []byte, dst any) error { 1960 s, ok := (dst).(Int64Scanner) 1961 if !ok { 1962 return ErrScanTargetTypeChanged 1963 } 1964 1965 if src == nil { 1966 return s.ScanInt64(Int8{}) 1967 } 1968 1969 n, err := strconv.ParseInt(string(src), 10, 64) 1970 if err != nil { 1971 return err 1972 } 1973 1974 err = s.ScanInt64(Int8{Int64: n, Valid: true}) 1975 if err != nil { 1976 return err 1977 } 1978 1979 return nil 1980 }