Skip to content

Commit aa11671

Browse files
authored
Merge branch 'master' into gtid_tagged_log_event_serialized
2 parents c74b18e + 14a14f7 commit aa11671

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+373
-281
lines changed

.golangci.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ linters:
33
enable:
44
# All code is ready for:
55
- errcheck
6-
- staticcheck
7-
- typecheck
8-
- unused
9-
- misspell
10-
- nolintlint
6+
- gofumpt
117
- goimports
12-
- nakedret
13-
- unconvert
14-
- whitespace
158
- govet
169
- gosimple
1710
- ineffassign
11+
- misspell
12+
- nakedret
13+
- nolintlint
14+
- staticcheck
15+
- typecheck
16+
- unconvert
17+
- unused
18+
- whitespace
1819
# ToDo:
1920
#- gocritic
2021
#- golint

README.md

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -190,54 +190,8 @@ You can see [go-mysql-elasticsearch](https://github.com/go-mysql-org/go-mysql-el
190190

191191
Client package supports a simple MySQL connection driver which you can use it to communicate with MySQL server.
192192

193-
### Example
194-
195-
```go
196-
import (
197-
"github.com/go-mysql-org/go-mysql/client"
198-
)
199-
200-
// Connect MySQL at 127.0.0.1:3306, with user root, an empty password and database test
201-
conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test")
202-
203-
// Or to use SSL/TLS connection if MySQL server supports TLS
204-
//conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.UseSSL(true)})
205-
206-
// Or to set your own client-side certificates for identity verification for security
207-
//tlsConfig := NewClientTLSConfig(caPem, certPem, keyPem, false, "your-server-name")
208-
//conn, _ := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.SetTLSConfig(tlsConfig)})
209-
210-
conn.Ping()
211-
212-
// Insert
213-
r, _ := conn.Execute(`insert into table (id, name) values (1, "abc")`)
214-
215-
// Get last insert id
216-
println(r.InsertId)
217-
// Or affected rows count
218-
println(r.AffectedRows)
219-
220-
// Select
221-
r, err := conn.Execute(`select id, name from table where id = 1`)
222-
223-
// Close result for reuse memory (it's not necessary but very useful)
224-
defer r.Close()
225-
226-
// Handle resultset
227-
v, _ := r.GetInt(0, 0)
228-
v, _ = r.GetIntByName(0, "id")
229-
230-
// Direct access to fields
231-
for _, row := range r.Values {
232-
for _, val := range row {
233-
_ = val.Value() // interface{}
234-
// or
235-
if val.Type == mysql.FieldValueTypeFloat {
236-
_ = val.AsFloat64() // float64
237-
}
238-
}
239-
}
240-
```
193+
For an example see [`example_client_test.go`](client/example_client_test.go). You can run this testable example with
194+
`go test -v ./client -run Example`.
241195

242196
Tested MySQL versions for the client include:
243197
- 5.5.x

canal/canal.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ type Canal struct {
5858
}
5959

6060
// canal will retry fetching unknown table's meta after UnknownTableRetryPeriod
61-
var UnknownTableRetryPeriod = time.Second * time.Duration(10)
62-
var ErrExcludedTable = errors.New("excluded table meta")
61+
var (
62+
UnknownTableRetryPeriod = time.Second * time.Duration(10)
63+
ErrExcludedTable = errors.New("excluded table meta")
64+
)
6365

6466
func NewCanal(cfg *Config) (*Canal, error) {
6567
c := new(Canal)

canal/canal_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func TestCreateTableExp(t *testing.T) {
230230
}
231231
}
232232
}
233+
233234
func TestAlterTableExp(t *testing.T) {
234235
cases := []string{
235236
"ALTER TABLE /*generated by server*/ `mydb`.`mytable` ADD `field2` DATE NULL AFTER `field1`;",

canal/handler.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ type EventHandler interface {
2424
String() string
2525
}
2626

27-
type DummyEventHandler struct {
28-
}
27+
type DummyEventHandler struct{}
2928

3029
func (h *DummyEventHandler) OnRotate(*replication.EventHeader, *replication.RotateEvent) error {
3130
return nil
3231
}
32+
3333
func (h *DummyEventHandler) OnTableChanged(*replication.EventHeader, string, string) error {
3434
return nil
3535
}
36+
3637
func (h *DummyEventHandler) OnDDL(*replication.EventHeader, mysql.Position, *replication.QueryEvent) error {
3738
return nil
3839
}
@@ -41,9 +42,11 @@ func (h *DummyEventHandler) OnXID(*replication.EventHeader, mysql.Position) erro
4142
func (h *DummyEventHandler) OnGTID(*replication.EventHeader, mysql.BinlogGTIDEvent) error {
4243
return nil
4344
}
45+
4446
func (h *DummyEventHandler) OnPosSynced(*replication.EventHeader, mysql.Position, mysql.GTIDSet, bool) error {
4547
return nil
4648
}
49+
4750
func (h *DummyEventHandler) OnRowsQueryEvent(*replication.RowsQueryEvent) error {
4851
return nil
4952
}

canal/rows_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,24 @@ func TestRowsEvent_handleUnsigned(t *testing.T) {
2828
// column 10 is out of range and should be ignored, don't panic.
2929
UnsignedColumns: []int{1, 3, 5, 7, 9, 10},
3030
},
31-
Rows: [][]interface{}{{
32-
int8(8), int8(8),
33-
int16(16), int16(16),
34-
int32(32), int32(32),
35-
int64(64), int64(64),
36-
int(128), int(128)},
31+
Rows: [][]interface{}{
32+
{
33+
int8(8), int8(8),
34+
int16(16), int16(16),
35+
int32(32), int32(32),
36+
int64(64), int64(64),
37+
int(128), int(128),
38+
},
3739
},
3840
},
39-
wantRows: [][]interface{}{{
40-
int8(8), uint8(8),
41-
int16(16), uint16(16),
42-
int32(32), uint32(32),
43-
int64(64), uint64(64),
44-
int(128), uint(128)},
41+
wantRows: [][]interface{}{
42+
{
43+
int8(8), uint8(8),
44+
int16(16), uint16(16),
45+
int32(32), uint32(32),
46+
int64(64), uint64(64),
47+
int(128), uint(128),
48+
},
4549
},
4650
},
4751
}

canal/sync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func (c *Canal) updateTable(header *replication.EventHeader, db, table string) (
252252
}
253253
return
254254
}
255+
255256
func (c *Canal) updateReplicationDelay(ev *replication.BinlogEvent) {
256257
var newDelay uint32
257258
now := uint32(utils.Now().Unix())

client/common_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"flag"
55
)
66

7-
var testUser = flag.String("user", "root", "MySQL user")
8-
var testPassword = flag.String("pass", "", "MySQL password")
9-
var testDB = flag.String("db", "test", "MySQL test database")
7+
var (
8+
testUser = flag.String("user", "root", "MySQL user")
9+
testPassword = flag.String("pass", "", "MySQL password")
10+
testDB = flag.String("db", "test", "MySQL test database")
11+
)

client/conn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (s *connTestSuite) TestSetQueryAttributes() {
203203
err := s.c.SetQueryAttributes(qa)
204204
require.NoError(s.T(), err)
205205
expected := []mysql.QueryAttribute{
206-
mysql.QueryAttribute{
206+
{
207207
Name: "qattr1",
208208
Value: "qattr1val",
209209
},

client/example_client_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package client_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-mysql-org/go-mysql/client"
7+
"github.com/go-mysql-org/go-mysql/mysql"
8+
)
9+
10+
func Example() {
11+
// Connect MySQL at 127.0.0.1:3306, with user root, an empty password and database test
12+
conn, err := client.Connect("127.0.0.1:3306", "root", "", "test")
13+
// Or to use SSL/TLS connection if MySQL server supports TLS
14+
// conn, err := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.UseSSL(true)})
15+
// Or to set your own client-side certificates for identity verification for security
16+
// tlsConfig := NewClientTLSConfig(caPem, certPem, keyPem, false, "your-server-name")
17+
// conn, err := client.Connect("127.0.0.1:3306", "root", "", "test", func(c *Conn) {c.SetTLSConfig(tlsConfig)})
18+
if err != nil {
19+
msg := fmt.Sprintf(`
20+
This example needs a MySQL listening on 127.0.0.1:3006 with user "root" and
21+
empty password. Please check the connectivity using mysql client.
22+
---
23+
Connect to MySQL failed: %v`, err)
24+
panic(msg)
25+
}
26+
27+
err = conn.Ping()
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
// (re)create the t1 table
33+
r, err := conn.Execute(`DROP TABLE IF EXISTS t1`)
34+
if err != nil {
35+
panic(err)
36+
}
37+
r.Close()
38+
r, err = conn.Execute(`CREATE TABLE t1 (id int PRIMARY KEY, name varchar(255))`)
39+
if err != nil {
40+
panic(err)
41+
}
42+
r.Close()
43+
44+
// Insert
45+
r, err = conn.Execute(`INSERT INTO t1(id, name) VALUES(1, "abc"),(2, "def")`)
46+
if err != nil {
47+
panic(err)
48+
}
49+
defer r.Close()
50+
51+
// Get last insert id and number of affected rows
52+
fmt.Printf("InsertId: %d, AffectedRows: %d\n", r.InsertId, r.AffectedRows)
53+
54+
// Select
55+
r, err = conn.Execute(`SELECT id, name FROM t1`)
56+
if err != nil {
57+
panic(err)
58+
}
59+
60+
// Handle resultset
61+
v, err := r.GetInt(0, 0)
62+
if err != nil {
63+
panic(err)
64+
}
65+
fmt.Printf("Value of Row 0, Column 0: %d\n", v)
66+
67+
v, err = r.GetIntByName(0, "id")
68+
if err != nil {
69+
panic(err)
70+
}
71+
fmt.Printf("Value of Row 0, Column 'id': %d\n", v)
72+
73+
// Direct access to fields
74+
for rownum, row := range r.Values {
75+
fmt.Printf("Row number %d\n", rownum)
76+
for colnum, val := range row {
77+
fmt.Printf("\tColumn number %d\n", colnum)
78+
79+
ival := val.Value() // interface{}
80+
fmt.Printf("\t\tvalue (type: %d): %#v\n", val.Type, ival)
81+
82+
if val.Type == mysql.FieldValueTypeSigned {
83+
fval := val.AsInt64()
84+
fmt.Printf("\t\tint64 value: %d\n", fval)
85+
}
86+
if val.Type == mysql.FieldValueTypeString {
87+
fval := val.AsString()
88+
fmt.Printf("\t\tstring value: %s\n", fval)
89+
}
90+
}
91+
}
92+
// Output:
93+
// InsertId: 0, AffectedRows: 2
94+
// Value of Row 0, Column 0: 1
95+
// Value of Row 0, Column 'id': 1
96+
// Row number 0
97+
// Column number 0
98+
// value (type: 2): 1
99+
// int64 value: 1
100+
// Column number 1
101+
// value (type: 4): []byte{0x61, 0x62, 0x63}
102+
// string value: abc
103+
// Row number 1
104+
// Column number 0
105+
// value (type: 2): 2
106+
// int64 value: 2
107+
// Column number 1
108+
// value (type: 4): []byte{0x64, 0x65, 0x66}
109+
// string value: def
110+
}

0 commit comments

Comments
 (0)