@Test public void testAutoIncrementDB2Database() throws Exception { for (Database database : TestContext.getInstance().getAllDatabases()) { if (database instanceof DB2Database) { CreateTableStatement statement = new CreateTableStatement(SCHEMA_NAME, TABLE_NAME); statement.addColumn( COLUMN_NAME1, TypeConverterFactory.getInstance().findTypeConverter(database).getDataType("BIGINT", true), new AutoIncrementConstraint(COLUMN_NAME1) ); Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null); assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT GENERATED BY DEFAULT AS IDENTITY)", generatedSql[0].toSql()); } } }
public Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { StringBuffer buffer = new StringBuffer(); buffer.append("CREATE TABLE ").append(database.escapeTableName(statement.getSchemaName(), statement.getTableName())).append(" "); buffer.append("("); boolean isSinglePrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() == 1; boolean isPrimaryKeyAutoIncrement = false; Iterator<String> columnIterator = statement.getColumns().iterator(); while (columnIterator.hasNext()) { String column = columnIterator.next(); buffer.append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), column)); buffer.append(" ").append(statement.getColumnTypes().get(column)); AutoIncrementConstraint autoIncrementConstraint = null; for (AutoIncrementConstraint currentAutoIncrementConstraint : statement.getAutoIncrementConstraints()) { if (column.equals(currentAutoIncrementConstraint.getColumnName())) { autoIncrementConstraint = currentAutoIncrementConstraint; break; } } boolean isAutoIncrementColumn = autoIncrementConstraint != null; boolean isPrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().contains(column); isPrimaryKeyAutoIncrement = isPrimaryKeyAutoIncrement || isPrimaryKeyColumn && isAutoIncrementColumn; if ((database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyColumn && isAutoIncrementColumn) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } buffer.append(" PRIMARY KEY AUTOINCREMENT"); } if (statement.getDefaultValue(column) != null) { Object defaultValue = statement.getDefaultValue(column); if (database instanceof MSSQLDatabase) { buffer.append(" CONSTRAINT ").append(((MSSQLDatabase) database).generateDefaultConstraintName(statement.getTableName(), column)); } buffer.append(" DEFAULT "); buffer.append(statement.getColumnTypes().get(column).convertObjectToString(defaultValue, database)); } if (isAutoIncrementColumn && ! (database instanceof SQLiteDatabase)) { if (database.supportsAutoIncrement()) { String autoIncrementClause = database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy()); if (!"".equals(autoIncrementClause)) { buffer.append(" ").append(autoIncrementClause); } } else { LogFactory.getLogger().warning(database.getTypeName()+" does not support autoincrement columns as request for "+(database.escapeTableName(statement.getSchemaName(), statement.getTableName()))); } } if (statement.getNotNullColumns().contains(column)) { buffer.append(" NOT NULL"); } else { if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase || database instanceof MySQLDatabase) { if (database instanceof MySQLDatabase && statement.getColumnTypes().get(column).getDataTypeName().equalsIgnoreCase("timestamp")) { } else { buffer.append(" NULL"); } } } if (database instanceof InformixDatabase && isSinglePrimaryKeyColumn) { buffer.append(" PRIMARY KEY"); } if (columnIterator.hasNext()) { buffer.append(", "); } } buffer.append(","); if (!( (database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyAutoIncrement) && !((database instanceof InformixDatabase) && isSinglePrimaryKeyColumn )) { if (statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() > 0) { if (!(database instanceof InformixDatabase)) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } } buffer.append(" PRIMARY KEY ("); buffer.append(database.escapeColumnNameList(StringUtils.join(statement.getPrimaryKeyConstraint().getColumns(), ", "))); buffer.append(")"); if (database instanceof OracleDatabase && statement.getPrimaryKeyConstraint().getTablespace() != null) { buffer.append(" USING INDEX TABLESPACE "); buffer.append(statement.getPrimaryKeyConstraint().getTablespace()); } buffer.append(","); } } for (ForeignKeyConstraint fkConstraint : statement.getForeignKeyConstraints()) { if (!(database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } String referencesString = fkConstraint.getReferences(); if (!referencesString.contains(".") && database.getDefaultSchemaName() != null) { referencesString = database.getDefaultSchemaName()+"."+referencesString; } buffer.append(" FOREIGN KEY (") .append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), fkConstraint.getColumn())) .append(") REFERENCES ") .append(referencesString); if (fkConstraint.isDeleteCascade()) { buffer.append(" ON DELETE CASCADE"); } if ((database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } if (fkConstraint.isInitiallyDeferred()) { buffer.append(" INITIALLY DEFERRED"); } if (fkConstraint.isDeferrable()) { buffer.append(" DEFERRABLE"); } buffer.append(","); } for (UniqueConstraint uniqueConstraint : statement.getUniqueConstraints()) { if (uniqueConstraint.getConstraintName() != null && !constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(" UNIQUE ("); buffer.append(database.escapeColumnNameList(StringUtils.join(uniqueConstraint.getColumns(), ", "))); buffer.append(")"); if (uniqueConstraint.getConstraintName() != null && constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(","); } String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")"; if (statement.getTablespace() != null && database.supportsTablespaces()) { if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) { sql += " ON " + statement.getTablespace(); } else if (database instanceof DB2Database || database instanceof InformixDatabase) { sql += " IN " + statement.getTablespace(); } else { sql += " TABLESPACE " + statement.getTablespace(); } } return new Sql[] { new UnparsedSql(sql) }; }
CreateTableGenerator extends AbstractSqlGenerator<CreateTableStatement> { public Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { StringBuffer buffer = new StringBuffer(); buffer.append("CREATE TABLE ").append(database.escapeTableName(statement.getSchemaName(), statement.getTableName())).append(" "); buffer.append("("); boolean isSinglePrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() == 1; boolean isPrimaryKeyAutoIncrement = false; Iterator<String> columnIterator = statement.getColumns().iterator(); while (columnIterator.hasNext()) { String column = columnIterator.next(); buffer.append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), column)); buffer.append(" ").append(statement.getColumnTypes().get(column)); AutoIncrementConstraint autoIncrementConstraint = null; for (AutoIncrementConstraint currentAutoIncrementConstraint : statement.getAutoIncrementConstraints()) { if (column.equals(currentAutoIncrementConstraint.getColumnName())) { autoIncrementConstraint = currentAutoIncrementConstraint; break; } } boolean isAutoIncrementColumn = autoIncrementConstraint != null; boolean isPrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().contains(column); isPrimaryKeyAutoIncrement = isPrimaryKeyAutoIncrement || isPrimaryKeyColumn && isAutoIncrementColumn; if ((database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyColumn && isAutoIncrementColumn) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } buffer.append(" PRIMARY KEY AUTOINCREMENT"); } if (statement.getDefaultValue(column) != null) { Object defaultValue = statement.getDefaultValue(column); if (database instanceof MSSQLDatabase) { buffer.append(" CONSTRAINT ").append(((MSSQLDatabase) database).generateDefaultConstraintName(statement.getTableName(), column)); } buffer.append(" DEFAULT "); buffer.append(statement.getColumnTypes().get(column).convertObjectToString(defaultValue, database)); } if (isAutoIncrementColumn && ! (database instanceof SQLiteDatabase)) { if (database.supportsAutoIncrement()) { String autoIncrementClause = database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy()); if (!"".equals(autoIncrementClause)) { buffer.append(" ").append(autoIncrementClause); } } else { LogFactory.getLogger().warning(database.getTypeName()+" does not support autoincrement columns as request for "+(database.escapeTableName(statement.getSchemaName(), statement.getTableName()))); } } if (statement.getNotNullColumns().contains(column)) { buffer.append(" NOT NULL"); } else { if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase || database instanceof MySQLDatabase) { if (database instanceof MySQLDatabase && statement.getColumnTypes().get(column).getDataTypeName().equalsIgnoreCase("timestamp")) { } else { buffer.append(" NULL"); } } } if (database instanceof InformixDatabase && isSinglePrimaryKeyColumn) { buffer.append(" PRIMARY KEY"); } if (columnIterator.hasNext()) { buffer.append(", "); } } buffer.append(","); if (!( (database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyAutoIncrement) && !((database instanceof InformixDatabase) && isSinglePrimaryKeyColumn )) { if (statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() > 0) { if (!(database instanceof InformixDatabase)) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } } buffer.append(" PRIMARY KEY ("); buffer.append(database.escapeColumnNameList(StringUtils.join(statement.getPrimaryKeyConstraint().getColumns(), ", "))); buffer.append(")"); if (database instanceof OracleDatabase && statement.getPrimaryKeyConstraint().getTablespace() != null) { buffer.append(" USING INDEX TABLESPACE "); buffer.append(statement.getPrimaryKeyConstraint().getTablespace()); } buffer.append(","); } } for (ForeignKeyConstraint fkConstraint : statement.getForeignKeyConstraints()) { if (!(database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } String referencesString = fkConstraint.getReferences(); if (!referencesString.contains(".") && database.getDefaultSchemaName() != null) { referencesString = database.getDefaultSchemaName()+"."+referencesString; } buffer.append(" FOREIGN KEY (") .append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), fkConstraint.getColumn())) .append(") REFERENCES ") .append(referencesString); if (fkConstraint.isDeleteCascade()) { buffer.append(" ON DELETE CASCADE"); } if ((database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } if (fkConstraint.isInitiallyDeferred()) { buffer.append(" INITIALLY DEFERRED"); } if (fkConstraint.isDeferrable()) { buffer.append(" DEFERRABLE"); } buffer.append(","); } for (UniqueConstraint uniqueConstraint : statement.getUniqueConstraints()) { if (uniqueConstraint.getConstraintName() != null && !constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(" UNIQUE ("); buffer.append(database.escapeColumnNameList(StringUtils.join(uniqueConstraint.getColumns(), ", "))); buffer.append(")"); if (uniqueConstraint.getConstraintName() != null && constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(","); } String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")"; if (statement.getTablespace() != null && database.supportsTablespaces()) { if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) { sql += " ON " + statement.getTablespace(); } else if (database instanceof DB2Database || database instanceof InformixDatabase) { sql += " IN " + statement.getTablespace(); } else { sql += " TABLESPACE " + statement.getTablespace(); } } return new Sql[] { new UnparsedSql(sql) }; } }
CreateTableGenerator extends AbstractSqlGenerator<CreateTableStatement> { public Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { StringBuffer buffer = new StringBuffer(); buffer.append("CREATE TABLE ").append(database.escapeTableName(statement.getSchemaName(), statement.getTableName())).append(" "); buffer.append("("); boolean isSinglePrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() == 1; boolean isPrimaryKeyAutoIncrement = false; Iterator<String> columnIterator = statement.getColumns().iterator(); while (columnIterator.hasNext()) { String column = columnIterator.next(); buffer.append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), column)); buffer.append(" ").append(statement.getColumnTypes().get(column)); AutoIncrementConstraint autoIncrementConstraint = null; for (AutoIncrementConstraint currentAutoIncrementConstraint : statement.getAutoIncrementConstraints()) { if (column.equals(currentAutoIncrementConstraint.getColumnName())) { autoIncrementConstraint = currentAutoIncrementConstraint; break; } } boolean isAutoIncrementColumn = autoIncrementConstraint != null; boolean isPrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().contains(column); isPrimaryKeyAutoIncrement = isPrimaryKeyAutoIncrement || isPrimaryKeyColumn && isAutoIncrementColumn; if ((database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyColumn && isAutoIncrementColumn) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } buffer.append(" PRIMARY KEY AUTOINCREMENT"); } if (statement.getDefaultValue(column) != null) { Object defaultValue = statement.getDefaultValue(column); if (database instanceof MSSQLDatabase) { buffer.append(" CONSTRAINT ").append(((MSSQLDatabase) database).generateDefaultConstraintName(statement.getTableName(), column)); } buffer.append(" DEFAULT "); buffer.append(statement.getColumnTypes().get(column).convertObjectToString(defaultValue, database)); } if (isAutoIncrementColumn && ! (database instanceof SQLiteDatabase)) { if (database.supportsAutoIncrement()) { String autoIncrementClause = database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy()); if (!"".equals(autoIncrementClause)) { buffer.append(" ").append(autoIncrementClause); } } else { LogFactory.getLogger().warning(database.getTypeName()+" does not support autoincrement columns as request for "+(database.escapeTableName(statement.getSchemaName(), statement.getTableName()))); } } if (statement.getNotNullColumns().contains(column)) { buffer.append(" NOT NULL"); } else { if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase || database instanceof MySQLDatabase) { if (database instanceof MySQLDatabase && statement.getColumnTypes().get(column).getDataTypeName().equalsIgnoreCase("timestamp")) { } else { buffer.append(" NULL"); } } } if (database instanceof InformixDatabase && isSinglePrimaryKeyColumn) { buffer.append(" PRIMARY KEY"); } if (columnIterator.hasNext()) { buffer.append(", "); } } buffer.append(","); if (!( (database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyAutoIncrement) && !((database instanceof InformixDatabase) && isSinglePrimaryKeyColumn )) { if (statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() > 0) { if (!(database instanceof InformixDatabase)) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } } buffer.append(" PRIMARY KEY ("); buffer.append(database.escapeColumnNameList(StringUtils.join(statement.getPrimaryKeyConstraint().getColumns(), ", "))); buffer.append(")"); if (database instanceof OracleDatabase && statement.getPrimaryKeyConstraint().getTablespace() != null) { buffer.append(" USING INDEX TABLESPACE "); buffer.append(statement.getPrimaryKeyConstraint().getTablespace()); } buffer.append(","); } } for (ForeignKeyConstraint fkConstraint : statement.getForeignKeyConstraints()) { if (!(database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } String referencesString = fkConstraint.getReferences(); if (!referencesString.contains(".") && database.getDefaultSchemaName() != null) { referencesString = database.getDefaultSchemaName()+"."+referencesString; } buffer.append(" FOREIGN KEY (") .append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), fkConstraint.getColumn())) .append(") REFERENCES ") .append(referencesString); if (fkConstraint.isDeleteCascade()) { buffer.append(" ON DELETE CASCADE"); } if ((database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } if (fkConstraint.isInitiallyDeferred()) { buffer.append(" INITIALLY DEFERRED"); } if (fkConstraint.isDeferrable()) { buffer.append(" DEFERRABLE"); } buffer.append(","); } for (UniqueConstraint uniqueConstraint : statement.getUniqueConstraints()) { if (uniqueConstraint.getConstraintName() != null && !constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(" UNIQUE ("); buffer.append(database.escapeColumnNameList(StringUtils.join(uniqueConstraint.getColumns(), ", "))); buffer.append(")"); if (uniqueConstraint.getConstraintName() != null && constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(","); } String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")"; if (statement.getTablespace() != null && database.supportsTablespaces()) { if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) { sql += " ON " + statement.getTablespace(); } else if (database instanceof DB2Database || database instanceof InformixDatabase) { sql += " IN " + statement.getTablespace(); } else { sql += " TABLESPACE " + statement.getTablespace(); } } return new Sql[] { new UnparsedSql(sql) }; }  }
CreateTableGenerator extends AbstractSqlGenerator<CreateTableStatement> { public Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { StringBuffer buffer = new StringBuffer(); buffer.append("CREATE TABLE ").append(database.escapeTableName(statement.getSchemaName(), statement.getTableName())).append(" "); buffer.append("("); boolean isSinglePrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() == 1; boolean isPrimaryKeyAutoIncrement = false; Iterator<String> columnIterator = statement.getColumns().iterator(); while (columnIterator.hasNext()) { String column = columnIterator.next(); buffer.append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), column)); buffer.append(" ").append(statement.getColumnTypes().get(column)); AutoIncrementConstraint autoIncrementConstraint = null; for (AutoIncrementConstraint currentAutoIncrementConstraint : statement.getAutoIncrementConstraints()) { if (column.equals(currentAutoIncrementConstraint.getColumnName())) { autoIncrementConstraint = currentAutoIncrementConstraint; break; } } boolean isAutoIncrementColumn = autoIncrementConstraint != null; boolean isPrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().contains(column); isPrimaryKeyAutoIncrement = isPrimaryKeyAutoIncrement || isPrimaryKeyColumn && isAutoIncrementColumn; if ((database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyColumn && isAutoIncrementColumn) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } buffer.append(" PRIMARY KEY AUTOINCREMENT"); } if (statement.getDefaultValue(column) != null) { Object defaultValue = statement.getDefaultValue(column); if (database instanceof MSSQLDatabase) { buffer.append(" CONSTRAINT ").append(((MSSQLDatabase) database).generateDefaultConstraintName(statement.getTableName(), column)); } buffer.append(" DEFAULT "); buffer.append(statement.getColumnTypes().get(column).convertObjectToString(defaultValue, database)); } if (isAutoIncrementColumn && ! (database instanceof SQLiteDatabase)) { if (database.supportsAutoIncrement()) { String autoIncrementClause = database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy()); if (!"".equals(autoIncrementClause)) { buffer.append(" ").append(autoIncrementClause); } } else { LogFactory.getLogger().warning(database.getTypeName()+" does not support autoincrement columns as request for "+(database.escapeTableName(statement.getSchemaName(), statement.getTableName()))); } } if (statement.getNotNullColumns().contains(column)) { buffer.append(" NOT NULL"); } else { if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase || database instanceof MySQLDatabase) { if (database instanceof MySQLDatabase && statement.getColumnTypes().get(column).getDataTypeName().equalsIgnoreCase("timestamp")) { } else { buffer.append(" NULL"); } } } if (database instanceof InformixDatabase && isSinglePrimaryKeyColumn) { buffer.append(" PRIMARY KEY"); } if (columnIterator.hasNext()) { buffer.append(", "); } } buffer.append(","); if (!( (database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyAutoIncrement) && !((database instanceof InformixDatabase) && isSinglePrimaryKeyColumn )) { if (statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() > 0) { if (!(database instanceof InformixDatabase)) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } } buffer.append(" PRIMARY KEY ("); buffer.append(database.escapeColumnNameList(StringUtils.join(statement.getPrimaryKeyConstraint().getColumns(), ", "))); buffer.append(")"); if (database instanceof OracleDatabase && statement.getPrimaryKeyConstraint().getTablespace() != null) { buffer.append(" USING INDEX TABLESPACE "); buffer.append(statement.getPrimaryKeyConstraint().getTablespace()); } buffer.append(","); } } for (ForeignKeyConstraint fkConstraint : statement.getForeignKeyConstraints()) { if (!(database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } String referencesString = fkConstraint.getReferences(); if (!referencesString.contains(".") && database.getDefaultSchemaName() != null) { referencesString = database.getDefaultSchemaName()+"."+referencesString; } buffer.append(" FOREIGN KEY (") .append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), fkConstraint.getColumn())) .append(") REFERENCES ") .append(referencesString); if (fkConstraint.isDeleteCascade()) { buffer.append(" ON DELETE CASCADE"); } if ((database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } if (fkConstraint.isInitiallyDeferred()) { buffer.append(" INITIALLY DEFERRED"); } if (fkConstraint.isDeferrable()) { buffer.append(" DEFERRABLE"); } buffer.append(","); } for (UniqueConstraint uniqueConstraint : statement.getUniqueConstraints()) { if (uniqueConstraint.getConstraintName() != null && !constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(" UNIQUE ("); buffer.append(database.escapeColumnNameList(StringUtils.join(uniqueConstraint.getColumns(), ", "))); buffer.append(")"); if (uniqueConstraint.getConstraintName() != null && constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(","); } String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")"; if (statement.getTablespace() != null && database.supportsTablespaces()) { if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) { sql += " ON " + statement.getTablespace(); } else if (database instanceof DB2Database || database instanceof InformixDatabase) { sql += " IN " + statement.getTablespace(); } else { sql += " TABLESPACE " + statement.getTablespace(); } } return new Sql[] { new UnparsedSql(sql) }; }  ValidationErrors validate(CreateTableStatement createTableStatement, Database database, SqlGeneratorChain sqlGeneratorChain); Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain); }
CreateTableGenerator extends AbstractSqlGenerator<CreateTableStatement> { public Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { StringBuffer buffer = new StringBuffer(); buffer.append("CREATE TABLE ").append(database.escapeTableName(statement.getSchemaName(), statement.getTableName())).append(" "); buffer.append("("); boolean isSinglePrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() == 1; boolean isPrimaryKeyAutoIncrement = false; Iterator<String> columnIterator = statement.getColumns().iterator(); while (columnIterator.hasNext()) { String column = columnIterator.next(); buffer.append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), column)); buffer.append(" ").append(statement.getColumnTypes().get(column)); AutoIncrementConstraint autoIncrementConstraint = null; for (AutoIncrementConstraint currentAutoIncrementConstraint : statement.getAutoIncrementConstraints()) { if (column.equals(currentAutoIncrementConstraint.getColumnName())) { autoIncrementConstraint = currentAutoIncrementConstraint; break; } } boolean isAutoIncrementColumn = autoIncrementConstraint != null; boolean isPrimaryKeyColumn = statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().contains(column); isPrimaryKeyAutoIncrement = isPrimaryKeyAutoIncrement || isPrimaryKeyColumn && isAutoIncrementColumn; if ((database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyColumn && isAutoIncrementColumn) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } buffer.append(" PRIMARY KEY AUTOINCREMENT"); } if (statement.getDefaultValue(column) != null) { Object defaultValue = statement.getDefaultValue(column); if (database instanceof MSSQLDatabase) { buffer.append(" CONSTRAINT ").append(((MSSQLDatabase) database).generateDefaultConstraintName(statement.getTableName(), column)); } buffer.append(" DEFAULT "); buffer.append(statement.getColumnTypes().get(column).convertObjectToString(defaultValue, database)); } if (isAutoIncrementColumn && ! (database instanceof SQLiteDatabase)) { if (database.supportsAutoIncrement()) { String autoIncrementClause = database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy()); if (!"".equals(autoIncrementClause)) { buffer.append(" ").append(autoIncrementClause); } } else { LogFactory.getLogger().warning(database.getTypeName()+" does not support autoincrement columns as request for "+(database.escapeTableName(statement.getSchemaName(), statement.getTableName()))); } } if (statement.getNotNullColumns().contains(column)) { buffer.append(" NOT NULL"); } else { if (database instanceof SybaseDatabase || database instanceof SybaseASADatabase || database instanceof MySQLDatabase) { if (database instanceof MySQLDatabase && statement.getColumnTypes().get(column).getDataTypeName().equalsIgnoreCase("timestamp")) { } else { buffer.append(" NULL"); } } } if (database instanceof InformixDatabase && isSinglePrimaryKeyColumn) { buffer.append(" PRIMARY KEY"); } if (columnIterator.hasNext()) { buffer.append(", "); } } buffer.append(","); if (!( (database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyAutoIncrement) && !((database instanceof InformixDatabase) && isSinglePrimaryKeyColumn )) { if (statement.getPrimaryKeyConstraint() != null && statement.getPrimaryKeyConstraint().getColumns().size() > 0) { if (!(database instanceof InformixDatabase)) { String pkName = StringUtils.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName()); if (pkName == null) { pkName = database.generatePrimaryKeyName(statement.getTableName()); } if (pkName != null) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(pkName)); } } buffer.append(" PRIMARY KEY ("); buffer.append(database.escapeColumnNameList(StringUtils.join(statement.getPrimaryKeyConstraint().getColumns(), ", "))); buffer.append(")"); if (database instanceof OracleDatabase && statement.getPrimaryKeyConstraint().getTablespace() != null) { buffer.append(" USING INDEX TABLESPACE "); buffer.append(statement.getPrimaryKeyConstraint().getTablespace()); } buffer.append(","); } } for (ForeignKeyConstraint fkConstraint : statement.getForeignKeyConstraints()) { if (!(database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } String referencesString = fkConstraint.getReferences(); if (!referencesString.contains(".") && database.getDefaultSchemaName() != null) { referencesString = database.getDefaultSchemaName()+"."+referencesString; } buffer.append(" FOREIGN KEY (") .append(database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), fkConstraint.getColumn())) .append(") REFERENCES ") .append(referencesString); if (fkConstraint.isDeleteCascade()) { buffer.append(" ON DELETE CASCADE"); } if ((database instanceof InformixDatabase)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName())); } if (fkConstraint.isInitiallyDeferred()) { buffer.append(" INITIALLY DEFERRED"); } if (fkConstraint.isDeferrable()) { buffer.append(" DEFERRABLE"); } buffer.append(","); } for (UniqueConstraint uniqueConstraint : statement.getUniqueConstraints()) { if (uniqueConstraint.getConstraintName() != null && !constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(" UNIQUE ("); buffer.append(database.escapeColumnNameList(StringUtils.join(uniqueConstraint.getColumns(), ", "))); buffer.append(")"); if (uniqueConstraint.getConstraintName() != null && constraintNameAfterUnique(database)) { buffer.append(" CONSTRAINT "); buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName())); } buffer.append(","); } String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")"; if (statement.getTablespace() != null && database.supportsTablespaces()) { if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) { sql += " ON " + statement.getTablespace(); } else if (database instanceof DB2Database || database instanceof InformixDatabase) { sql += " IN " + statement.getTablespace(); } else { sql += " TABLESPACE " + statement.getTablespace(); } } return new Sql[] { new UnparsedSql(sql) }; }  ValidationErrors validate(CreateTableStatement createTableStatement, Database database, SqlGeneratorChain sqlGeneratorChain); Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain);  }