Skip to content

Commit b505953

Browse files
authored
dbutil: add interpolateParams=true into DSN (#628)
close #627
1 parent d3dcedd commit b505953

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

pkg/dbutil/common.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,52 @@ func GetDBConfigFromEnv(schema string) DBConfig {
110110
}
111111
}
112112

113+
type DSNType interface {
114+
DSNString() string
115+
}
116+
117+
type DSNStringType struct {
118+
Key string
119+
Value string
120+
}
121+
122+
func (s DSNStringType) DSNString() string {
123+
// key='val'. add single quote for better compatibility.
124+
return fmt.Sprintf("&%s=%%27%s%%27", s.Key, url.QueryEscape(s.Value))
125+
}
126+
127+
type DSNBoolType struct {
128+
Key string
129+
Value bool
130+
}
131+
132+
func (b DSNBoolType) DSNString() string {
133+
return fmt.Sprintf("&%s=%t", b.Key, b.Value)
134+
}
135+
136+
// OpenDB opens a mysql connection FD
137+
func OpenDBWithDSN(cfg DBConfig, vars []DSNType) (*sql.DB, error) {
138+
var dbDSN string
139+
if len(cfg.Snapshot) != 0 {
140+
log.Info("create connection with snapshot", zap.String("snapshot", cfg.Snapshot))
141+
dbDSN = fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4&tidb_snapshot=%s", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Snapshot)
142+
} else {
143+
dbDSN = fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4", cfg.User, cfg.Password, cfg.Host, cfg.Port)
144+
}
145+
146+
for _, dsnType := range vars {
147+
dbDSN += dsnType.DSNString()
148+
}
149+
150+
dbConn, err := sql.Open("mysql", dbDSN)
151+
if err != nil {
152+
return nil, errors.Trace(err)
153+
}
154+
155+
err = dbConn.Ping()
156+
return dbConn, errors.Trace(err)
157+
}
158+
113159
// OpenDB opens a mysql connection FD
114160
func OpenDB(cfg DBConfig, vars map[string]string) (*sql.DB, error) {
115161
var dbDSN string

sync_diff_inspector/source/common/conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
)
2323

2424
// CreateDB creates sql.DB used for select data
25-
func CreateDB(ctx context.Context, dbConfig *dbutil.DBConfig, vars map[string]string, num int) (db *sql.DB, err error) {
26-
db, err = dbutil.OpenDB(*dbConfig, vars)
25+
func CreateDB(ctx context.Context, dbConfig *dbutil.DBConfig, vars []dbutil.DSNType, num int) (db *sql.DB, err error) {
26+
db, err = dbutil.OpenDBWithDSN(*dbConfig, vars)
2727
if err != nil {
2828
return nil, errors.Errorf("create db connections %s error %v", dbConfig.String(), err)
2929
}

sync_diff_inspector/source/source.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ func buildSourceFromCfg(ctx context.Context, tableDiffs []*common.TableDiff, con
235235
return NewMySQLSources(ctx, tableDiffs, dbs, connCount, f)
236236
}
237237

238-
func getAutoSnapshotPosition(dbConfig *dbutil.DBConfig, vars map[string]string) (string, string, error) {
239-
tmpConn, err := dbutil.OpenDB(*dbConfig, vars)
238+
func getAutoSnapshotPosition(dbConfig *dbutil.DBConfig, vars []dbutil.DSNType) (string, string, error) {
239+
tmpConn, err := dbutil.OpenDBWithDSN(*dbConfig, vars)
240240
if err != nil {
241241
return "", "", errors.Annotatef(err, "connecting to auto-position tidb_snapshot failed")
242242
}
@@ -250,9 +250,16 @@ func getAutoSnapshotPosition(dbConfig *dbutil.DBConfig, vars map[string]string)
250250
}
251251

252252
func initDBConn(ctx context.Context, cfg *config.Config) error {
253-
// Unified time zone
254-
vars := map[string]string{
255-
"time_zone": UnifiedTimeZone,
253+
vars := []dbutil.DSNType{
254+
// Unified time zone
255+
dbutil.DSNStringType{
256+
Key: "time_zone",
257+
Value: UnifiedTimeZone,
258+
},
259+
dbutil.DSNBoolType{
260+
Key: "interpolateParams",
261+
Value: true,
262+
},
256263
}
257264

258265
// Fill in tidb_snapshot if it is set to AUTO

0 commit comments

Comments
 (0)