Skip to content

Commit

Permalink
fix encoding date value #12
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaifullin committed Feb 3, 2018
1 parent d5f58d0 commit 95e1832
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
9 changes: 9 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/suite"
"net/http"
"time"
)

var (
Expand Down Expand Up @@ -83,6 +84,14 @@ func (s *connSuite) TestExec() {
"",
[]interface{}{int64(3), Array([]int16{1, 2})},
},
{
"INSERT INTO data (d, t) VALUES (?, ?)",
"",
[]interface{}{
Date(time.Date(2016, 4, 4, 0, 0, 0, 0, time.Local)),
time.Date(2016, 4, 4, 0, 0, 0, 0, time.Local),
},
},
}
for _, tc := range testCases {
result, err := s.conn.Exec(tc.query, tc.args...)
Expand Down
2 changes: 1 addition & 1 deletion encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (e *textEncoder) Encode(value driver.Value) string {
case []byte:
return string(v)
case time.Time:
return quote(formatTime(v))
return formatTime(v)
}

vv := reflect.ValueOf(value)
Expand Down
4 changes: 2 additions & 2 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func TestTextEncoder(t *testing.T) {
{float32(1), "1"},
{float64(1), "1"},
{dt, "'2011-03-06 06:20:00'"},
{d, "'2012-05-31'"},
{d, "'2012-05-31 00:00:00'"},
{"hello", "'hello'"},
{[]byte("hello"), "hello"},
{`\\'hello`, `'\\\\\'hello'`},
{[]byte(`\\'hello`), `\\'hello`},
{[]int32{1, 2}, "[1,2]"},
{&d, "'2012-05-31'"},
{&d, "'2012-05-31 00:00:00'"},
}

enc := new(textEncoder)
Expand Down
10 changes: 5 additions & 5 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func unqoute(s string) string {
}

func formatTime(value time.Time) string {
h, m, s := value.Clock()
if (h + m + s + value.Nanosecond()) == 0 {
return value.Format(dateFormat)
}
return value.Format(timeFormat)
return quote(value.Format(timeFormat))
}

func formatDate(value time.Time) string {
return quote(value.Format(dateFormat))
}

func readResponse(response *http.Response) (result []byte, err error) {
Expand Down
12 changes: 9 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ func Array(v interface{}) driver.Valuer {
}

// Date returns date for t
func Date(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
func Date(t time.Time) driver.Valuer {
return date(t)
}

type array struct {
Expand All @@ -24,3 +23,10 @@ type array struct {
func (a array) Value() (driver.Value, error) {
return []byte(textEncode.Encode(a.v)), nil
}

type date time.Time

// Value implements driver.Valuer
func (d date) Value() (driver.Value, error) {
return []byte(formatDate(time.Time(d))), nil
}
6 changes: 4 additions & 2 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func TestArray(t *testing.T) {
}

func TestDate(t *testing.T) {
dt := time.Date(2016, 4, 4, 11, 22, 33, 0, time.Local)
d := time.Date(2016, 4, 4, 0, 0, 0, 0, time.Local)
assert.Equal(t, d, Date(dt))
dv, err := Date(d).Value()
if assert.NoError(t, err) {
assert.Equal(t, []byte("'2016-04-04'"), dv)
}
}

0 comments on commit 95e1832

Please sign in to comment.