forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_query.go
68 lines (58 loc) · 2.04 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
// Copyright 2016, 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 tabletmanager
import (
"github.com/youtube/vitess/go/sqltypes"
"golang.org/x/net/context"
querypb "github.com/youtube/vitess/go/vt/proto/query"
)
// ExecuteFetchAsDba will execute the given query, possibly disabling binlogs and reload schema.
// Should be called under RPCWrap.
func (agent *ActionAgent) ExecuteFetchAsDba(ctx context.Context, query string, 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(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 {
agent.QueryServiceControl.ReloadSchema()
}
return sqltypes.ResultToProto3(result), err
}
// ExecuteFetchAsApp will execute the given query, possibly disabling binlogs.
// Should be called under RPCWrap.
func (agent *ActionAgent) ExecuteFetchAsApp(ctx context.Context, query string, maxrows int) (*querypb.QueryResult, error) {
// get a connection
conn, err := agent.MysqlDaemon.GetAppConnection()
if err != nil {
return nil, err
}
defer conn.Recycle()
result, err := conn.ExecuteFetch(query, maxrows, true /*wantFields*/)
return sqltypes.ResultToProto3(result), err
}