diff --git a/src/main/java/com/avaje/ebean/config/dbplatform/Oracle10Platform.java b/src/main/java/com/avaje/ebean/config/dbplatform/Oracle10Platform.java index 9ef0d67f4..bad74a1b5 100644 --- a/src/main/java/com/avaje/ebean/config/dbplatform/Oracle10Platform.java +++ b/src/main/java/com/avaje/ebean/config/dbplatform/Oracle10Platform.java @@ -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(); diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java index 357e54375..3cb08b322 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/BaseDdlHandler.java @@ -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); } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java index f4b9153c7..2d90543d2 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/TableDdl.java @@ -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; + } diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseColumnDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseColumnDdl.java deleted file mode 100644 index a0693d5c8..000000000 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseColumnDdl.java +++ /dev/null @@ -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 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(); - } -} diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java index b8dda019e..59c92a4f8 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/BaseTableDdl.java @@ -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 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. */ diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/DbQuotes.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/DbQuotes.java deleted file mode 100644 index 3322d71e2..000000000 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/DbQuotes.java +++ /dev/null @@ -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; - } - -} diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexColumns.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexColumns.java index d0e752674..f0475f468 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexColumns.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexColumns.java @@ -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(); - } - } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexSet.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexSet.java index 5b59f538a..d4421524e 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexSet.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/IndexSet.java @@ -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) { - - } } diff --git a/src/test/java/com/avaje/tests/query/TestQueryFindPagedList.java b/src/test/java/com/avaje/tests/query/TestQueryFindPagedList.java index 24788d93a..aeb229a46 100644 --- a/src/test/java/com/avaje/tests/query/TestQueryFindPagedList.java +++ b/src/test/java/com/avaje/tests/query/TestQueryFindPagedList.java @@ -14,9 +14,8 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; public class TestQueryFindPagedList extends BaseTestCase { @@ -67,25 +66,11 @@ public class TestQueryFindPagedList extends BaseTestCase { // fetch less that total orders (page size 3) PagedList pagedList = Ebean.find(Order.class).findPagedList(0, 3); - LoggedSqlCollector.start(); - // sleep a little to give the logger registration time - // as sometimes the first query executes too fast to - // be captured in this multithreaded test - Thread.sleep(10); - pagedList.loadRowCount(); List orders = pagedList.getList(); int totalRowCount = pagedList.getTotalRowCount(); - List loggedSql = LoggedSqlCollector.stop(); - assertThat(orders.size()).isLessThan(totalRowCount); - assertThat(loggedSql).hasSize(2); - - String firstTxn = loggedSql.get(0).substring(0, 10); - String secTxn = loggedSql.get(1).substring(0, 10); - - assertNotEquals(firstTxn, secTxn); }