for( QNameInfo qNameInfo : this._state.qNameInfos().get().values() )
{
QNameType type = qNameInfo.getQNameType();
TableElementListBuilder builder = d.createTableElementListBuilder();
builder
.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 ) );
if( type.equals( QNameType.PROPERTY ) )
{
builder.addTableElement( d.createColumnDefinition( QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, this._primitiveTypes
.get( Integer.class ), true ) );
if( qNameInfo.getCollectionDepth() > 0 )
{
builder.addTableElement( d.createColumnDefinition( QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME, this
.getCollectionPathDataType(), false ) );
}
this.appendColumnDefinitionsForProperty( builder, qNameInfo );
builder.addTableElement( d.createTableConstraintDefinition( d.createForeignKeyConstraintBuilder()
.addSourceColumns( QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME )
.setTargetTableName( t.tableName( schemaName, ALL_QNAMES_TABLE_NAME ) )
.addTargetColumns( ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME )
.setOnUpdate( ReferentialAction.CASCADE )
.setOnDelete( ReferentialAction.CASCADE )
.createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE
) );
}
else
{
if( type.equals( QNameType.ASSOCIATION ) )
{
builder
.addTableElement( d.createColumnDefinition( QNAME_TABLE_VALUE_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()
) );
}
else if( type.equals( QNameType.MANY_ASSOCIATION ) )
{
builder
.addTableElement( d.createColumnDefinition( QNAME_TABLE_ASSOCIATION_INDEX_COLUMN_NAME, this._primitiveTypes
.get( Integer.class ), false ) )
.addTableElement( d.createColumnDefinition( QNAME_TABLE_VALUE_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()
) );
}
else
{
throw new IllegalArgumentException( "Did not how to create table for qName type: " + type + "." );
}
builder
.addTableElement( d.createTableConstraintDefinition( d.createForeignKeyConstraintBuilder()
.addSourceColumns( QNAME_TABLE_VALUE_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
)
);
tablePKs.put( qNameInfo.getTableName(), 0L );
}
builder
.addTableElement(
d.createTableConstraintDefinition( d.createForeignKeyConstraintBuilder()
.addSourceColumns( ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME )
.setTargetTableName( t.tableName( schemaName, ALL_QNAMES_TABLE_NAME ) )
.addTargetColumns( ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME )
.setOnUpdate( ReferentialAction.CASCADE )
.setOnDelete( ReferentialAction.CASCADE )
.createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE
)
);
stmt.execute( this._vendor.toString( d.createTableDefinitionBuilder()
.setTableName( t.tableName( schemaName, qNameInfo.getTableName() ) )
.setTableContentsSource( builder.createExpression() )
.createExpression()
) );
// stmt.execute( "COMMENT ON TABLE " + schemaName + "." + qNameInfo.getTableName() + " IS '"
// + qNameInfo.getQName() + "'" );