NSArray expressions = super.primaryKeyConstraintStatementsForEntityGroup(entityGroup);
if( (expressions != null) && (expressions.count() > 0) ) primaryKeyConstraintExpressions.addObjectsFromArray(expressions);
for (Enumeration attributeEnumerator = primaryKeyAttributes.objectEnumerator(); attributeEnumerator.hasMoreElements(); ) {
String columnName = ((EOAttribute)attributeEnumerator.nextElement()).columnName();
columnNameDictionary.setObjectForKey(columnName, entity.externalName() + "." + columnName);
EOSQLExpression expression = this._expressionForString("create " + ( singlePrimaryKey ? "unique" : "" ) + " index " + entity.externalName() + " " + columnName);
if(expression != null) primaryKeyConstraintExpressions.addObject( expression );
}
}
}
for (Enumeration enumerator = entityGroup.objectEnumerator(); enumerator.hasMoreElements(); ) {
EOEntity entity = (EOEntity)enumerator.nextElement();
String tableName = entity.externalName();
if( (tableName != null) && ( ! "".equals(tableName) ) ) {
for (Enumeration relationshipEnumerator = entity.relationships().objectEnumerator(); relationshipEnumerator.hasMoreElements(); ) {
EORelationship relationship = (EORelationship)relationshipEnumerator.nextElement();
if( ! relationship.isFlattened() ) {
NSArray destinationAttributes = relationship.destinationAttributes();
// First exclude all the destination entity primary keys
for (Enumeration attributeEnumerator = relationship.destinationEntity().primaryKeyAttributes().objectEnumerator(); attributeEnumerator.hasMoreElements(); ) {
EOAttribute attribute = (EOAttribute)attributeEnumerator.nextElement();
columnNameDictionary.setObjectForKey(attribute.columnName(), relationship.destinationEntity().externalName() + "." + attribute.columnName());
}
// Then deal with our end of things
for (Enumeration attributeEnumerator = relationship.sourceAttributes().objectEnumerator(); attributeEnumerator.hasMoreElements(); ) {
EOAttribute attribute = (EOAttribute)attributeEnumerator.nextElement();
if( (! this.isSinglePrimaryKeyAttribute(attribute)) && (columnNameDictionary.objectForKey(tableName + "." + attribute.columnName()) != null) ) {
columnNameDictionary.setObjectForKey(attribute.columnName(), tableName + "." + attribute.columnName());
EOSQLExpression expression = this._expressionForString("create index " + tableName + " " + attribute.columnName());
if(expression != null) primaryKeyConstraintExpressions.addObject( expression );
}
}
// Then deal with the other side
if(entity.model() == relationship.destinationEntity().model()) {
for (Enumeration attributeEnumerator = relationship.destinationAttributes().objectEnumerator(); attributeEnumerator.hasMoreElements(); ) {
EOAttribute attribute = (EOAttribute)attributeEnumerator.nextElement();
String destinationTableName = relationship.destinationEntity().externalName();
if( (destinationTableName != null) && ( ! "".equals(destinationTableName) ) ) {
if( (! this.isSinglePrimaryKeyAttribute(attribute)) && (columnNameDictionary.objectForKey(destinationTableName + "." + attribute.columnName()) != null) ) {
columnNameDictionary.setObjectForKey(attribute.columnName(), destinationTableName + "." + attribute.columnName());
EOSQLExpression expression = this._expressionForString("create index " + destinationTableName + " " + attribute.columnName());
if(expression != null) primaryKeyConstraintExpressions.addObject( expression );
}
if( (! relationship.isCompound() ) && (relationship.sourceAttributes().count() == 1) && (relationship.destinationAttributes().count() == 1) ) {
String semantics;
switch(relationship.joinSemantic()) {
case EORelationship.FullOuterJoin: // '\001'
case EORelationship.LeftOuterJoin: // '\002'
case EORelationship.RightOuterJoin: // '\003'
semantics = "*";
break;
default:
semantics = "=";
break;
}
String sourceColumn = ((EOAttribute)relationship.sourceAttributes().objectAtIndex(0)).columnName();
String destinationColumn = ((EOAttribute)relationship.destinationAttributes().objectAtIndex(0)).columnName();
EOSQLExpression expression = this._expressionForString("delete from _SYS_RELATIONSHIP where relationshipName = '" + relationship.name() + "' and source_table = '" + tableName + "' ");
if(expression != null) primaryKeyConstraintExpressions.addObject( expression );
expression = this._expressionForString("insert into _SYS_RELATIONSHIP (relationshipName, source_table, source_column, dest_table, dest_column, operator, one_to_many) values ('" + relationship.name() + "','" + tableName + "','" + sourceColumn + "','" + destinationTableName + "','" + destinationColumn + "','" + semantics + "'," + (relationship.isToMany() ? 1 : 0) + ")");
if(expression != null) primaryKeyConstraintExpressions.addObject( expression );
}
}