-
Notifications
You must be signed in to change notification settings - Fork 40
/
statement.go
144 lines (124 loc) · 3.31 KB
/
statement.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
package impala
import (
"context"
"database/sql/driver"
"fmt"
"regexp"
"strings"
"time"
"github.com/bippio/go-impala/hive"
)
// Stmt is statement
type Stmt struct {
stmt string
conn *Conn
}
// Close statement. No-op
func (s *Stmt) Close() error {
return nil
}
// NumInput returns number of inputs
func (s *Stmt) NumInput() int {
return -1
}
// CheckNamedValue is called before passing arguments to the driver
// and is called in place of any ColumnConverter. CheckNamedValue must do type
// validation and conversion as appropriate for the driver.
func (s *Stmt) CheckNamedValue(val *driver.NamedValue) error {
t, ok := val.Value.(time.Time)
if ok {
val.Value = t.Format(hive.TimestampFormat)
return nil
}
return driver.ErrSkip
}
// Exec executes a query that doesn't return rows
func (s *Stmt) Exec(args []driver.Value) (driver.Result, error) {
nargs := make([]driver.NamedValue, len(args))
for i, arg := range args {
nargs[i] = driver.NamedValue{Ordinal: i, Value: arg}
}
return s.ExecContext(context.Background(), nargs)
}
// Query executes a query that may return rows
func (s *Stmt) Query(args []driver.Value) (driver.Rows, error) {
nargs := make([]driver.NamedValue, len(args))
for i, arg := range args {
nargs[i] = driver.NamedValue{Ordinal: i, Value: arg}
}
return s.QueryContext(context.Background(), nargs)
}
// QueryContext executes a query that may return rows
func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
session, err := s.conn.OpenSession(ctx)
if err != nil {
return nil, err
}
stmt := statement(s.stmt, args)
return query(ctx, session, stmt)
}
// ExecContext executes a query that doesn't return rows
func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
session, err := s.conn.OpenSession(ctx)
if err != nil {
return nil, err
}
stmt := statement(s.stmt, args)
return exec(ctx, session, stmt)
}
func template(query string) string {
ordinal := 1
for {
idx := strings.Index(query, "?")
if idx == -1 {
break
}
placeholder := fmt.Sprintf("@p%d", ordinal)
query = strings.Replace(query, "?", placeholder, 1)
ordinal++
}
return query
}
func statement(tmpl string, args []driver.NamedValue) string {
stmt := tmpl
for _, arg := range args {
var re *regexp.Regexp
if arg.Name != "" {
re = regexp.MustCompile(fmt.Sprintf("@%s%s", arg.Name, `\b`))
} else {
re = regexp.MustCompile(fmt.Sprintf("@p%d%s", arg.Ordinal, `\b`))
}
val := fmt.Sprintf("%v", arg.Value)
stmt = re.ReplaceAllString(stmt, val)
}
return stmt
}
func query(ctx context.Context, session *hive.Session, stmt string) (driver.Rows, error) {
operation, err := session.ExecuteStatement(ctx, stmt)
if err != nil {
return nil, err
}
schema, err := operation.GetResultSetMetadata(ctx)
if err != nil {
return nil, err
}
rs, err := operation.FetchResults(ctx, schema)
if err != nil {
return nil, err
}
return &Rows{
rs: rs,
schema: schema,
closefn: func() error { return operation.Close(ctx) },
}, nil
}
func exec(ctx context.Context, session *hive.Session, stmt string) (driver.Result, error) {
operation, err := session.ExecuteStatement(ctx, stmt)
if err != nil {
return nil, err
}
if err := operation.Close(ctx); err != nil {
return nil, err
}
return driver.ResultNoRows, nil
}