Skip to content

Commit 2c2cb73

Browse files
authored
Peco 1016 (#157)
2 parents d1e4dc5 + b7780c8 commit 2c2cb73

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

connection.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql/driver"
66
"fmt"
7+
"strconv"
78
"time"
89

910
"github.com/databricks/databricks-sql-go/driverctx"
@@ -342,7 +343,7 @@ func (c *conn) executeStatement(ctx context.Context, query string, args []driver
342343
}
343344

344345
func namedValuesToTSparkParams(args []driver.NamedValue) []*cli_service.TSparkParameter {
345-
var ts []string = []string{"STRING", "DOUBLE", "BOOLEAN"}
346+
var ts []string = []string{"STRING", "DOUBLE", "BOOLEAN", "TIMESTAMP", "FLOAT", "INTEGER", "TINYINT", "SMALLINT", "BIGINT"}
346347
var params []*cli_service.TSparkParameter
347348
for i := range args {
348349
arg := args[i]
@@ -366,47 +367,51 @@ func namedValuesToTSparkParams(args []driver.NamedValue) []*cli_service.TSparkPa
366367
case int:
367368
f := float64(t)
368369
param.Value.DoubleValue = &f
369-
param.Type = &ts[1]
370+
param.Type = &ts[5]
370371
case uint:
371372
f := float64(t)
372373
param.Value.DoubleValue = &f
373-
param.Type = &ts[1]
374+
param.Type = &ts[5]
374375
case int8:
375376
f := float64(t)
376377
param.Value.DoubleValue = &f
377-
param.Type = &ts[1]
378+
param.Type = &ts[6]
378379
case uint8:
379380
f := float64(t)
380381
param.Value.DoubleValue = &f
381-
param.Type = &ts[1]
382+
param.Type = &ts[6]
382383
case int16:
383384
f := float64(t)
384385
param.Value.DoubleValue = &f
385-
param.Type = &ts[1]
386+
param.Type = &ts[7]
386387
case uint16:
387388
f := float64(t)
388389
param.Value.DoubleValue = &f
389-
param.Type = &ts[1]
390+
param.Type = &ts[7]
390391
case int32:
391392
f := float64(t)
392393
param.Value.DoubleValue = &f
393-
param.Type = &ts[1]
394+
param.Type = &ts[5]
394395
case uint32:
395396
f := float64(t)
396397
param.Value.DoubleValue = &f
397-
param.Type = &ts[1]
398+
param.Type = &ts[5]
398399
case int64:
399-
f := float64(t)
400-
param.Value.DoubleValue = &f
401-
param.Type = &ts[1]
400+
s := strconv.FormatInt(t, 10)
401+
param.Value.StringValue = &s
402+
param.Type = &ts[8]
402403
case uint64:
403-
f := float64(t)
404-
param.Value.DoubleValue = &f
405-
param.Type = &ts[1]
404+
s := strconv.FormatUint(t, 10)
405+
param.Value.StringValue = &s
406+
param.Type = &ts[8]
406407
case float32:
407408
f := float64(t)
408409
param.Value.DoubleValue = &f
409-
param.Type = &ts[1]
410+
param.Type = &ts[4]
411+
case time.Time:
412+
s := t.String()
413+
param.Value.StringValue = &s
414+
param.Type = &ts[3]
410415
default:
411416
s := fmt.Sprintf("%s", arg.Value)
412417
param.Value.StringValue = &s

statement_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package dbsql
33
import (
44
"context"
55
"database/sql/driver"
6+
"testing"
7+
"time"
8+
69
"github.com/apache/thrift/lib/go/thrift"
710
"github.com/databricks/databricks-sql-go/internal/cli_service"
811
"github.com/databricks/databricks-sql-go/internal/client"
912
"github.com/databricks/databricks-sql-go/internal/config"
1013
"github.com/stretchr/testify/assert"
11-
"testing"
1214
)
1315

1416
func TestStmt_Close(t *testing.T) {
@@ -164,3 +166,13 @@ func TestStmt_QueryContext(t *testing.T) {
164166
assert.Equal(t, testQuery, savedQueryString)
165167
})
166168
}
169+
func TestParameters(t *testing.T) {
170+
t.Run("Parameter casting should be correct", func(t *testing.T) {
171+
values := [3]driver.NamedValue{{Ordinal: 1, Name: "", Value: float32(5)}, {Ordinal: 2, Name: "", Value: time.Now()}, {Ordinal: 3, Name: "", Value: int64(5)}}
172+
parameters := namedValuesToTSparkParams(values[:])
173+
assert.Equal(t, &cli_service.TSparkParameterValue{DoubleValue: thrift.Float64Ptr(5)}, parameters[0].Value)
174+
assert.NotNil(t, parameters[1].Value.StringValue)
175+
assert.Equal(t, string("TIMESTAMP"), *parameters[1].Type)
176+
assert.Equal(t, &cli_service.TSparkParameterValue{StringValue: strPtr("5")}, parameters[2].Value)
177+
})
178+
}

0 commit comments

Comments
 (0)