Example #1
0
        public string BackupToQuery(string tablename, WebPath webPath)
        {
            MapperProvider defaultProvider = MapperProvider.DefaultProvider;
            string         str             = string.Concat("SELECT * FROM ", tablename);
            string         dataSetQuery    = SourceBuilder.GetDataSetQuery(defaultProvider.QueryForValueSet(str, null, CommandType.Text).Tables[0], tablename);

            File.WriteAllText(webPath.Path, dataSetQuery);
            return(string.Concat(new string[] { "<a href='", webPath.AbsolutePath, "'>", tablename, "</a>" }));
        }
Example #2
0
        public static MapperCommand GetCommand(MapperProvider provider, string commandId, params object[] source)
        {
            CommandItem item = CommandRepository.Instance[commandId];

            if (item == null)
            {
                throw new Exception(string.Concat(commandId, " not exist mapper command"));
            }
            return(MapperCommand.GetCommand(provider, item, source));
        }
Example #3
0
        public static MapperCommand GetTextCommand(MapperProvider provider, string text, params object[] source)
        {
            if (provider == null)
            {
                provider = MapperProvider.DefaultProvider;
            }
            List <ParameterSource> parameterSources = MapperCommand.ConvertParameterSource(provider, source);
            MapperCommand          mapperCommand    = new MapperCommand(provider)
            {
                parameterSource = parameterSources,
                Type            = Test.Core.CommandType.Text,
                Text            = text
            };

            mapperCommand.BuildCommand();
            return(mapperCommand);
        }
Example #4
0
        public static void LoadMapper <T>(ref T iMapper, MapperProvider provider = null)
            where T : IMapper
        {
            Type type = typeof(T);
            Type item = null;

            if (!BaseMapper.mapperTypes.TryGetValue(type, out item))
            {
                BaseMapper.LoadAssembliy(type.GetTypeInfo().Assembly);
                if (!BaseMapper.mapperTypes.ContainsKey(type))
                {
                    throw new Exception(string.Concat(type.FullName, " not exist mapper concrete"));
                }
                item = BaseMapper.mapperTypes[type];
            }
            iMapper          = (T)Activator.CreateInstance(item);
            iMapper.Provider = provider ?? MapperProvider.DefaultProvider;
        }
Example #5
0
        internal static IMapper CreateMapper(Type type, MapperProvider provider = null)
        {
            Type item = null;

            if (!BaseMapper.mapperTypes.TryGetValue(type, out item))
            {
                BaseMapper.LoadAssembliy(type.GetTypeInfo().Assembly);
                if (!BaseMapper.mapperTypes.ContainsKey(type))
                {
                    throw new Exception(string.Concat(type.FullName, " not exist mapper concrete"));
                }
                item = BaseMapper.mapperTypes[type];
            }
            IMapper mapper = (IMapper)Activator.CreateInstance(item);

            mapper.Provider = provider ?? MapperProvider.DefaultProvider;
            return(mapper);
        }
Example #6
0
        private static MapperCommand GetCommand(MapperProvider provider, CommandItem item, params object[] source)
        {
            if (provider == null)
            {
                provider = MapperProvider.DefaultProvider;
            }
            List <ParameterSource> parameterSources = MapperCommand.ConvertParameterSource(provider, source);
            MapperCommand          mapperCommand    = new MapperCommand(provider)
            {
                commandId       = item.Id,
                parameterSource = parameterSources,
                Type            = item.Type,
                Text            = item.GetParameterMatchText(parameterSources),
                Result          = item.ResultType,
                Timeout         = item.Timeout
            };

            mapperCommand.BuildCommand();
            return(mapperCommand);
        }
Example #7
0
        public static MapperProvider CreateProvider(string name)
        {
            MapperProvider connectionString = null;

            MapperProvider.MapperProviderList.TryGetValue(name, out connectionString);
            if (connectionString == null)
            {
                Test.Core.MapperProviderInfo mapperProviderInfo = null;
                MapperProvider.MapperProviderInfo.TryGetValue(name, out mapperProviderInfo);
                if (mapperProviderInfo == null)
                {
                    throw new Exception(string.Concat(name, " not exist MapperProviderInfo"));
                }
                connectionString                  = (MapperProvider)Activator.CreateInstance(MapperProvider.MapperProviderType[mapperProviderInfo.ProviderType]);
                connectionString.Name             = name;
                connectionString.connectionString = mapperProviderInfo.ConnectionString;
                connectionString.catalog          = mapperProviderInfo.Catalog;
                connectionString.Parameter        = mapperProviderInfo.Parameter;
                MapperProvider.MapperProviderList.Add(name, connectionString);
            }
            return(connectionString);
        }
Example #8
0
 public static T CreateMapper <T>(string provider)
     where T : IMapper
 {
     return(BaseMapper.CreateMapper <T>(MapperProvider.CreateProvider(provider)));
 }
Example #9
0
 public static ValueTable QueryForValueTable(MapperProvider provider, string commandId, params object[] param)
 {
     return(MapperCommand.GetCommand(provider, commandId, param).QueryForValueTable());
 }
Example #10
0
        public static void CreateMapperMember(object model)
        {
            Type type = model.GetType();
            List <BaseMapperService.MapperMemberInfo> mapperMemberInfos = null;

            if (!BaseMapperService.mapperMemberInfo.TryGetValue(type, out mapperMemberInfos))
            {
                lock (BaseMapperService._lock)
                {
                    if (!BaseMapperService.mapperMemberInfo.TryGetValue(type, out mapperMemberInfos))
                    {
                        mapperMemberInfos = new List <BaseMapperService.MapperMemberInfo>();
                        FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                        for (int i = 0; i < (int)fields.Length; i++)
                        {
                            FieldInfo fieldInfo = fields[i];
                            Type      fieldType = fieldInfo.FieldType;
                            TypeInfo  typeInfo  = fieldType.GetTypeInfo();
                            if (typeInfo.ImplementedInterfaces.Contains <Type>(typeof(IMapper)) && !typeInfo.IsSubclassOf(typeof(BaseMapper)) && fieldInfo.GetValue(model) == null)
                            {
                                List <ProviderParameterAttribute> list                        = fieldType.GetCustomAttributes <ProviderParameterAttribute>().ToList <ProviderParameterAttribute>();
                                MapperProviderAttribute           customAttribute             = fieldInfo.GetCustomAttribute <MapperProviderAttribute>();
                                List <ProviderParameterAttribute> providerParameterAttributes = fieldInfo.GetCustomAttributes <ProviderParameterAttribute>().ToList <ProviderParameterAttribute>();
                                foreach (ProviderParameterAttribute providerParameterAttribute in list)
                                {
                                    if (providerParameterAttributes.Exists((ProviderParameterAttribute p) => p.Name == providerParameterAttribute.Name))
                                    {
                                        continue;
                                    }
                                    providerParameterAttributes.Add(providerParameterAttribute);
                                }
                                if (customAttribute != null)
                                {
                                    MapperProvider mapperProvider = MapperProvider.CreateProvider(customAttribute.Name);
                                    mapperMemberInfos.Add(new BaseMapperService.MapperMemberInfo()
                                    {
                                        Field     = fieldInfo,
                                        Type      = fieldType,
                                        Parameter = list,
                                        Provider  = mapperProvider
                                    });
                                }
                                else
                                {
                                    mapperMemberInfos.Add(new BaseMapperService.MapperMemberInfo()
                                    {
                                        Field     = fieldInfo,
                                        Type      = fieldType,
                                        Parameter = list
                                    });
                                }
                            }
                        }
                        BaseMapperService.mapperMemberInfo.Add(type, mapperMemberInfos);
                    }
                }
            }
            foreach (BaseMapperService.MapperMemberInfo mapperMemberInfo in mapperMemberInfos)
            {
                IMapper mapper = BaseMapper.CreateMapper(mapperMemberInfo.Type, mapperMemberInfo.Provider);
                mapperMemberInfo.Field.SetValue(model, mapper);
            }
        }
Example #11
0
        public string BuildCommand(string tablename)
        {
            MapperProvider defaultProvider = MapperProvider.DefaultProvider;
            string         pascalCase      = tablename.Substring(3);

            pascalCase = SourceBuilder.ToPascalCase(pascalCase);
            string   str      = string.Concat("SELECT * FROM ", tablename);
            ValueSet valueSet = defaultProvider.QueryForValueSet(str, null, CommandType.Text);

            str = string.Concat("select object_id from sys.all_objects where type_desc='USER_TABLE' and name='", tablename, "'");
            string str1 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();

            str = string.Concat("select * from sys.index_columns where object_id='", str1, "'");
            ValueTable    item = defaultProvider.QueryForValueSet(str, null, CommandType.Text).Tables[0];
            List <string> strs = new List <string>();

            foreach (ValueRow row in item.Rows)
            {
                str = string.Concat(new object[] { "select name from sys.columns where object_id='", str1, "' AND column_id='", row["column_id"], "'" });
                string str2 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
                strs.Add(str2);
            }
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<mapper>\r\n\t<commands>\r\n");
            stringBuilder.AppendFormat("\t\t<post id=\"{0}.Create\">\r\n\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("INSERT INTO {0}", tablename);
            stringBuilder.Append("(");
            int num = 0;

            foreach (ValueColumn column in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}", (num > 0 ? ", " : " "), column.ColumnName);
                num++;
            }
            stringBuilder.AppendFormat(" )\r\n\t\t\tVALUES(", Array.Empty <object>());
            num = 0;
            foreach (ValueColumn valueColumn in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}#{{{1}}}", (num > 0 ? ", " : " "), SourceBuilder.ToPascalCase(valueColumn.ColumnName));
                num++;
            }
            stringBuilder.AppendFormat(" )", Array.Empty <object>());
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</post>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<get id=\"{0}.Get\">\r\n\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("SELECT ", Array.Empty <object>());
            num = 0;
            foreach (ValueColumn column1 in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}", (num > 0 ? ", " : string.Empty), column1.ColumnName);
                num++;
            }
            stringBuilder.AppendFormat(" FROM {0} \r\n\t\t\tWHERE ", tablename);
            num = 0;
            foreach (string str3 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str3, SourceBuilder.ToPascalCase(str3));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</get>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<get id=\"{0}.GetList\">\r\n\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("SELECT ", Array.Empty <object>());
            num = 0;
            foreach (ValueColumn valueColumn1 in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}", (num > 0 ? ", " : string.Empty), valueColumn1.ColumnName);
                num++;
            }
            stringBuilder.AppendFormat(" FROM {0} \r\n\t\t\tWHERE ", tablename);
            num = 0;
            foreach (string str4 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str4, SourceBuilder.ToPascalCase(str4));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</get>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<patch id=\"{0}.Update\">\r\n\t\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("UPDATE {0} SET \r\n\t\t\t", tablename);
            num = 0;
            foreach (ValueColumn column2 in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? ", " : string.Empty), column2.ColumnName, SourceBuilder.ToPascalCase(column2.ColumnName));
                stringBuilder.AppendFormat("\r\n\t\t\t", Array.Empty <object>());
                num++;
            }
            stringBuilder.AppendFormat("WHERE ", Array.Empty <object>());
            num = 0;
            foreach (string str5 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str5, SourceBuilder.ToPascalCase(str5));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</patch>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<delete id=\"{0}.Delete\">\r\n\t\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("DELETE FROM {0} WHERE ", tablename);
            num = 0;
            foreach (string str6 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str6, SourceBuilder.ToPascalCase(str6));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</delete>\r\n\r\n");
            stringBuilder.Append("\t</commands>\r\n\r\n");
            stringBuilder.Append("\t<matches>\r\n");
            stringBuilder.Append("\t</matches>\r\n");
            stringBuilder.Append("</mapper>");
            return(stringBuilder.ToString());
        }
Example #12
0
 public MapperCommand(MapperProvider provider, string text)
 {
     this.provider = provider;
     this.Text     = text;
 }
Example #13
0
        public string BuildEntity(string tablename)
        {
            MapperProvider defaultProvider = MapperProvider.DefaultProvider;
            string         str             = string.Concat("SELECT * FROM ", tablename);
            ValueSet       valueSet        = defaultProvider.QueryForValueSet(str, null, CommandType.Text);
            StringBuilder  stringBuilder   = new StringBuilder();

            stringBuilder.AppendLine("using Test.Core;");
            stringBuilder.AppendLine("using System;");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine(string.Concat("namespace ", this._namespace));
            stringBuilder.AppendLine("{");
            str = string.Concat("select object_id from sys.all_objects where type_desc='USER_TABLE' and name='", tablename, "'");
            string        str1 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
            List <string> strs = new List <string>();

            str = string.Concat("select * from sys.columns where object_id='", str1, "'");
            ValueTable item = defaultProvider.QueryForValueSet(str, null, CommandType.Text).Tables[0];

            str = string.Concat("select * from sys.index_columns where object_id='", str1, "'");
            ValueTable    valueTable = defaultProvider.QueryForValueSet(str, null, CommandType.Text).Tables[0];
            List <string> strs1      = new List <string>();

            foreach (ValueRow row in valueTable.Rows)
            {
                str = string.Concat(new object[] { "select name from sys.columns where object_id='", str1, "' AND column_id='", row["column_id"], "'" });
                string str2 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
                strs1.Add(str2);
            }
            stringBuilder.AppendLine(string.Format("\tpublic class {0}Entity : BaseEntity", this.GetCName(tablename, "TB_")));
            stringBuilder.AppendLine("\t{");
            foreach (ValueColumn column in valueSet.Tables[0].Columns)
            {
                ValueRow valueRow = item.Rows.FirstOrDefault <ValueRow>((ValueRow p) => p["name"].ToString() == column.ColumnName);
                string   str3     = "string";
                string   str4     = column.DataType.ToString();
                if (str4 == "System.Int32")
                {
                    str3 = "int";
                }
                else if (str4 == "System.Int64")
                {
                    str3 = "long";
                }
                else if (str4 == "System.Double")
                {
                    str3 = "double";
                }
                else if (str4 == "System.Single")
                {
                    str3 = "float";
                }
                else if (str4 == "System.DateTime")
                {
                    str3 = "DateTime";
                }
                else
                {
                    str3 = (str4 == "System.Boolean" ? "bool" : "string");
                }
                stringBuilder.AppendFormat(string.Concat("\t\t[Column(\"", column.ColumnName, "\", "), Array.Empty <object>());
                str = string.Concat("select name from sys.types where system_type_id='", valueRow["system_type_id"], "'");
                string       name    = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
                MemberInfo[] members = typeof(SqlDataType).GetMembers();
                for (int i = 0; i < (int)members.Length; i++)
                {
                    MemberInfo memberInfo = members[i];
                    if (memberInfo.Name.ToLower() == name)
                    {
                        name = memberInfo.Name;
                    }
                }
                stringBuilder.AppendFormat(", SqlDataType.{0}", name);
                if ((!(bool)valueRow["is_nullable"] || column.Unique || strs1.Contains(column.ColumnName)) && strs1.Contains(column.ColumnName))
                {
                    stringBuilder.AppendFormat("SqlColumnType.PrimaryKey", Array.Empty <object>());
                }
                stringBuilder.AppendLine(")]");
                stringBuilder.AppendLine(string.Format("\t\tpublic {0} {1} {{ get; set; }}", str3, this.GetCName(column.ColumnName, "")));
                stringBuilder.AppendLine();
            }
            stringBuilder.AppendLine("\t}");
            stringBuilder.AppendLine("}");
            return(stringBuilder.ToString());
        }
Example #14
0
 public static void Initialize(IEnumerable <Assembly> assemblies, string path)
 {
     CommandRepository.CreateRepository(path);
     MapperProvider.SearchMapperProvider(assemblies.Concat <Assembly>((IEnumerable <Assembly>)(new Assembly[] { typeof(MapperProvider).GetTypeInfo().Assembly })));
     BaseMapper.Initialize(assemblies);
 }
Example #15
0
 public MapperCommand(MapperProvider provider)
 {
     this.provider = provider;
 }
Example #16
0
 public BaseMapper(MapperProvider provider)
 {
     this.Provider = provider;
 }
Example #17
0
        private static List <ParameterSource> ConvertParameterSource(MapperProvider provider, object[] source)
        {
            if (source == null)
            {
                source = new object[0];
            }
            for (int i = 0; i < (int)source.Length; i++)
            {
                if (source[i] == null)
                {
                    source[i] = new ParameterSource();
                }
            }
            List <ParameterSource> parameterSources = new List <ParameterSource>((int)source.Length);

            parameterSources.AddRange(provider.Parameter);
            object[] objArray = source;
            for (int j = 0; j < (int)objArray.Length; j++)
            {
                object obj = objArray[j];
                if (!(obj is ValueObject))
                {
                    if (!(obj is Dictionary <string, object>))
                    {
                        goto Label1;
                    }
                    Dictionary <string, object> strs = obj as Dictionary <string, object>;
                    Dictionary <string, object> .KeyCollection.Enumerator enumerator = strs.Keys.GetEnumerator();
                    try
                    {
                        while (enumerator.MoveNext())
                        {
                            string current = enumerator.Current;
                            parameterSources.Add(new ParameterSource()
                            {
                                Name  = current,
                                Value = strs[current]
                            });
                        }
                        goto Label0;
                    }
                    finally
                    {
                        ((IDisposable)enumerator).Dispose();
                    }
                }
                else
                {
                    System.Type type = obj.GetType();
                    parameterSources.Add(new ParameterSource()
                    {
                        Name  = type.Name,
                        Value = type
                    });
                }
Label1:
                ParamterTypeAttribute customAttribute = obj.GetType().GetTypeInfo().GetCustomAttribute <ParamterTypeAttribute>();
                PropertyInfo[] properties = ((customAttribute == null ? obj.GetType() : customAttribute.ParamterType)).GetProperties();
                for (int k = 0; k < (int)properties.Length; k++)
                {
                    PropertyInfo propertyInfo = properties[k];
                    if (propertyInfo.GetCustomAttribute <IgnoreAttribute>() == null)
                    {
                        parameterSources.Add(new ParameterSource()
                        {
                            Name   = propertyInfo.Name,
                            Column = propertyInfo.GetCustomAttribute <ColumnAttribute>(),
                            Value  = propertyInfo.GetValue(obj)
                        });
                    }
                }
                Label0 :;
            }
            return(parameterSources);
        }
Example #18
0
 public static T QueryForScalar <T>(MapperProvider provider, string commandId, params object[] param)
 {
     return((T)MapperCommand.GetCommand(provider, commandId, param).QueryForScalar());
 }
Example #19
0
 public static int Query(MapperProvider provider, string commandId, params object[] param)
 {
     return(MapperCommand.GetCommand(provider, commandId, param).Query());
 }
Example #20
0
        public static MapperCommand GetCommand(string commandId, params object[] source)
        {
            CommandItem item = CommandRepository.Instance[commandId];

            if (item == null)
            {
                throw new Exception(string.Concat(commandId, " not exist mapper command"));
            }
            return(MapperCommand.GetCommand((string.IsNullOrEmpty(item.Provider) ? MapperProvider.DefaultProvider : MapperProvider.CreateProvider(item.Provider)), item, source));
        }
Example #21
0
 public BaseMapper()
 {
     this.Provider = MapperProvider.DefaultProvider;
 }
Example #22
0
 public MapperCommand()
 {
     this.provider = MapperProvider.DefaultProvider;
 }
Example #23
0
 public static void Run(IEnumerable <Assembly> assemblies, string mapperPath, long startSequence = -1L)
 {
     BaseCodeInitializer.Initialize(assemblies, startSequence);
     MapperProvider.Initialize(assemblies, mapperPath);
     ValueObject.Initialize(assemblies);
 }
Example #24
0
 public MapperCommand(string text)
 {
     this.provider = MapperProvider.DefaultProvider;
     this.Text     = text;
 }