finally
{
SQLUtil.closeQuietly( rs );
}
SQLVendor vendor = this._vendor;
DefinitionFactory d = vendor.getDefinitionFactory();
TableReferenceFactory t = vendor.getTableReferenceFactory();
Statement stmt = connection.createStatement();
// @formatter:off
try
{
if( !schemaFound )
{
stmt.execute(
vendor.toString(
d
.createSchemaDefinitionBuilder()
.setSchemaName( schemaName )
.createExpression()
)
);
LOGGER.debug( "Database schema created" );
}
this.testRequiredCapabilities( connection );
LOGGER.debug( "Underlying database fullfill required capabilities" );
stmt.execute(
vendor.toString(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, USED_CLASSES_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( USED_CLASSES_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( Integer.class ), false ) )
.addTableElement( d.createColumnDefinition( USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( USED_CLASSES_TABLE_PK_COLUMN_NAME )
.createExpression()
) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.UNIQUE )
.addColumns( USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME )
.createExpression()
) )
.createExpression()
)
.createExpression()
)
);
tablePKs.put( USED_CLASSES_TABLE_NAME, 0L );
stmt.execute(
vendor.toString(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, ENTITY_TYPES_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( ENTITY_TYPES_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( ENTITY_TYPE_PK_TYPE ), false ) )
.addTableElement( d.createColumnDefinition( ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( ENTITY_TYPES_TABLE_PK_COLUMN_NAME )
.createExpression()
) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.UNIQUE )
.addColumns( ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME )
.createExpression()
) )
.createExpression()
)
.createExpression()
)
);
tablePKs.put( ENTITY_TYPES_TABLE_NAME, 0L );
stmt.execute(
vendor.toString(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, ENTITY_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( ENTITY_PK_TYPE ), false, AutoGenerationPolicy.BY_DEFAULT ) )
.addTableElement( d.createColumnDefinition( ENTITY_TABLE_IDENTITY_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createColumnDefinition( ENTITY_TABLE_MODIFIED_COLUMN_NAME, this._primitiveTypes
.get( Date.class ), false ) )
.addTableElement( d.createColumnDefinition( ENTITY_TABLE_VERSION_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createColumnDefinition( ENTITY_TABLE_APPLICATION_VERSION_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( ENTITY_TABLE_PK_COLUMN_NAME )
.createExpression()
) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.UNIQUE )
.addColumns( ENTITY_TABLE_IDENTITY_COLUMN_NAME )
.createExpression()
) )
.createExpression()
)
.createExpression()
)
);
tablePKs.put( ENTITY_TABLE_NAME, 0L );
stmt.execute(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, ENTITY_TYPES_JOIN_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( ENTITY_PK_TYPE ), false ) )
.addTableElement( d.createColumnDefinition( ENTITY_TYPES_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( ENTITY_TYPE_PK_TYPE ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( ENTITY_TABLE_PK_COLUMN_NAME, ENTITY_TYPES_TABLE_PK_COLUMN_NAME )
.createExpression()
) )
.addTableElement( d.createTableConstraintDefinition( d.createForeignKeyConstraintBuilder()
.addSourceColumns( ENTITY_TABLE_PK_COLUMN_NAME )
.setTargetTableName( t.tableName( schemaName, ENTITY_TABLE_NAME ) )
.addTargetColumns( ENTITY_TABLE_PK_COLUMN_NAME )
.setOnDelete( ReferentialAction.CASCADE )
.setOnUpdate( ReferentialAction.CASCADE )
.createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE
) )
.addTableElement( d.createTableConstraintDefinition( d.createForeignKeyConstraintBuilder()
.addSourceColumns( ENTITY_TYPES_TABLE_PK_COLUMN_NAME )
.setTargetTableName( t.tableName( schemaName, ENTITY_TYPES_TABLE_NAME ) )
.addTargetColumns( ENTITY_TYPES_TABLE_PK_COLUMN_NAME )
.setOnDelete( ReferentialAction.RESTRICT )
.setOnDelete( ReferentialAction.CASCADE )
.createExpression(), ConstraintCharacteristics.NOT_DEFERRABLE ) )
.createExpression()
).createExpression()
.toString()
);
stmt.execute(
vendor.toString(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, ENUM_LOOKUP_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( ENUM_LOOKUP_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( Integer.class ), false ) )
.addTableElement( d.createColumnDefinition( ENUM_LOOKUP_TABLE_ENUM_VALUE_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( ENUM_LOOKUP_TABLE_PK_COLUMN_NAME )
.createExpression()
) )
.createExpression()
)
.createExpression()
)
);
tablePKs.put( ENUM_LOOKUP_TABLE_NAME, 0L );
stmt.execute(
vendor.toString(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, USED_QNAMES_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( USED_QNAMES_TABLE_QNAME_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createColumnDefinition( USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( USED_QNAMES_TABLE_QNAME_COLUMN_NAME, USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME )
.createExpression()
) )
.createExpression()
)
.createExpression()
)
);
stmt.execute(
vendor.toString(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, ALL_QNAMES_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( ALL_QNAMES_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( Integer.class ), false ) )
.addTableElement( d.createColumnDefinition( ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes
.get( ENTITY_PK_TYPE ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME )
.createExpression()
) )
.addTableElement( d.createTableConstraintDefinition( d.createForeignKeyConstraintBuilder()
.addSourceColumns( ENTITY_TABLE_PK_COLUMN_NAME )
.setTargetTableName( t.tableName( schemaName, ENTITY_TABLE_NAME ) )
.addTargetColumns( ENTITY_TABLE_PK_COLUMN_NAME )
.setOnUpdate( ReferentialAction.CASCADE )
.setOnDelete( ReferentialAction.CASCADE )
.createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE
) )
.createExpression()
)
.createExpression()
)
);
tablePKs.put( ALL_QNAMES_TABLE_NAME, 0L );
stmt.execute(
vendor.toString(
d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, APP_VERSION_TABLE_NAME ) )
.setTableContentsSource(
d.createTableElementListBuilder()
.addTableElement( d.createColumnDefinition( APP_VERSION_PK_COLUMN_NAME, this._primitiveTypes
.get( String.class ), false ) )
.addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
.setUniqueness( UniqueSpecification.PRIMARY_KEY )
.addColumns( APP_VERSION_PK_COLUMN_NAME )
.createExpression()
) )
.createExpression()
)
.createExpression()
)
);
ModificationFactory m = vendor.getModificationFactory();
PreparedStatement ps = connection.prepareStatement(
vendor.toString(
m.insert()
.setTableName( t.tableName( schemaName, APP_VERSION_TABLE_NAME ) )
.setColumnSource(
m.columnSourceByValues()
.addValues( vendor.getLiteralFactory().param() )
.createExpression()
)
.createExpression()
)
);