Skip to content
This repository
branch: master
Daniel Farina January 05, 2013
file 122 lines (108 sloc) 2.428 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
package pq

import (
"database/sql/driver"
"encoding/hex"
"fmt"
"strconv"
"time"
)

func encode(x interface{}, pgtypoid oid) []byte {
switch v := x.(type) {
case int64:
return []byte(fmt.Sprintf("%d", v))
case float32, float64:
return []byte(fmt.Sprintf("%f", v))
case []byte:
if pgtypoid == t_bytea {
return []byte(fmt.Sprintf("\\x%x", v))
}

return v
case string:
if pgtypoid == t_bytea {
return []byte(fmt.Sprintf("\\x%x", v))
}

return []byte(v)
case bool:
return []byte(fmt.Sprintf("%t", v))
case time.Time:
return []byte(v.Format(time.RFC3339Nano))
default:
errorf("encode: unknown type for %T", v)
}

panic("not reached")
}

func decode(s []byte, typ oid) interface{} {
switch typ {
case t_bytea:
s = s[2:] // trim off "\\x"
d := make([]byte, hex.DecodedLen(len(s)))
_, err := hex.Decode(d, s)
if err != nil {
errorf("%s", err)
}
return d
case t_timestamptz:
return mustParse("2006-01-02 15:04:05-07", typ, s)
case t_timestamp:
return mustParse("2006-01-02 15:04:05", typ, s)
case t_time:
return mustParse("15:04:05", typ, s)
case t_timetz:
return mustParse("15:04:05-07", typ, s)
case t_date:
return mustParse("2006-01-02", typ, s)
case t_bool:
return s[0] == 't'
case t_int8, t_int2, t_int4:
i, err := strconv.ParseInt(string(s), 10, 64)
if err != nil {
errorf("%s", err)
}
return i
case t_float4, t_float8:
bits := 64
if typ == t_float4 {
bits = 32
}
f, err := strconv.ParseFloat(string(s), bits)
if err != nil {
errorf("%s", err)
}
return f
}

return s
}

func mustParse(f string, typ oid, s []byte) time.Time {
str := string(s)

// Special case until time.Parse bug is fixed:
// http://code.google.com/p/go/issues/detail?id=3487
if str[len(str)-2] == '.' {
str += "0"
}

// check for a 30-minute-offset timezone
if (typ == t_timestamptz || typ == t_timetz) &&
str[len(str)-3] == ':' {
f += ":00"
}
t, err := time.Parse(f, str)
if err != nil {
errorf("decode: %s", err)
}
return t
}

type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}

// Scan implements the Scanner interface.
func (nt *NullTime) Scan(value interface{}) error {
nt.Time, nt.Valid = value.(time.Time)
return nil
}

// Value implements the driver Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}
Something went wrong with that request. Please try again.