Skip to content

Commit da6b126

Browse files
mkargfilipelautert
authored andcommitted
rewrite: Simplified MSSQLDatabase escaping
1 parent e8cfa59 commit da6b126

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

liquibase-standard/src/main/java/liquibase/database/core/MSSQLDatabase.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ public String escapeTableName(String catalogName, String schemaName, String tabl
309309
// it anyway.
310310
//
311311
tableName = escapeObjectName(catalogName, schemaName, tableName, Table.class);
312-
if (tableName != null && tableName.contains(" ") && ! tableName.startsWith("\"")) {
313-
tableName = "\"" + tableName + "\"";
312+
if (tableName != null && tableName.contains(" ") && ! isQuoted(tableName)) {
313+
tableName = quoteObject(tableName, Table.class);
314314
}
315315
return tableName;
316316
}
@@ -323,8 +323,8 @@ public String escapeTablespaceName(String tablespaceName) {
323323
// it anyway.
324324
//
325325
tablespaceName = escapeObjectName(tablespaceName, Tablespace.class);
326-
if (tablespaceName != null && tablespaceName.contains(" ") && ! tablespaceName.startsWith("\"")) {
327-
tablespaceName = "\"" + tablespaceName + "\"";
326+
if (tablespaceName != null && tablespaceName.contains(" ") && ! isQuoted(tablespaceName)) {
327+
tablespaceName = quoteObject(tablespaceName, Tablespace.class);
328328
}
329329
return tablespaceName;
330330
}
@@ -745,4 +745,8 @@ public static class SpecificConfiguration implements AutoloadedConfigurations {
745745
.setDescription("Number of bytes needed to store one character (depends on database's character encoding)")
746746
.build();
747747
}
748+
749+
private boolean isQuoted(final String name) {
750+
return name.startsWith(getQuotingStartCharacter()) || name.startsWith("\"");
751+
}
748752
}

liquibase-standard/src/test/java/liquibase/database/core/MSSQLDatabaseTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ public void testEscapeTablespaceName_withSpaces() throws DatabaseException {
187187
// When: Escaping the tablespace name
188188
String result = database.escapeTablespaceName(tablespaceName);
189189

190-
// Then: The tablespace name should be bracket-escaped (spaces require brackets) and wrapped in double quotes
191-
assertEquals("\"[My Tablespace]\"", result);
190+
// Then: The tablespace name should be bracket-escaped (spaces require brackets)
191+
assertEquals("[My Tablespace]", result);
192192
} catch (final DatabaseException e) {
193193
throw e;
194194
}
@@ -251,8 +251,8 @@ public void testEscapeTablespaceName_withSpacesAndSpecialCharacters() throws Dat
251251
// When: Escaping the tablespace name
252252
String result = database.escapeTablespaceName(tablespaceName);
253253

254-
// Then: The tablespace name should be bracket-escaped and double-quoted
255-
assertEquals("\"[My €Tablespace]\"", result);
254+
// Then: The tablespace name should be bracket-escaped
255+
assertEquals("[My €Tablespace]", result);
256256
} catch (final DatabaseException e) {
257257
throw e;
258258
}

liquibase-standard/src/test/java/liquibase/sqlgenerator/core/CreateTableGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ public void testTablespaceEscaping_mssql_withSpaces() {
13151315

13161316
// Then: The tablespace should be escaped with brackets and wrapped in double quotes
13171317
assertTrue(
1318-
"Expected tablespace with spaces to be bracket-escaped and double-quoted, but got: " + generatedSql[0].toSql(), generatedSql[0].toSql().contains("ON \"[My Tablespace]\""));
1318+
"Expected tablespace with spaces to be bracket-escaped, but got: " + generatedSql[0].toSql(), generatedSql[0].toSql().contains("ON [My Tablespace]"));
13191319
}
13201320
}
13211321
}

0 commit comments

Comments
 (0)