mirror of https://github.com/jackc/pgx.git
Add date transcoding
parent
c2ddbc0163
commit
ef470b1e30
|
@ -6,6 +6,7 @@ import (
|
|||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var literalPattern *regexp.Regexp = regexp.MustCompile(`\$\d+`)
|
||||
|
@ -43,6 +44,8 @@ func (c *Connection) SanitizeSql(sql string, args ...interface{}) (output string
|
|||
return strconv.FormatInt(int64(arg), 10)
|
||||
case int64:
|
||||
return strconv.FormatInt(int64(arg), 10)
|
||||
case time.Time:
|
||||
return c.QuoteString(arg.Format("2006-01-02 15:04:05.999999999 -0700"))
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(arg), 10)
|
||||
case uint8:
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
@ -88,6 +89,11 @@ func init() {
|
|||
// varchar -- same as text
|
||||
ValueTranscoders[Oid(1043)] = ValueTranscoders[Oid(25)]
|
||||
|
||||
// date
|
||||
ValueTranscoders[Oid(1082)] = &ValueTranscoder{
|
||||
DecodeText: decodeDateFromText,
|
||||
EncodeTo: encodeDate}
|
||||
|
||||
// use text transcoder for anything we don't understand
|
||||
defaultTranscoder = ValueTranscoders[Oid(25)]
|
||||
}
|
||||
|
@ -262,3 +268,19 @@ func encodeBytea(w *MessageWriter, value interface{}) {
|
|||
w.Write(int32(len(b)))
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
func decodeDateFromText(mr *MessageReader, size int32) interface{} {
|
||||
s := mr.ReadByteString(size)
|
||||
t, err := time.ParseInLocation("2006-01-02", s, time.Local)
|
||||
if err != nil {
|
||||
panic("Can't decode date")
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func encodeDate(w *MessageWriter, value interface{}) {
|
||||
t := value.(time.Time)
|
||||
s := t.Format("2006-01-02")
|
||||
w.Write(int32(len(s)))
|
||||
w.WriteString(s)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package pgx_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDateTranscode(t *testing.T) {
|
||||
conn := getSharedConnection()
|
||||
|
||||
actualDate := time.Date(2013, 1, 2, 0, 0, 0, 0, time.Local)
|
||||
|
||||
var v interface{}
|
||||
var d time.Time
|
||||
|
||||
v = mustSelectValue(t, conn, "select $1::date", actualDate)
|
||||
d = v.(time.Time)
|
||||
if !actualDate.Equal(d) {
|
||||
t.Errorf("Did not transcode date successfully: %v is not %v", v, actualDate)
|
||||
}
|
||||
|
||||
mustPrepare(t, conn, "testTranscode", "select $1::date")
|
||||
defer func() {
|
||||
if err := conn.Deallocate("testTranscode"); err != nil {
|
||||
t.Fatalf("Unable to deallocate prepared statement: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
v = mustSelectValue(t, conn, "testTranscode", actualDate)
|
||||
d = v.(time.Time)
|
||||
if !actualDate.Equal(d) {
|
||||
t.Errorf("Did not transcode date successfully: %v is not %v", v, actualDate)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue