diff --git a/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractBeanVisitor.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractBeanVisitor.java index 5f4afab3b..b83d4cd03 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractBeanVisitor.java +++ b/src/main/java/com/avaje/ebeaninternal/server/ddl/AbstractBeanVisitor.java @@ -1,46 +1,46 @@ -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); - } - } -} +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/DdlGenContext.java b/src/main/java/com/avaje/ebeaninternal/server/ddl/DdlGenContext.java index 9c14138bf..4c4cb5fa6 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/ddl/DdlGenContext.java +++ b/src/main/java/com/avaje/ebeaninternal/server/ddl/DdlGenContext.java @@ -1,252 +1,252 @@ -package com.avaje.ebeaninternal.server.ddl; - -import java.io.StringWriter; -import java.sql.Types; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import com.avaje.ebean.config.NamingConvention; -import com.avaje.ebean.config.dbplatform.DatabasePlatform; -import com.avaje.ebean.config.dbplatform.DbDdlSyntax; -import com.avaje.ebean.config.dbplatform.DbType; -import com.avaje.ebean.config.dbplatform.DbTypeMap; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.lib.util.StringHelper; -import com.avaje.ebeaninternal.server.type.ScalarType; - -/** - * The context used during DDL generation. - */ -public class DdlGenContext { - - private final StringWriter stringWriter = new StringWriter(); - - /** - * Used to map bean types to DB specific types. - */ - private final DbTypeMap dbTypeMap; - - /** - * Handles DB specific DDL syntax. - */ - private final DbDdlSyntax ddlSyntax; - - /** - * The new line character that is used. - */ - private final String newLine; - - /** - * Last content written (used with removeLast()) - */ - private final List contentBuffer = new ArrayList(); - - private Set intersectionTables = new HashSet(); - - private List intersectionTablesCreateDdl = new ArrayList(); - private List intersectionTablesFkDdl = new ArrayList(); - - private final DatabasePlatform dbPlatform; - - /** The Naming convention used to define FK an IX names */ - private final NamingConvention namingConvention; - - /** The global fk count used to keep FK names unique */ - private int fkCount; - - /** The ix count. */ - private int ixCount; - - public DdlGenContext(DatabasePlatform dbPlatform, NamingConvention namingConvention){ - this.dbPlatform = dbPlatform; - this.dbTypeMap = dbPlatform.getDbTypeMap(); - this.ddlSyntax = dbPlatform.getDbDdlSyntax(); - this.newLine = ddlSyntax.getNewLine(); - this.namingConvention = namingConvention; - } - - /** - * Return the dbPlatform. - */ - public DatabasePlatform getDbPlatform() { - return dbPlatform; - } - - public boolean isProcessIntersectionTable(String tableName){ - return intersectionTables.add(tableName); - } - - public void addCreateIntersectionTable(String createTableDdl){ - intersectionTablesCreateDdl.add(createTableDdl); - } - - public void addIntersectionTableFk(String intTableFk){ - intersectionTablesFkDdl.add(intTableFk); - } - - public void addIntersectionCreateTables() { - for (String intTableCreate : intersectionTablesCreateDdl) { - write(newLine); - write(intTableCreate); - } - } - - public void addIntersectionFkeys() { - write(newLine); - write(newLine); - for (String intTableFk : intersectionTablesFkDdl) { - write(newLine); - write(intTableFk); - } - } - - /** - * Return the generated content (DDL script). - */ - public String getContent(){ - return stringWriter.toString(); - } - - /** - * Return the map used to determine the DB specific type - * for a given bean property. - */ - public DbTypeMap getDbTypeMap() { - return dbTypeMap; - } - - /** - * Return object to handle DB specific DDL syntax. - */ - public DbDdlSyntax getDdlSyntax() { - return ddlSyntax; - } - - public String getColumnDefn(BeanProperty p) { - DbType dbType = getDbType(p); - return p.renderDbType(dbType); - } - - private DbType getDbType(BeanProperty p) { - - ScalarType scalarType = p.getScalarType(); - if (scalarType == null) { - throw new RuntimeException("No scalarType for " + p.getFullBeanName()); - } - - if (p.isDbEncrypted()){ - return dbTypeMap.get(p.getDbEncryptedType()); - } - - int jdbcType = scalarType.getJdbcType(); - if (p.isLob() && jdbcType == Types.VARCHAR){ - // workaround for Postgres TEXT type which is - // VARCHAR in jdbc API but TEXT in ddl - jdbcType = Types.CLOB; - } - return dbTypeMap.get(jdbcType); - } - /** - * Write content to the buffer. - */ - public DdlGenContext write(String content, int minWidth){ - - content = pad(content, minWidth); - - contentBuffer.add(content); - - return this; - - } - - /** - * Write content to the buffer. - */ - public DdlGenContext write(String content){ - return write(content, 0); - } - - public DdlGenContext writeNewLine() { - write(newLine); - return this; - } - - /** - * Remove the last content that was written. - */ - public DdlGenContext removeLast() { - if (!contentBuffer.isEmpty()){ - contentBuffer.remove(contentBuffer.size()-1); - } else { - throw new RuntimeException("No lastContent to remove?"); - } - return this; - } - - /** - * Flush the content to the buffer. - */ - public DdlGenContext flush() { - if (!contentBuffer.isEmpty()){ - for (String s:contentBuffer){ - - if (s != null){ - stringWriter.write(s); - } - } - contentBuffer.clear(); - } - return this; - } - - private String padding(int length){ - - StringBuffer sb = new StringBuffer(length); - for (int i = 0; i < length; i++) { - sb.append(" "); - } - return sb.toString(); - } - - public String pad(String content, int minWidth){ - if (minWidth > 0 && content.length() < minWidth){ - int padding = minWidth - content.length(); - return content + padding(padding); - } - return content; - } - - /** - * @return the namingConvention - */ - public NamingConvention getNamingConvention() { - return namingConvention; - } - - /** - * @return the incremented fkCount - */ - public int incrementFkCount() { - return ++fkCount; - } - - /** - * @return the incremented ixCount - */ - public int incrementIxCount() { - return ++ixCount; - } - - /** - * Strips off the Database Platform specific quoted identifier characters. - */ - public String removeQuotes(String dbColumn) { - - dbColumn = StringHelper.replaceString(dbColumn, dbPlatform.getOpenQuote(), ""); - dbColumn = StringHelper.replaceString(dbColumn, dbPlatform.getCloseQuote(), ""); - - return dbColumn; - } -} +package com.avaje.ebeaninternal.server.ddl; + +import java.io.StringWriter; +import java.sql.Types; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.avaje.ebean.config.NamingConvention; +import com.avaje.ebean.config.dbplatform.DatabasePlatform; +import com.avaje.ebean.config.dbplatform.DbDdlSyntax; +import com.avaje.ebean.config.dbplatform.DbType; +import com.avaje.ebean.config.dbplatform.DbTypeMap; +import com.avaje.ebeaninternal.server.deploy.BeanProperty; +import com.avaje.ebeaninternal.server.lib.util.StringHelper; +import com.avaje.ebeaninternal.server.type.ScalarType; + +/** + * The context used during DDL generation. + */ +public class DdlGenContext { + + private final StringWriter stringWriter = new StringWriter(); + + /** + * Used to map bean types to DB specific types. + */ + private final DbTypeMap dbTypeMap; + + /** + * Handles DB specific DDL syntax. + */ + private final DbDdlSyntax ddlSyntax; + + /** + * The new line character that is used. + */ + private final String newLine; + + /** + * Last content written (used with removeLast()) + */ + private final List contentBuffer = new ArrayList(); + + private Set intersectionTables = new HashSet(); + + private List intersectionTablesCreateDdl = new ArrayList(); + private List intersectionTablesFkDdl = new ArrayList(); + + private final DatabasePlatform dbPlatform; + + /** The Naming convention used to define FK an IX names */ + private final NamingConvention namingConvention; + + /** The global fk count used to keep FK names unique */ + private int fkCount; + + /** The ix count. */ + private int ixCount; + + public DdlGenContext(DatabasePlatform dbPlatform, NamingConvention namingConvention){ + this.dbPlatform = dbPlatform; + this.dbTypeMap = dbPlatform.getDbTypeMap(); + this.ddlSyntax = dbPlatform.getDbDdlSyntax(); + this.newLine = ddlSyntax.getNewLine(); + this.namingConvention = namingConvention; + } + + /** + * Return the dbPlatform. + */ + public DatabasePlatform getDbPlatform() { + return dbPlatform; + } + + public boolean isProcessIntersectionTable(String tableName){ + return intersectionTables.add(tableName); + } + + public void addCreateIntersectionTable(String createTableDdl){ + intersectionTablesCreateDdl.add(createTableDdl); + } + + public void addIntersectionTableFk(String intTableFk){ + intersectionTablesFkDdl.add(intTableFk); + } + + public void addIntersectionCreateTables() { + for (String intTableCreate : intersectionTablesCreateDdl) { + write(newLine); + write(intTableCreate); + } + } + + public void addIntersectionFkeys() { + write(newLine); + write(newLine); + for (String intTableFk : intersectionTablesFkDdl) { + write(newLine); + write(intTableFk); + } + } + + /** + * Return the generated content (DDL script). + */ + public String getContent(){ + return stringWriter.toString(); + } + + /** + * Return the map used to determine the DB specific type + * for a given bean property. + */ + public DbTypeMap getDbTypeMap() { + return dbTypeMap; + } + + /** + * Return object to handle DB specific DDL syntax. + */ + public DbDdlSyntax getDdlSyntax() { + return ddlSyntax; + } + + public String getColumnDefn(BeanProperty p) { + DbType dbType = getDbType(p); + return p.renderDbType(dbType); + } + + private DbType getDbType(BeanProperty p) { + + ScalarType scalarType = p.getScalarType(); + if (scalarType == null) { + throw new RuntimeException("No scalarType for " + p.getFullBeanName()); + } + + if (p.isDbEncrypted()){ + return dbTypeMap.get(p.getDbEncryptedType()); + } + + int jdbcType = scalarType.getJdbcType(); + if (p.isLob() && jdbcType == Types.VARCHAR){ + // workaround for Postgres TEXT type which is + // VARCHAR in jdbc API but TEXT in ddl + jdbcType = Types.CLOB; + } + return dbTypeMap.get(jdbcType); + } + /** + * Write content to the buffer. + */ + public DdlGenContext write(String content, int minWidth){ + + content = pad(content, minWidth); + + contentBuffer.add(content); + + return this; + + } + + /** + * Write content to the buffer. + */ + public DdlGenContext write(String content){ + return write(content, 0); + } + + public DdlGenContext writeNewLine() { + write(newLine); + return this; + } + + /** + * Remove the last content that was written. + */ + public DdlGenContext removeLast() { + if (!contentBuffer.isEmpty()){ + contentBuffer.remove(contentBuffer.size()-1); + } else { + throw new RuntimeException("No lastContent to remove?"); + } + return this; + } + + /** + * Flush the content to the buffer. + */ + public DdlGenContext flush() { + if (!contentBuffer.isEmpty()){ + for (String s:contentBuffer){ + + if (s != null){ + stringWriter.write(s); + } + } + contentBuffer.clear(); + } + return this; + } + + private String padding(int length){ + + StringBuffer sb = new StringBuffer(length); + for (int i = 0; i < length; i++) { + sb.append(" "); + } + return sb.toString(); + } + + public String pad(String content, int minWidth){ + if (minWidth > 0 && content.length() < minWidth){ + int padding = minWidth - content.length(); + return content + padding(padding); + } + return content; + } + + /** + * @return the namingConvention + */ + public NamingConvention getNamingConvention() { + return namingConvention; + } + + /** + * @return the incremented fkCount + */ + public int incrementFkCount() { + return ++fkCount; + } + + /** + * @return the incremented ixCount + */ + public int incrementIxCount() { + return ++ixCount; + } + + /** + * Strips off the Database Platform specific quoted identifier characters. + */ + public String removeQuotes(String dbColumn) { + + dbColumn = StringHelper.replaceString(dbColumn, dbPlatform.getOpenQuote(), ""); + dbColumn = StringHelper.replaceString(dbColumn, dbPlatform.getCloseQuote(), ""); + + return dbColumn; + } +}