From 5e4e8531e8bd5917b35c4ee510a444156496ba13 Mon Sep 17 00:00:00 2001 From: chenpeng Date: Thu, 18 Jul 2019 18:57:42 +0800 Subject: [PATCH 1/3] bugfix: literalTime use t.UTC() , change the origin location, so , time insert to database be changed and error --- sql_dialect.go | 2 +- sql_dialect_test.go | 39 ++++++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/sql_dialect.go b/sql_dialect.go index e693df7a..fc309106 100644 --- a/sql_dialect.go +++ b/sql_dialect.go @@ -891,7 +891,7 @@ func (d *sqlDialect) literalTime(b sb.SQLBuilder, t time.Time) { d.placeHolderSQL(b, t) return } - d.Literal(b, t.UTC().Format(d.dialectOptions.TimeFormat)) + d.Literal(b, t.Format(d.dialectOptions.TimeFormat)) } // Generates SQL for a Float Value diff --git a/sql_dialect_test.go b/sql_dialect_test.go index b9a47d51..3ea088af 100644 --- a/sql_dialect_test.go +++ b/sql_dialect_test.go @@ -3,6 +3,7 @@ package goqu import ( "database/sql/driver" "fmt" + "github.com/stretchr/testify/require" "regexp" "testing" "time" @@ -1173,30 +1174,38 @@ func (dts *dialectTestSuite) TestLiteral_BoolTypes() { } func (dts *dialectTestSuite) TestLiteral_TimeTypes() { + asiaShanghai, err := time.LoadLocation("Asia/Shanghai") + require.NoError(dts.T(), err) + t := dts.T() d := sqlDialect{DefaultDialectOptions()} - now := time.Now().UTC() var nt *time.Time + testDatas := []time.Time{ + time.Now().UTC(), + time.Now().In(asiaShanghai), + } - b := sb.NewSQLBuilder(false) - d.Literal(b.Clear(), now) - dts.assertNotPreparedSQL(t, b, "'"+now.Format(time.RFC3339Nano)+"'") + for _, now := range testDatas { + b := sb.NewSQLBuilder(false) + d.Literal(b.Clear(), now) + dts.assertNotPreparedSQL(t, b, "'"+now.Format(time.RFC3339Nano)+"'") - d.Literal(b.Clear(), &now) - dts.assertNotPreparedSQL(t, b, "'"+now.Format(time.RFC3339Nano)+"'") + d.Literal(b.Clear(), &now) + dts.assertNotPreparedSQL(t, b, "'"+now.Format(time.RFC3339Nano)+"'") - d.Literal(b.Clear(), nt) - dts.assertNotPreparedSQL(t, b, "NULL") + d.Literal(b.Clear(), nt) + dts.assertNotPreparedSQL(t, b, "NULL") - b = sb.NewSQLBuilder(true) - d.Literal(b.Clear(), now) - dts.assertPreparedSQL(t, b, "?", []interface{}{now}) + b = sb.NewSQLBuilder(true) + d.Literal(b.Clear(), now) + dts.assertPreparedSQL(t, b, "?", []interface{}{now}) - d.Literal(b.Clear(), &now) - dts.assertPreparedSQL(t, b, "?", []interface{}{now}) + d.Literal(b.Clear(), &now) + dts.assertPreparedSQL(t, b, "?", []interface{}{now}) - d.Literal(b.Clear(), nt) - dts.assertPreparedSQL(t, b, "NULL", emptyArgs) + d.Literal(b.Clear(), nt) + dts.assertPreparedSQL(t, b, "NULL", emptyArgs) + } } func (dts *dialectTestSuite) TestLiteral_NilTypes() { From 90b418b94d5f1c69ae8805cf3f7837f2972f0f54 Mon Sep 17 00:00:00 2001 From: chenpeng Date: Fri, 19 Jul 2019 09:48:11 +0800 Subject: [PATCH 2/3] refactor: TestLiteral_TimeTypes --- sql_dialect_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sql_dialect_test.go b/sql_dialect_test.go index 3ea088af..e9e46901 100644 --- a/sql_dialect_test.go +++ b/sql_dialect_test.go @@ -1174,12 +1174,11 @@ func (dts *dialectTestSuite) TestLiteral_BoolTypes() { } func (dts *dialectTestSuite) TestLiteral_TimeTypes() { - asiaShanghai, err := time.LoadLocation("Asia/Shanghai") - require.NoError(dts.T(), err) - t := dts.T() d := sqlDialect{DefaultDialectOptions()} var nt *time.Time + asiaShanghai, err := time.LoadLocation("Asia/Shanghai") + require.NoError(t, err) testDatas := []time.Time{ time.Now().UTC(), time.Now().In(asiaShanghai), From e437c144a455799ed1d9fad73d7445f04137e979 Mon Sep 17 00:00:00 2001 From: chenpeng Date: Fri, 19 Jul 2019 10:26:36 +0800 Subject: [PATCH 3/3] bugfix: TestLiteral_TimeTypes : Using a reference for the variable on range scope --- sql_dialect_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sql_dialect_test.go b/sql_dialect_test.go index e9e46901..63a37a8a 100644 --- a/sql_dialect_test.go +++ b/sql_dialect_test.go @@ -3,11 +3,12 @@ package goqu import ( "database/sql/driver" "fmt" - "github.com/stretchr/testify/require" "regexp" "testing" "time" + "github.com/stretchr/testify/require" + "github.com/doug-martin/goqu/v7/exp" "github.com/doug-martin/goqu/v7/internal/errors" "github.com/doug-martin/goqu/v7/internal/sb" @@ -1184,7 +1185,8 @@ func (dts *dialectTestSuite) TestLiteral_TimeTypes() { time.Now().In(asiaShanghai), } - for _, now := range testDatas { + for _, n := range testDatas { + var now = n b := sb.NewSQLBuilder(false) d.Literal(b.Clear(), now) dts.assertNotPreparedSQL(t, b, "'"+now.Format(time.RFC3339Nano)+"'")