forked from vitessio/vitess
/
connection.go
147 lines (126 loc) · 3.48 KB
/
connection.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
// Copyright 2012, 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 tabletserver
import (
"strings"
"time"
"github.com/youtube/vitess/go/mysql"
"github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/vt/dbconfigs"
)
var mysqlStats *stats.Timings
func init() {
mysqlStats = stats.NewTimings("Mysql")
}
type PoolConnection interface {
ExecuteFetch(query string, maxrows int, wantfields bool) (*proto.QueryResult, error)
ExecuteStreamFetch(query string, callback func(*proto.QueryResult) error, streamBufferSize int) error
VerifyStrict() bool
Id() int64
Close()
IsClosed() bool
Recycle()
}
type CreateConnectionFunc func() (connection *DBConnection, err error)
// DBConnection re-exposes mysql.Connection with some wrapping.
type DBConnection struct {
*mysql.Connection
}
func (conn *DBConnection) handleError(err error) {
if sqlErr, ok := err.(*mysql.SqlError); ok {
if sqlErr.Number() >= 2000 && sqlErr.Number() <= 2018 { // mysql connection errors
conn.Close()
}
if sqlErr.Number() == 1317 { // Query was interrupted
conn.Close()
}
}
}
func (dbc *DBConnection) ExecuteFetch(query string, maxrows int, wantfields bool) (*proto.QueryResult, error) {
start := time.Now()
mqr, err := dbc.Connection.ExecuteFetch(query, maxrows, wantfields)
if err != nil {
mysqlStats.Record("Exec", start)
dbc.handleError(err)
return nil, err
}
mysqlStats.Record("Exec", start)
qr := proto.QueryResult(*mqr)
return &qr, nil
}
func (conn *DBConnection) ExecuteStreamFetch(query string, callback func(*proto.QueryResult) error, streamBufferSize int) error {
start := time.Now()
err := conn.Connection.ExecuteStreamFetch(query)
if err != nil {
mysqlStats.Record("ExecStream", start)
conn.handleError(err)
return err
}
defer conn.CloseResult()
// first call the callback with the fields
err = callback(&proto.QueryResult{Fields: conn.Fields()})
if err != nil {
return err
}
// then get all the rows, sending them as we reach a decent packet size
// start with a pre-allocated array of 256 rows capacity
qr := &proto.QueryResult{Rows: make([][]sqltypes.Value, 0, 256)}
byteCount := 0
for {
row, err := conn.FetchNext()
if err != nil {
return err
}
if row == nil {
break
}
qr.Rows = append(qr.Rows, row)
for _, s := range row {
byteCount += len(s.Raw())
}
if byteCount >= streamBufferSize {
err = callback(qr)
if err != nil {
return err
}
// empty the rows so we start over, but we keep the
// same capacity
qr.Rows = qr.Rows[:0]
byteCount = 0
}
}
if len(qr.Rows) > 0 {
err = callback(qr)
if err != nil {
return err
}
}
return nil
}
var getModeSql = "select @@global.sql_mode"
func (conn *DBConnection) VerifyStrict() bool {
qr, err := conn.ExecuteFetch(getModeSql, 2, false)
if err != nil {
return false
}
if len(qr.Rows) == 0 {
return false
}
return strings.Contains(qr.Rows[0][0].String(), "STRICT_TRANS_TABLES")
}
func CreateGenericConnection(info *mysql.ConnectionParams) (*DBConnection, error) {
params, err := dbconfigs.MysqlParams(info)
if err != nil {
return nil, err
}
c, err := mysql.Connect(params)
return &DBConnection{c}, err
}
func GenericConnectionCreator(info *mysql.ConnectionParams) CreateConnectionFunc {
return func() (connection *DBConnection, err error) {
return CreateGenericConnection(info)
}
}