Skip to content

Commit

Permalink
some finishing up
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabogatsch committed Jan 3, 2025
1 parent 81c5c2b commit 34e60b9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 45 deletions.
22 changes: 4 additions & 18 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,6 @@ func (s *Stmt) bindInterval(val Interval, n int) (returnState, error) {
return returnState(state), nil
}

func (s *Stmt) bindDecimal(val Decimal, n int) (returnState, error) {
v, err := hugeIntFromNative(val.Value)
if err != nil {
return stateError, err
}

decimal := C.duckdb_decimal{
width: C.uint8_t(val.Width),
scale: C.uint8_t(val.Scale),
value: v,
}
state := C.duckdb_bind_decimal(*s.stmt, C.idx_t(n+1), decimal)
return returnState(state), nil
}

func (s *Stmt) bindTimestamp(val driver.NamedValue, t Type, n int) (returnState, error) {
ts, err := getCTimestamp(t, val.Value)
if err != nil {
Expand Down Expand Up @@ -250,10 +235,9 @@ func (s *Stmt) bindComplexValue(val driver.NamedValue, n int) (returnState, erro
TYPE_ARRAY, TYPE_ENUM:
// FIXME: for timestamps: distinguish between timestamp[_s|ms|ns] once available.
// FIXME: for other types: duckdb_param_logical_type once available, then create duckdb_value + duckdb_bind_value
// FIXME: for other types: implement NamedValueChecker to support custom data types.
name := typeToStringMap[t]
return stateError, addIndexToError(unsupportedTypeError(name), n+1)
case TYPE_UUID:
// TODO: need to create value?
}
return stateError, addIndexToError(unsupportedTypeError(unknownTypeErrMsg), n+1)
}
Expand Down Expand Up @@ -281,7 +265,9 @@ func (s *Stmt) bindValue(val driver.NamedValue, n int) (returnState, error) {
case *big.Int:
return s.bindHugeint(v, n)
case Decimal:
return s.bindDecimal(v, n)
// FIXME: implement NamedValueChecker to support custom data types.
name := typeToStringMap[TYPE_DECIMAL]
return stateError, addIndexToError(unsupportedTypeError(name), n+1)
case uint8:
state := C.duckdb_bind_uint8(*s.stmt, C.idx_t(n+1), C.uint8_t(v))
return returnState(state), nil
Expand Down
10 changes: 5 additions & 5 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func (u *UUID) String() string {
// The value is computed as: upper * 2^64 + lower

func hugeIntToUUID(hi C.duckdb_hugeint) []byte {
var uuid [uuid_length]byte
// We need to flip the sign bit of the signed hugeint to transform it to UUID bytes
binary.BigEndian.PutUint64(uuid[:8], uint64(hi.upper)^1<<63)
binary.BigEndian.PutUint64(uuid[8:], uint64(hi.lower))
return uuid[:]
// Flip the sign bit of the signed hugeint to transform it to UUID bytes.
var val [uuid_length]byte
binary.BigEndian.PutUint64(val[:8], uint64(hi.upper)^1<<63)
binary.BigEndian.PutUint64(val[8:], uint64(hi.lower))
return val[:]
}

func uuidToHugeInt(uuid UUID) C.duckdb_hugeint {
Expand Down
22 changes: 0 additions & 22 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,28 +444,6 @@ func TestDecimal(t *testing.T) {
}
})

t.Run("Bind and INSERT a DECIMAL into a table", func(t *testing.T) {
_, err := db.Exec(`CREATE TABLE tbl (d1 DECIMAL(5, 2), d2 DECIMAL(22, 3))`)
require.NoError(t, err)

bigNumber, success := new(big.Int).SetString("1234567890123456789234", 10)
require.Equal(t, true, success)
d1 := Decimal{Value: big.NewInt(-12345), Width: 5, Scale: 2}
d2 := Decimal{Value: bigNumber, Width: 22, Scale: 3}

_, err = db.Exec(`INSERT INTO tbl VALUES (?, ?)`, d1, d2)
require.NoError(t, err)

var res Decimal
err = db.QueryRow(`SELECT d1 FROM tbl`).Scan(&res)
require.NoError(t, err)
compareDecimal(t, d1, res)

err = db.QueryRow(`SELECT d2 FROM tbl`).Scan(&res)
require.NoError(t, err)
compareDecimal(t, d2, res)
})

require.NoError(t, db.Close())
}

Expand Down

0 comments on commit 34e60b9

Please sign in to comment.