Source File
encode.go
Belonging Package
github.com/lib/pq
package pq
import (
)
func ( *parameterStatus, interface{}) []byte {
switch v := .(type) {
case []byte:
return
default:
return encode(, , oid.T_unknown)
}
}
func ( *parameterStatus, interface{}, oid.Oid) []byte {
switch v := .(type) {
case int64:
return strconv.AppendInt(nil, , 10)
case float64:
return strconv.AppendFloat(nil, , 'f', -1, 64)
case []byte:
if == oid.T_bytea {
return encodeBytea(.serverVersion, )
}
return
case string:
if == oid.T_bytea {
return encodeBytea(.serverVersion, []byte())
}
return []byte()
case bool:
return strconv.AppendBool(nil, )
case time.Time:
return formatTs()
default:
errorf("encode: unknown type for %T", )
}
panic("not reached")
}
func ( *parameterStatus, []byte, oid.Oid, format) interface{} {
switch {
case formatBinary:
return binaryDecode(, , )
case formatText:
return textDecode(, , )
default:
panic("not reached")
}
}
func ( *parameterStatus, []byte, oid.Oid) interface{} {
switch {
case oid.T_bytea:
return
case oid.T_int8:
return int64(binary.BigEndian.Uint64())
case oid.T_int4:
return int64(int32(binary.BigEndian.Uint32()))
case oid.T_int2:
return int64(int16(binary.BigEndian.Uint16()))
case oid.T_uuid:
, := decodeUUIDBinary()
if != nil {
panic()
}
return
default:
errorf("don't know how to decode binary parameter of type %d", uint32())
}
panic("not reached")
}
func ( *parameterStatus, []byte, oid.Oid) interface{} {
switch {
case oid.T_char, oid.T_varchar, oid.T_text:
return string()
case oid.T_bytea:
, := parseBytea()
if != nil {
errorf("%s", )
}
return
case oid.T_timestamptz:
return parseTs(.currentLocation, string())
case oid.T_timestamp, oid.T_date:
return parseTs(nil, string())
case oid.T_time:
return mustParse("15:04:05", , )
case oid.T_timetz:
return mustParse("15:04:05-07", , )
case oid.T_bool:
return [0] == 't'
case oid.T_int8, oid.T_int4, oid.T_int2:
, := strconv.ParseInt(string(), 10, 64)
if != nil {
errorf("%s", )
}
return
, := strconv.ParseFloat(string(), 64)
if != nil {
errorf("%s", )
}
return
}
return
}
func ( *parameterStatus, []byte, interface{}) []byte {
switch v := .(type) {
case int64:
return strconv.AppendInt(, , 10)
case float64:
return strconv.AppendFloat(, , 'f', -1, 64)
case []byte:
:= encodeBytea(.serverVersion, )
return appendEscapedText(, string())
case string:
return appendEscapedText(, )
case bool:
return strconv.AppendBool(, )
case time.Time:
return append(, formatTs()...)
case nil:
return append(, "\\N"...)
default:
errorf("encode: unknown type for %T", )
}
panic("not reached")
}
func ( []byte, string) []byte {
:= false
:= 0
var byte
if ( == oid.T_timestamptz || == oid.T_timetz) &&
[len()-3] == ':' {
+= ":00"
}
, := time.Parse(, )
if != nil {
errorf("decode: %s", )
}
return
}
var errInvalidTimestamp = errors.New("invalid timestamp")
type timestampParser struct {
err error
}
func ( *timestampParser) ( string, byte, int) {
if .err != nil {
return
}
if +1 > len() {
.err = errInvalidTimestamp
return
}
if := []; != && .err == nil {
.err = fmt.Errorf("expected '%v' at position %v; got '%v'", , , )
}
}
func ( *timestampParser) ( string, int, int) int {
if .err != nil {
return 0
}
if < 0 || < 0 || > || > len() {
.err = errInvalidTimestamp
return 0
}
, := strconv.Atoi([:])
if != nil {
if .err == nil {
.err = fmt.Errorf("expected number; got '%v'", )
}
return 0
}
return
}
var globalLocationCache = newLocationCache()
func () *locationCache {
return &locationCache{cache: make(map[int]*time.Location)}
}
func ( *locationCache) ( int) *time.Location {
.lock.Lock()
defer .lock.Unlock()
, := .cache[]
if ! {
= time.FixedZone("", )
.cache[] =
}
return
}
var infinityTsEnabled = false
var infinityTsNegative time.Time
var infinityTsPositive time.Time
const (
infinityTsEnabledAlready = "pq: infinity timestamp enabled already"
infinityTsNegativeMustBeSmaller = "pq: infinity timestamp: negative value must be smaller (before) than positive"
)
func ( time.Time, time.Time) {
if infinityTsEnabled {
panic(infinityTsEnabledAlready)
}
if !.Before() {
panic(infinityTsNegativeMustBeSmaller)
}
infinityTsEnabled = true
infinityTsNegative =
infinityTsPositive =
}
func () {
infinityTsEnabled = false
}
func ( *time.Location, string) interface{} {
switch {
case "-infinity":
if infinityTsEnabled {
return infinityTsNegative
}
return []byte()
case "infinity":
if infinityTsEnabled {
return infinityTsPositive
}
return []byte()
}
, := ParseTimestamp(, )
if != nil {
panic()
}
return
}
:= .mustAtoi(, 0, )
:= + 3
:= .mustAtoi(, +1, )
.expect(, '-', )
:= + 3
:= .mustAtoi(, +1, )
:= + len("01-01") + 1
:= strings.HasSuffix(, " BC")
if {
+= 3
}
var , , int
if len() > {
.expect(, ' ', )
:= + 3
.expect(, ':', )
= .mustAtoi(, +1, )
:= + 3
.expect(, ':', )
= .mustAtoi(, +1, )
:= + 3
= .mustAtoi(, +1, )
}
var int
switch := []; {
case '-':
= -1
case '+':
= +1
default:
return time.Time{}, fmt.Errorf("expected '-' or '+' at position %v; got %v", , )
}
:= .mustAtoi(, +1, +3)
+= 3
var , int
if < len() && [] == ':' {
= .mustAtoi(, +1, +3)
+= 3
}
if < len() && [] == ':' {
= .mustAtoi(, +1, +3)
+= 3
}
= * (( * 60 * 60) + ( * 60) + )
}
var int
if {
= 1 -
+= 3
} else {
=
}
if < len() {
return time.Time{}, fmt.Errorf("expected end of input, got %v", [:])
}
:= time.Date(, time.Month(), ,
, , , ,
globalLocationCache.getLocation())
if !.After(infinityTsNegative) {
return []byte("-infinity")
if !.Before(infinityTsPositive) {
return []byte("infinity")
}
}
return FormatTimestamp()
}
:= false
for len() > 0 {
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |