Skip to content

Commit

Permalink
Support bool alias type (#1156)
Browse files Browse the repository at this point in the history
* support custom bool

* use generic sql.Scanner lib/column/bool.go

---------

Co-authored-by: Kuba Kaflik <jakub@kaflik.pl>
  • Loading branch information
yogasw and jkaflik authored Dec 19, 2023
1 parent 1466b2c commit cb7bdb4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/column/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (col *Bool) ScanRow(dest any, row int) error {
case **bool:
*d = new(bool)
**d = col.row(row)
case *sql.NullBool:
case sql.Scanner:
return d.Scan(col.row(row))
default:
return &ColumnConverterError{
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 cb7bdb4

Please sign in to comment.