Add more quotes, for history tables

This commit is contained in:
Roland Praml
2022-03-21 15:01:43 +01:00
parent 04d2a20945
commit 2a924d8d32
3 changed files with 25 additions and 7 deletions
@@ -127,11 +127,11 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
}
protected String historyTableName(String baseTableName) {
return normalise(baseTableName) + historySuffix;
return quote(normalise(baseTableName) + historySuffix);
}
protected String historyViewName(String baseTableName) {
return normalise(baseTableName) + viewSuffix;
return quote(normalise(baseTableName) + viewSuffix);
}
protected String procedureName(String baseTableName) {
@@ -193,7 +193,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
String platformType = platformDdl.convert(type);
buffer.append(" ");
buffer.append(columnName, 29);
buffer.append(quote(columnName), 29);
buffer.append(platformType);
}
@@ -201,7 +201,7 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
apply
.append("create view ").append(historyViewName(baseTableName))
.append(" as select * from ").append(baseTableName)
.append(" as select * from ").append(quote(baseTableName))
.append(" union all select * from ").append(historyTableName(baseTableName))
.endOfStatement();
}
@@ -268,4 +268,8 @@ public abstract class DbTriggerBasedHistoryDdl implements PlatformHistoryDdl {
public boolean alterHistoryTables() {
return true;
}
protected String quote(String dbName) {
return platformDdl.quote(dbName);
}
}
@@ -31,7 +31,7 @@ public class H2HistoryDdl extends DbTriggerBasedHistoryDdl {
// Note that this does not take into account the historyTable name (excepts _history suffix) and
// does not take into account excluded columns (all columns included in history)
apply
.append("create trigger ").append(triggerName).append(" before update,delete on ").append(baseTable)
.append("create trigger ").append(triggerName).append(" before update,delete on ").append(quote(baseTable))
.append(" for each row call \"" + TRIGGER_CLASS + "\";").newLine();
}
@@ -20,12 +20,17 @@ import io.ebeaninternal.dbmigration.model.MTable;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Controls the DDL generation for a specific database platform.
*/
public class PlatformDdl {
// matches on pattern "check ( COLUMNAME ... )". ColumnName is match group 2;
private static final Pattern CHECK_PATTERN = Pattern.compile("(.*?\\( *)([^ ]+)(.*)");
protected final DatabasePlatform platform;
protected PlatformHistoryDdl historyDdl = new NoHistorySupportDdl();
@@ -278,7 +283,7 @@ public class PlatformDdl {
* Returns the check constraint.
*/
public String createCheckConstraint(String ckName, String checkConstraint) {
return " constraint " + maxConstraintName(ckName) + " " + checkConstraint;
return " constraint " + maxConstraintName(ckName) + " " + quoteCheckConstraint(checkConstraint);
}
/**
@@ -612,9 +617,10 @@ public class PlatformDdl {
* Alter table adding the check constraint.
*/
public String alterTableAddCheckConstraint(String tableName, String checkConstraintName, String checkConstraint) {
return "alter table " + quote(tableName) + " " + addConstraint + " " + maxConstraintName(checkConstraintName) + " " + checkConstraint;
return "alter table " + quote(tableName) + " " + addConstraint + " " + maxConstraintName(checkConstraintName) + " " + quoteCheckConstraint(checkConstraint);
}
/**
* Alter column setting the default value.
* <p>
@@ -782,4 +788,12 @@ public class PlatformDdl {
return platform.convertQuotedIdentifiers(dbName);
}
protected String quoteCheckConstraint(String checkConstraint) {
Matcher matcher = CHECK_PATTERN.matcher(checkConstraint);
if (matcher.matches()) {
return matcher.replaceFirst("$1" + quote(matcher.group(2)) + "$3");
}
return checkConstraint;
}
}