DBZ-1834 Support for escaping

This commit is contained in:
Jiri Pechanec 2020-03-02 10:14:43 +01:00 committed by Gunnar Morling
parent 0f11f7e6de
commit e4b4aa818a
2 changed files with 37 additions and 2 deletions

View File

@ -219,7 +219,8 @@ public TableId parseQualifiedTableId(MySqlParser.FullIdContext fullIdContext) {
String tableName = null;
final char EMPTY = '\0';
char lastQuote = EMPTY;
for (char c : fullTableName) {
for (int i = 0; i < fullTableName.length; i++) {
char c = fullTableName[i];
if (isQuote(c)) {
// Opening quote
if (lastQuote == EMPTY) {
@ -227,7 +228,14 @@ public TableId parseQualifiedTableId(MySqlParser.FullIdContext fullIdContext) {
}
// Closing quote
else if (lastQuote == c) {
lastQuote = EMPTY;
// escape of quote by doubling
if (i < fullTableName.length - 1 && fullTableName[i + 1] == c) {
component.append(c);
i++;
}
else {
lastQuote = EMPTY;
}
}
// Quote that is part of name
else {

View File

@ -58,6 +58,33 @@ public void beforeEach() {
tables = new Tables();
}
@Test
@FixFor("DBZ-1834")
public void shouldHandleQuotes() {
String ddl = "CREATE TABLE mytable1 (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE `mytable2` (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE db.`mytable3` (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE `db`.`mytable4` (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE `db.mytable5` (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE `db`.`myta``ble6` (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE `db`.`mytable7``` (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE ```db`.`mytable8` (`i.d` INT PRIMARY KEY)"
+ "CREATE TABLE ```db`.`myta\"\"ble9` (`i.d` INT PRIMARY KEY)";
parser.parse(ddl, tables);
assertThat(((MySqlAntlrDdlParser) parser).getParsingExceptionsFromWalker().size()).isEqualTo(0);
assertThat(tables.size()).isEqualTo(9);
Assertions.assertThat(tables.forTable(null, null, "mytable1")).isNotNull();
Assertions.assertThat(tables.forTable(null, null, "mytable2")).isNotNull();
Assertions.assertThat(tables.forTable("db", null, "mytable3")).isNotNull();
Assertions.assertThat(tables.forTable("db", null, "mytable4")).isNotNull();
Assertions.assertThat(tables.forTable("db", null, "mytable5")).isNotNull();
Assertions.assertThat(tables.forTable("db", null, "myta`ble6")).isNotNull();
Assertions.assertThat(tables.forTable("db", null, "mytable7`")).isNotNull();
Assertions.assertThat(tables.forTable("`db", null, "mytable8")).isNotNull();
Assertions.assertThat(tables.forTable("`db", null, "myta\"\"ble9")).isNotNull();
}
@Test
@FixFor("DBZ-1645")
public void shouldUpdateAndRenameTable() {