Skip to content

Commit 97db5db

Browse files
authored
Use cached increment value (#33)
* Use cached increment value * Bump sql * fix test * fix test
1 parent db94195 commit 97db5db

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.17
55
require (
66
github.com/go-rel/primaryreplica v0.4.0
77
github.com/go-rel/rel v0.38.0
8-
github.com/go-rel/sql v0.11.0
8+
github.com/go-rel/sql v0.12.0
99
github.com/go-sql-driver/mysql v1.6.0
1010
github.com/stretchr/testify v1.8.0
1111
)

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
77
github.com/go-rel/primaryreplica v0.4.0 h1:lhU+4dh0/sDQEs602Chiz0SJDXewPU06baWQlx7oB3c=
88
github.com/go-rel/primaryreplica v0.4.0/go.mod h1:HUBz+BUvUcg9JpRRk9PstV9J/qlEOqaHIIllGncbUs8=
99
github.com/go-rel/rel v0.33.1/go.mod h1:DhB9Xmt/ymaumJAB6Z3Kq+IybLwQhhrzr6ZLeXMygPo=
10-
github.com/go-rel/rel v0.37.0/go.mod h1:Zq18pQqXZbDh2JBCo29jgt+y90nZWkUvI+W9Ls29ans=
1110
github.com/go-rel/rel v0.38.0 h1:XooFDMrzHNaZSNvH1ZrEpcn/7TvPz37z1kA66N3Ahjo=
1211
github.com/go-rel/rel v0.38.0/go.mod h1:Zq18pQqXZbDh2JBCo29jgt+y90nZWkUvI+W9Ls29ans=
13-
github.com/go-rel/sql v0.11.0 h1:MeyoMtfDpn9sZSQNMuo7YbN8M/z74eIy9UMN3m6uonQ=
14-
github.com/go-rel/sql v0.11.0/go.mod h1:L4XKALdxaEGwT7ngflceoHVFSooUJap5TO/Yu8FGKJI=
12+
github.com/go-rel/sql v0.12.0 h1:1iIm2JgUr854TjN2C2403A9nZKH1RwbMJp09SQC4HO8=
13+
github.com/go-rel/sql v0.12.0/go.mod h1:Usxy37iCTA5aIqoJGekV4ATdCUOK5w2FiR00/VvvLJQ=
1514
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
1615
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
1716
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

mysql.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package mysql
1515

1616
import (
17-
"context"
1817
db "database/sql"
1918
"strings"
2019

@@ -49,14 +48,19 @@ func New(database *db.DB) rel.Adapter {
4948
DeleteBuilder: deleteBuilder,
5049
TableBuilder: tableBuilder,
5150
IndexBuilder: indexBuilder,
52-
IncrementFunc: incrementFunc,
51+
Increment: getIncrement(database),
5352
ErrorMapper: errorMapper,
5453
DB: database,
5554
}
5655
}
5756

5857
// Open mysql connection using dsn.
5958
func Open(dsn string) (rel.Adapter, error) {
59+
var database, err = db.Open("mysql", rewriteDsn(dsn))
60+
return New(database), err
61+
}
62+
63+
func rewriteDsn(dsn string) string {
6064
// force clientFoundRows=true
6165
// this allows not found record check when updating a record.
6266
if strings.ContainsRune(dsn, '?') {
@@ -65,8 +69,7 @@ func Open(dsn string) (rel.Adapter, error) {
6569
dsn += "?clientFoundRows=true"
6670
}
6771

68-
var database, err = db.Open("mysql", dsn)
69-
return New(database), err
72+
return dsn
7073
}
7174

7275
// MustOpen mysql connection using dsn.
@@ -79,19 +82,14 @@ func MustOpen(dsn string) rel.Adapter {
7982
return adapter
8083
}
8184

82-
func incrementFunc(adapter sql.SQL) int {
85+
func getIncrement(database *db.DB) int {
8386
var (
8487
variable string
8588
increment int
86-
rows, err = adapter.DoQuery(context.TODO(), "SHOW VARIABLES LIKE 'auto_increment_increment';", nil)
89+
row = database.QueryRow("SHOW VARIABLES LIKE 'auto_increment_increment';")
8790
)
8891

89-
check(err)
90-
91-
defer rows.Close()
92-
rows.Next()
93-
check(rows.Scan(&variable, &increment))
94-
92+
check(row.Scan(&variable, &increment))
9593
return increment
9694
}
9795

mysql_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,6 @@ func TestAdapter_PrimaryReplica_specs(t *testing.T) {
125125
AdapterSpecs(t, repo)
126126
}
127127

128-
func TestAdapter_Open(t *testing.T) {
129-
// with parameter
130-
assert.NotPanics(t, func() {
131-
adapter, _ := Open("root@tcp(localhost:3306)/rel_test?charset=utf8")
132-
defer adapter.Close()
133-
})
134-
135-
// without paremeter
136-
assert.NotPanics(t, func() {
137-
adapter, _ := Open("root@tcp(localhost:3306)/rel_test")
138-
defer adapter.Close()
139-
})
140-
}
141-
142128
func TestAdapter_Transaction_commitError(t *testing.T) {
143129
adapter, err := Open(dsn())
144130
assert.Nil(t, err)
@@ -164,6 +150,14 @@ func TestAdapter_Exec_error(t *testing.T) {
164150
assert.NotNil(t, err)
165151
}
166152

153+
func TestRewriteDsn(t *testing.T) {
154+
// with parameter
155+
assert.Contains(t, rewriteDsn("root@tcp(localhost:3306)/rel_test?charset=utf8"), "&clientFoundRows=true")
156+
157+
// without paremeter
158+
assert.Contains(t, rewriteDsn("root@tcp(localhost:3306)/rel_test"), "?clientFoundRows=true")
159+
}
160+
167161
func TestCheck(t *testing.T) {
168162
assert.Panics(t, func() {
169163
check(errors.New("error"))

0 commit comments

Comments
 (0)