github.com/jackc/pgx/v5@v5.5.5/pgtype/zeronull/int.go.erb (about)

     1  package zeronull
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"fmt"
     6  	"math"
     7  
     8  	"github.com/jackc/pgx/v5/pgtype"
     9  )
    10  
    11  <% [2, 4, 8].each do |pg_byte_size| %>
    12  <% pg_bit_size = pg_byte_size * 8 %>
    13  type Int<%= pg_byte_size %> int<%= pg_bit_size %>
    14  
    15  func (Int<%= pg_byte_size %>) SkipUnderlyingTypePlan() {}
    16  
    17  // ScanInt64 implements the Int64Scanner interface.
    18  func (dst *Int<%= pg_byte_size %>) ScanInt64(n int64, valid bool) error {
    19  	if !valid {
    20  		*dst = 0
    21  		return nil
    22  	}
    23  
    24  	if n < math.MinInt<%= pg_bit_size %> {
    25  		return fmt.Errorf("%d is greater than maximum value for Int<%= pg_byte_size %>", n)
    26  	}
    27  	if n > math.MaxInt<%= pg_bit_size %> {
    28  		return fmt.Errorf("%d is greater than maximum value for Int<%= pg_byte_size %>", n)
    29  	}
    30  	*dst = Int<%= pg_byte_size %>(n)
    31  
    32  	return nil
    33  }
    34  
    35  // Scan implements the database/sql Scanner interface.
    36  func (dst *Int<%= pg_byte_size %>) Scan(src any) error {
    37  	if src == nil {
    38  		*dst = 0
    39  		return nil
    40  	}
    41  
    42  	var nullable pgtype.Int<%= pg_byte_size %>
    43  	err := nullable.Scan(src)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	*dst = Int<%= pg_byte_size %>(nullable.Int<%= pg_bit_size %>)
    49  
    50  	return nil
    51  }
    52  
    53  // Value implements the database/sql/driver Valuer interface.
    54  func (src Int<%= pg_byte_size %>) Value() (driver.Value, error) {
    55  	if src == 0 {
    56  		return nil, nil
    57  	}
    58  	return int64(src), nil
    59  }
    60  <% end %>