public override string GenerateCreateRelationshipSql(RelationshipMapping rm) { //TODO: check rm? StringBuilder sqlCreateRelationship = new StringBuilder(); TypeMapping childTypeMapping = this.GetTypeMapping(rm.ChildType, true); TypeMapping parentTypeMapping = this.GetTypeMapping(rm.ParentType, true); sqlCreateRelationship.AppendFormat("ALTER TABLE [{0}] \r\n",childTypeMapping.TableName); sqlCreateRelationship.AppendFormat("ADD CONSTRAINT {0} \r\n", rm.Name); sqlCreateRelationship.AppendFormat("FOREIGN KEY ([{0}]) REFERENCES [{1}]([{2}]) ON DELETE NO ACTION", childTypeMapping.MemberMappings.GetByName(rm.ChildMember).ColumnName, parentTypeMapping.TableName, parentTypeMapping.MemberMappings.GetByName(rm.ParentMember).ColumnName); return sqlCreateRelationship.ToString(); }
public void Add(RelationshipMapping value) { base.Add(value.Name,value); }
private bool IsParent(RelationshipMapping rm) { return rm.ParentType.IsInstanceOfType(CacheSource) && !rm.ChildType.IsInstanceOfType(CacheSource); }
private void AddDefaultRelationshipMappings(Type type) { RelationshipMappingAttribute[] relationshipMappingAttributes = (RelationshipMappingAttribute[])type.GetCustomAttributes(typeof(RelationshipMappingAttribute),false); foreach(RelationshipMappingAttribute rma in relationshipMappingAttributes){ RelationshipMapping rm = new RelationshipMapping(); rm.Name = rma.Name; if(rma.ParentType != null){ rm.ParentType = rma.ParentType; rm.ChildType = type; } else { rm.ParentType = type; rm.ChildType = rma.ChildType; } rm.ParentMember = rma.ParentMember; rm.ChildMember = rma.ChildMember; rm.Type = rma.Type; //TODO: rename RelationshipType relationshipMappings.Add(rm); } }
private void AddDefaultCacheMappings(MemberInfo memberInfo, TypeMapping typeMapping) { RelationshipMappingAttribute rma = (RelationshipMappingAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(RelationshipMappingAttribute),false); if(rma != null){ RelationshipMapping rm = new RelationshipMapping(); rm.Name = rma.Name; if(rma.ParentType != null){ rm.ParentType = rma.ParentType; rm.ChildType = typeMapping.MappedType; } else { rm.ParentType = typeMapping.MappedType; rm.ChildType = rma.ChildType; } rm.ParentMember = rma.ParentMember; rm.ChildMember = rma.ChildMember; rm.Type = rma.Type; relationshipMappings.Add(rm); } CacheMappingAttribute ca = (CacheMappingAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(CacheMappingAttribute), false); if(ca != null){ CacheMapping cm = new CacheMapping(); cm.Relationship = ca.Relationship == null ? rma.Name : ca.Relationship; cm.MemberInfo = memberInfo; cm.DataStore = this.dataStore; cm.OrderBy = ca.OrderBy; typeMapping.CacheMappings.Add(cm); } else if(rma != null){ CacheMapping cm = new CacheMapping(); cm.Relationship = rma.Name; cm.MemberInfo = memberInfo; cm.DataStore = this.dataStore; typeMapping.CacheMappings.Add(cm); } }
public abstract string GenerateCreateRelationshipSql(RelationshipMapping rm);
public void CreateStorage(TypeMapping typeMapping) { if(typeMapping == null){ throw new ArgumentNullException("typeMapping"); } IDataAccessCommand createCommand; RelationshipMapping baseMapping = null; if(typeMapping.BaseType != null){ TypeMapping baseTypeMapping = this.GetTypeMapping(typeMapping.BaseType); TypeMappingState state = CheckMapping(baseTypeMapping); if (state.Condition == TypeMappingState.TypeMappingStateCondition.TableMissing){ CreateStorage(baseTypeMapping); } else if (state.Condition != TypeMappingState.TypeMappingStateCondition.None) { throw new TypeMappingException("TypeMappingState: " + state.Condition.ToString()); } baseMapping = new RelationshipMapping(); baseMapping.Name = string.Format("{0}_{1}",typeMapping.TableName,baseTypeMapping.TableName); baseMapping.ChildType = typeMapping.MappedType; baseMapping.ParentType = baseTypeMapping.MappedType; baseMapping.ChildMember = typeMapping.PrimaryKey.MemberInfo.Name; baseMapping.ParentMember = baseTypeMapping.PrimaryKey.MemberInfo.Name; baseMapping.Type = Relationship.OneToOne; } createCommand = ManagedDataStore.CreateDataAccessCommand( GenerateCreateTableSql(typeMapping), CommandType.Text); createCommand.ExecuteNonQuery(); if(baseMapping != null){ createCommand = ManagedDataStore.CreateDataAccessCommand( GenerateCreateRelationshipSql(baseMapping), CommandType.Text); createCommand.ExecuteNonQuery(); } if(typeMapping.UseStoredProcedures){ CreateInsertProcedure(typeMapping); CreateUpdateProcedure(typeMapping); CreateDeleteProcedure(typeMapping); } foreach(RelationshipMapping rm in RelationshipMappings){ if(rm.ChildType == typeMapping.MappedType){ createCommand = ManagedDataStore.CreateDataAccessCommand( GenerateCreateRelationshipSql(rm), CommandType.Text); createCommand.ExecuteNonQuery(); } } }