Skip to content

Commit

Permalink
Go bindings support time.Time (#968)
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Jastrzebski <[email protected]>
  • Loading branch information
haaawk authored Jan 30, 2024
1 parent 4332bb6 commit 3c7c2f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion bindings/go/libsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,25 @@ func (r *rows) Next(dest []sqldriver.Value) error {
if statusCode != 0 {
return libsqlError(fmt.Sprint("failed to get string for column ", i), statusCode, errMsg)
}
dest[i] = C.GoString(ptr)
str := C.GoString(ptr)
C.libsql_free_string(ptr)
for _, format := range []string{
"2006-01-02 15:04:05.999999999-07:00",
"2006-01-02T15:04:05.999999999-07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02T15:04:05.999999999",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
"2006-01-02 15:04",
"2006-01-02T15:04",
"2006-01-02",
} {
if t, err := time.ParseInLocation(format, str, time.UTC); err == nil {
dest[i] = t
return nil
}
}
dest[i] = str
}
}
return nil
Expand Down
5 changes: 4 additions & 1 deletion bindings/go/libsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,9 @@ func TestDataTypes(t *testing.T) {
float8 float64
nullFloat sql.NullFloat64
bytea []byte
Time time.Time
)
db.t.FatalOnError(db.QueryRowContext(db.ctx, "SELECT 'foobar' as text, NULL as text, NULL as integer, 42 as integer, 1 as boolean, X'000102' as bytea, 3.14 as float8, NULL as float8;").Scan(&text, &nullText, &nullInteger, &integer, &boolean, &bytea, &float8, &nullFloat))
db.t.FatalOnError(db.QueryRowContext(db.ctx, "SELECT 'foobar' as text, NULL as text, NULL as integer, 42 as integer, 1 as boolean, X'000102' as bytea, 3.14 as float8, NULL as float8, '0001-01-01 01:00:00+00:00' as time;").Scan(&text, &nullText, &nullInteger, &integer, &boolean, &bytea, &float8, &nullFloat, &Time))
switch {
case text != "foobar":
t.Error("value mismatch - text")
Expand All @@ -717,6 +718,8 @@ func TestDataTypes(t *testing.T) {
t.Error("value mismatch - bytea")
case nullFloat.Valid:
t.Error("null float is valid")
case !Time.Equal(time.Time{}.Add(time.Hour)):
t.Error("value mismatch - time")
}
}

Expand Down

0 comments on commit 3c7c2f3

Please sign in to comment.