Skip to content

Commit b2fce4b

Browse files
authored
Merge pull request #26 from arvidfm/type-conversion
Add missing type conversions
2 parents 10fb39c + e7fd059 commit b2fce4b

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

rows.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"database/sql/driver"
55
"errors"
66
"fmt"
7-
gms "github.com/dolthub/go-mysql-server/sql"
87
"io"
8+
9+
gms "github.com/dolthub/go-mysql-server/sql"
10+
"github.com/dolthub/go-mysql-server/sql/types"
911
)
1012

1113
var _ driver.Rows = (*doltRows)(nil)
@@ -58,6 +60,24 @@ func (rows *doltRows) Next(dest []driver.Value) error {
5860
if err != nil {
5961
return fmt.Errorf("error processing column %d: %w", i, err)
6062
}
63+
} else if geomValue, ok := nextRow[i].(types.GeometryValue); ok {
64+
dest[i] = geomValue.Serialize()
65+
} else if enumType, ok := rows.sch[i].Type.(gms.EnumType); ok {
66+
if v, _, err := enumType.Convert(nextRow[i]); err != nil {
67+
return fmt.Errorf("could not convert to expected enum type for column %d: %w", i, err)
68+
} else if enumStr, ok := enumType.At(int(v.(uint16))); !ok {
69+
return fmt.Errorf("not a valid enum index for column %d: %v", i, v)
70+
} else {
71+
dest[i] = enumStr
72+
}
73+
} else if setType, ok := rows.sch[i].Type.(gms.SetType); ok {
74+
if v, _, err := setType.Convert(nextRow[i]); err != nil {
75+
return fmt.Errorf("could not convert to expected set type for column %d: %w", i, err)
76+
} else if setStr, err := setType.BitsToString(v.(uint64)); err != nil {
77+
return fmt.Errorf("could not convert value to set string for column %d: %w", i, err)
78+
} else {
79+
dest[i] = setStr
80+
}
6181
} else {
6282
dest[i] = nextRow[i]
6383
}

smoke_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,43 @@ func TestQueryContextInitialization(t *testing.T) {
136136
require.NoError(t, conn.Close())
137137
}
138138

139+
// TestTypes asserts that various MySQL types are returned as the expected Go type by the driver.
140+
func TestTypes(t *testing.T) {
141+
conn, cleanupFunc := initializeTestDatabaseConnection(t, false)
142+
defer cleanupFunc()
143+
144+
ctx := context.Background()
145+
_, err := conn.ExecContext(ctx, `
146+
create table testtable (
147+
enum_col ENUM('a', 'b', 'c'),
148+
set_col SET('a', 'b', 'c'),
149+
json_col JSON,
150+
blob_col BLOB,
151+
text_col TEXT,
152+
geom_col POINT,
153+
date_col DATETIME
154+
);
155+
156+
insert into testtable values ('b', 'a,c', '{"key": 42}', 'data', 'text', Point(5, -5), NOW());
157+
`)
158+
require.NoError(t, err)
159+
160+
row := conn.QueryRowContext(ctx, "select * from testtable")
161+
vals := make([]any, 7)
162+
ptrs := make([]any, 7)
163+
for i := range vals {
164+
ptrs[i] = &vals[i]
165+
}
166+
require.NoError(t, row.Scan(ptrs...))
167+
require.Equal(t, "b", vals[0])
168+
require.Equal(t, "a,c", vals[1])
169+
require.Equal(t, `{"key": 42}`, vals[2])
170+
require.Equal(t, []byte(`data`), vals[3])
171+
require.Equal(t, "text", vals[4])
172+
require.IsType(t, []byte(nil), vals[5])
173+
require.IsType(t, time.Time{}, vals[6])
174+
}
175+
139176
// initializeTestDatabaseConnection create a test database called testdb and initialize a database/sql connection
140177
// using the Dolt driver. The connection, |conn|, is returned, and |cleanupFunc| is a function that the test function
141178
// should defer in order to properly dispose of test resources.

0 commit comments

Comments
 (0)