forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
200 lines (176 loc) · 4.7 KB
/
client.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package client
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"github.com/youtube/vitess/go/vt/vtgate/vtgateconn"
"golang.org/x/net/context"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
func init() {
sql.Register("vitess", drv{})
}
type drv struct {
}
// Open must be called with a JSON string that looks like this:
// {"protocol": "gorpc", "address": "localhost:1111", "tablet_type": "master", "timeout": 1000000000}
// protocol specifies the rpc protocol to use.
// address specifies the address for the VTGate to connect to.
// tablet_type represents the consistency level of your operations.
// For example "replica" means eventually consistent reads, while
// "master" supports transactions and gives you read-after-write consistency.
// timeout is specified in nanoseconds. It applies for all operations.
func (d drv) Open(name string) (driver.Conn, error) {
c := &conn{TabletType: "master"}
err := json.Unmarshal([]byte(name), c)
if err != nil {
return nil, err
}
c.tabletType, err = topoproto.ParseTabletType(c.TabletType)
if err != nil {
return nil, err
}
err = c.dial()
if err != nil {
return nil, err
}
return c, nil
}
type conn struct {
Protocol string
Address string
TabletType string `json:"tablet_type"`
Streaming bool
Timeout time.Duration
tabletType pb.TabletType
vtgateConn *vtgateconn.VTGateConn
tx *vtgateconn.VTGateTx
}
func (c *conn) dial() error {
var err error
if c.Protocol == "" {
c.vtgateConn, err = vtgateconn.Dial(context.Background(), c.Address, c.Timeout)
} else {
c.vtgateConn, err = vtgateconn.DialProtocol(context.Background(), c.Protocol, c.Address, c.Timeout)
}
return err
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
return &stmt{c: c, query: query}, nil
}
func (c *conn) Close() error {
c.vtgateConn.Close()
return nil
}
func (c *conn) Begin() (driver.Tx, error) {
if c.Streaming {
return nil, errors.New("transaction not allowed for streaming connection")
}
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
tx, err := c.vtgateConn.Begin(ctx)
if err != nil {
return nil, err
}
c.tx = tx
return c, nil
}
func (c *conn) Commit() error {
if c.tx == nil {
return errors.New("commit: not in transaction")
}
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer func() {
cancel()
c.tx = nil
}()
return c.tx.Commit(ctx)
}
func (c *conn) Rollback() error {
if c.tx == nil {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer func() {
cancel()
c.tx = nil
}()
return c.tx.Rollback(ctx)
}
type stmt struct {
c *conn
query string
}
func (s *stmt) Close() error {
return nil
}
func (s *stmt) NumInput() int {
return -1
}
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.c.Timeout)
defer cancel()
if s.c.Streaming {
return nil, errors.New("Exec not allowed for streaming connections")
}
var qr *mproto.QueryResult
var err error
if s.c.tx == nil {
qr, err = s.c.vtgateConn.Execute(ctx, s.query, makeBindVars(args), s.c.tabletType)
} else {
qr, err = s.c.tx.Execute(ctx, s.query, makeBindVars(args), s.c.tabletType, false)
}
if err != nil {
return nil, err
}
return result{int64(qr.InsertId), int64(qr.RowsAffected)}, nil
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.c.Timeout)
if s.c.Streaming {
qrc, errFunc, err := s.c.vtgateConn.StreamExecute(ctx, s.query, makeBindVars(args), s.c.tabletType)
if err != nil {
return nil, err
}
return newStreamingRows(qrc, errFunc, cancel), nil
}
defer cancel()
var qr *mproto.QueryResult
var err error
if s.c.tx == nil {
qr, err = s.c.vtgateConn.Execute(ctx, s.query, makeBindVars(args), s.c.tabletType)
} else {
qr, err = s.c.tx.Execute(ctx, s.query, makeBindVars(args), s.c.tabletType, false)
}
if err != nil {
return nil, err
}
return newRows(qr), nil
}
func makeBindVars(args []driver.Value) map[string]interface{} {
if len(args) == 0 {
return map[string]interface{}{}
}
bv := make(map[string]interface{}, len(args))
for i, v := range args {
bv[fmt.Sprintf("v%d", i+1)] = v
}
return bv
}
type result struct {
insertid, rowsaffected int64
}
func (r result) LastInsertId() (int64, error) {
return r.insertid, nil
}
func (r result) RowsAffected() (int64, error) {
return r.rowsaffected, nil
}