Skip to content

Commit

Permalink
support custom bool
Browse files Browse the repository at this point in the history
  • Loading branch information
yogasw committed Dec 8, 2023
1 parent a6e001e commit cd85a47
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/column/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func (col *Bool) ScanRow(dest any, row int) error {
case *sql.NullBool:
return d.Scan(col.row(row))
default:
if scan, ok := dest.(sql.Scanner); ok {
return scan.Scan(col.row(row))
}
return &ColumnConverterError{
Op: "ScanRow",
To: fmt.Sprintf("%T", dest),
Expand Down
33 changes: 33 additions & 0 deletions tests/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,36 @@ func TestBoolValuer(t *testing.T) {
i += 1
}
}

type CustomBool bool

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

func TestCustomBool(t *testing.T) {
conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
ctx := context.Background()
const ddl = `
CREATE TABLE bool_custom (
Col1 Bool
) Engine MergeTree() ORDER BY tuple()
`
conn.Exec(ctx, "DROP TABLE bool_custom")
require.NoError(t, conn.Exec(ctx, ddl))
require.NoError(t, err)
batch, err := conn.PrepareBatch(ctx, "INSERT INTO bool_custom")
require.NoError(t, err)
require.NoError(t, batch.Append(true))
require.NoError(t, batch.Send())
row := conn.QueryRow(ctx, "SELECT * FROM bool_custom")
var col1 CustomBool
require.NoError(t, row.Scan(&col1))
require.Equal(t, true, bool(col1))
}

0 comments on commit cd85a47

Please sign in to comment.