decred.org/dcrdex@v1.0.5/server/db/driver/pg/types.go (about)

     1  package pg
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // fastUint64 provides an efficient Scanner for 64-bit integer data. With a
     8  // regular uint64 or any other integer type that itself does not implement
     9  // Scanner, the database/sql.Scan implementation will coerce type by converting
    10  // to and from a string, so we avoid this expensive operation by implementing a
    11  // Scanner that uses a type assertion. This only works for columns with a
    12  // postgresql type of INT8 (8-byte integer) that scan a value of type int64.
    13  type fastUint64 uint64
    14  
    15  func (n *fastUint64) Scan(value any) error {
    16  	if value == nil {
    17  		*n = 0
    18  		return fmt.Errorf("NULL not supported")
    19  	}
    20  
    21  	v, ok := value.(int64)
    22  	if !ok {
    23  		return fmt.Errorf("not a signed 64-bit integer: %T", value)
    24  	}
    25  	*n = fastUint64(v)
    26  	return nil
    27  
    28  	// NOTE: If we want to be able to scan all other interger types and coerce
    29  	// them into uint64, we can switch on each signed type. PostgreSQL has no
    30  	// unsigned types, so just int8, int16, int32, and int64.
    31  
    32  	// switch v := value.(type) {
    33  	// case int64:
    34  	// 	*n = fastUint64(v)
    35  	// case int32:
    36  	// 	*n = fastUint64(v)
    37  	// case int16:
    38  	// 	*n = fastUint64(v)
    39  	// case int8:
    40  	// 	*n = fastUint64(v)
    41  	// default:
    42  	// 	return fmt.Errorf("not a signed integer: %t", value)
    43  	// }
    44  	// return nil
    45  }