Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support driver.Valuer in String and FixedString columns #946

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/column/fixed_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package column

import (
"database/sql"
"database/sql/driver"
"encoding"
"fmt"
"github.com/ClickHouse/ch-go/proto"
Expand Down Expand Up @@ -153,15 +154,38 @@ func (col *FixedString) AppendRow(v interface{}) (err error) {
return err
}
default:
if s, ok := v.(fmt.Stringer); ok {
return col.AppendRow(s.String())
} else {
if s, ok := v.(driver.Valuer); ok {
val, err := s.Value()
if err != nil {
return &ColumnConverterError{
Op: "AppendRow",
To: "String",
From: fmt.Sprintf("%T", s),
Hint: "could not get driver.Valuer value",
}
}

if s, ok := val.(string); ok {
return col.AppendRow(s)
}

return &ColumnConverterError{
Op: "AppendRow",
To: "FixedString",
To: "String",
From: fmt.Sprintf("%T", v),
Hint: "driver.Valuer value is not a string",
}
}

if s, ok := v.(fmt.Stringer); ok {
return col.AppendRow(s.String())
}

return &ColumnConverterError{
Op: "AppendRow",
To: "String",
From: fmt.Sprintf("%T", v),
}
}
col.col.Append(data)
return nil
Expand Down
30 changes: 27 additions & 3 deletions lib/column/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package column

import (
"database/sql"
"database/sql/driver"
"encoding"
"fmt"
"github.com/ClickHouse/ch-go/proto"
Expand Down Expand Up @@ -115,15 +116,38 @@ func (col *String) AppendRow(v interface{}) error {
case nil:
col.col.Append("")
default:
if s, ok := v.(fmt.Stringer); ok {
return col.AppendRow(s.String())
} else {
if s, ok := v.(driver.Valuer); ok {
val, err := s.Value()
if err != nil {
return &ColumnConverterError{
Op: "AppendRow",
To: "String",
From: fmt.Sprintf("%T", s),
Hint: "could not get driver.Valuer value",
}
}

if s, ok := val.(string); ok {
return col.AppendRow(s)
}

return &ColumnConverterError{
Op: "AppendRow",
To: "String",
From: fmt.Sprintf("%T", v),
Hint: "driver.Valuer value is not a string",
}
}

if s, ok := v.(fmt.Stringer); ok {
return col.AppendRow(s.String())
}

return &ColumnConverterError{
Op: "AppendRow",
To: "String",
From: fmt.Sprintf("%T", v),
}
}
return nil
}
Expand Down
42 changes: 42 additions & 0 deletions tests/fixed_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package tests
import (
"context"
"crypto/rand"
"fmt"
"github.com/stretchr/testify/require"
"testing"

Expand Down Expand Up @@ -362,3 +363,44 @@ func TestFixedStringFlush(t *testing.T) {
}
require.Equal(t, 1000, i)
}

func TestFixedStringFromDriverValuerType(t *testing.T) {
conn, err := GetConnection("native", nil, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
ctx := context.Background()

require.NoError(t, err)
require.NoError(t, conn.Ping(ctx))
if !CheckMinServerServerVersion(conn, 21, 9, 0) {
t.Skip(fmt.Errorf("unsupported clickhouse version"))
return
}
const ddl = `
CREATE TABLE test_fixed_string (
Col1 FixedString(5)
, Col2 FixedString(5)
) Engine MergeTree() ORDER BY tuple()
`
defer func() {
conn.Exec(ctx, "DROP TABLE test_fixed_string")
}()
require.NoError(t, conn.Exec(ctx, ddl))
batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_fixed_string")
require.NoError(t, err)

type data struct {
Col1 string `ch:"Col1"`
Col2 testStringSerializer `ch:"Col2"`
}
require.NoError(t, batch.AppendStruct(&data{
Col1: "Value",
Col2: testStringSerializer{"Value"},
}))
require.NoError(t, batch.Send())

var dest data
require.NoError(t, conn.QueryRow(ctx, "SELECT * FROM test_fixed_string").ScanStruct(&dest))
assert.Equal(t, "Value", dest.Col1)
assert.Equal(t, testStringSerializer{"Value"}, dest.Col2)
}
58 changes: 58 additions & 0 deletions tests/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package tests
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"github.com/stretchr/testify/require"
"testing"
Expand Down Expand Up @@ -309,3 +310,60 @@ func TestStringFlush(t *testing.T) {
}
require.Equal(t, 1000, i)
}

type testStringSerializer struct {
val string
}

func (c testStringSerializer) Value() (driver.Value, error) {
return c.val, nil
}

func (c *testStringSerializer) Scan(src any) error {
if t, ok := src.(string); ok {
*c = testStringSerializer{val: t}
return nil
}
return fmt.Errorf("cannot scan %T into testStringSerializer", src)
}

func TestStringFromDriverValuerType(t *testing.T) {
conn, err := GetConnection("native", nil, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
ctx := context.Background()

require.NoError(t, err)
require.NoError(t, conn.Ping(ctx))
if !CheckMinServerServerVersion(conn, 21, 9, 0) {
t.Skip(fmt.Errorf("unsupported clickhouse version"))
return
}
const ddl = `
CREATE TABLE test_string (
Col1 String
, Col2 String
) Engine MergeTree() ORDER BY tuple()
`
defer func() {
conn.Exec(ctx, "DROP TABLE test_string")
}()
require.NoError(t, conn.Exec(ctx, ddl))
batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_string")
require.NoError(t, err)

type data struct {
Col1 string `ch:"Col1"`
Col2 testStringSerializer `ch:"Col2"`
}
require.NoError(t, batch.AppendStruct(&data{
Col1: "Value",
Col2: testStringSerializer{"Value"},
}))
require.NoError(t, batch.Send())

var dest data
require.NoError(t, conn.QueryRow(ctx, "SELECT * FROM test_string").ScanStruct(&dest))
assert.Equal(t, "Value", dest.Col1)
assert.Equal(t, testStringSerializer{"Value"}, dest.Col2)
}