forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_query.go
109 lines (92 loc) · 3.34 KB
/
rpc_query.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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tabletmanager
import (
"golang.org/x/net/context"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/log"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// ExecuteFetchAsDba will execute the given query, possibly disabling binlogs and reload schema.
func (agent *ActionAgent) ExecuteFetchAsDba(ctx context.Context, query []byte, dbName string, maxrows int, disableBinlogs bool, reloadSchema bool) (*querypb.QueryResult, error) {
// get a connection
conn, err := agent.MysqlDaemon.GetDbaConnection()
if err != nil {
return nil, err
}
defer conn.Close()
// disable binlogs if necessary
if disableBinlogs {
_, err := conn.ExecuteFetch("SET sql_log_bin = OFF", 0, false)
if err != nil {
return nil, err
}
}
if dbName != "" {
// This execute might fail if db does not exist.
// Error is ignored because given query might create this database.
conn.ExecuteFetch("USE "+dbName, 1, false)
}
// run the query
result, err := conn.ExecuteFetch(string(query), maxrows, true /*wantFields*/)
// re-enable binlogs if necessary
if disableBinlogs && !conn.IsClosed() {
_, err := conn.ExecuteFetch("SET sql_log_bin = ON", 0, false)
if err != nil {
// if we can't reset the sql_log_bin flag,
// let's just close the connection.
conn.Close()
}
}
if err == nil && reloadSchema {
reloadErr := agent.QueryServiceControl.ReloadSchema(ctx)
if reloadErr != nil {
log.Errorf("failed to reload the schema %v", reloadErr)
}
}
return sqltypes.ResultToProto3(result), err
}
// ExecuteFetchAsAllPrivs will execute the given query, possibly reloading schema.
func (agent *ActionAgent) ExecuteFetchAsAllPrivs(ctx context.Context, query []byte, dbName string, maxrows int, reloadSchema bool) (*querypb.QueryResult, error) {
// get a connection
conn, err := agent.MysqlDaemon.GetAllPrivsConnection()
if err != nil {
return nil, err
}
defer conn.Close()
if dbName != "" {
// This execute might fail if db does not exist.
// Error is ignored because given query might create this database.
conn.ExecuteFetch("USE "+dbName, 1, false)
}
// run the query
result, err := conn.ExecuteFetch(string(query), maxrows, true /*wantFields*/)
if err == nil && reloadSchema {
reloadErr := agent.QueryServiceControl.ReloadSchema(ctx)
if reloadErr != nil {
log.Errorf("failed to reload the schema %v", reloadErr)
}
}
return sqltypes.ResultToProto3(result), err
}
// ExecuteFetchAsApp will execute the given query.
func (agent *ActionAgent) ExecuteFetchAsApp(ctx context.Context, query []byte, maxrows int) (*querypb.QueryResult, error) {
// get a connection
conn, err := agent.MysqlDaemon.GetAppConnection(ctx)
if err != nil {
return nil, err
}
defer conn.Recycle()
result, err := conn.ExecuteFetch(string(query), maxrows, true /*wantFields*/)
return sqltypes.ResultToProto3(result), err
}