Skip to content

Commit

Permalink
allow to pass []interface{} to FetchOne (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
beltran authored Jul 12, 2020
1 parent 65689e5 commit 5cc2ac0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
32 changes: 32 additions & 0 deletions hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions hive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5cc2ac0

Please sign in to comment.