Skip to content

Commit f459d79

Browse files
committed
Merge branch 'master' into simple-benchmarks
2 parents 014e34c + b14a441 commit f459d79

File tree

4 files changed

+105
-104
lines changed

4 files changed

+105
-104
lines changed

buffer.go

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99

1010
package mysql
1111

12-
import (
13-
"io"
14-
)
12+
import "io"
1513

16-
const (
17-
defaultBufSize = 4096
18-
)
14+
const defaultBufSize = 4096
1915

2016
type buffer struct {
2117
buf []byte
@@ -31,11 +27,19 @@ func newBuffer(rd io.Reader) *buffer {
3127
}
3228
}
3329

34-
// fill reads at least _need_ bytes in the buffer
35-
// existing data in the buffer gets lost
30+
// fill reads into the buffer until at least _need_ bytes are in it
3631
func (b *buffer) fill(need int) (err error) {
32+
// move existing data to the beginning
33+
if b.length > 0 && b.idx > 0 {
34+
copy(b.buf[0:b.length], b.buf[b.idx:])
35+
}
36+
37+
// grow buffer if necessary
38+
if need > len(b.buf) {
39+
b.grow(need)
40+
}
41+
3742
b.idx = 0
38-
b.length = 0
3943

4044
var n int
4145
for b.length < need {
@@ -51,34 +55,33 @@ func (b *buffer) fill(need int) (err error) {
5155
return
5256
}
5357

54-
// read len(p) bytes
55-
func (b *buffer) read(p []byte) (err error) {
56-
need := len(p)
57-
58-
if b.length < need {
59-
if b.length > 0 {
60-
copy(p[0:b.length], b.buf[b.idx:])
61-
need -= b.length
62-
p = p[b.length:]
63-
64-
b.idx = 0
65-
b.length = 0
66-
}
58+
// grow the buffer to at least the given size
59+
// credit for this code snippet goes to Maxim Khitrov
60+
// https://groups.google.com/forum/#!topic/golang-nuts/ETbw1ECDgRs
61+
func (b *buffer) grow(size int) {
62+
// If append would be too expensive, alloc a new slice
63+
if size > 2*cap(b.buf) {
64+
newBuf := make([]byte, size)
65+
copy(newBuf, b.buf)
66+
b.buf = newBuf
67+
return
68+
}
6769

68-
if need >= len(b.buf) {
69-
var n int
70-
has := 0
71-
for err == nil && need > has {
72-
n, err = b.rd.Read(p[has:])
73-
has += n
74-
}
75-
return
76-
}
70+
for cap(b.buf) < size {
71+
b.buf = append(b.buf[:cap(b.buf)], 0)
72+
}
73+
b.buf = b.buf[:cap(b.buf)]
74+
}
7775

76+
// returns next N bytes from buffer.
77+
// The returned slice is only guaranteed to be valid until the next read
78+
func (b *buffer) readNext(need int) (p []byte, err error) {
79+
if b.length < need {
80+
// refill
7881
err = b.fill(need) // err deferred
7982
}
8083

81-
copy(p, b.buf[b.idx:])
84+
p = b.buf[b.idx : b.idx+need]
8285
b.idx += need
8386
b.length -= need
8487
return

connection.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
212212
}
213213

214214
// Gets the value of the given MySQL System Variable
215+
// The returned byte slice is only valid until the next read
215216
func (mc *mysqlConn) getSystemVar(name string) (val []byte, err error) {
216217
// Send command
217218
err = mc.writeCommandPacketStr(comQuery, "SELECT @@"+name)

driver_test.go

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,55 @@ func init() {
4242
}
4343
}
4444

45+
type DBTest struct {
46+
*testing.T
47+
db *sql.DB
48+
}
49+
50+
func runTests(t *testing.T, name, dsn string, tests ...func(dbt *DBTest)) {
51+
if !available {
52+
t.Logf("MySQL-Server not running on %s. Skipping %s", netAddr, name)
53+
return
54+
}
55+
56+
db, err := sql.Open("mysql", dsn)
57+
if err != nil {
58+
t.Fatalf("Error connecting: %v", err)
59+
}
60+
defer db.Close()
61+
62+
db.Exec("DROP TABLE IF EXISTS test")
63+
64+
dbt := &DBTest{t, db}
65+
for _, test := range tests {
66+
test(dbt)
67+
dbt.db.Exec("DROP TABLE IF EXISTS test")
68+
}
69+
}
70+
71+
func (dbt *DBTest) fail(method, query string, err error) {
72+
if len(query) > 300 {
73+
query = "[query too large to print]"
74+
}
75+
dbt.Fatalf("Error on %s %s: %v", method, query, err)
76+
}
77+
78+
func (dbt *DBTest) mustExec(query string, args ...interface{}) (res sql.Result) {
79+
res, err := dbt.db.Exec(query, args...)
80+
if err != nil {
81+
dbt.fail("Exec", query, err)
82+
}
83+
return res
84+
}
85+
86+
func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows) {
87+
rows, err := dbt.db.Query(query, args...)
88+
if err != nil {
89+
dbt.fail("Query", query, err)
90+
}
91+
return rows
92+
}
93+
4594
func TestCharset(t *testing.T) {
4695
mustSetCharset := func(charsetParam, expected string) {
4796
db, err := sql.Open("mysql", strings.Replace(dsn, charset, charsetParam, 1))
@@ -101,57 +150,8 @@ func TestFailingCharset(t *testing.T) {
101150
}
102151
}
103152

104-
type DBTest struct {
105-
*testing.T
106-
db *sql.DB
107-
}
108-
109-
func runTests(t *testing.T, name string, tests ...func(dbt *DBTest)) {
110-
if !available {
111-
t.Logf("MySQL-Server not running on %s. Skipping %s", netAddr, name)
112-
return
113-
}
114-
115-
db, err := sql.Open("mysql", dsn)
116-
if err != nil {
117-
t.Fatalf("Error connecting: %v", err)
118-
}
119-
defer db.Close()
120-
121-
db.Exec("DROP TABLE IF EXISTS test")
122-
123-
dbt := &DBTest{t, db}
124-
for _, test := range tests {
125-
test(dbt)
126-
dbt.db.Exec("DROP TABLE IF EXISTS test")
127-
}
128-
}
129-
130-
func (dbt *DBTest) fail(method, query string, err error) {
131-
if len(query) > 300 {
132-
query = "[query too large to print]"
133-
}
134-
dbt.Fatalf("Error on %s %s: %v", method, query, err)
135-
}
136-
137-
func (dbt *DBTest) mustExec(query string, args ...interface{}) (res sql.Result) {
138-
res, err := dbt.db.Exec(query, args...)
139-
if err != nil {
140-
dbt.fail("Exec", query, err)
141-
}
142-
return res
143-
}
144-
145-
func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows) {
146-
rows, err := dbt.db.Query(query, args...)
147-
if err != nil {
148-
dbt.fail("Query", query, err)
149-
}
150-
return rows
151-
}
152-
153153
func TestRawBytesResultExceedsBuffer(t *testing.T) {
154-
runTests(t, "TestRawBytesResultExceedsBuffer", func(dbt *DBTest) {
154+
runTests(t, "TestRawBytesResultExceedsBuffer", dsn, func(dbt *DBTest) {
155155
// defaultBufSize from buffer.go
156156
expected := strings.Repeat("abc", defaultBufSize)
157157
rows := dbt.mustQuery("SELECT '" + expected + "'")
@@ -168,7 +168,7 @@ func TestRawBytesResultExceedsBuffer(t *testing.T) {
168168
}
169169

170170
func TestCRUD(t *testing.T) {
171-
runTests(t, "TestCRUD", func(dbt *DBTest) {
171+
runTests(t, "TestCRUD", dsn, func(dbt *DBTest) {
172172
// Create Table
173173
dbt.mustExec("CREATE TABLE test (value BOOL)")
174174

@@ -260,7 +260,7 @@ func TestCRUD(t *testing.T) {
260260
}
261261

262262
func TestInt(t *testing.T) {
263-
runTests(t, "TestInt", func(dbt *DBTest) {
263+
runTests(t, "TestInt", dsn, func(dbt *DBTest) {
264264
types := [5]string{"TINYINT", "SMALLINT", "MEDIUMINT", "INT", "BIGINT"}
265265
in := int64(42)
266266
var out int64
@@ -307,7 +307,7 @@ func TestInt(t *testing.T) {
307307
}
308308

309309
func TestFloat(t *testing.T) {
310-
runTests(t, "TestFloat", func(dbt *DBTest) {
310+
runTests(t, "TestFloat", dsn, func(dbt *DBTest) {
311311
types := [2]string{"FLOAT", "DOUBLE"}
312312
in := float32(42.23)
313313
var out float32
@@ -330,7 +330,7 @@ func TestFloat(t *testing.T) {
330330
}
331331

332332
func TestString(t *testing.T) {
333-
runTests(t, "TestString", func(dbt *DBTest) {
333+
runTests(t, "TestString", dsn, func(dbt *DBTest) {
334334
types := [6]string{"CHAR(255)", "VARCHAR(255)", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT"}
335335
in := "κόσμε üöäßñóùéàâÿœ'îë Árvíztűrő いろはにほへとちりぬるを イロハニホヘト דג סקרן чащах น่าฟังเอย"
336336
var out string
@@ -470,18 +470,15 @@ func TestDateTime(t *testing.T) {
470470
}
471471
}
472472

473-
oldDsn := dsn
474-
usedDsn := oldDsn + "&sql_mode=ALLOW_INVALID_DATES"
473+
timeDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
475474
for _, v := range setups {
476475
s = v
477-
dsn = usedDsn + s.dsnSuffix
478-
runTests(t, "TestDateTime", testTime)
476+
runTests(t, "TestDateTime", timeDsn+s.dsnSuffix, testTime)
479477
}
480-
dsn = oldDsn
481478
}
482479

483480
func TestNULL(t *testing.T) {
484-
runTests(t, "TestNULL", func(dbt *DBTest) {
481+
runTests(t, "TestNULL", dsn, func(dbt *DBTest) {
485482
nullStmt, err := dbt.db.Prepare("SELECT NULL")
486483
if err != nil {
487484
dbt.Fatal(err)
@@ -597,7 +594,7 @@ func TestNULL(t *testing.T) {
597594
}
598595

599596
func TestLongData(t *testing.T) {
600-
runTests(t, "TestLongData", func(dbt *DBTest) {
597+
runTests(t, "TestLongData", dsn, func(dbt *DBTest) {
601598
var maxAllowedPacketSize int
602599
err := dbt.db.QueryRow("select @@max_allowed_packet").Scan(&maxAllowedPacketSize)
603600
if err != nil {
@@ -654,7 +651,7 @@ func TestLongData(t *testing.T) {
654651
}
655652

656653
func TestLoadData(t *testing.T) {
657-
runTests(t, "TestLoadData", func(dbt *DBTest) {
654+
runTests(t, "TestLoadData", dsn, func(dbt *DBTest) {
658655
verifyLoadDataResult := func() {
659656
rows, err := dbt.db.Query("SELECT * FROM test")
660657
if err != nil {
@@ -741,7 +738,9 @@ func TestLoadData(t *testing.T) {
741738
}
742739

743740
func TestStrict(t *testing.T) {
744-
runTests(t, "TestStrict", func(dbt *DBTest) {
741+
// ALLOW_INVALID_DATES to get rid of stricter modes - we want to test for warnings, not errors
742+
relaxedDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
743+
runTests(t, "TestStrict", relaxedDsn, func(dbt *DBTest) {
745744
dbt.mustExec("CREATE TABLE test (a TINYINT NOT NULL, b CHAR(4))")
746745

747746
var queries = [...]struct {
@@ -808,7 +807,7 @@ func TestStrict(t *testing.T) {
808807
// Special cases
809808

810809
func TestRowsClose(t *testing.T) {
811-
runTests(t, "TestRowsClose", func(dbt *DBTest) {
810+
runTests(t, "TestRowsClose", dsn, func(dbt *DBTest) {
812811
rows, err := dbt.db.Query("SELECT 1")
813812
if err != nil {
814813
dbt.Fatal(err)
@@ -833,7 +832,7 @@ func TestRowsClose(t *testing.T) {
833832
// dangling statements
834833
// http://code.google.com/p/go/issues/detail?id=3865
835834
func TestCloseStmtBeforeRows(t *testing.T) {
836-
runTests(t, "TestCloseStmtBeforeRows", func(dbt *DBTest) {
835+
runTests(t, "TestCloseStmtBeforeRows", dsn, func(dbt *DBTest) {
837836
stmt, err := dbt.db.Prepare("SELECT 1")
838837
if err != nil {
839838
dbt.Fatal(err)
@@ -874,7 +873,7 @@ func TestCloseStmtBeforeRows(t *testing.T) {
874873
// It is valid to have multiple Rows for the same Stmt
875874
// http://code.google.com/p/go/issues/detail?id=3734
876875
func TestStmtMultiRows(t *testing.T) {
877-
runTests(t, "TestStmtMultiRows", func(dbt *DBTest) {
876+
runTests(t, "TestStmtMultiRows", dsn, func(dbt *DBTest) {
878877
stmt, err := dbt.db.Prepare("SELECT 1 UNION SELECT 0")
879878
if err != nil {
880879
dbt.Fatal(err)
@@ -989,7 +988,7 @@ func TestConcurrent(t *testing.T) {
989988
t.Log("CONCURRENT env var not set. Skipping TestConcurrent")
990989
return
991990
}
992-
runTests(t, "TestConcurrent", func(dbt *DBTest) {
991+
runTests(t, "TestConcurrent", dsn, func(dbt *DBTest) {
993992
var max int
994993
err := dbt.db.QueryRow("SELECT @@max_connections").Scan(&max)
995994
if err != nil {

packets.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ import (
2626
// Read packet to buffer 'data'
2727
func (mc *mysqlConn) readPacket() (data []byte, err error) {
2828
// Read packet header
29-
data = make([]byte, 4)
30-
err = mc.buf.read(data)
29+
data, err = mc.buf.readNext(4)
3130
if err != nil {
3231
errLog.Print(err.Error())
3332
return nil, driver.ErrBadConn
3433
}
3534

3635
// Packet Length [24 bit]
37-
pktLen := uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16
36+
pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)
3837

3938
if pktLen < 1 {
4039
errLog.Print(errMalformPkt.Error())
@@ -52,8 +51,7 @@ func (mc *mysqlConn) readPacket() (data []byte, err error) {
5251
mc.sequence++
5352

5453
// Read packet body [pktLen bytes]
55-
data = make([]byte, pktLen)
56-
err = mc.buf.read(data)
54+
data, err = mc.buf.readNext(pktLen)
5755
if err == nil {
5856
if pktLen < maxPacketSize {
5957
return data, nil

0 commit comments

Comments
 (0)