-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
check, diff: auto discover ansi-quotes #381
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8e64b35
check: auto discover ansi-quotes
lance6716 b7ad4b1
fix more
lance6716 5711a1b
fix diff
lance6716 f41b4d8
fix test
lance6716 efda573
address comment
lance6716 07e08d1
try fix test
lance6716 0c6b15a
Merge branch 'master' into remove-ansi-quotes
lance6716 3dccc00
address comments
lance6716 85b4deb
fix test
lance6716 ae6761e
Merge branch 'master' into remove-ansi-quotes
lance6716 f70d144
address comment
lance6716 b2a1b77
Merge remote-tracking branch 'origin/remove-ansi-quotes' into remove-…
lance6716 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,8 +72,6 @@ type DBConfig struct { | |
Schema string `toml:"schema" json:"schema"` | ||
|
||
Snapshot string `toml:"snapshot" json:"snapshot"` | ||
|
||
SQLMode string `toml:"sql-mode" json:"sql-mode"` | ||
} | ||
|
||
// String returns native format of database configuration | ||
|
@@ -606,6 +604,61 @@ func GetDBVersion(ctx context.Context, db *sql.DB) (string, error) { | |
return "", ErrVersionNotFound | ||
} | ||
|
||
// GetSessionVariable gets server's session variable, although argument is *sql.DB, (session) system variables may be | ||
// set through DSN | ||
func GetSessionVariable(db *sql.DB, variable string) (value string, err error) { | ||
query := fmt.Sprintf("SHOW VARIABLES LIKE '%s'", variable) | ||
rows, err := db.Query(query) | ||
|
||
if err != nil { | ||
return "", errors.Trace(err) | ||
} | ||
defer rows.Close() | ||
|
||
// Show an example. | ||
/* | ||
mysql> SHOW VARIABLES LIKE "binlog_format"; | ||
+---------------+-------+ | ||
| Variable_name | Value | | ||
+---------------+-------+ | ||
| binlog_format | ROW | | ||
+---------------+-------+ | ||
*/ | ||
|
||
for rows.Next() { | ||
err = rows.Scan(&variable, &value) | ||
if err != nil { | ||
return "", errors.Trace(err) | ||
} | ||
} | ||
|
||
if rows.Err() != nil { | ||
return "", errors.Trace(err) | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// GetSQLMode returns sql_mode. | ||
func GetSQLMode(db *sql.DB) (tmysql.SQLMode, error) { | ||
sqlMode, err := GetSessionVariable(db, "sql_mode") | ||
if err != nil { | ||
return tmysql.ModeNone, err | ||
} | ||
|
||
mode, err := tmysql.GetSQLMode(sqlMode) | ||
return mode, errors.Trace(err) | ||
} | ||
|
||
// HasAnsiQuotesMode checks whether database has `ANSI_QUOTES` set | ||
func HasAnsiQuotesMode(db *sql.DB) (bool, error) { | ||
mode, err := GetSQLMode(db) | ||
if err != nil { | ||
return false, err | ||
} | ||
return mode.HasANSIQuotesMode(), nil | ||
} | ||
|
||
// IsTiDB returns true if this database is tidb | ||
func IsTiDB(ctx context.Context, db *sql.DB) (bool, error) { | ||
version, err := GetDBVersion(ctx, db) | ||
|
@@ -766,8 +819,8 @@ func DeleteRows(ctx context.Context, db *sql.DB, schemaName string, tableName st | |
return DeleteRows(ctx, db, schemaName, tableName, where, args) | ||
} | ||
|
||
// GetParser gets parser according to sql mode | ||
func GetParser(sqlModeStr string) (*parser.Parser, error) { | ||
// getParser gets parser according to sql mode | ||
func getParser(sqlModeStr string) (*parser.Parser, error) { | ||
if len(sqlModeStr) == 0 { | ||
return parser.New(), nil | ||
} | ||
|
@@ -780,3 +833,16 @@ func GetParser(sqlModeStr string) (*parser.Parser, error) { | |
parser2.SetSQLMode(sqlMode) | ||
return parser2, nil | ||
} | ||
|
||
// GetParserForDB discovers ANSI_QUOTES in db's session variables and returns a proper parser | ||
func GetParserForDB(db *sql.DB) (*parser.Parser, error) { | ||
ansiQuotes, err := HasAnsiQuotesMode(db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sqlMode := "" | ||
if ansiQuotes { | ||
sqlMode = "ANSI_QUOTES" | ||
} | ||
return getParser(sqlMode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, going to align DM's parser to this behaviour |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do other places still need this (
sql_mode
may including others rather thanANSI_QUOTES
)?But we may remove it now and add it back if needed later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess other places don't require this
SQLMode
otherwise it won't compile. and in #280 this field is used for parser only