From 5cc2ac031d4d9b90b9ef7a27c93c44d8250dc268 Mon Sep 17 00:00:00 2001 From: Jaume Marhuenda Date: Sun, 12 Jul 2020 00:11:49 -0400 Subject: [PATCH] allow to pass []interface{} to FetchOne (#107) --- hive.go | 32 ++++++++++++++++++++++++++++++++ hive_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/hive.go b/hive.go index cc259fd..93e6085 100644 --- a/hive.go +++ b/hive.go @@ -672,6 +672,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { } for i := 0; i < len(c.queue); i++ { if c.queue[i].IsSetBinaryVal() { + if dests[i] == nil { + dests[i] = c.queue[i].BinaryVal.Values[c.columnIndex] + continue + } d, ok := dests[i].(*[]byte) if !ok { c.Err = fmt.Errorf("Unexpected data type %T for value %v (should be %T)", dests[i], c.queue[i].BinaryVal.Values[c.columnIndex], c.queue[i].BinaryVal.Values[c.columnIndex]) @@ -683,6 +687,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { *d = c.queue[i].BinaryVal.Values[c.columnIndex] } } else if c.queue[i].IsSetByteVal() { + if dests[i] == nil { + dests[i] = c.queue[i].ByteVal.Values[c.columnIndex] + continue + } d, ok := dests[i].(*int8) if !ok { d, ok := dests[i].(**int8) @@ -701,6 +709,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { } } else if c.queue[i].IsSetI16Val() { + if dests[i] == nil { + dests[i] = c.queue[i].I16Val.Values[c.columnIndex] + continue + } d, ok := dests[i].(*int16) if !ok { d, ok := dests[i].(**int16) @@ -718,6 +730,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { *d = c.queue[i].I16Val.Values[c.columnIndex] } } else if c.queue[i].IsSetI32Val() { + if dests[i] == nil { + dests[i] = c.queue[i].I32Val.Values[c.columnIndex] + continue + } d, ok := dests[i].(*int32) if !ok { d, ok := dests[i].(**int32) @@ -735,6 +751,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { *d = c.queue[i].I32Val.Values[c.columnIndex] } } else if c.queue[i].IsSetI64Val() { + if dests[i] == nil { + dests[i] = c.queue[i].I64Val.Values[c.columnIndex] + continue + } d, ok := dests[i].(*int64) if !ok { d, ok := dests[i].(**int64) @@ -752,6 +772,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { *d = c.queue[i].I64Val.Values[c.columnIndex] } } else if c.queue[i].IsSetStringVal() { + if dests[i] == nil { + dests[i] = c.queue[i].StringVal.Values[c.columnIndex] + continue + } d, ok := dests[i].(*string) if !ok { d, ok := dests[i].(**string) @@ -769,6 +793,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { *d = c.queue[i].StringVal.Values[c.columnIndex] } } else if c.queue[i].IsSetDoubleVal() { + if dests[i] == nil { + dests[i] = c.queue[i].DoubleVal.Values[c.columnIndex] + continue + } d, ok := dests[i].(*float64) if !ok { d, ok := dests[i].(**float64) @@ -786,6 +814,10 @@ func (c *Cursor) FetchOne(ctx context.Context, dests ...interface{}) { *d = c.queue[i].DoubleVal.Values[c.columnIndex] } } else if c.queue[i].IsSetBoolVal() { + if dests[i] == nil { + dests[i] = c.queue[i].BoolVal.Values[c.columnIndex] + continue + } d, ok := dests[i].(*bool) if !ok { d, ok := dests[i].(**bool) diff --git a/hive_test.go b/hive_test.go index b33d367..0604e7f 100644 --- a/hive_test.go +++ b/hive_test.go @@ -528,6 +528,9 @@ func TestSimpleSelect(t *testing.T) { var s string var i int32 cursor.FetchOne(context.Background(), &i, &s) + if cursor.Error() != nil { + t.Fatal(cursor.Error()) + } closeAll(t, connection, cursor) } @@ -1299,6 +1302,45 @@ func TestTypes(t *testing.T) { closeAll(t, connection, cursor) } +func TestTypesInterface(t *testing.T) { + connection, cursor := makeConnection(t, 1000) + prepareAllTypesTable(t, cursor) + + cursor.Execute(context.Background(), "SELECT * FROM all_types", false) + if cursor.Error() != nil { + t.Fatal(cursor.Error()) + } + + i := make([]interface{}, 15) + expected := make([]interface{}, 15) + expected[0] = true + expected[1] = int8(127) + expected[2] = int16(32767) + expected[3] = int32(2147483647) + expected[4] = int64(9223372036854775807) + expected[5] = float64(0.5) + expected[6] = float64(0.25) + expected[7] = "a string" + expected[8] = "1970-01-01 00:00:00" + expected[9] = []uint8{49, 50, 51} + expected[10] = "[1,2]" + expected[11] = "{1:2,3:4}" + expected[12] = "{\"a\":1,\"b\":2}" + expected[13] = "{0:1}" + expected[14] = "0.1" + + cursor.FetchOne(context.Background(), i...) + if cursor.Err != nil { + t.Fatal(cursor.Err) + } + + if !reflect.DeepEqual(i, expected) { + t.Fatalf("Expected array: %+v, got: %+v", expected, i) + } + + closeAll(t, connection, cursor) +} + func TestTypesWithPointer(t *testing.T) { connection, cursor := makeConnection(t, 1000) prepareAllTypesTable(t, cursor)