コード例 #1
0
        private static void ReadOracle(Object item, DbDataReader dr, int id, PropertyModel info)
        {
            object value    = null;
            var    typeName = dr.GetDataTypeName(id);

            if (string.Compare(typeName, "clob", true) == 0 || string.Compare(typeName, "nclob", true) == 0)
            {
                var temp = BaseEmit.Invoke(dr, dr.GetType().GetMethod("GetOracleClob"), new object[] { id });
                if (temp != null)
                {
                    value = BaseEmit.Get(temp, "Value");
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Close"), null);
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Dispose"), null);
                }
            }
            else if (string.Compare(typeName, "blob", true) == 0)
            {
                var temp = BaseEmit.Invoke(dr, dr.GetType().GetMethod("GetOracleBlob"), new object[] { id });
                if (temp != null)
                {
                    value = BaseEmit.Get(temp, "Value");
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Close"), null);
                    BaseEmit.Invoke(temp, temp.GetType().GetMethod("Dispose"), null);
                }
            }
            else
            {
                value = dr.GetValue(id);
            }

            if (!dr.IsDBNull(id))
            {
                BaseEmit.Set(item, info.Name, value);
            }
        }
コード例 #2
0
ファイル: BaseCodeDom.cs プロジェクト: weizhonzhen/FastData
        public static bool GetResult(string code, string references = null)
        {
            //动态编译
            var compiler = new CSharpCodeProvider().CreateCompiler();
            var param    = new CompilerParameters();

            param.ReferencedAssemblies.Add("System.dll");
            param.ReferencedAssemblies.Add("System.Core.dll");
            param.ReferencedAssemblies.Add("mscorlib.dll");

            var assembly = AppDomain.CurrentDomain.GetAssemblies().ToList().Find(a => a.FullName.Split(',')[0] == references);

            if (assembly == null)
            {
                assembly = Assembly.Load(references);
            }

            param.ReferencedAssemblies.Add(assembly.Location);

            param.GenerateExecutable = false;
            param.GenerateInMemory   = true;
            var result = compiler.CompileAssemblyFromSource(param, GetCode(code, references));

            if (result.Errors.HasErrors)
            {
                var error = new StringBuilder();
                error.AppendFormat("code:{0},error info:", GetCode(code));

                foreach (CompilerError info in result.Errors)
                {
                    error.Append(info.ErrorText);
                }

                BaseLog.SaveLog(error.ToString(), "DynamicCompiler");

                return(false);
            }
            else
            {
                assembly = result.CompiledAssembly;
                var instance = assembly.CreateInstance("DynamicCode.Condition");
                var method   = instance.GetType().GetMethod("OutPut");
                return((bool)BaseEmit.Invoke(instance, method, null));
            }
        }
コード例 #3
0
        /// <summary>
        /// 解析表达式
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="isRight"></param>
        /// <returns></returns>
        private static string RouteExpressionHandler(ConfigModel config, Expression exp, ExpressionType expType, ref List <string> leftList, ref List <string> rightList, ref List <System.Type> typeList, ref StringBuilder sb, ref string strType, ref int i, bool isRight = false)
        {
            var isReturnNull = false;

            if (exp is BinaryExpression)
            {
                BinaryExpression be = (BinaryExpression)exp;

                return(BinaryExpressionHandler(config, be.Left, be.Right, be.NodeType, ref leftList, ref rightList, ref typeList, ref sb, ref strType, ref i, isRight));
            }
            else if (exp is MemberExpression)
            {
                if ((exp as MemberExpression).Expression is ParameterExpression)
                {
                    return((exp as MemberExpression).Member.Name);
                }
                else
                {
                    if (Expression.Lambda(exp).Compile().DynamicInvoke() == null)
                    {
                        typeList.Add("".GetType());
                    }
                    else
                    {
                        typeList.Add(Expression.Lambda(exp).Compile().DynamicInvoke().GetType());
                    }
                    return(Expression.Lambda(exp).Compile().DynamicInvoke().ToStr());
                }
            }
            else if (exp is NewArrayExpression)
            {
                NewArrayExpression naExp   = (NewArrayExpression)exp;
                StringBuilder      sbArray = new StringBuilder();
                foreach (Expression expression in naExp.Expressions)
                {
                    sbArray.AppendFormat(",{0}", RouteExpressionHandler(config, expression, expType, ref leftList, ref rightList, ref typeList, ref sb, ref strType, ref i, isRight));
                }

                return(sbArray.Length == 0 ? "" : sbArray.Remove(0, 1).ToString());
            }
            else if (exp is MethodCallExpression)
            {
                if (isRight)
                {
                    typeList.Add(Expression.Lambda(exp).Compile().DynamicInvoke().GetType());
                    return(Expression.Lambda(exp).Compile().DynamicInvoke().ToStr());
                }
                else
                {
                    isRight = false;
                    try
                    {
                        var asName = "";
                        var meExp  = (MethodCallExpression)(exp.ReduceExtensions().Reduce());

                        #region 表别名
                        if (meExp.Object != null)
                        {
                            if (meExp.Object is MemberExpression && (meExp.Object as MemberExpression).Expression is ParameterExpression)
                            {
                                asName = string.Format("{0}.", ((meExp.Object as MemberExpression).Expression as ParameterExpression).Name);
                            }
                            else if (meExp.Object is UnaryExpression)
                            {
                                asName = string.Format("{0}.", (((meExp.Object as UnaryExpression).Operand as MemberExpression).Expression as ParameterExpression).Name);
                            }
                        }
                        #endregion

                        if (meExp.Object is MemberExpression && !string.IsNullOrEmpty(asName))
                        {
                            #region system的方法转sql的系统函数
                            var mMethod = meExp.Method.Name;
                            var mName   = ((MemberExpression)meExp.Object).Member.Name;
                            var mValue  = "";
                            var mStar   = "";
                            var mLength = "";
                            var mCount  = 0;

                            meExp.Arguments.ToList().ForEach(a =>
                            {
                                mCount++;
                                mValue = Expression.Lambda(a).Compile().DynamicInvoke().ToString();

                                if (meExp.Arguments.Count == 2)
                                {
                                    if (mCount == 1)
                                    {
                                        mStar = Expression.Lambda(a).Compile().DynamicInvoke().ToString();
                                    }

                                    if (mCount == 2)
                                    {
                                        mLength = Expression.Lambda(a).Compile().DynamicInvoke().ToString();
                                    }
                                }
                            });

                            if (string.Compare(mMethod, "contains", true) == 0)
                            {
                                sb.AppendFormat(" {2}{0} like {3}{0}{1}", mName, i, asName, config.Flag);
                                leftList.Add(mName);
                                rightList.Add(string.Format("%{0}%", mValue));
                                i++;
                            }
                            else if (string.Compare(mMethod, "endswith", true) == 0)
                            {
                                sb.AppendFormat(" {2}{0} like {3}{0}{1}", mName, i, asName, config.Flag);
                                leftList.Add(mName);
                                rightList.Add(string.Format("%{0}", mValue));
                            }
                            else if (string.Compare(mMethod, "startswith", true) == 0)
                            {
                                sb.AppendFormat(" {2}{0} like {3}{0}{1}", mName, i, asName, config.Flag);
                                leftList.Add(mName);
                                rightList.Add(string.Format("{0}%", mValue));
                                i++;
                            }
                            else if (string.Compare(mMethod, "substring", true) == 0)
                            {
                                var tempType = "";
                                if (expType == ExpressionType.Goto)
                                {
                                    tempType = "=";
                                }
                                else
                                {
                                    tempType = ExpressionTypeCast(expType);
                                }

                                if (config.DbType == DataDbType.SqlServer)
                                {
                                    sb.AppendFormat(" substring({4}{0},{2},{3}) {6} {5}{0}{1}", mName, i, mStar, mLength, asName, config.Flag, tempType);
                                }
                                else if (config.DbType == DataDbType.Oracle || config.DbType == DataDbType.MySql || config.DbType == DataDbType.DB2)
                                {
                                    sb.AppendFormat(" substr({4}{0},{2},{3}) {6} {5}{0}{1}", mName, i, mStar, mLength, asName, config.Flag, tempType);
                                }

                                leftList.Add(mName);
                                i++;
                            }
                            else if (string.Compare(mMethod, "toupper", true) == 0)
                            {
                                var tempType = "";
                                if (expType == ExpressionType.Goto)
                                {
                                    tempType = "=";
                                }
                                else
                                {
                                    tempType = ExpressionTypeCast(expType);
                                }
                                sb.AppendFormat(" upper({0}{1}) {4} {2}{1}{3}", asName, mName, config.Flag, i, tempType);

                                leftList.Add(mName);
                                i++;
                            }
                            else if (string.Compare(mMethod, "CompareTo", true) == 0)
                            {
                                var tempType = "";
                                if (expType == ExpressionType.Goto)
                                {
                                    tempType = "=";
                                }
                                else
                                {
                                    tempType = ExpressionTypeCast(expType);
                                }
                                sb.AppendFormat(" upper({0}{1}) {4} upper({2}{1}{3})", asName, mName, config.Flag, i, tempType);

                                leftList.Add(mName);
                                rightList.Add(mValue.ToString());
                                i++;
                            }
                            else if (string.Compare(mMethod, "tolower", true) == 0)
                            {
                                var tempType = "";
                                if (expType == ExpressionType.Goto)
                                {
                                    tempType = "=";
                                }
                                else
                                {
                                    tempType = ExpressionTypeCast(expType);
                                }
                                sb.AppendFormat(" lower({0}{1}) {4} {2}{1}{3}", asName, mName, config.Flag, i, tempType);

                                leftList.Add(mName);
                                i++;
                            }
                            #endregion
                        }

                        if (meExp.Object == null && meExp.Method.Name == "Contains" && meExp.Arguments.Count == 2)
                        {
                            #region array.Contains
                            var array = Expression.Lambda(meExp.Arguments[0]).Compile().DynamicInvoke() as Array;
                            var mName = (meExp.Arguments[1] as MemberExpression).Member.Name;
                            asName = string.Format("{0}.", ((meExp.Arguments[1] as MemberExpression).Expression as ParameterExpression).Name);

                            sb.AppendFormat(" {0}{1} in (", asName, mName);
                            for (int ary = 0; ary < array.Length; ary++)
                            {
                                sb.AppendFormat("{0}{1}{2},", config.Flag, mName, i);
                                leftList.Add(mName);
                                rightList.Add(array.GetValue(ary).ToStr());
                                i++;
                            }
                            sb.Remove(sb.Length - 1, 1);
                            sb.Append(")");
                            #endregion
                        }

                        if (string.IsNullOrEmpty(asName) && meExp.Method.Name == "Contains" && meExp.Arguments.Count == 1)
                        {
                            #region list.Contains
                            var mName = (meExp.Arguments[0] as MemberExpression).Member.Name;
                            asName = string.Format("{0}.", ((meExp.Arguments[0] as MemberExpression).Expression as ParameterExpression).Name);
                            var model = Expression.Lambda(meExp.Object).Compile().DynamicInvoke();
                            var count = (int)BaseEmit.Invoke(model, model.GetType().GetMethod("get_Count"), null);

                            sb.AppendFormat(" {0}{1} in (", asName, mName);
                            for (var j = 0; j < count; j++)
                            {
                                sb.AppendFormat("{0}{1}{2},", config.Flag, mName, i);
                                leftList.Add(mName);
                                rightList.Add(BaseEmit.Invoke(model, model.GetType().GetMethod("get_Item"), new object[] { j }).ToStr());
                                i++;
                            }
                            sb.Remove(sb.Length - 1, 1);
                            sb.Append(")");
                            #endregion
                        }

                        if (isReturnNull)
                        {
                            return("");
                        }
                        else
                        {
                            return(sb.ToString());
                        }
                    }
                    catch
                    {
                        return("");
                    }
                }
            }
            else if (exp is ConstantExpression)
            {
                typeList.Add("".GetType());
                ConstantExpression cExp = (ConstantExpression)exp;
                if (cExp.Value == null)
                {
                    return("null");
                }
                else
                {
                    return(cExp.Value.ToString());
                }
            }
            else if (exp is UnaryExpression)
            {
                var ue = ((UnaryExpression)exp);
                return(RouteExpressionHandler(config, ue.Operand, expType, ref leftList, ref rightList, ref typeList, ref sb, ref strType, ref i, isRight));
            }

            return(null);
        }
コード例 #4
0
        /// <summary>
        ///  to list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static IList ToList(System.Type type, Object model, DbDataReader dr, ConfigModel config, List <string> field = null)
        {
            var list    = Activator.CreateInstance(type);
            var colList = new List <string>();

            if (dr == null)
            {
                return(null);
            }

            if (dr.HasRows)
            {
                colList = GetCol(dr);
            }

            var propertyList = PropertyCache.GetPropertyInfo(model, config.IsPropertyCache);

            while (dr.Read())
            {
                var item = Activator.CreateInstance(model.GetType());

                if (field == null || field.Count == 0)
                {
                    foreach (var info in propertyList)
                    {
                        if (!colList.Exists(a => string.Compare(a, info.Name, true) == 0))
                        {
                            continue;
                        }

                        if (info.PropertyType.IsGenericType && info.PropertyType.GetGenericTypeDefinition() != typeof(Nullable <>))
                        {
                            continue;
                        }

                        item = SetValue(item, dr, info, config);
                    }
                }
                else
                {
                    for (var i = 0; i < field.Count; i++)
                    {
                        if (!colList.Exists(a => string.Compare(a, field[i], true) == 0))
                        {
                            continue;
                        }

                        if (propertyList.Exists(a => string.Compare(a.Name, field[i], true) == 0))
                        {
                            var info = propertyList.Find(a => string.Compare(a.Name, field[i], true) == 0);
                            item = SetValue(item, dr, info, config);
                        }
                    }
                }

                list.GetType().GetMethods().ToList().ForEach(m =>
                {
                    if (m.Name == "Add")
                    {
                        BaseEmit.Invoke(list, m, new object[] { item });
                    }
                });
            }

            return((IList)list);
        }