Skip to content

Commit c87af9a

Browse files
committed
Merge branch 'master' of github.com:go-mysql-org/go-mysql into gtid_tag
2 parents 944fbdd + 6bb9e67 commit c87af9a

File tree

15 files changed

+108
-82
lines changed

15 files changed

+108
-82
lines changed

README.md

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
# go-mysql
22

3-
A pure go library to handle MySQL network protocol and replication.
3+
A pure go library to handle MySQL network protocol and replication as used by MySQL and MariaDB.
44

55
![semver](https://img.shields.io/github/v/tag/go-mysql-org/go-mysql)
66
![example workflow](https://github.com/go-mysql-org/go-mysql/actions/workflows/ci.yml/badge.svg)
77
![gomod version](https://img.shields.io/github/go-mod/go-version/go-mysql-org/go-mysql/master)
8-
9-
## How to migrate to this repo
10-
To change the used package in your repo it's enough to add this `replace` directive to your `go.mod`:
11-
```
12-
replace github.com/siddontang/go-mysql => github.com/go-mysql-org/go-mysql v1.10.0
13-
```
14-
15-
v1.10.0 - is the last tag in repo, feel free to choose what you want.
8+
[![Go Reference](https://pkg.go.dev/badge/github.com/go-mysql-org/go-mysql.svg)](https://pkg.go.dev/github.com/go-mysql-org/go-mysql)
169

1710
## Changelog
1811
This repo uses [Changelog](CHANGELOG.md).
1912

2013
---
2114
# Content
22-
* [Replication](#replication)
23-
* [Incremental dumping](#canal)
24-
* [Client](#client)
25-
* [Fake server](#server)
26-
* [database/sql like driver](#driver)
27-
* [Logging](#logging)
15+
* [Replication](#replication) - Process events from a binlog stream.
16+
* [Incremental dumping](#canal) - Sync from MySQL to Redis, Elasticsearch, etc.
17+
* [Client](#client) - Simple MySQL client.
18+
* [Fake server](#server) - server side of the MySQL protocol, as library.
19+
* [database/sql like driver](#driver) - An alternative `database/sql` driver for MySQL.
20+
* [Logging](#logging) - Custom logging options.
21+
* [Migration](#how-to-migrate-to-this-repo) - Information for how to migrate if you used the old location of this project.
22+
23+
## Examples
24+
25+
The `cmd` directory contains example applications that can be build by running `make build` in the root of the project. The resulting binaries will be places in `bin/`.
26+
27+
- `go-binlogparser`: parses a binlog file at a given offset
28+
- `go-canal`: streams binlog events from a server to canal
29+
- `go-mysqlbinlog`: streams binlog events
30+
- `go-mysqldump`: like `mysqldump`, but in Go
31+
- `go-mysqlserver`: fake MySQL server
2832

2933
## Replication
3034

@@ -55,9 +59,10 @@ syncer := replication.NewBinlogSyncer(cfg)
5559
streamer, _ := syncer.StartSync(mysql.Position{binlogFile, binlogPos})
5660

5761
// or you can start a gtid replication like
62+
// gtidSet, _ := mysql.ParseGTIDSet(mysql.MySQLFlavor, "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2")
5863
// streamer, _ := syncer.StartSyncGTID(gtidSet)
59-
// the mysql GTID set likes this "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2"
60-
// the mariadb GTID set likes this "0-1-100"
64+
// the mysql GTID set is like this "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2" and uses mysql.MySQLFlavor
65+
// the mariadb GTID set is like this "0-1-100" and uses mysql.MariaDBFlavor
6166

6267
for {
6368
ev, _ := streamer.GetEvent(context.Background())
@@ -111,7 +116,7 @@ Query: DROP TABLE IF EXISTS `test_replication` /* generated by server */
111116

112117
## Canal
113118

114-
Canal is a package that can sync your MySQL into everywhere, like Redis, Elasticsearch.
119+
Canal is a package that can sync your MySQL into everywhere, like Redis, Elasticsearch.
115120

116121
First, canal will dump your MySQL data then sync changed data using binlog incrementally.
117122

@@ -161,7 +166,7 @@ func main() {
161166
}
162167
```
163168

164-
You can see [go-mysql-elasticsearch](https://github.com/siddontang/go-mysql-elasticsearch) for how to sync MySQL data into Elasticsearch.
169+
You can see [go-mysql-elasticsearch](https://github.com/go-mysql-org/go-mysql-elasticsearch) for how to sync MySQL data into Elasticsearch.
165170

166171
## Client
167172

@@ -342,7 +347,7 @@ import (
342347
func main() {
343348
// dsn format: "user:password@addr?dbname"
344349
dsn := "[email protected]:3306?test"
345-
db, _ := sql.Open(dsn)
350+
db, _ := sql.Open("mysql", dsn)
346351
db.Close()
347352
}
348353
```
@@ -455,6 +460,25 @@ func main() {
455460
}
456461
```
457462

463+
### Custom Driver Name
464+
465+
A custom driver name can be set via build options: `-ldflags '-X "github.com/go-mysql-org/go-mysql/driver.driverName=gomysql"'`.
466+
467+
This can be useful when using [GORM](https://gorm.io/docs/connecting_to_the_database.html#Customize-Driver):
468+
469+
```go
470+
import (
471+
_ "github.com/go-mysql-org/go-mysql/driver"
472+
"gorm.io/driver/mysql"
473+
"gorm.io/gorm"
474+
)
475+
476+
db, err := gorm.Open(mysql.New(mysql.Config{
477+
DriverName: "gomysql",
478+
DSN: "gorm:[email protected]:3306/test",
479+
}))
480+
```
481+
458482
### Custom NamedValueChecker
459483

460484
Golang allows for custom handling of query arguments before they are passed to the driver
@@ -516,17 +540,19 @@ import "github.com/siddontang/go-log/log"
516540

517541
Or you can implement your own [`log.Handler`](https://pkg.go.dev/github.com/siddontang/go-log/log#Handler).
518542

519-
## Donate
520-
521-
If you like the project and want to buy me a cola, you can through:
522-
523-
|PayPal|微信|
524-
|------|---|
525-
|[![](https://www.paypalobjects.com/webstatic/paypalme/images/pp_logo_small.png)](https://paypal.me/siddontang)|[![](https://github.com/siddontang/blog/blob/master/donate/weixin.png)|
543+
## How to migrate to this repo
544+
To change the used package in your repo it's enough to add this `replace` directive to your `go.mod`:
545+
```
546+
replace github.com/siddontang/go-mysql => github.com/go-mysql-org/go-mysql v1.10.0
547+
```
526548

527-
## Feedback
549+
This can be done by running this command:
550+
```
551+
go mod edit -replace=github.com/siddontang/go-mysql=github.com/go-mysql-org/[email protected]
552+
```
528553

529-
go-mysql is still in development, your feedback is very welcome.
554+
v1.10.0 - is the last tag in repo, feel free to choose what you want.
530555

556+
## Credits
531557

532-
Gmail: siddontang@gmail.com
558+
go-mysql was started by @siddontang and has many [contributors](https://github.com/go-mysql-org/go-mysql/graphs/contributors)

client/resp.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010

1111
"github.com/pingcap/errors"
12-
"github.com/siddontang/go/hack"
1312

1413
. "github.com/go-mysql-org/go-mysql/mysql"
1514
"github.com/go-mysql-org/go-mysql/utils"
@@ -78,11 +77,11 @@ func (c *Conn) handleErrorPacket(data []byte) error {
7877
if c.capability&CLIENT_PROTOCOL_41 > 0 {
7978
// skip '#'
8079
pos++
81-
e.State = hack.String(data[pos : pos+5])
80+
e.State = utils.ByteSliceToString(data[pos : pos+5])
8281
pos += 5
8382
}
8483

85-
e.Message = hack.String(data[pos:])
84+
e.Message = utils.ByteSliceToString(data[pos:])
8685

8786
return e
8887
}
@@ -372,7 +371,7 @@ func (c *Conn) readResultColumns(result *Result) (err error) {
372371
return err
373372
}
374373

375-
result.FieldNames[hack.String(result.Fields[i].Name)] = i
374+
result.FieldNames[utils.ByteSliceToString(result.Fields[i].Name)] = i
376375

377376
i++
378377
}

driver/driver.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717

1818
"github.com/go-mysql-org/go-mysql/client"
1919
"github.com/go-mysql-org/go-mysql/mysql"
20+
"github.com/go-mysql-org/go-mysql/utils"
2021
"github.com/pingcap/errors"
21-
"github.com/siddontang/go/hack"
2222
)
2323

2424
var customTLSMutex sync.Mutex
@@ -352,7 +352,7 @@ func newRows(r *mysql.Resultset) (*rows, error) {
352352
rs.columns = make([]string, len(r.Fields))
353353

354354
for i, f := range r.Fields {
355-
rs.columns[i] = hack.String(f.Name)
355+
rs.columns[i] = utils.ByteSliceToString(f.Name)
356356
}
357357
rs.step = 0
358358

@@ -389,13 +389,15 @@ func (r *rows) Next(dest []sqldriver.Value) error {
389389
return nil
390390
}
391391

392+
var driverName = "mysql"
393+
392394
func init() {
393395
options["compress"] = CompressOption
394396
options["collation"] = CollationOption
395397
options["readTimeout"] = ReadTimeoutOption
396398
options["writeTimeout"] = WriteTimeoutOption
397399

398-
sql.Register("mysql", driver{})
400+
sql.Register(driverName, driver{})
399401
}
400402

401403
// SetCustomTLSConfig sets a custom TLSConfig for the address (host:port) of the supplied DSN.

driver/driver_options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/pingcap/errors"
19-
"github.com/siddontang/go/log"
19+
"github.com/siddontang/go-log/log"
2020
"github.com/stretchr/testify/require"
2121

2222
"github.com/go-mysql-org/go-mysql/client"

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ require (
1515
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb
1616
github.com/pingcap/tidb/pkg/parser v0.0.0-20241118164214-4f047be191be
1717
github.com/shopspring/decimal v1.2.0
18-
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726
1918
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07
2019
github.com/stretchr/testify v1.8.4
2120
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
4848
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
4949
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
5050
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
51-
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 h1:xT+JlYxNGqyT+XcU8iUrN18JYed2TvG9yN5ULG2jATM=
52-
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
5351
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07 h1:oI+RNwuC9jF2g2lP0u0cVEEZrc/AYBCuFdvwrLWM/6Q=
5452
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07/go.mod h1:yFdBgwXP24JziuRl2NMUahT7nGLNOKi1SIiFxMttVD4=
5553
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

mysql/mysql_gtid.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"strconv"
1111
"strings"
1212

13+
"github.com/go-mysql-org/go-mysql/utils"
1314
"github.com/google/uuid"
1415
"github.com/pingcap/errors"
15-
"github.com/siddontang/go/hack"
1616
)
1717

1818
// Like MySQL GTID Interval struct, [start, stop), left closed and right open
@@ -318,7 +318,7 @@ func (s *UUIDSet) MinusInterval(in IntervalSlice) {
318318
}
319319

320320
func (s *UUIDSet) String() string {
321-
return hack.String(s.Bytes())
321+
return utils.ByteSliceToString(s.Bytes())
322322
}
323323

324324
func (s *UUIDSet) encode(w io.Writer) {
@@ -571,7 +571,7 @@ func (s *MysqlGTIDSet) String() string {
571571
sep = ","
572572
}
573573

574-
return hack.String(buf.Bytes())
574+
return utils.ByteSliceToString(buf.Bytes())
575575
}
576576

577577
func (s *MysqlGTIDSet) Encode() []byte {

mysql/resultset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"strconv"
66
"sync"
77

8+
"github.com/go-mysql-org/go-mysql/utils"
89
"github.com/pingcap/errors"
9-
"github.com/siddontang/go/hack"
1010
)
1111

1212
type StreamingType int
@@ -263,7 +263,7 @@ func (r *Resultset) GetString(row, column int) (string, error) {
263263
case string:
264264
return v, nil
265265
case []byte:
266-
return hack.String(v), nil
266+
return utils.ByteSliceToString(v), nil
267267
case int, int8, int16, int32, int64,
268268
uint, uint8, uint16, uint32, uint64:
269269
return fmt.Sprintf("%d", v), nil

mysql/resultset_helper.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"strconv"
66

77
"github.com/pingcap/errors"
8-
"github.com/siddontang/go/hack"
8+
9+
"github.com/go-mysql-org/go-mysql/utils"
910
)
1011

1112
func FormatTextValue(value interface{}) ([]byte, error) {
@@ -37,7 +38,7 @@ func FormatTextValue(value interface{}) ([]byte, error) {
3738
case []byte:
3839
return v, nil
3940
case string:
40-
return hack.Slice(v), nil
41+
return utils.StringToByteSlice(v), nil
4142
case nil:
4243
return nil, nil
4344
default:
@@ -74,7 +75,7 @@ func formatBinaryValue(value interface{}) ([]byte, error) {
7475
case []byte:
7576
return v, nil
7677
case string:
77-
return hack.Slice(v), nil
78+
return utils.StringToByteSlice(v), nil
7879
default:
7980
return nil, errors.Errorf("invalid type %T", value)
8081
}
@@ -128,7 +129,7 @@ func BuildSimpleTextResultset(names []string, values [][]interface{}) (*Resultse
128129

129130
if len(values) == 0 {
130131
for i, name := range names {
131-
r.Fields[i] = &Field{Name: hack.Slice(name), Charset: 33, Type: MYSQL_TYPE_NULL}
132+
r.Fields[i] = &Field{Name: utils.StringToByteSlice(name), Charset: 33, Type: MYSQL_TYPE_NULL}
132133
}
133134
return r, nil
134135
}
@@ -145,7 +146,7 @@ func BuildSimpleTextResultset(names []string, values [][]interface{}) (*Resultse
145146
return nil, errors.Trace(err)
146147
}
147148
if r.Fields[j] == nil {
148-
r.Fields[j] = &Field{Name: hack.Slice(names[j]), Type: typ}
149+
r.Fields[j] = &Field{Name: utils.StringToByteSlice(names[j]), Type: typ}
149150
err = formatField(r.Fields[j], value)
150151
if err != nil {
151152
return nil, errors.Trace(err)
@@ -213,7 +214,7 @@ func BuildSimpleBinaryResultset(names []string, values [][]interface{}) (*Result
213214
if i == 0 {
214215
field := &Field{Type: typ}
215216
r.Fields[j] = field
216-
field.Name = hack.Slice(names[j])
217+
field.Name = utils.StringToByteSlice(names[j])
217218

218219
if err = formatField(field, value); err != nil {
219220
return nil, errors.Trace(err)

mysql/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"time"
1717

1818
"github.com/Masterminds/semver"
19+
"github.com/go-mysql-org/go-mysql/utils"
1920
"github.com/pingcap/errors"
20-
"github.com/siddontang/go/hack"
2121
)
2222

2323
func Pstack() string {
@@ -375,7 +375,7 @@ var (
375375
func Escape(sql string) string {
376376
dest := make([]byte, 0, 2*len(sql))
377377

378-
for _, w := range hack.Slice(sql) {
378+
for _, w := range utils.StringToByteSlice(sql) {
379379
if c := EncodeMap[w]; c == DONTESCAPE {
380380
dest = append(dest, w)
381381
} else {

0 commit comments

Comments
 (0)