public RelationLinkType TryAddField(SingleRelationFieldMapping relateMapping) { var last = items[items.Count - 1]; if (!Equals(relateMapping.MasterMapping, last.DataMapping)) { throw new LightDataException(string.Format(SR.RelationFieldError, relateMapping.MasterMapping.ObjectType, relateMapping.FieldName)); } var keys = relateMapping.GetKeyPairs(); if (keys.Length != keyCount) { return(RelationLinkType.NoMatch); } var masters = new string[keyCount]; var relates = new string[keyCount]; for (var i = 0; i < keyCount; i++) { masters[i] = keys[i].MasterKey; relates[i] = keys[i].RelateKey; } if (!IsMatch(last.Keys, masters)) { return(RelationLinkType.NoMatch); } PrevFieldPath = LastFieldPath; LastFieldPath = $"{last.CurrentFieldPath}.{relateMapping.FieldName}"; var len = items.Count - 1; for (var i = 0; i < len; i++) { var item = items[i]; if (Equals(relateMapping.RelateMapping, item.DataMapping)) { if (IsMatch(item.Keys, relates)) { CycleFieldPath = item.CurrentFieldPath; return(RelationLinkType.Cycle); } throw new LightDataException(string.Format(SR.RelationFieldKeyNotMatch, relateMapping.MasterMapping.ObjectType, relateMapping.FieldName)); } } var relateItem = new RelationItem { DataMapping = relateMapping.RelateMapping, FieldMapping = relateMapping, PrevFieldPath = PrevFieldPath, CurrentFieldPath = LastFieldPath, Keys = relates }; items.Add(relateItem); return(RelationLinkType.AddLink); }
public RelationLinkType TryAddField(SingleRelationFieldMapping relateMapping) { RelationItem last = items[items.Count - 1]; if (!Object.Equals(relateMapping.MasterMapping, last.DataMapping)) { throw new LightDataException(string.Format(SR.RelationFieldError, relateMapping.MasterMapping.ObjectType, relateMapping.FieldName)); } RelationKey[] keys = relateMapping.GetKeyPairs(); if (keys.Length != keyCount) { return(RelationLinkType.NoMatch); } string[] masters = new string[this.keyCount]; string[] relates = new string[this.keyCount]; for (int i = 0; i < this.keyCount; i++) { masters[i] = keys[i].MasterKey; relates[i] = keys[i].RelateKey; } if (!IsMatch(last.Keys, masters)) { return(RelationLinkType.NoMatch); } prevFieldPath = lastFieldPath; lastFieldPath = string.Format("{0}.{1}", last.CurrentFieldPath, relateMapping.FieldName); int len = items.Count - 1; for (int i = 0; i < len; i++) { RelationItem item = items[i]; if (Object.Equals(relateMapping.RelateMapping, item.DataMapping)) { if (IsMatch(item.Keys, relates)) { cycleFieldPath = item.CurrentFieldPath; return(RelationLinkType.Cycle); } else { throw new LightDataException(string.Format(SR.RelationFieldKeyNotMatch, relateMapping.MasterMapping.ObjectType, relateMapping.FieldName)); } } } RelationItem relateItem = new RelationItem() { DataMapping = relateMapping.RelateMapping, FieldMapping = relateMapping, PrevFieldPath = prevFieldPath, CurrentFieldPath = lastFieldPath, Keys = relates }; items.Add(relateItem); return(RelationLinkType.AddLink); }
internal SingleRelationFieldMapping[] GetSingleRelationFieldMappings() { var len = _singleRelationFields.Count; var array = new SingleRelationFieldMapping[len]; var index = 0; foreach (var item in _singleRelationFields) { array[index] = item; index++; } return(array); }
internal SingleRelationFieldMapping[] GetSingleRelationFieldMappings() { int len = this._singleRelationFields.Count; SingleRelationFieldMapping[] array = new SingleRelationFieldMapping[len]; int index = 0; foreach (SingleRelationFieldMapping item in this._singleRelationFields) { array[index] = item; index++; } return(array); }
public RelationLink(SingleRelationFieldMapping rootRelationMapping, string fieldPath) { if (rootRelationMapping == null) { throw new ArgumentNullException(nameof(rootRelationMapping)); } //this.rootRelationMapping = rootRelationMapping; var keys = rootRelationMapping.GetKeyPairs(); keyCount = keys.Length; var masters = new string[keyCount]; var relates = new string[keyCount]; for (var i = 0; i < keyCount; i++) { masters[i] = keys[i].MasterKey; relates[i] = keys[i].RelateKey; } LastFieldPath = $"{fieldPath}.{rootRelationMapping.FieldName}"; PrevFieldPath = fieldPath; var masterItem = new RelationItem { DataMapping = rootRelationMapping.MasterMapping, FieldMapping = null, //PrevFieldPath = prevFieldPath, CurrentFieldPath = PrevFieldPath, Keys = masters }; var relateItem = new RelationItem { DataMapping = rootRelationMapping.RelateMapping, FieldMapping = rootRelationMapping, PrevFieldPath = PrevFieldPath, CurrentFieldPath = LastFieldPath, Keys = relates }; items.Add(masterItem); items.Add(relateItem); }
private void InitialRelationField() { var properties = ObjectTypeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance); var collectionTmpList = new List <CollectionRelationFieldMapping>(); var singleTmpList = new List <SingleRelationFieldMapping>(); foreach (var pi in properties) { var config = MapperConfigManager.LoadRelationDataFieldConfig(ObjectType, pi); if (config != null && config.RelationKeyCount > 0) { var type = pi.PropertyType; var typeInfo = type.GetTypeInfo(); if (typeInfo.IsGenericType) { var frameType = type.GetGenericTypeDefinition(); if (frameType == LCollectionFrameType || frameType.FullName == "System.Collections.Generic.ICollection`1") { var arguments = typeInfo.GetGenericArguments(); type = arguments[0]; var handler = new PropertyHandler(pi); var keyPairs = config.GetRelationKeys(); var mapping = new CollectionRelationFieldMapping(pi.Name, this, type, keyPairs, handler); collectionTmpList.Add(mapping); } } else { var handler = new PropertyHandler(pi); var keyPairs = config.GetRelationKeys(); var mapping = new SingleRelationFieldMapping(pi.Name, this, type, keyPairs, handler); singleTmpList.Add(mapping); } } } _collectionRelationFields = new ReadOnlyCollection <CollectionRelationFieldMapping>(collectionTmpList); _singleRelationFields = new ReadOnlyCollection <SingleRelationFieldMapping>(singleTmpList); }
public RelationLink(SingleRelationFieldMapping rootRelationMapping, string fieldPath) { if (rootRelationMapping == null) { throw new ArgumentNullException(nameof(rootRelationMapping)); } //this.rootRelationMapping = rootRelationMapping; RelationKey[] keys = rootRelationMapping.GetKeyPairs(); this.keyCount = keys.Length; string[] masters = new string[this.keyCount]; string[] relates = new string[this.keyCount]; for (int i = 0; i < this.keyCount; i++) { masters[i] = keys[i].MasterKey; relates[i] = keys[i].RelateKey; } lastFieldPath = string.Format("{0}.{1}", fieldPath, rootRelationMapping.FieldName); prevFieldPath = fieldPath; RelationItem masterItem = new RelationItem() { DataMapping = rootRelationMapping.MasterMapping, FieldMapping = null, //PrevFieldPath = prevFieldPath, CurrentFieldPath = prevFieldPath, Keys = masters }; RelationItem relateItem = new RelationItem() { DataMapping = rootRelationMapping.RelateMapping, FieldMapping = rootRelationMapping, PrevFieldPath = prevFieldPath, CurrentFieldPath = lastFieldPath, Keys = relates }; items.Add(masterItem); items.Add(relateItem); }
void LoadJoinRelate() { LoadEntityMapping(this.rootMapping, null); JoinSelector joinSelector = new JoinSelector(); List <RelationItem> items = new List <RelationItem>(); Dictionary <string, RelationItem> relationItemDict = new Dictionary <string, RelationItem>(); int tindex = 0; foreach (RelationLink link in linkList) { RelationItem[] sitems = link.GetRelationItems(); foreach (RelationItem item in sitems) { if (!items.Contains(item)) { if (item.FieldMapping == null && tindex != 0) { continue; } item.AliasName = "T" + tindex; tindex++; items.Add(item); relationItemDict.Add(item.CurrentFieldPath, item); } } } //if (this.rootMapping != items [0].DataMapping) { // throw new LightDataException (RE.RelationMapEntityMappingError); //} RelationItem rootItem = items[0]; mapDict.Add(items[0].CurrentFieldPath, items[0]); List <DataFieldInfo> rootInfoList = new List <DataFieldInfo>(); foreach (DataFieldMapping field in this.rootMapping.DataEntityFields) { DataFieldInfo info = new DataFieldInfo(field); string aliasName = string.Format("{0}_{1}", rootItem.AliasName, info.FieldName); AliasDataFieldInfo alias = new AliasDataFieldInfo(info, aliasName, rootItem.AliasName); //alias.AliasTablesName = rootItem.AliasName; joinSelector.SetAliasDataField(alias); rootInfoList.Add(alias); fieldInfoDict.Add(string.Format("{0}.{1}", items[0].CurrentFieldPath, field.IndexName), alias); } tableInfoDict.Add(items[0].CurrentFieldPath, rootInfoList.ToArray()); for (int i = 1; i < items.Count; i++) { RelationItem mitem = items[i]; mapDict.Add(mitem.CurrentFieldPath, mitem); SingleRelationFieldMapping fieldMapping = mitem.FieldMapping; DataEntityMapping mapping = fieldMapping.RelateMapping; DataFieldExpression expression = null; //DataFieldInfoRelation [] relations = fieldMapping.GetDataFieldInfoRelations (); RelationItem ritem = mapDict[mitem.PrevFieldPath]; string malias = ritem.AliasName; string ralias = mitem.AliasName; DataFieldInfoRelation[] relations = fieldMapping.CreateDataFieldInfoRelations(malias, ralias); foreach (DataFieldInfoRelation relation in relations) { DataFieldInfo minfo = relation.MasterInfo; //minfo.AliasTableName = malias; DataFieldInfo rinfo = relation.RelateInfo; //rinfo.AliasTableName = ralias; DataFieldMatchExpression keyExpression = new DataFieldMatchExpression(minfo, rinfo, QueryPredicate.Eq); expression = DataFieldExpression.And(expression, keyExpression); //expression = DataFieldExpression.And (expression, minfo == rinfo); } List <DataFieldInfo> infoList = new List <DataFieldInfo>(); foreach (DataFieldMapping field in mapping.DataEntityFields) { DataFieldInfo info = new DataFieldInfo(field); string aliasName = string.Format("{0}_{1}", ralias, info.FieldName); AliasDataFieldInfo alias = new AliasDataFieldInfo(info, aliasName, ralias); //alias.AliasTableName = ralias; joinSelector.SetAliasDataField(alias); infoList.Add(alias); fieldInfoDict.Add(string.Format("{0}.{1}", mitem.CurrentFieldPath, field.Name), alias); } tableInfoDict.Add(mitem.CurrentFieldPath, infoList.ToArray()); JoinConnect connect = new JoinConnect(JoinType.LeftJoin, expression); EntityJoinModel model = new EntityJoinModel(mapping, ralias, connect, null, null, JoinSetting.None); this.selector = joinSelector; this.models.Add(model); } }