-
Notifications
You must be signed in to change notification settings - Fork 1
/
stmt.go
161 lines (129 loc) · 3.23 KB
/
stmt.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package taos
import (
"database/sql/driver"
"errors"
"log"
jsoniter "github.com/json-iterator/go"
)
// taosStmt for sql statement
type taosStmt struct {
sqlStr string
paramCount int
conn *conn
}
// Close implement for stmt
func (stmt *taosStmt) Close() error {
return nil
}
// Query implement for Query
func (stmt *taosStmt) Query(args []driver.Value) (driver.Rows, error) {
// log.Println("do query", args)
querySql := stmt.sqlStr
if len(args) != 0 {
if !stmt.conn.drv.cfg.interpolateParams {
return nil, driver.ErrSkip
}
// try client-side prepare to reduce roundtrip
prepared, err := interpolateParams(stmt.sqlStr, args)
if err != nil {
return nil, err
}
querySql = prepared
}
query, err := stmt.conn.drv.query(querySql)
if err != nil {
log.Println(err)
return nil, err
}
any := jsoniter.Get(query)
status := any.Get("status").ToString()
if status != "succ" {
code := any.Get("code").ToInt32()
err, ok := errorMap[code]
if !ok {
return nil, errors.New("[" + any.Get("code").ToString() + "]:" + any.Get("desc").ToString())
}
return nil, err
}
cms := make([]*columnMeta, 0)
iter := any.Get("column_meta")
for i := 0; i < iter.Size(); i++ {
item := iter.Get(i)
cm := &columnMeta{}
any2 := jsoniter.Get([]byte(item.ToString()))
cm.Name = any2.Get(0).ToString()
cm.Type = any2.Get(1).ToInt()
cm.Len = any2.Get(2).ToInt()
cms = append(cms, cm)
}
cols := make([]string, 0)
ss := any.Get("head").ToString()
err = jsoniter.Unmarshal([]byte(ss), &cols)
if err != nil {
log.Println(err)
return nil, err
}
iterData := any.Get("data")
data := make([][]interface{}, 0)
for i := 0; i < iterData.Size(); i++ {
item := iterData.Get(i)
any := jsoniter.Get([]byte(item.ToString()))
dataItem := make([]interface{}, 0)
for j := 0; j < any.Size(); j++ {
dataItem = append(dataItem, any.Get(j).GetInterface())
}
data = append(data, dataItem)
}
size := any.Get("rows").ToInt64()
if int(size) != len(data) {
size = int64(len(data))
// log.Println("TDengine bug: result rows not equal data size, ", querySql, string(query))
}
taosRows := taosRows{
Size: size,
Len: size,
Cols: cols,
ColumnMetas: cms,
Data: data,
}
return &taosRows, nil
}
// NumInput row numbers
func (stmt *taosStmt) NumInput() int {
// don't know how many row numbers
return stmt.paramCount
}
// Exec exec implement
func (stmt *taosStmt) Exec(args []driver.Value) (driver.Result, error) {
querySql := stmt.sqlStr
if len(args) != 0 {
if !stmt.conn.drv.cfg.interpolateParams {
return nil, driver.ErrSkip
}
// try client-side prepare to reduce roundtrip
prepared, err := interpolateParams(stmt.sqlStr, args)
if err != nil {
return nil, err
}
querySql = prepared
}
query, err := stmt.conn.drv.query(querySql)
if err != nil {
log.Println(querySql, err)
return nil, err
}
any := jsoniter.Get(query)
status := any.Get("status").ToString()
if status != "succ" {
code := any.Get("code").ToInt32()
err, ok := errorMap[code]
if !ok {
return nil, errors.New("[" + any.Get("code").ToString() + "]:" + any.Get("desc").ToString())
}
return nil, err
}
re := &taosResult{
RAf: any.Get("data", 0).ToInt64(),
}
return re, nil
}