public override void StartElement(XmlReader reader, IDictionary <String, String> attributes) { String localName = reader.Name; tempValue = new StringBuilder(); if (localName.Equals(Constants.ENTITY_DESCRIPTOR, StringComparison.OrdinalIgnoreCase)) { entityDescriptor = new EntityDescriptor(); } else if (localName.Equals(Constants.ENTITY_DESCRIPTOR_PROPERTY, StringComparison.OrdinalIgnoreCase)) { propertyName = attributes[Constants.ENTITY_DESCRIPTOR_NAME]; } else if (localName.Equals(Constants.ENTITY_DESCRIPTOR_ATTRIBUTE, StringComparison.OrdinalIgnoreCase)) { currentAttribute = new EntityDescriptor.Attribute(); isAttribute = true; } else if (localName.Equals(Constants.ENTITY_DESCRIPTOR_INDEX, StringComparison.OrdinalIgnoreCase)) { currentIndex = new EntityDescriptor.Index(); isIndex = true; } else if (localName.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP, StringComparison.OrdinalIgnoreCase)) { currectRelationship = new EntityDescriptor.Relationship(); isRelationship = true; } }
private ICollection <EntityDescriptor.Attribute> GetForeignKeys(EntityDescriptor entityDescriptor) { IEnumerator <EntityDescriptor.Relationship> oneToManyRealtionships = entityDescriptor.GetManyToOneRelationships(); IEnumerator <EntityDescriptor.Relationship> manyToManyRealtionships = entityDescriptor.GetManyToManyRelationships(); ICollection <EntityDescriptor.Attribute> foreignAttributes = new List <EntityDescriptor.Attribute>(); IEnumerator <EntityDescriptor.Attribute> attributes = entityDescriptor.GetAttributes(); while (attributes.MoveNext()) { EntityDescriptor.Attribute attribute = attributes.Current; if (attribute.IsPrimaryKey()) { foreignAttributes.Add(attribute); } } while (oneToManyRealtionships.MoveNext()) { EntityDescriptor.Relationship relationship = oneToManyRealtionships.Current; EntityDescriptor referedEntityDescriptor = relationship.GetReferedEntityDescriptor(); ICollection <EntityDescriptor.Attribute> referedForeignKeys = GetForeignKeys(referedEntityDescriptor); IEnumerator <EntityDescriptor.Attribute> referedForeignKeysIterate = referedForeignKeys.GetEnumerator(); while (referedForeignKeysIterate.MoveNext()) { foreignAttributes.Add(referedForeignKeysIterate.Current); } } while (manyToManyRealtionships.MoveNext()) { EntityDescriptor.Relationship relationship = manyToManyRealtionships.Current; EntityDescriptor referedEntityDescriptor = relationship.GetReferedEntityDescriptor(); ICollection <EntityDescriptor.Attribute> referedForeignKeys = GetForeignKeys(referedEntityDescriptor); IEnumerator <EntityDescriptor.Attribute> referedForeignKeysIterate = referedForeignKeys.GetEnumerator(); while (referedForeignKeysIterate.MoveNext()) { foreignAttributes.Add(referedForeignKeysIterate.Current); } } return(foreignAttributes); }
public override String FormForeignKeyQuery(IDictionary <String, Object> parameters) { EntityDescriptor child = (EntityDescriptor)parameters[IQueryBuilder.FORM_FOREIGN_KEYS_DATABASE_DESCRIPTOR_PARAMETER]; IEnumerator <EntityDescriptor.Relationship> oneToManyRealtionships = child.GetManyToOneRelationships(); IEnumerator <EntityDescriptor.Relationship> manyToManyRealtionships = child.GetManyToManyRelationships(); ICollection <EntityDescriptor.Relationship> relationships = new List <EntityDescriptor.Relationship>(); while (oneToManyRealtionships.MoveNext()) { EntityDescriptor.Relationship relationship = oneToManyRealtionships.Current; relationships.Add(relationship); } while (manyToManyRealtionships.MoveNext()) { EntityDescriptor.Relationship relationship = manyToManyRealtionships.Current; relationships.Add(relationship); } StringBuilder foreignKeysQuery = new StringBuilder(); IEnumerator <EntityDescriptor.Relationship> relationshipsIterator = relationships.GetEnumerator(); while (relationshipsIterator.MoveNext()) { StringBuilder foreignKeyQuery = new StringBuilder(); EntityDescriptor.Relationship relationship = relationshipsIterator.Current; EntityDescriptor referedEntityDescriptor = relationship.GetReferedEntityDescriptor(); if (referedEntityDescriptor == null) { referedEntityDescriptor = ResourceManager.GetInstance().RequiredEntityDescriptorBasedOnClassName(relationship.GetReferTo()); relationship.SetReferedEntityDescriptor(referedEntityDescriptor); relationship.SetReferedEntityDescriptor(referedEntityDescriptor); } String parentTable = referedEntityDescriptor.GetTableName(); ICollection <EntityDescriptor.Attribute> foreignAttributes = null; try { foreignAttributes = GetForeignKeys(referedEntityDescriptor); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(QueryBuilder).FullName, "formForeignKeys", "Database Exception caught while getting foreign columns, " + databaseException.GetMessage()); throw new DeploymentException(typeof(QueryBuilder).FullName, "formForeignKeys", "Database Exception caught while getting foreign columns, " + databaseException.GetMessage()); } IEnumerator <EntityDescriptor.Attribute> foreignAttributesIterate = foreignAttributes.GetEnumerator(); ICollection <String> foreignKeys = new List <String>(); while (foreignAttributesIterate.MoveNext()) { foreignKeys.Add(foreignAttributesIterate.Current.GetColumnName()); } IEnumerator <String> foreignKeysIterate = foreignKeys.GetEnumerator(); foreignKeyQuery.Append("FOREIGN KEY("); int index = 0; while (foreignKeysIterate.MoveNext()) { if (index == 0) { foreignKeyQuery.Append(foreignKeysIterate.Current); } else { foreignKeyQuery.Append(", " + foreignKeysIterate.Current); } index++; } foreignKeyQuery.Append(") REFERENCES " + parentTable + "("); foreignKeysIterate = foreignKeys.GetEnumerator(); index = 0; while (foreignKeysIterate.MoveNext()) { if (index == 0) { foreignKeyQuery.Append(foreignKeysIterate.Current); } else { foreignKeyQuery.Append(", " + foreignKeysIterate.Current); } index++; } foreignKeyQuery.Append(")"); String onDeleteAction = relationship.GetOnDelete(); String onUpdateAction = relationship.GetOnUpdate(); if (onDeleteAction != null && onDeleteAction.Length > 0) { if (onDeleteAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_CASCADE, StringComparison.OrdinalIgnoreCase)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_DELETE + " " + Constants.QUERY_BUILDER_CASCADE); } else if (onDeleteAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_RESTRICT)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_DELETE + " " + Constants.QUERY_BUILDER_RESTRICT); } else if (onDeleteAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_NO_ACTION)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_DELETE + " " + Constants.QUERY_BUILDER_NO_ACTION); } else if (onDeleteAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_SET_NULL)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_DELETE + " " + Constants.QUERY_BUILDER_SET_NULL); } else if (onDeleteAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_SET_DEFAULT)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_DELETE + " " + Constants.QUERY_BUILDER_SET_DEFAULT); } } if (onUpdateAction != null && onUpdateAction.Length > 0) { if (onUpdateAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_CASCADE)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_UPDATE + " " + Constants.QUERY_BUILDER_CASCADE); } else if (onUpdateAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_RESTRICT)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_UPDATE + " " + Constants.QUERY_BUILDER_RESTRICT); } else if (onUpdateAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_NO_ACTION)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_UPDATE + " " + Constants.QUERY_BUILDER_NO_ACTION); } else if (onUpdateAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_SET_NULL)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_UPDATE + " " + Constants.QUERY_BUILDER_SET_NULL); } else if (onUpdateAction.Equals(Constants.ENTITY_DESCRIPTOR_RELATIONSHIP_SET_DEFAULT)) { foreignKeyQuery.Append(" " + Constants.QUERY_BUILDER_ON_UPDATE + " " + Constants.QUERY_BUILDER_SET_DEFAULT); } } if (foreignKeyQuery.Length > 0) { if (foreignKeysQuery.Length <= 0) { foreignKeysQuery.Append(" " + foreignKeyQuery); } else { foreignKeysQuery.Append(", " + foreignKeyQuery); } } } return(foreignKeysQuery.ToString()); }