public NSArray statementsToDropPrimaryKeyConstraintsOnEntityGroups( NSArray entityGroups, EOSchemaSynchronizationModelChanges changes, EOSchemaGenerationOptions options) { if (entityGroups == null) return NSArray.EmptyArray; if (changes == null) changes = newChanges(); NSMutableArray expressions = new NSMutableArray(); for (Enumeration enumerator = entityGroups.objectEnumerator(); enumerator.hasMoreElements(); ) { NSArray entities = (NSArray) enumerator.nextElement(); EOEntity _last = (EOEntity) entities.lastObject(); // only need entity to get the table name for the group String nameInObjectStore = _nameInObjectStoreForEntityGroupWithChangeDictionary( entities, changes.changesForTableNamed(_last.externalName())); if ((nameInObjectStore != null) && (!"".equals(nameInObjectStore))) { expressions.addObject( this._expressionForString( "delete from _SYS_RELATIONSHIP where source_table = '" + nameInObjectStore + "' or dest_table = '" + nameInObjectStore + "'")); } } return expressions.immutableClone(); }
/* * [PJYF Oct 19 2004] * This is a bad hack to get WO to create indes for external keys. * We use the primary key constrain generation to create our indexes. * But we need to be carefull not to overwrite previous constrains * */ protected boolean isSinglePrimaryKeyAttribute(EOAttribute attribute) { if (attribute == null) return false; EOEntity entity = (EOEntity) attribute.entity(); if ((entity == null) || entity.isAbstractEntity() || (entity.externalName() == null)) return false; NSArray primaryKeyAttributes = entity.primaryKeyAttributes(); if (primaryKeyAttributes.count() != 1) return false; return attribute.name().equals(((EOAttribute) primaryKeyAttributes.lastObject()).name()); }
public String _alterPhraseDeletingColumnsWithNames( NSArray columnNames, NSArray entityGroup, EOSchemaGenerationOptions options) { StringBuffer phrase = new StringBuffer(); int j = columnNames.count(); for (int i = 0; i < j; i++) { phrase.append( "" + (i == 0 ? "" : this._alterPhraseJoinString()) + "remove column " + columnNames.objectAtIndex(i)); } return phrase.toString(); }
public NSArray statementsToImplementPrimaryKeyConstraintsOnEntityGroups( NSArray entityGroups, EOSchemaSynchronizationModelChanges changes, EOSchemaGenerationOptions options) { NSArray primaryKeyExpressions = this.primaryKeyConstraintStatementsForEntityGroups(entityGroups); NSMutableArray createStatements = new NSMutableArray(); NSMutableArray otherStatements = new NSMutableArray(); for (Enumeration enumerator = primaryKeyExpressions.objectEnumerator(); enumerator.hasMoreElements(); ) { EOSQLExpression expression = (EOSQLExpression) enumerator.nextElement(); String statement = expression.statement(); if (statement.startsWith("create")) { createStatements.addObject(expression); } else if (!statement.startsWith("delete from _SYS_RELATIONSHIP")) { otherStatements.addObject(expression); } } return createStatements.arrayByAddingObjectsFromArray(otherStatements); }
public NSArray primaryKeyConstraintStatementsForEntityGroup(NSArray entityGroup) { if (entityGroup == null) return NSArray.EmptyArray; NSMutableDictionary columnNameDictionary = new NSMutableDictionary(); NSMutableArray primaryKeyConstraintExpressions = new NSMutableArray(); for (Enumeration enumerator = entityGroup.objectEnumerator(); enumerator.hasMoreElements(); ) { EOEntity entity = (EOEntity) enumerator.nextElement(); String tableName = entity.externalName(); NSArray primaryKeyAttributes = entity.primaryKeyAttributes(); boolean singlePrimaryKey = primaryKeyAttributes.count() == 1; if ((tableName != null) && (!"".equals(tableName)) && (primaryKeyAttributes.count() > 0)) { 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); } } } } } } } } return primaryKeyConstraintExpressions.immutableClone(); }