#374 - Merge BaseColumnDdl into BaseTableDdl

This commit is contained in:
Robin Bygrave
2015-08-14 12:14:18 +12:00
parent 851f40e7b0
commit 5166fbed96
9 changed files with 194 additions and 286 deletions
@@ -14,7 +14,7 @@ public class Oracle10Platform extends DatabasePlatform {
public Oracle10Platform() {
super();
this.name = "oracle";
this.maxIntersectionTableName = 30;
this.maxTableNameLength = 30;
// OnQueryOnly.CLOSE as a performance optimisation on Oracle
this.onQueryOnly = OnQueryOnly.CLOSE;
this.dbEncrypt = new Oracle10DbEncrypt();
@@ -2,7 +2,6 @@ package com.avaje.ebean.dbmigration.ddlgeneration;
import com.avaje.ebean.config.DbConstraintNaming;
import com.avaje.ebean.config.NamingConvention;
import com.avaje.ebean.dbmigration.ddlgeneration.platform.BaseColumnDdl;
import com.avaje.ebean.dbmigration.ddlgeneration.platform.BaseTableDdl;
import com.avaje.ebean.dbmigration.ddlgeneration.platform.PlatformDdl;
import com.avaje.ebean.dbmigration.migration.AddColumn;
@@ -19,13 +18,10 @@ import java.util.List;
*/
public class BaseDdlHandler implements DdlHandler {
protected final ColumnDdl columnDdl;
protected final TableDdl tableDdl;
public BaseDdlHandler(NamingConvention namingConvention, DbConstraintNaming naming, PlatformDdl platformDdl) {
this.tableDdl = new BaseTableDdl(namingConvention, naming, platformDdl);
this.columnDdl = new BaseColumnDdl(platformDdl);
}
@Override
@@ -52,16 +48,16 @@ public class BaseDdlHandler implements DdlHandler {
@Override
public void generate(DdlWrite writer, AddColumn addColumn) throws IOException {
columnDdl.generate(writer, addColumn);
tableDdl.generate(writer, addColumn);
}
@Override
public void generate(DdlWrite writer, DropColumn dropColumn) throws IOException {
columnDdl.generate(writer, dropColumn);
tableDdl.generate(writer, dropColumn);
}
@Override
public void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException {
columnDdl.generate(writer, alterColumn);
tableDdl.generate(writer, alterColumn);
}
}
@@ -1,6 +1,9 @@
package com.avaje.ebean.dbmigration.ddlgeneration;
import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.CreateTable;
import com.avaje.ebean.dbmigration.migration.DropColumn;
import java.io.IOException;
@@ -13,4 +16,20 @@ public interface TableDdl {
* Generate the create table DDL.
*/
void generate(DdlWrite writer, CreateTable createTable) throws IOException;
/**
* Write the add column change.
*/
void generate(DdlWrite writer, AddColumn addColumn) throws IOException;
/**
* Write the drop column change.
*/
void generate(DdlWrite writer, DropColumn dropColumn) throws IOException;
/**
* Write the alter column changes.
*/
void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException;
}
@@ -1,172 +0,0 @@
package com.avaje.ebean.dbmigration.ddlgeneration.platform;
import com.avaje.ebean.dbmigration.ddlgeneration.ColumnDdl;
import com.avaje.ebean.dbmigration.ddlgeneration.DdlBuffer;
import com.avaje.ebean.dbmigration.ddlgeneration.DdlWrite;
import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.Column;
import com.avaje.ebean.dbmigration.migration.DropColumn;
import java.io.IOException;
import java.util.List;
/**
*/
public class BaseColumnDdl implements ColumnDdl {
protected final PlatformDdl platformDdl;
public BaseColumnDdl(PlatformDdl platformDdl) {
this.platformDdl = platformDdl;
}
@Override
public void generate(DdlWrite writer, AddColumn addColumn) throws IOException {
String tableName = addColumn.getTableName();
List<Column> columns = addColumn.getColumn();
for (Column column : columns) {
// apply
alterTableAddColumn(writer.apply(), tableName, column);
// rollback
alterTableDropColumn(writer.rollback(), tableName, column.getName());
}
}
@Override
public void generate(DdlWrite writer, DropColumn dropColumn) throws IOException {
String tableName = dropColumn.getTableName();
alterTableDropColumn(writer.apply(), tableName, dropColumn.getColumnName());
// no good rollback option here, it is best if drop columns
// are put into a separate changeSet that is run last
}
@Override
public void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException {
if (isTrue(alterColumn.isHistoryExclude())) {
historyExcludeColumn(writer, alterColumn);
} else if (isFalse(alterColumn.isHistoryExclude())) {
historyIncludeColumn(writer, alterColumn);
}
if (hasValue(alterColumn.getDropForeignKey())) {
dropForeignKey(writer, alterColumn);
}
if (hasValue(alterColumn.getReferences())) {
addForeignKey(writer, alterColumn);
}
if (hasValue(alterColumn.getDropUnique())) {
dropUniqueConstraint(writer, alterColumn);
}
if (hasValue(alterColumn.getUnique())) {
addUniqueConstraint(writer, alterColumn);
}
if (hasValue(alterColumn.getUniqueOneToOne())) {
addUniqueOneToOneConstraint(writer, alterColumn);
}
}
protected void addForeignKey(DdlWrite writer, AlterColumn alterColumn) {
}
protected void dropForeignKey(DdlWrite writer, AlterColumn alter) throws IOException {
String tableName = alter.getTableName();
String fkName = alter.getDropForeignKey();
writer.apply()
.append(platformDdl.alterTableDropForeignKey(tableName, fkName))
.endOfStatement();
}
protected void dropUniqueConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
String tableName = alter.getTableName();
String uqName = alter.getDropUnique();
writer.apply()
.append(platformDdl.dropIndex(uqName, tableName))
.endOfStatement();
}
protected void addUniqueOneToOneConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
addUniqueConstraint(writer, alter, alter.getUniqueOneToOne());
}
protected void addUniqueConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
addUniqueConstraint(writer, alter, alter.getUnique());
}
protected void addUniqueConstraint(DdlWrite writer, AlterColumn alter, String uqName) throws IOException {
String tableName = alter.getTableName();
String columnName = alter.getColumnName();
String[] cols = {columnName};
writer.apply()
.append(platformDdl.createExternalUniqueForOneToOne(uqName, tableName, cols))
.endOfStatement();
writer.rollbackForeignKeys()
.append(platformDdl.dropIndex(uqName, tableName))
.endOfStatement();
}
protected void historyIncludeColumn(DdlWrite writer, AlterColumn alterColumn) {
platformDdl.historyIncludeColumn(writer, alterColumn);
}
protected void historyExcludeColumn(DdlWrite writer, AlterColumn alterColumn) {
platformDdl.historyExcludeColumn(writer, alterColumn);
}
protected void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException {
buffer.append("alter table ").append(tableName)
.append(" drop column ").append(columnName)
.endOfStatement().end();
}
protected void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column) throws IOException {
buffer.append("alter table ").append(tableName)
.append(" add column ").append(column.getName())
.append(" ").append(column.getType());
if (Boolean.TRUE.equals(column.isNotnull())) {
buffer.append(" not null");
}
if (hasValue(column.getCheckConstraint())) {
buffer.append(" ").append(column.getCheckConstraint());
}
buffer.endOfStatement().end();
}
protected boolean isFalse(Boolean value) {
return value != null && !value;
}
protected boolean isTrue(Boolean value) {
return value != null && value;
}
protected boolean hasValue(String value) {
return value != null && !value.trim().isEmpty();
}
}
@@ -8,8 +8,11 @@ import com.avaje.ebean.dbmigration.ddlgeneration.DdlBuffer;
import com.avaje.ebean.dbmigration.ddlgeneration.DdlWrite;
import com.avaje.ebean.dbmigration.ddlgeneration.TableDdl;
import com.avaje.ebean.dbmigration.ddlgeneration.platform.util.IndexSet;
import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.Column;
import com.avaje.ebean.dbmigration.migration.CreateTable;
import com.avaje.ebean.dbmigration.migration.DropColumn;
import com.avaje.ebean.dbmigration.migration.ForeignKey;
import com.avaje.ebean.dbmigration.model.MTable;
@@ -122,7 +125,7 @@ public class BaseTableDdl implements TableDdl {
dropTable(writer.rollback(), tableName);
if (useSequence) {
String pkCol = singleColumnPrimaryKey ? pk.get(0).getName() : null;
String pkCol = pk.get(0).getName();
writeSequence(writer, createTable, pkCol);
}
@@ -211,7 +214,6 @@ public class BaseTableDdl implements TableDdl {
writeForeignKey(write, fkName, tableName, cols, refTableName, refColumns, key.getIndexName());
}
}
protected void writeForeignKey(DdlWrite write, String tableName, Column column) throws IOException {
@@ -235,17 +237,7 @@ public class BaseTableDdl implements TableDdl {
tableName = lowerName(tableName);
DdlBuffer fkeyBuffer = write.applyForeignKeys();
fkeyBuffer
.append("alter table ").append(tableName)
.append(" add constraint ").append(fkName)
.append(" foreign key");
appendColumns(columns, fkeyBuffer);
fkeyBuffer
.append(" references ")
.append(lowerName(refTable));
appendColumns(refColumns, fkeyBuffer);
fkeyBuffer.appendWithSpace(platformDdl.getForeignKeyRestrict())
.endOfStatement();
alterTableAddForeignKey(fkeyBuffer, fkName, tableName, columns, refTable, refColumns);
if (indexName != null) {
// no matching unique constraint so add the index
@@ -270,6 +262,21 @@ public class BaseTableDdl implements TableDdl {
}
protected void alterTableAddForeignKey(DdlBuffer buffer, String fkName, String tableName, String[] columns, String refTable, String[] refColumns) throws IOException {
buffer
.append("alter table ").append(tableName)
.append(" add constraint ").append(fkName)
.append(" foreign key");
appendColumns(columns, buffer);
buffer
.append(" references ")
.append(lowerName(refTable));
appendColumns(refColumns, buffer);
buffer.appendWithSpace(platformDdl.getForeignKeyRestrict())
.endOfStatement();
}
private void appendColumns(String[] columns, DdlBuffer buffer) throws IOException {
buffer.append(" (");
for (int i = 0; i < columns.length; i++) {
@@ -439,6 +446,156 @@ public class BaseTableDdl implements TableDdl {
return pk;
}
@Override
public void generate(DdlWrite writer, AddColumn addColumn) throws IOException {
String tableName = addColumn.getTableName();
List<Column> columns = addColumn.getColumn();
for (Column column : columns) {
// apply
alterTableAddColumn(writer.apply(), tableName, column);
// rollback
alterTableDropColumn(writer.rollback(), tableName, column.getName());
}
}
@Override
public void generate(DdlWrite writer, DropColumn dropColumn) throws IOException {
String tableName = dropColumn.getTableName();
alterTableDropColumn(writer.apply(), tableName, dropColumn.getColumnName());
// no good rollback option here, it is best if drop columns
// are put into a separate changeSet that is run last
}
@Override
public void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException {
if (isTrue(alterColumn.isHistoryExclude())) {
historyExcludeColumn(writer, alterColumn);
} else if (isFalse(alterColumn.isHistoryExclude())) {
historyIncludeColumn(writer, alterColumn);
}
if (hasValue(alterColumn.getDropForeignKey())) {
alterColumnDropForeignKey(writer, alterColumn);
}
if (hasValue(alterColumn.getReferences())) {
alterColumnAddForeignKey(writer, alterColumn);
}
if (hasValue(alterColumn.getDropUnique())) {
alterColumnDropUniqueConstraint(writer, alterColumn);
}
if (hasValue(alterColumn.getUnique())) {
alterColumnAddUniqueConstraint(writer, alterColumn);
}
if (hasValue(alterColumn.getUniqueOneToOne())) {
alterColumnAddUniqueOneToOneConstraint(writer, alterColumn);
}
}
protected void alterColumnAddForeignKey(DdlWrite writer, AlterColumn alterColumn) throws IOException {
String tableName = alterColumn.getTableName();
String fkName = alterColumn.getForeignKeyName();
String[] cols = {alterColumn.getColumnName()};
String references = alterColumn.getReferences();
int pos = references.lastIndexOf('.');
if (pos == -1) {
throw new IllegalStateException("Expecting period '.' character for table.column split but not found in [" + references + "]");
}
String refTableName = references.substring(0, pos);
String refColumnName = references.substring(pos + 1);
String[] refCols = {refColumnName};
alterTableAddForeignKey(writer.apply(), fkName, tableName, cols, refTableName, refCols);
}
protected void alterColumnDropForeignKey(DdlWrite writer, AlterColumn alter) throws IOException {
String tableName = alter.getTableName();
String fkName = alter.getDropForeignKey();
writer.apply()
.append(platformDdl.alterTableDropForeignKey(tableName, fkName))
.endOfStatement();
}
protected void alterColumnDropUniqueConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
String tableName = alter.getTableName();
String uqName = alter.getDropUnique();
writer.apply()
.append(platformDdl.dropIndex(uqName, tableName))
.endOfStatement();
}
protected void alterColumnAddUniqueOneToOneConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
addUniqueConstraint(writer, alter, alter.getUniqueOneToOne());
}
protected void alterColumnAddUniqueConstraint(DdlWrite writer, AlterColumn alter) throws IOException {
addUniqueConstraint(writer, alter, alter.getUnique());
}
protected void addUniqueConstraint(DdlWrite writer, AlterColumn alter, String uqName) throws IOException {
String tableName = alter.getTableName();
String columnName = alter.getColumnName();
String[] cols = {columnName};
writer.apply()
.append(platformDdl.createExternalUniqueForOneToOne(uqName, tableName, cols))
.endOfStatement();
writer.rollbackForeignKeys()
.append(platformDdl.dropIndex(uqName, tableName))
.endOfStatement();
}
protected void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException {
buffer.append("alter table ").append(tableName)
.append(" drop column ").append(columnName)
.endOfStatement().end();
}
protected void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column) throws IOException {
buffer.append("alter table ").append(tableName)
.append(" add column ").append(column.getName())
.append(" ").append(column.getType());
if (isTrue(column.isNotnull())) {
buffer.append(" not null");
}
if (hasValue(column.getCheckConstraint())) {
buffer.append(" ").append(column.getCheckConstraint());
}
buffer.endOfStatement().end();
}
protected void historyIncludeColumn(DdlWrite writer, AlterColumn alterColumn) {
platformDdl.historyIncludeColumn(writer, alterColumn);
}
protected void historyExcludeColumn(DdlWrite writer, AlterColumn alterColumn) {
platformDdl.historyExcludeColumn(writer, alterColumn);
}
protected boolean isFalse(Boolean value) {
return value != null && !value;
}
/**
* Return true if null or trimmed string is empty.
*/
@@ -1,48 +0,0 @@
package com.avaje.ebean.dbmigration.ddlgeneration.platform.util;
/**
* Used to normalise table and column names which means stripping out
* quoted identifier characters and any catalog or schema prefix.
*/
public class DbQuotes {
private final String[] quotedIdentifiers;
public DbQuotes() {
this.quotedIdentifiers = new String[]{"\"", "'", "[", "]", "`"};
}
public DbQuotes(String[] quotedIdentifiers) {
this.quotedIdentifiers = quotedIdentifiers;
}
/**
* Trim off the platform quoted identifier quotes like [ ' and ".
*/
public boolean notQuoted(String tableName) {
// remove quoted identifier characters
for (int i = 0; i < quotedIdentifiers.length; i++) {
if (tableName.contains(quotedIdentifiers[i])){
return false;
}
}
return true;
}
/**
* Trim off the platform quoted identifier quotes like [ ' and ".
*/
public String trimQuotes(String tableName) {
if (tableName == null) {
return "";
}
// remove quoted identifier characters
for (int i = 0; i < quotedIdentifiers.length; i++) {
tableName = tableName.replace(quotedIdentifiers[i], "");
}
return tableName;
}
}
@@ -62,28 +62,4 @@ public class IndexColumns {
columns.add(column);
}
/**
* Return the columns as a string array.
*/
public String[] columnsArray() {
return columns.toArray(new String[columns.size()]);
}
/**
* Return the column names all joined with underscore.
*/
public String joinedNames() {
if (columns.size() == 1) {
return columns.get(0);
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < columns.size(); i++) {
if (i > 0) {
sb.append("_");
}
sb.append(columns.get(i));
}
return sb.toString();
}
}
}
@@ -1,7 +1,5 @@
package com.avaje.ebean.dbmigration.ddlgeneration.platform.util;
import com.avaje.ebean.dbmigration.migration.Column;
import java.util.ArrayList;
import java.util.List;
@@ -79,7 +77,4 @@ public class IndexSet {
return indexes;
}
public void addIndex(Column column) {
}
}