diff --git a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java index 6af9caca0..5894fe077 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java +++ b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java @@ -249,7 +249,7 @@ public final class DefaultServer implements SpiEbeanServer { for (SpiEbeanPlugin plugin : ServiceLoader.load(SpiEbeanPlugin.class)) { spiPlugins.add(plugin); - plugin.setup(this, this.getDatabasePlatform(), config); + plugin.setup(this, config); if (plugin instanceof DdlGenerator) { // backwards compatible @@ -261,7 +261,7 @@ public final class DefaultServer implements SpiEbeanServer { // ServiceLoader not finding ddlGenerator (typically OSGi) ddlGenerator = new DdlGenerator(); spiPlugins.add(ddlGenerator); - ddlGenerator.setup(this, this.getDatabasePlatform(), config); + ddlGenerator.setup(this, config); } ebeanPlugins = Collections.unmodifiableList(spiPlugins); diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractBeanVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractBeanVisitor.java deleted file mode 100644 index b83d4cd03..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractBeanVisitor.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.InheritInfo; -import com.avaje.ebeaninternal.server.deploy.InheritInfoVisitor; - -/** - * Base BeanVisitor that can help visiting inherited properties. - * - * @author rbygrave - */ -public abstract class AbstractBeanVisitor implements BeanVisitor { - - /** - * Visit all the other inheritance properties that are not on the root. - */ - public void visitInheritanceProperties(BeanDescriptor> descriptor, PropertyVisitor pv) { - - InheritInfo inheritInfo = descriptor.getInheritInfo(); - if (inheritInfo != null && inheritInfo.isRoot()){ - // add all properties on the children objects - InheritChildVisitor childVisitor = new InheritChildVisitor(pv); - inheritInfo.visitChildren(childVisitor); - } - } - - - /** - * Helper used to visit all the inheritInfo/BeanDescriptor in - * the inheritance hierarchy (to add their 'local' properties). - */ - protected static class InheritChildVisitor implements InheritInfoVisitor { - - final PropertyVisitor pv; - - protected InheritChildVisitor(PropertyVisitor pv) { - this.pv = pv; - } - - public void visit(InheritInfo inheritInfo) { - BeanProperty[] propertiesLocal = inheritInfo.getBeanDescriptor().propertiesLocal(); - VisitorUtil.visit(propertiesLocal, pv); - } - } -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractPropertyVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractPropertyVisitor.java deleted file mode 100644 index 28c1a0232..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractPropertyVisitor.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound; - -/** - * Has no implementation. Can be used as a base object so that - * selective methods can be implemented. - */ -public abstract class AbstractPropertyVisitor implements PropertyVisitor { - - public void visitEmbedded(BeanPropertyAssocOne> p) { - } - - public void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne> embedded) { - } - - public void visitMany(BeanPropertyAssocMany> p) { - } - - public void visitOneExported(BeanPropertyAssocOne> p) { - } - - public void visitOneImported(BeanPropertyAssocOne> p) { - } - - public void visitScalar(BeanProperty p) { - } - - public void visitCompound(BeanPropertyCompound p) { - } - - public void visitCompoundScalar(BeanPropertyCompound compound, BeanProperty p) { - } - - - - -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/AddForeignKeysVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/AddForeignKeysVisitor.java deleted file mode 100644 index bbb34b40a..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/AddForeignKeysVisitor.java +++ /dev/null @@ -1,166 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebean.config.dbplatform.DbDdlSyntax; -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound; -import com.avaje.ebeaninternal.server.deploy.TableJoin; -import com.avaje.ebeaninternal.server.deploy.TableJoinColumn; - -/** - * Used to generate the foreign key DDL and related indexes. - */ -public class AddForeignKeysVisitor extends AbstractBeanVisitor { - - final DdlGenContext ctx; - - final FkeyPropertyVisitor pv; - - public AddForeignKeysVisitor(boolean addMode, DdlGenContext ctx) { - this.ctx = ctx; - this.pv = new FkeyPropertyVisitor(addMode, this, ctx); - } - - public boolean visitBean(BeanDescriptor> descriptor) { - return descriptor.isInheritanceRoot(); - } - - public void visitBeanEnd(BeanDescriptor> descriptor) { - - visitInheritanceProperties(descriptor, pv); - } - - public void visitBegin() { - } - - public void visitEnd() { - ctx.addIntersectionFkeys(); - } - - public PropertyVisitor visitProperty(BeanProperty p) { - return pv; - } - - - public static class FkeyPropertyVisitor extends BaseTablePropertyVisitor { - - /** - * Set to false when generating drop foreign key statements. - */ - final boolean addMode; - - final DdlGenContext ctx; - - final AddForeignKeysVisitor parent; - - public FkeyPropertyVisitor(boolean addMode, AddForeignKeysVisitor parent, DdlGenContext ctx) { - this.addMode = addMode; - this.parent = parent; - this.ctx = ctx; - } - - @Override - public void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne> embedded) { - // not interested - } - - @Override - public void visitOneImported(BeanPropertyAssocOne> p) { - - // alter table {basetable} add foreign key (...) references {} (...) on delete restrict on update restrict; - // Alter table o_address add Foreign Key (country_code) references o_country (code) on delete restrict on update restrict; - - String baseTable = p.getBeanDescriptor().getBaseTable(); - TableJoin tableJoin = p.getTableJoin(); - TableJoinColumn[] columns = tableJoin.columns(); - - String tableName = p.getBeanDescriptor().getBaseTable(); - DbDdlSyntax ddlSyntax = ctx.getDdlSyntax(); - String fkName = ddlSyntax.getForeignKeyName(tableName, p.getName(), ctx.incrementFkCount()); - - if (!addMode) { - // look to generate drop foreign key statement - String dropKeyConstraintPrefix = ddlSyntax.dropKeyConstraintPrefix(tableName, fkName); - if (dropKeyConstraintPrefix != null && !dropKeyConstraintPrefix.isEmpty()) { - ctx.write(dropKeyConstraintPrefix).write(" "); - } - } - - ctx.write("alter table ").write(baseTable).write(addOrDrop()); - if (fkName != null) { - ctx.write("constraint ").write(fkName).write(" "); - } - if (addMode) { - ctx.write("foreign key ("); - for (int i = 0; i < columns.length; i++) { - if (i > 0) { - ctx.write(","); - } - ctx.write(columns[i].getLocalDbColumn()); - } - ctx.write(")"); - - ctx.write(" references "); - ctx.write(tableJoin.getTable()); - ctx.write(" ("); - for (int i = 0; i < columns.length; i++) { - if (i > 0) { - ctx.write(","); - } - ctx.write(columns[i].getForeignDbColumn()); - } - ctx.write(")"); - - String fkeySuffix = ctx.getDdlSyntax().getForeignKeySuffix(); - if (fkeySuffix != null) { - ctx.write(" ").write(fkeySuffix); - } - } - ctx.write(";").writeNewLine(); - - if (addMode && ddlSyntax.isRenderIndexForFkey()){ - - //create index idx_fk_o_address_ctry on o_address(country_code); - ctx.write("create index "); - - String idxName = ddlSyntax.getIndexName(tableName, p.getName(), ctx.incrementIxCount()); - if (idxName != null){ - ctx.write(idxName); - } - - ctx.write(" on ").write(baseTable).write(" ("); - for (int i = 0; i < columns.length; i++) { - if (i > 0){ - ctx.write(","); - } - ctx.write(columns[i].getLocalDbColumn()); - } - ctx.write(");").writeNewLine(); - } - } - - /** - * Return 'add' or 'drop' of foreign key. - */ - protected String addOrDrop() { - return addMode ? " add " : " drop " ; - } - - @Override - public void visitScalar(BeanProperty p) { - // not interested - } - - @Override - public void visitCompound(BeanPropertyCompound p) { - // not interested - } - - @Override - public void visitCompoundScalar(BeanPropertyCompound compound, BeanProperty p) { - // not interested - } - } - -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/BaseTablePropertyVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/BaseTablePropertyVisitor.java deleted file mode 100644 index b02109deb..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/BaseTablePropertyVisitor.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound; - -/** - * Used to help mark PropertyVisitor methods that need to be implemented - * to visit base table properties. - */ -public abstract class BaseTablePropertyVisitor implements PropertyVisitor { - - /** - * Not required in that you can use the visitEmbeddedScalar. - */ - public void visitEmbedded(BeanPropertyAssocOne> p) { - } - - /** - * Override this method. - */ - public abstract void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne> embedded); - - /** - * Not part of base table. - */ - public void visitMany(BeanPropertyAssocMany> p) { - } - - /** - * Not part of base table. - */ - public void visitOneExported(BeanPropertyAssocOne> p) { - } - - /** - * Override this method for the foreign key. - */ - public abstract void visitOneImported(BeanPropertyAssocOne> p); - - /** - * Override this method for normal scalar property. - */ - public abstract void visitScalar(BeanProperty p); - - /** - * Not required in that the scalar properties map to the columns. - */ - public void visitCompound(BeanPropertyCompound p){ - } - - /** - * Override this method for scalar property inside a Immutable Compound Value object. - */ - public abstract void visitCompoundScalar(BeanPropertyCompound compound, BeanProperty p); - - - -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/BeanVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/BeanVisitor.java deleted file mode 100644 index 50ffa643f..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/BeanVisitor.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; - -/** - * Visitor pattern for visiting a BeanDescriptor and potentially all its bean - * properties. - */ -public interface BeanVisitor { - - /** - * Starting. - */ - void visitBegin(); - - /** - * Visit a BeanDescriptor and return true to continue visiting the bean - * (return false to skip visiting this bean). - */ - boolean visitBean(BeanDescriptor> descriptor); - - /** - * Visit a property potentially return a specific PropertyVisitor. - *
- * A PropertyVisitor can be returned to more easily process bean properties - * by their specific type. - *
- */ - PropertyVisitor visitProperty(BeanProperty p); - - /** - * Finished visiting the BeanDescriptor. - */ - void visitBeanEnd(BeanDescriptor> descriptor); - - /** - * Finished all visiting. - */ - void visitEnd(); - -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateIndexVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateIndexVisitor.java deleted file mode 100644 index 0d11865d8..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateIndexVisitor.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound; - -/** - * A visitor that creates indexes for columns annotated with ColumnIndex - * - * @author rvbiljouw - * @see com.avaje.ebean.annotation.Index - */ -public class CreateIndexVisitor extends AbstractBeanVisitor { - private final IndexPropertyVisitor iv; - - public CreateIndexVisitor(DdlGenContext ctx) { - this.iv = new IndexPropertyVisitor(ctx); - } - - @Override - public void visitBegin() { - - } - - @Override - public boolean visitBean(BeanDescriptor> descriptor) { - return descriptor.isInheritanceRoot(); - } - - @Override - public PropertyVisitor visitProperty(BeanProperty p) { - return iv; - } - - @Override - public void visitBeanEnd(BeanDescriptor> descriptor) { - visitInheritanceProperties(descriptor, iv); - } - - @Override - public void visitEnd() { - - } - - private static final class IndexPropertyVisitor extends BaseTablePropertyVisitor { - private final DdlGenContext ctx; - - public IndexPropertyVisitor(DdlGenContext ctx) { - this.ctx = ctx; - } - - @Override - public void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne> embedded) { - - } - - @Override - public void visitOneImported(BeanPropertyAssocOne> p) { - - } - - @Override - public void visitScalar(BeanProperty p) { - String baseTable = p.getBeanDescriptor().getBaseTable(); - if (p.isIndexed()) { - String indexName = p.getIndexName(); - if (indexName.length() == 0) { - indexName = ctx.getDdlSyntax().getIndexName(baseTable, p.getDbColumn(), ctx.incrementIxCount()); - } - ctx.write("create index ") - .write(indexName) - .write(" on ") - .write(baseTable) - .write("(") - .write(p.getDbColumn()) - .write(");") - .writeNewLine(); - } - } - - @Override - public void visitCompoundScalar(BeanPropertyCompound compound, BeanProperty p) { - - } - } -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateIntersectionTable.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateIntersectionTable.java deleted file mode 100644 index e7c114fdc..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateIntersectionTable.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; -import com.avaje.ebeaninternal.server.deploy.TableJoin; -import com.avaje.ebeaninternal.server.deploy.TableJoinColumn; - - -/** - * Create the sql to create the intersection table and its foreign key - * constraints. - */ -public class CreateIntersectionTable { - - private static final String NEW_LINE = "\n"; - - private final DdlGenContext ctx; - - private final BeanPropertyAssocMany> manyProp; - private final TableJoin intersectionTableJoin; - private final TableJoin tableJoin; - - private final StringBuilder sb = new StringBuilder(); - - private final StringBuilder pkeySb = new StringBuilder(); - - private int foreignKeyCount; - - private final int maxFkeyLength; - - public CreateIntersectionTable(DdlGenContext ctx, BeanPropertyAssocMany> manyProp) { - this.ctx = ctx; - this.manyProp = manyProp; - this.intersectionTableJoin = manyProp.getIntersectionTableJoin(); - this.tableJoin = manyProp.getTableJoin(); - this.maxFkeyLength = ctx.getDdlSyntax().getMaxConstraintNameLength()-3; - } - - public void build() { - - String createTable = buildCreateTable(); - ctx.addCreateIntersectionTable(createTable); - - foreignKeyCount = 0; - buildFkConstraints(); - } - - private void buildFkConstraints() { - - BeanDescriptor> localDesc = manyProp.getBeanDescriptor(); - String fk1 = buildFkConstraints(localDesc, intersectionTableJoin.columns(), true); - ctx.addIntersectionTableFk(fk1); - - BeanDescriptor> targetDesc = manyProp.getTargetDescriptor(); - String fk2 = buildFkConstraints(targetDesc, tableJoin.columns(), false); - ctx.addIntersectionTableFk(fk2); - - } - - - - private String getFkNameSuffix() { - - foreignKeyCount++; - - if (foreignKeyCount > 9){ - return "_"+foreignKeyCount; - } else { - return "_0"+foreignKeyCount; - } - } - - private String getFkNameWithSuffix(String fkName) { - if (fkName.length() > maxFkeyLength){ - fkName = fkName.substring(0, maxFkeyLength); - } - return fkName+getFkNameSuffix(); - } - - private String buildFkConstraints(BeanDescriptor> desc, TableJoinColumn[] columns, boolean direction) { - - - StringBuilder fkBuf = new StringBuilder(); - - String fkName = "fk_"+intersectionTableJoin.getTable()+"_"+desc.getBaseTable(); - - fkName = getFkNameWithSuffix(fkName); - - fkBuf.append("alter table "); - fkBuf.append(intersectionTableJoin.getTable()); - fkBuf.append(" add constraint ").append(fkName); - - fkBuf.append(" foreign key ("); - - for (int i = 0; i < columns.length; i++) { - if (i > 0) { - fkBuf.append(", "); - } - String col = direction ? columns[i].getForeignDbColumn() : columns[i].getLocalDbColumn(); - fkBuf.append(col); - } - fkBuf.append(") references ").append(desc.getBaseTable()).append(" ("); - - for (int i = 0; i < columns.length; i++) { - if (i > 0) { - fkBuf.append(", "); - } - String col = !direction ? columns[i].getForeignDbColumn() : columns[i].getLocalDbColumn(); - fkBuf.append(col); - } - fkBuf.append(")"); - - String fkeySuffix = ctx.getDdlSyntax().getForeignKeySuffix(); - if (fkeySuffix != null){ - fkBuf.append(" "); - fkBuf.append(fkeySuffix); - } - fkBuf.append(";").append(NEW_LINE); - - return fkBuf.toString(); - } - - private String buildCreateTable() { - - BeanDescriptor> localDesc = manyProp.getBeanDescriptor(); - BeanDescriptor> targetDesc = manyProp.getTargetDescriptor(); - - sb.append("create table "); - sb.append(intersectionTableJoin.getTable()); - sb.append(" (").append(NEW_LINE); - - TableJoinColumn[] columns = intersectionTableJoin.columns(); - for (int i = 0; i < columns.length; i++) { - - addColumn(localDesc, columns[i].getForeignDbColumn(), columns[i].getLocalDbColumn()); - } - - TableJoinColumn[] otherColumns = tableJoin.columns(); - for (int i = 0; i < otherColumns.length; i++) { - - addColumn(targetDesc, otherColumns[i].getLocalDbColumn(), otherColumns[i].getForeignDbColumn()); - } - - sb.append(" constraint pk_").append(intersectionTableJoin.getTable()); - sb.append(" primary key (").append(pkeySb.toString().substring(2)); - sb.append("))").append(NEW_LINE).append(";").append(NEW_LINE); - - return sb.toString(); - } - - private void addColumn(BeanDescriptor> desc, String column, String findPropColumn) { - - pkeySb.append(", "); - pkeySb.append(column); - - writeColumn(column); - - BeanProperty p = desc.getIdBinder().findBeanProperty(findPropColumn); - if (p == null) { - throw new RuntimeException("Could not find id property for " + findPropColumn); - } - - String columnDefn = ctx.getColumnDefn(p); - sb.append(columnDefn); - sb.append(" not null"); - sb.append(",").append(NEW_LINE); - } - - private void writeColumn(String columnName) { - sb.append(" ").append(ctx.pad(columnName, 30)).append(" "); - } -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateSequenceVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateSequenceVisitor.java deleted file mode 100644 index eb2690296..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateSequenceVisitor.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Used to generate the drop table DDL script. - */ -public class CreateSequenceVisitor implements BeanVisitor { - - private static final Logger logger = LoggerFactory.getLogger(CreateSequenceVisitor.class); - - private final DdlGenContext ctx; - - private final boolean supportsSequence; - - public CreateSequenceVisitor(DdlGenContext ctx) { - this.ctx = ctx; - this.supportsSequence = ctx.getDbPlatform().getDbIdentity().isSupportsSequence(); - } - - public boolean visitBean(BeanDescriptor> descriptor) { - - if (!descriptor.isInheritanceRoot()){ - return false; - } - if (descriptor.getSequenceName() == null) { - return false; - } - - if (!supportsSequence){ - // Hopefully a generic test case - String msg = "Not creating sequence "+descriptor.getSequenceName()+" on Bean "+descriptor.getName() - +" as DatabasePlatform does not support sequences"; - logger.warn(msg); - return false; - } - - String sequenceName = descriptor.getSequenceName(); - if (sequenceName != null) { - int initialValue = descriptor.getSequenceInitialValue(); - int allocationSize = descriptor.getSequenceAllocationSize(); - String createSeq = ctx.getDbPlatform().ddlCreateSequence(sequenceName, initialValue, allocationSize); - ctx.write(createSeq).writeNewLine().writeNewLine(); - } - return true; - } - - - public void visitBeanEnd(BeanDescriptor> descriptor) { - } - - public void visitBegin() { - } - - public void visitEnd() { - } - - public PropertyVisitor visitProperty(BeanProperty p) { - // Return null as we are not interested in properties - return null; - } - -} diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateTableColumnVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateTableColumnVisitor.java deleted file mode 100644 index 03f73e061..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateTableColumnVisitor.java +++ /dev/null @@ -1,231 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import java.sql.Types; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.avaje.ebean.config.TableName; -import com.avaje.ebean.config.dbplatform.DbDdlSyntax; -import com.avaje.ebean.config.dbplatform.IdType; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound; -import com.avaje.ebeaninternal.server.deploy.TableJoin; -import com.avaje.ebeaninternal.server.deploy.TableJoinColumn; -import com.avaje.ebeaninternal.server.deploy.id.ImportedId; -import com.avaje.ebeaninternal.server.lib.util.StringHelper; - -/** - * Used as part of CreateTableVisitor to generated the create table DDL script. - */ -public class CreateTableColumnVisitor extends BaseTablePropertyVisitor { - - private static final Logger logger = LoggerFactory.getLogger(CreateTableColumnVisitor.class); - - private final DdlGenContext ctx; - - private final DbDdlSyntax ddl; - - private final CreateTableVisitor parent; - - private BeanPropertyAssocOne> embedded; - - public CreateTableColumnVisitor(CreateTableVisitor parent, DdlGenContext ctx) { - this.parent = parent; - this.ctx = ctx; - this.ddl = ctx.getDdlSyntax(); - } - - @Override - public void visitMany(BeanPropertyAssocMany> p) { - if (p.isManyToMany()) { - //noinspection StatementWithEmptyBody - if (p.getMappedBy() != null) { - // only create on other 'owning' side - - } else { - TableJoin intersectionTableJoin = p.getIntersectionTableJoin(); - - // check if the intersection table has already been created - String intTable = intersectionTableJoin.getTable(); - if (ctx.isProcessIntersectionTable(intTable)) { - // build the create table and fkey constraints - // putting the DDL into ctx for later output as we are - // in the middle of rendering the create table DDL - new CreateIntersectionTable(ctx, p).build(); - } - } - } - } - - public void visitCompoundScalar(BeanPropertyCompound compound, BeanProperty p) { - visitScalar(p); - } - - public void visitCompound(BeanPropertyCompound p) { - // do nothing - } - - @Override - public void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne> embedded) { - - this.embedded = embedded; - visitScalar(p); - } - - private StringBuilder createUniqueConstraintBuffer(String table, String column) { - - String uqConstraintName = "uq_"+TableName.parse(table)+"_"+column; - - if (uqConstraintName.length() > ddl.getMaxConstraintNameLength()) { - uqConstraintName = uqConstraintName.substring(0, ddl.getMaxConstraintNameLength()); - } - - uqConstraintName = ctx.removeQuotes(uqConstraintName); - uqConstraintName = StringHelper.replaceString(uqConstraintName, " ", "_"); - - StringBuilder constraintExpr = new StringBuilder(); - constraintExpr.append("constraint ").append(uqConstraintName).append(" unique ("); - - return constraintExpr; - } - - @Override - public void visitOneImported(BeanPropertyAssocOne> p) { - - ImportedId importedId = p.getImportedId(); - - TableJoinColumn[] columns = p.getTableJoin().columns(); - if (columns.length == 0) { - String msg = "No join columns for " + p.getFullBeanName(); - throw new RuntimeException(msg); - } - - StringBuilder constraintExpr = createUniqueConstraintBuffer(p.getBeanDescriptor().getBaseTable(), columns[0].getLocalDbColumn()); - - for (int i = 0; i < columns.length; i++) { - - String dbCol = columns[i].getLocalDbColumn(); - - if (i > 0) { - constraintExpr.append(", "); - } - constraintExpr.append(dbCol); - - if (parent.isDbColumnWritten(dbCol)) { - continue; - } - - parent.writeColumnName(dbCol, p); - - BeanProperty importedProperty = importedId.findMatchImport(dbCol); - if (importedProperty != null) { - - String columnDefn = ctx.getColumnDefn(importedProperty); - ctx.write(columnDefn); - - } else { - throw new RuntimeException("Imported BeanProperty not found?"); - } - - if (!p.isNullable()) { - ctx.write(" not null"); - } - ctx.write(",").writeNewLine(); - } - constraintExpr.append(")"); - - if (p.isOneToOne()) { - if (ddl.isAddOneToOneUniqueContraint()) { - parent.addUniqueConstraint(constraintExpr.toString()); - } - } - - } - - @Override - public void visitScalar(BeanProperty p) { - - if (p.isSecondaryTable()) { - return; - } - - if (parent.isDbColumnWritten(p.getDbColumn())) { - return; - } - - parent.writeColumnName(p.getDbColumn(), p); - - String columnDefn = ctx.getColumnDefn(p); - - boolean identity = isIdentity(p); - if (identity) { - ctx.write(ddl.getIdentityColumnDefn(columnDefn)); - } else { - ctx.write(columnDefn); - } - - if (p.isId() && ddl.isInlinePrimaryKeyConstraint()){ - ctx.write(" primary key"); - - } else if (!p.isNullable() || p.isDDLNotNull()) { - ctx.write(" not null"); - } - - if (identity) { - writeIdentitySuffix(); - } - - if (p.isUnique() && !p.isId()) { - parent.addUniqueConstraint(createUniqueConstraint(p)); - } - - parent.addCheckConstraint(p); - ctx.write(",").writeNewLine(); - } - - private String createUniqueConstraint(BeanProperty p) { - - String baseTable = p.getBeanDescriptor().getBaseTable(); - if (baseTable == null) { - // Embedded bean property that has unique constraint on it - baseTable = embedded.getBeanDescriptor().getBaseTable(); - } - StringBuilder expr = createUniqueConstraintBuffer(baseTable, p.getDbColumn()); - - expr.append(p.getDbColumn()).append(")"); - return expr.toString(); - } - - protected void writeIdentitySuffix() { - String identity = ddl.getIdentitySuffix(); - if (identity != null && identity.length() > 0) { - ctx.write(" ").write(identity); - } - } - - protected boolean isIdentity(BeanProperty p) { - - if (p.isId()) { - try { - IdType idType = p.getBeanDescriptor().getIdType(); - - if (idType.equals(IdType.IDENTITY)) { - - int jdbcType = p.getScalarType().getJdbcType(); - if (jdbcType == Types.INTEGER || jdbcType == Types.BIGINT || jdbcType == Types.SMALLINT) { - - return true; - } - } - } catch (Exception e) { - String msg = "Error determining identity on property " + p.getFullBeanName(); - logger.error(msg, e); - } - } - return false; - } - -} \ No newline at end of file diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateTableVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateTableVisitor.java deleted file mode 100644 index 5c11200f4..000000000 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/CreateTableVisitor.java +++ /dev/null @@ -1,264 +0,0 @@ -package com.avaje.ebeaninternal.server.ddl; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.avaje.ebean.config.TableName; -import com.avaje.ebean.config.dbplatform.DbDdlSyntax; -import com.avaje.ebean.config.dbplatform.DbType; -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne; -import com.avaje.ebeaninternal.server.deploy.BeanPropertyCompound; -import com.avaje.ebeaninternal.server.deploy.CompoundUniqueContraint; -import com.avaje.ebeaninternal.server.deploy.InheritInfo; -import com.avaje.ebeaninternal.server.deploy.parse.SqlReservedWords; - -/** - * Used to generated the create table DDL script. - */ -public class CreateTableVisitor extends AbstractBeanVisitor { - - private static final Logger logger = LoggerFactory.getLogger(CreateTableVisitor.class); - - private final DdlGenContext ctx; - - private final PropertyVisitor pv; - - private final DbDdlSyntax ddl; - - private final int columnNameWidth; - - // avoid writing columns twice, e.g. when used in associations with insertable=false and updateable=false - private final Set- * Typically check constraint based on Enum mapping values. - *
- */ - protected void addCheckConstraint(BeanProperty p, String prefix, String constraintExpression) { - - if (p != null && constraintExpression != null) { - - // build constraint clause - String s = "constraint " + getConstraintName(prefix, p) + " " + constraintExpression; - - // add to list as we render all check constraints just prior to primary key - checkConstraints.add(s); - } - } - - protected String getConstraintName(String prefix, BeanProperty p) { - return prefix + table + "_" + p.getDbColumn(); - } - - protected void addUniqueConstraint(String constraintExpression) { - uniqueConstraints.add(constraintExpression); - } - - protected void addCheckConstraint(String constraintExpression) { - checkConstraints.add(constraintExpression); - } - - protected void addCheckConstraint(BeanProperty p) { - addCheckConstraint(p, "ck_", p.getDbConstraintExpression()); - } - - public boolean visitBean(BeanDescriptor> descriptor) { - - wroteColumns.clear(); - - if (!descriptor.isInheritanceRoot()) { - return false; - } - - ctx.write("create table "); - writeTableName(descriptor); - ctx.write(" (").writeNewLine(); - - InheritInfo inheritInfo = descriptor.getInheritInfo(); - if (inheritInfo != null && inheritInfo.isRoot()) { - String discColumn = inheritInfo.getDiscriminatorColumn(); - int discType = inheritInfo.getDiscriminatorType(); - int discLength = inheritInfo.getDiscriminatorLength(); - DbType dbType = ctx.getDbTypeMap().get(discType); - String discDbType = dbType.renderType(discLength, 0); - - writeColumnName(discColumn, null); - ctx.write(discDbType); - ctx.write(" not null,"); - ctx.writeNewLine(); - } - - return true; - } - - public void visitBeanEnd(BeanDescriptor> descriptor) { - - visitInheritanceProperties(descriptor, pv); - - if (checkConstraints.size() > 0) { - for (String checkConstraint : checkConstraints) { - ctx.write(" ").write(checkConstraint).write(",").writeNewLine(); - } - checkConstraints = new ArrayList