From 8aac4433064883abe7d29d6b1e5191e0a2de3001 Mon Sep 17 00:00:00 2001 From: Kuba Kaflik Date: Mon, 29 Jan 2024 12:47:21 +0100 Subject: [PATCH] Fix bind for seconds scale DateTime (#1184) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fixing timezone problem * Add test case and fix statement generation --------- Co-authored-by: Devop- Co-authored-by: EyĆ¼p SARAL <127626139+Eyup-Devop@users.noreply.github.com> --- bind.go | 8 +++++--- tests/issues/1169_test.go | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/issues/1169_test.go diff --git a/bind.go b/bind.go index 7f5ac393de..6fd6676963 100644 --- a/bind.go +++ b/bind.go @@ -59,8 +59,10 @@ func DateNamed(name string, value time.Time, scale TimeUnit) driver.NamedDateVal } } -var bindNumericRe = regexp.MustCompile(`\$[0-9]+`) -var bindPositionalRe = regexp.MustCompile(`[^\\][?]`) +var ( + bindNumericRe = regexp.MustCompile(`\$[0-9]+`) + bindPositionalRe = regexp.MustCompile(`[^\\][?]`) +) func bind(tz *time.Location, query string, args ...any) (string, error) { if len(args) == 0 { @@ -262,7 +264,7 @@ func formatTime(tz *time.Location, scale TimeUnit, value time.Time) (string, err return fmt.Sprintf("toDateTime64('%s', %d)", value.Format(fmt.Sprintf("2006-01-02 15:04:05.%0*d", int(scale*3), 0)), int(scale*3)), nil } if scale == Seconds { - return value.Format(fmt.Sprintf("toDateTime('2006-01-02 15:04:05', '%s')", value.Location().String())), nil + return fmt.Sprintf("toDateTime('%s', '%s')", value.Format("2006-01-02 15:04:05"), value.Location().String()), nil } return fmt.Sprintf("toDateTime64('%s', %d, '%s')", value.Format(fmt.Sprintf("2006-01-02 15:04:05.%0*d", int(scale*3), 0)), int(scale*3), value.Location().String()), nil } diff --git a/tests/issues/1169_test.go b/tests/issues/1169_test.go new file mode 100644 index 0000000000..e37aea9881 --- /dev/null +++ b/tests/issues/1169_test.go @@ -0,0 +1,42 @@ +package issues + +import ( + "context" + "testing" + "time" + + "github.com/ClickHouse/clickhouse-go/v2" + clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests" + "github.com/stretchr/testify/require" +) + +func Test1169(t *testing.T) { + var ( + conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{ + "max_execution_time": 60, + "allow_experimental_object_type": true, + }, nil, &clickhouse.Compression{ + Method: clickhouse.CompressionLZ4, + }) + ) + ctx := context.Background() + require.NoError(t, err) + const ddl = "CREATE TABLE test_1169 (Col1 DateTime) Engine MergeTree() ORDER BY tuple()" + require.NoError(t, conn.Exec(ctx, ddl)) + defer func() { + conn.Exec(ctx, "DROP TABLE IF EXISTS test_1169") + }() + + location := time.FixedZone("Etc/GMT+2", -int(time.Hour*2/time.Second)) + date, err := time.ParseInLocation("2006-01-02 15:04:05", "2024-01-03 11:22:33", location) + require.NoError(t, err) + + err = conn.Exec(ctx, "INSERT INTO test_1169 (Col1) VALUES (?)", date) + require.NoError(t, err) + + // select + var actualDate time.Time + err = conn.QueryRow(ctx, "SELECT Col1 FROM test_1169").Scan(&actualDate) + require.NoError(t, err) + require.Equal(t, actualDate.In(location), date) +}