...

Source file src/github.com/jackc/pgtype/pguint32.go

Documentation: github.com/jackc/pgtype

     1  package pgtype
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"math"
     8  	"strconv"
     9  
    10  	"github.com/jackc/pgio"
    11  )
    12  
    13  // pguint32 is the core type that is used to implement PostgreSQL types such as
    14  // CID and XID.
    15  type pguint32 struct {
    16  	Uint   uint32
    17  	Status Status
    18  }
    19  
    20  // Set converts from src to dst. Note that as pguint32 is not a general
    21  // number type Set does not do automatic type conversion as other number
    22  // types do.
    23  func (dst *pguint32) Set(src interface{}) error {
    24  	switch value := src.(type) {
    25  	case int64:
    26  		if value < 0 {
    27  			return fmt.Errorf("%d is less than minimum value for pguint32", value)
    28  		}
    29  		if value > math.MaxUint32 {
    30  			return fmt.Errorf("%d is greater than maximum value for pguint32", value)
    31  		}
    32  		*dst = pguint32{Uint: uint32(value), Status: Present}
    33  	case uint32:
    34  		*dst = pguint32{Uint: value, Status: Present}
    35  	default:
    36  		return fmt.Errorf("cannot convert %v to pguint32", value)
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func (dst pguint32) Get() interface{} {
    43  	switch dst.Status {
    44  	case Present:
    45  		return dst.Uint
    46  	case Null:
    47  		return nil
    48  	default:
    49  		return dst.Status
    50  	}
    51  }
    52  
    53  // AssignTo assigns from src to dst. Note that as pguint32 is not a general number
    54  // type AssignTo does not do automatic type conversion as other number types do.
    55  func (src *pguint32) AssignTo(dst interface{}) error {
    56  	switch v := dst.(type) {
    57  	case *uint32:
    58  		if src.Status == Present {
    59  			*v = src.Uint
    60  		} else {
    61  			return fmt.Errorf("cannot assign %v into %T", src, dst)
    62  		}
    63  	case **uint32:
    64  		if src.Status == Present {
    65  			n := src.Uint
    66  			*v = &n
    67  		} else {
    68  			*v = nil
    69  		}
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func (dst *pguint32) DecodeText(ci *ConnInfo, src []byte) error {
    76  	if src == nil {
    77  		*dst = pguint32{Status: Null}
    78  		return nil
    79  	}
    80  
    81  	n, err := strconv.ParseUint(string(src), 10, 32)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	*dst = pguint32{Uint: uint32(n), Status: Present}
    87  	return nil
    88  }
    89  
    90  func (dst *pguint32) DecodeBinary(ci *ConnInfo, src []byte) error {
    91  	if src == nil {
    92  		*dst = pguint32{Status: Null}
    93  		return nil
    94  	}
    95  
    96  	if len(src) != 4 {
    97  		return fmt.Errorf("invalid length: %v", len(src))
    98  	}
    99  
   100  	n := binary.BigEndian.Uint32(src)
   101  	*dst = pguint32{Uint: n, Status: Present}
   102  	return nil
   103  }
   104  
   105  func (src pguint32) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
   106  	switch src.Status {
   107  	case Null:
   108  		return nil, nil
   109  	case Undefined:
   110  		return nil, errUndefined
   111  	}
   112  
   113  	return append(buf, strconv.FormatUint(uint64(src.Uint), 10)...), nil
   114  }
   115  
   116  func (src pguint32) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
   117  	switch src.Status {
   118  	case Null:
   119  		return nil, nil
   120  	case Undefined:
   121  		return nil, errUndefined
   122  	}
   123  
   124  	return pgio.AppendUint32(buf, src.Uint), nil
   125  }
   126  
   127  // Scan implements the database/sql Scanner interface.
   128  func (dst *pguint32) Scan(src interface{}) error {
   129  	if src == nil {
   130  		*dst = pguint32{Status: Null}
   131  		return nil
   132  	}
   133  
   134  	switch src := src.(type) {
   135  	case uint32:
   136  		*dst = pguint32{Uint: src, Status: Present}
   137  		return nil
   138  	case int64:
   139  		*dst = pguint32{Uint: uint32(src), Status: Present}
   140  		return nil
   141  	case string:
   142  		return dst.DecodeText(nil, []byte(src))
   143  	case []byte:
   144  		srcCopy := make([]byte, len(src))
   145  		copy(srcCopy, src)
   146  		return dst.DecodeText(nil, srcCopy)
   147  	}
   148  
   149  	return fmt.Errorf("cannot scan %T", src)
   150  }
   151  
   152  // Value implements the database/sql/driver Valuer interface.
   153  func (src pguint32) Value() (driver.Value, error) {
   154  	switch src.Status {
   155  	case Present:
   156  		return int64(src.Uint), nil
   157  	case Null:
   158  		return nil, nil
   159  	default:
   160  		return nil, errUndefined
   161  	}
   162  }
   163  

View as plain text