Add date transcoding
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var literalPattern *regexp.Regexp = regexp.MustCompile(`\$\d+`)
|
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)
|
return strconv.FormatInt(int64(arg), 10)
|
||||||
case int64:
|
case int64:
|
||||||
return strconv.FormatInt(int64(arg), 10)
|
return strconv.FormatInt(int64(arg), 10)
|
||||||
|
case time.Time:
|
||||||
|
return c.QuoteString(arg.Format("2006-01-02 15:04:05.999999999 -0700"))
|
||||||
case uint:
|
case uint:
|
||||||
return strconv.FormatUint(uint64(arg), 10)
|
return strconv.FormatUint(uint64(arg), 10)
|
||||||
case uint8:
|
case uint8:
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -88,6 +89,11 @@ func init() {
|
|||||||
// varchar -- same as text
|
// varchar -- same as text
|
||||||
ValueTranscoders[Oid(1043)] = ValueTranscoders[Oid(25)]
|
ValueTranscoders[Oid(1043)] = ValueTranscoders[Oid(25)]
|
||||||
|
|
||||||
|
// date
|
||||||
|
ValueTranscoders[Oid(1082)] = &ValueTranscoder{
|
||||||
|
DecodeText: decodeDateFromText,
|
||||||
|
EncodeTo: encodeDate}
|
||||||
|
|
||||||
// use text transcoder for anything we don't understand
|
// use text transcoder for anything we don't understand
|
||||||
defaultTranscoder = ValueTranscoders[Oid(25)]
|
defaultTranscoder = ValueTranscoders[Oid(25)]
|
||||||
}
|
}
|
||||||
@@ -262,3 +268,19 @@ func encodeBytea(w *MessageWriter, value interface{}) {
|
|||||||
w.Write(int32(len(b)))
|
w.Write(int32(len(b)))
|
||||||
w.Write(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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user