Convert int4 and int8 to new system
Note: purposely disabled some tests and composite support that needs to be restored later in v5 development.
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
// Do not edit. Generated from pgtype/zeronull/int.go.erb
|
||||
package zeronull
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Int2 int16
|
||||
|
||||
// ScanInt64 implements the Int64Scanner interface.
|
||||
func (dst *Int2) ScanInt64(n int64, valid bool) error {
|
||||
if !valid {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if n < math.MinInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", n)
|
||||
}
|
||||
if n > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", n)
|
||||
}
|
||||
*dst = Int2(n)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int2) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var nullable pgtype.Int2
|
||||
err := nullable.Scan(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int2(nullable.Int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int2) Value() (driver.Value, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return int64(src), nil
|
||||
}
|
||||
|
||||
type Int4 int32
|
||||
|
||||
// ScanInt64 implements the Int64Scanner interface.
|
||||
func (dst *Int4) ScanInt64(n int64, valid bool) error {
|
||||
if !valid {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if n < math.MinInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", n)
|
||||
}
|
||||
if n > math.MaxInt32 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int4", n)
|
||||
}
|
||||
*dst = Int4(n)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int4) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var nullable pgtype.Int4
|
||||
err := nullable.Scan(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int4(nullable.Int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int4) Value() (driver.Value, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return int64(src), nil
|
||||
}
|
||||
|
||||
type Int8 int64
|
||||
|
||||
// ScanInt64 implements the Int64Scanner interface.
|
||||
func (dst *Int8) ScanInt64(n int64, valid bool) error {
|
||||
if !valid {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if n < math.MinInt64 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int8", n)
|
||||
}
|
||||
if n > math.MaxInt64 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int8", n)
|
||||
}
|
||||
*dst = Int8(n)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int8) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var nullable pgtype.Int8
|
||||
err := nullable.Scan(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int8(nullable.Int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int8) Value() (driver.Value, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return int64(src), nil
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package zeronull
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
<% [2, 4, 8].each do |pg_byte_size| %>
|
||||
<% pg_bit_size = pg_byte_size * 8 %>
|
||||
type Int<%= pg_byte_size %> int<%= pg_bit_size %>
|
||||
|
||||
// ScanInt64 implements the Int64Scanner interface.
|
||||
func (dst *Int<%= pg_byte_size %>) ScanInt64(n int64, valid bool) error {
|
||||
if !valid {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if n < math.MinInt<%= pg_bit_size %> {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int<%= pg_byte_size %>", n)
|
||||
}
|
||||
if n > math.MaxInt<%= pg_bit_size %> {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int<%= pg_byte_size %>", n)
|
||||
}
|
||||
*dst = Int<%= pg_byte_size %>(n)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int<%= pg_byte_size %>) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var nullable pgtype.Int<%= pg_byte_size %>
|
||||
err := nullable.Scan(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int<%= pg_byte_size %>(nullable.Int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int<%= pg_byte_size %>) Value() (driver.Value, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return int64(src), nil
|
||||
}
|
||||
<% end %>
|
||||
@@ -1,55 +0,0 @@
|
||||
package zeronull
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Int2 int16
|
||||
|
||||
// ScanInt64 implements the Int64Scanner interface.
|
||||
func (dst *Int2) ScanInt64(n int64, valid bool) error {
|
||||
if !valid {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if n < math.MinInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", n)
|
||||
}
|
||||
if n > math.MaxInt16 {
|
||||
return fmt.Errorf("%d is greater than maximum value for Int2", n)
|
||||
}
|
||||
*dst = Int2(n)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int2) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var nullable pgtype.Int2
|
||||
err := nullable.Scan(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int2(nullable.Int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int2) Value() (driver.Value, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return int64(src), nil
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
)
|
||||
|
||||
func TestInt2Transcode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "int2", []interface{}{
|
||||
(zeronull.Int2)(1),
|
||||
(zeronull.Int2)(0),
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt2ConvertsGoZeroToNull(t *testing.T) {
|
||||
testutil.TestGoZeroToNullConversion(t, "int2", (zeronull.Int2)(0))
|
||||
}
|
||||
|
||||
func TestInt2ConvertsNullToGoZero(t *testing.T) {
|
||||
testutil.TestNullToGoZeroConversion(t, "int2", (zeronull.Int2)(0))
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package zeronull
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Int4 int32
|
||||
|
||||
func (dst *Int4) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Int4
|
||||
err := nullable.DecodeText(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Int4(nullable.Int)
|
||||
} else {
|
||||
*dst = 0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Int4) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Int4
|
||||
err := nullable.DecodeBinary(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Int4(nullable.Int)
|
||||
} else {
|
||||
*dst = 0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src Int4) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Int4{
|
||||
Int: int32(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeText(ci, buf)
|
||||
}
|
||||
|
||||
func (src Int4) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Int4{
|
||||
Int: int32(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeBinary(ci, buf)
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int4) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var nullable pgtype.Int4
|
||||
err := nullable.Scan(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int4(nullable.Int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int4) Value() (driver.Value, error) {
|
||||
return pgtype.EncodeValueText(src)
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
)
|
||||
|
||||
func TestInt4Transcode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "int4", []interface{}{
|
||||
(zeronull.Int4)(1),
|
||||
(zeronull.Int4)(0),
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt4ConvertsGoZeroToNull(t *testing.T) {
|
||||
testutil.TestGoZeroToNullConversion(t, "int4", (zeronull.Int4)(0))
|
||||
}
|
||||
|
||||
func TestInt4ConvertsNullToGoZero(t *testing.T) {
|
||||
testutil.TestNullToGoZeroConversion(t, "int4", (zeronull.Int4)(0))
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package zeronull
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Int8 int64
|
||||
|
||||
func (dst *Int8) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Int8
|
||||
err := nullable.DecodeText(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Int8(nullable.Int)
|
||||
} else {
|
||||
*dst = 0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Int8) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
var nullable pgtype.Int8
|
||||
err := nullable.DecodeBinary(ci, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nullable.Valid {
|
||||
*dst = Int8(nullable.Int)
|
||||
} else {
|
||||
*dst = 0
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src Int8) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Int8{
|
||||
Int: int64(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeText(ci, buf)
|
||||
}
|
||||
|
||||
func (src Int8) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
if src == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
nullable := pgtype.Int8{
|
||||
Int: int64(src),
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
return nullable.EncodeBinary(ci, buf)
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Int8) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var nullable pgtype.Int8
|
||||
err := nullable.Scan(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Int8(nullable.Int)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src Int8) Value() (driver.Value, error) {
|
||||
return pgtype.EncodeValueText(src)
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
)
|
||||
|
||||
func TestInt8Transcode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "int8", []interface{}{
|
||||
(zeronull.Int8)(1),
|
||||
(zeronull.Int8)(0),
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt8ConvertsGoZeroToNull(t *testing.T) {
|
||||
testutil.TestGoZeroToNullConversion(t, "int8", (zeronull.Int8)(0))
|
||||
}
|
||||
|
||||
func TestInt8ConvertsNullToGoZero(t *testing.T) {
|
||||
testutil.TestNullToGoZeroConversion(t, "int8", (zeronull.Int8)(0))
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// Do not edit. Generated from pgtype/zeronull/int_test.go.erb
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
)
|
||||
|
||||
func TestInt2Transcode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "int2", []interface{}{
|
||||
(zeronull.Int2)(1),
|
||||
(zeronull.Int2)(0),
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt2ConvertsGoZeroToNull(t *testing.T) {
|
||||
testutil.TestGoZeroToNullConversion(t, "int2", (zeronull.Int2)(0))
|
||||
}
|
||||
|
||||
func TestInt2ConvertsNullToGoZero(t *testing.T) {
|
||||
testutil.TestNullToGoZeroConversion(t, "int2", (zeronull.Int2)(0))
|
||||
}
|
||||
|
||||
func TestInt4Transcode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "int4", []interface{}{
|
||||
(zeronull.Int4)(1),
|
||||
(zeronull.Int4)(0),
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt4ConvertsGoZeroToNull(t *testing.T) {
|
||||
testutil.TestGoZeroToNullConversion(t, "int4", (zeronull.Int4)(0))
|
||||
}
|
||||
|
||||
func TestInt4ConvertsNullToGoZero(t *testing.T) {
|
||||
testutil.TestNullToGoZeroConversion(t, "int4", (zeronull.Int4)(0))
|
||||
}
|
||||
|
||||
func TestInt8Transcode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "int8", []interface{}{
|
||||
(zeronull.Int8)(1),
|
||||
(zeronull.Int8)(0),
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt8ConvertsGoZeroToNull(t *testing.T) {
|
||||
testutil.TestGoZeroToNullConversion(t, "int8", (zeronull.Int8)(0))
|
||||
}
|
||||
|
||||
func TestInt8ConvertsNullToGoZero(t *testing.T) {
|
||||
testutil.TestNullToGoZeroConversion(t, "int8", (zeronull.Int8)(0))
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package zeronull_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype/testutil"
|
||||
"github.com/jackc/pgx/v5/pgtype/zeronull"
|
||||
)
|
||||
|
||||
<% [2, 4, 8].each do |pg_byte_size| %>
|
||||
<% pg_bit_size = pg_byte_size * 8 %>
|
||||
func TestInt<%= pg_byte_size %>Transcode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "int<%= pg_byte_size %>", []interface{}{
|
||||
(zeronull.Int<%= pg_byte_size %>)(1),
|
||||
(zeronull.Int<%= pg_byte_size %>)(0),
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt<%= pg_byte_size %>ConvertsGoZeroToNull(t *testing.T) {
|
||||
testutil.TestGoZeroToNullConversion(t, "int<%= pg_byte_size %>", (zeronull.Int<%= pg_byte_size %>)(0))
|
||||
}
|
||||
|
||||
func TestInt<%= pg_byte_size %>ConvertsNullToGoZero(t *testing.T) {
|
||||
testutil.TestNullToGoZeroConversion(t, "int<%= pg_byte_size %>", (zeronull.Int<%= pg_byte_size %>)(0))
|
||||
}
|
||||
<% end %>
|
||||
Reference in New Issue
Block a user