-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrows_test.go
63 lines (53 loc) · 1.14 KB
/
rows_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package gomaxcompute
import (
"database/sql"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNext(t *testing.T) {
a := assert.New(t)
db, err := sql.Open("maxcompute", cfg4test.FormatDSN())
a.NoError(err)
const sql = `SELECT * from gomaxcompute_test;`
rows, err := db.Query(sql)
defer rows.Close()
a.NoError(err)
cols, _ := rows.Columns()
cnt := len(cols)
values := make([]interface{}, cnt)
row := make([]interface{}, cnt)
for rows.Next() {
cts, err := rows.ColumnTypes()
a.NoError(err)
for i, ct := range cts {
v, err := createByType(ct.ScanType())
a.NoError(err)
values[i] = v
}
err = rows.Scan(values...)
a.NoError(err)
for i, val := range values {
v, err := parseVal(val)
a.NoError(err)
row[i] = v
}
}
}
func createByType(rt reflect.Type) (interface{}, error) {
switch rt {
case builtinString:
return new(string), nil
default:
return nil, fmt.Errorf("unrecognized column scan type %v", rt)
}
}
func parseVal(val interface{}) (interface{}, error) {
switch v := val.(type) {
case *string:
return *v, nil
default:
return nil, fmt.Errorf("unrecogized type %v", v)
}
}