public void Store() { var mappings = _handler.Mappings <RelationAttribute>(); foreach (var item in mappings) { _mapping[item.Member] = item.Instance; } var members = new List <MemberInfo>(_handler.Members <RelationAttribute>()); //传入到排列树 PermutationTree <MemberInfo> trees = new PermutationTree <MemberInfo>(members.ToArray()); //获取排列结果 List <List <MemberInfo> > results = trees.SumA(2, members.Count); //多泛型生成器实例 GsOperator gs = null; //关联关系排列类型 Type[] ts = null; //循环关联关系,每个元素是一种排列 for (int i = 0; i < results.Count; i += 1) { int gsCount = results[i].Count; //获取此种成员排列对应的类型排列 ts = GetTypes(results[i].ToArray()); switch (gsCount) { case 2: gs = new GsOperator(typeof(RelationSql <, ,>), ts); break; case 3: gs = new GsOperator(typeof(RelationSql <, , ,>), ts); break; case 4: gs = new GsOperator(typeof(RelationSql <, , , ,>), ts); break; case 5: gs = new GsOperator(typeof(RelationSql <, , , , ,>), ts); break; case 6: gs = new GsOperator(typeof(RelationSql <, , , , , ,>), ts); break; case 7: gs = new GsOperator(typeof(RelationSql <, , , , , , ,>), ts); break; default: break; } Func <MemberInfo, string> filter = (item) => { if (_mapping.ContainsKey(item)) { string result = _mapping[item].ColumnName; if (result != null) { return(result); } } return(item.Name); }; MemberInfo[] parameter = results[i].ToArray(); string[] sources = new string[parameter.Length]; string[] table = new string[parameter.Length]; for (int j = 0; j < parameter.Length; j += 1) { table[j] = parameter[j].Name; sources[j] = filter(parameter[j]); } MemberGetter[] getters = new MemberGetter[parameter.Length]; for (int j = 0; j < sources.Length; j += 1) { var instance = _mapping[parameter[j]]; getters[j] = MebOperator.Getter(instance.RelationType, instance.ColumnName); } gs["Table"] = _model.TableName; gs["Primary"] = _model.PrimaryKey; gs.Set("SourceConditions", sources); gs.Set("TableConditions", table); gs.Set("Getters", getters); //public static string CountFromTable gs.Set("CountFromTable", SelectCountByCondition(parameter)); //public static string GetFromTable; gs.Set("GetFromTable", SelectString(parameter)); //public static string ModifyFromTable; gs.Set("ModifyFromTable", UpdateString(parameter)); //public static string DeleteFromTable; gs.Set("DeletePreFromTable", DeletePreString(parameter[0])); gs.Set("DeleteAftFromTable", DeleteAftString(parameter)); //public static string AddFromTable; gs.Set("AddFromTable", InsertString(parameter)); //public static string CountFromSource gs.Set("CountFromSource", SelectCountByCondition(parameter, filter)); //public static string GetFromSource; gs.Set("GetFromSource", SelectString(parameter, filter)); //public static string ModifyFromSource; gs.Set("ModifyFromSource", UpdateString(parameter, filter)); //public static string DeleteFromSource; gs.Set("DeletePreFromSource", DeletePreString(parameter[0], filter)); gs.Set("DeleteAftFromSource", DeleteAftString(parameter, filter)); //public static string AddFromSource; gs.Set("AddFromSource", InsertString(parameter, filter)); } }
public BaseHandler(string spiltes, Type entity_type) { _model = new MakerModel(); if (spiltes == null) { _model.Left = default(char); _model.Right = default(char); } else { _model.Left = spiltes[0]; _model.Right = spiltes[1]; } if (entity_type == null) { return; } _model.PrimaryKey = null; _entity_type = entity_type; //创建标签Helper _handler = new AttrOperator(_entity_type); //表名解析 var table = _handler.ClassInstance <TableAttribute>(); _model.TableName = table.Name; //主键解析 var primary = _handler.Mapping <PrimaryKeyAttribute>(); if (primary.Instance != null) { _model.PrimaryKey = primary.Member.Name; _primary_manually = primary.Instance.IsManually; _primary_member = primary.Member; } // 忽略掉Ignore标签的成员 var ignores = _handler.Members <IgnoreAttribute>(); _model.LoadMembers(_handler._members); _model.AddIgnores(ignores); //列名映射解析 ConcurrentDictionary <MemberInfo, string> _column_mapping = new ConcurrentDictionary <MemberInfo, string>(); ConcurrentDictionary <string, string> _string_mapping = new ConcurrentDictionary <string, string>(); var mappings = _handler.Mappings <ColumnAttribute>(); foreach (var item in _handler._members) { _column_mapping[item] = item.Name; _string_mapping[item.Name] = item.Name; } foreach (var item in mappings) { _column_mapping[item.Member] = item.Instance.Name; _string_mapping[item.Member.Name] = item.Instance.Name; } _model.ColumnMapping = _column_mapping; _model.StringMapping = _string_mapping; //填充属性 //静态sql生成器。例如 MakerModel<Student> GsOperator gs = new GsOperator(typeof(MakerModel <>), _entity_type); gs.Set("PrimaryKey", _model.PrimaryKey); gs.Set("TableName", _model.TableName); gs.Set("Left", _model.Left); gs.Set("Right", _model.Right); gs.Set("Members", _model.Members); gs.Set("ColumnMapping", _column_mapping); gs.Set("StringMapping", _string_mapping); AttrOperator attr = new AttrOperator(_entity_type); var _sql_type = attr.ClassInstance <TableAttribute>().Type; gs.Set("OperatorType", _sql_type); }