...

Source file src/github.com/jackc/pgtype/zeronull/int2.go

Documentation: github.com/jackc/pgtype/zeronull

     1  package zeronull
     2  
     3  import (
     4  	"database/sql/driver"
     5  
     6  	"github.com/jackc/pgtype"
     7  )
     8  
     9  type Int2 int16
    10  
    11  func (dst *Int2) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
    12  	var nullable pgtype.Int2
    13  	err := nullable.DecodeText(ci, src)
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	if nullable.Status == pgtype.Present {
    19  		*dst = Int2(nullable.Int)
    20  	} else {
    21  		*dst = 0
    22  	}
    23  
    24  	return nil
    25  }
    26  
    27  func (dst *Int2) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
    28  	var nullable pgtype.Int2
    29  	err := nullable.DecodeBinary(ci, src)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	if nullable.Status == pgtype.Present {
    35  		*dst = Int2(nullable.Int)
    36  	} else {
    37  		*dst = 0
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  func (src Int2) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
    44  	if src == 0 {
    45  		return nil, nil
    46  	}
    47  
    48  	nullable := pgtype.Int2{
    49  		Int:    int16(src),
    50  		Status: pgtype.Present,
    51  	}
    52  
    53  	return nullable.EncodeText(ci, buf)
    54  }
    55  
    56  func (src Int2) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
    57  	if src == 0 {
    58  		return nil, nil
    59  	}
    60  
    61  	nullable := pgtype.Int2{
    62  		Int:    int16(src),
    63  		Status: pgtype.Present,
    64  	}
    65  
    66  	return nullable.EncodeBinary(ci, buf)
    67  }
    68  
    69  // Scan implements the database/sql Scanner interface.
    70  func (dst *Int2) Scan(src interface{}) error {
    71  	if src == nil {
    72  		*dst = 0
    73  		return nil
    74  	}
    75  
    76  	var nullable pgtype.Int2
    77  	err := nullable.Scan(src)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	*dst = Int2(nullable.Int)
    83  
    84  	return nil
    85  }
    86  
    87  // Value implements the database/sql/driver Valuer interface.
    88  func (src Int2) Value() (driver.Value, error) {
    89  	return pgtype.EncodeValueText(src)
    90  }
    91  

View as plain text