forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
123 lines (110 loc) · 3.4 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
// 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 framework
import (
"errors"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/vt/callerid"
"github.com/youtube/vitess/go/vt/proto/query"
"github.com/youtube/vitess/go/vt/tabletserver"
"github.com/youtube/vitess/go/vt/tabletserver/proto"
"golang.org/x/net/context"
qrpb "github.com/youtube/vitess/go/vt/proto/query"
vtpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// QueryClient provides a convenient wrapper for TabletServer's query service.
// It's not thread safe, but you can create multiple clients that point to the
// same server.
type QueryClient struct {
ctx context.Context
target query.Target
server *tabletserver.TabletServer
transactionID int64
}
// NewDefaultClient creates a new client for the default server.
func NewDefaultClient() *QueryClient {
return &QueryClient{
ctx: callerid.NewContext(
context.Background(),
&vtpb.CallerID{},
&qrpb.VTGateCallerID{Username: "dev"},
),
target: Target,
server: DefaultServer,
}
}
// NewClient creates a new client for the specified server.
func NewClient(ctx context.Context, server *tabletserver.TabletServer) *QueryClient {
return &QueryClient{
ctx: ctx,
target: Target,
server: server,
}
}
// Begin begins a transaction.
func (client *QueryClient) Begin() error {
if client.transactionID != 0 {
return errors.New("already in transaction")
}
var txinfo proto.TransactionInfo
err := client.server.Begin(client.ctx, &client.target, &proto.Session{}, &txinfo)
if err != nil {
return err
}
client.transactionID = txinfo.TransactionId
return nil
}
// Commit commits the current transaction.
func (client *QueryClient) Commit() error {
defer func() { client.transactionID = 0 }()
return client.server.Commit(client.ctx, &client.target, &proto.Session{TransactionId: client.transactionID})
}
// Rollback rolls back the current transaction.
func (client *QueryClient) Rollback() error {
defer func() { client.transactionID = 0 }()
return client.server.Rollback(client.ctx, &client.target, &proto.Session{TransactionId: client.transactionID})
}
// Execute executes a query.
func (client *QueryClient) Execute(query string, bindvars map[string]interface{}) (*mproto.QueryResult, error) {
var qr = &mproto.QueryResult{}
err := client.server.Execute(
client.ctx,
&client.target,
&proto.Query{
Sql: query,
BindVariables: bindvars,
TransactionId: client.transactionID,
},
qr,
)
return qr, err
}
// StreamExecute executes a query & streams the results.
func (client *QueryClient) StreamExecute(query string, bindvars map[string]interface{}, sendReply func(*mproto.QueryResult) error) error {
return client.server.StreamExecute(
client.ctx,
&client.target,
&proto.Query{
Sql: query,
BindVariables: bindvars,
TransactionId: client.transactionID,
},
sendReply,
)
}
// ExecuteBatch executes a batch of queries.
func (client *QueryClient) ExecuteBatch(queries []proto.BoundQuery, asTransaction bool) (*proto.QueryResultList, error) {
var qr = &proto.QueryResultList{}
err := client.server.ExecuteBatch(
client.ctx,
&client.target,
&proto.QueryList{
Queries: queries,
AsTransaction: asTransaction,
TransactionId: client.transactionID,
},
qr,
)
return qr, err
}