-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
116 lines (107 loc) · 2.63 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
package tablestore
import (
"database/sql/driver"
"encoding/base64"
"errors"
"fmt"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"reflect"
"strings"
)
type stmt struct {
query string
index []int
client *tablestore.TableStoreClient
}
func newStmt(client *tablestore.TableStoreClient, query string) *stmt {
return &stmt{
client: client,
query: query,
index: findPlaceholders(query),
}
}
// Query executes a query that may return rows, such as a SELECT
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
query, err := s.interpolateParams(args)
if err != nil {
return nil, err
}
resp, err := s.client.SQLQuery(&tablestore.SQLQueryRequest{Query: query})
if err != nil {
return nil, err
}
return newRows(resp.ResultSet), nil
}
// Exec executes a query that doesn't return rows, such as an INSERT
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
query, err := s.interpolateParams(args)
if err != nil {
return nil, err
}
_, err = s.client.SQLQuery(&tablestore.SQLQueryRequest{Query: query})
if err != nil {
return nil, err
}
return new(noResult), nil
}
// Close closes the statement.
func (s *stmt) Close() error {
return nil
}
// NumInput returns the number of placeholder parameters.
func (s *stmt) NumInput() int {
return len(s.index)
}
func (s *stmt) interpolateParams(params []driver.Value) (string, error) {
if len(s.index) != len(params) {
return "", ErrPlaceholderCount
}
if len(params) == 0 {
return s.query, nil
}
var lastAfterIndex int
var builder strings.Builder
for i, v := range params {
builder.WriteString(s.query[lastAfterIndex:s.index[i]])
lastAfterIndex = s.index[i] + 1
if v == nil {
builder.WriteString("NULL")
} else {
switch val := v.(type) {
case bool, int, int8, int16, int32, int64, float32, float64:
builder.WriteString(fmt.Sprintf("%v", val))
case string:
builder.WriteRune('\'')
builder.WriteString(strings.Replace(val, "'", "\\'", -1))
builder.WriteRune('\'')
case []byte:
builder.WriteString(fmt.Sprintf("FROM_BASE64('%s')", base64.StdEncoding.EncodeToString(val)))
default:
return "", errors.New(fmt.Sprintf("tablestore: unsupported date type `%s`", reflect.TypeOf(v).Name()))
}
}
}
builder.WriteString(s.query[lastAfterIndex:])
return builder.String(), nil
}
func findPlaceholders(query string) []int {
var quote uint8
var index []int
for i := 0; i < len(query); i++ {
switch query[i] {
case '\\':
i++
case '\'', '"', '`':
if quote == query[i] {
quote = 0
} else {
quote = query[i]
}
case '?':
if quote == 0 {
index = append(index, i)
}
}
}
return index
}