예제 #1
0
        /// <summary>
        /// 调整DbCommand命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public virtual void PrepareCommand(DbCommand cmd)
        {
            bool isStoredProcedure = (cmd.CommandType == CommandType.StoredProcedure);

            if (!isStoredProcedure)
            {
                cmd.CommandText = DataUtils.FormatSQL(cmd.CommandText, leftToken, rightToken);
            }

            foreach (DbParameter p in cmd.Parameters)
            {
                if (!isStoredProcedure)
                {
                    //TODO 这里可以继续优化
                    if (cmd.CommandText.IndexOf(p.ParameterName, StringComparison.Ordinal) == -1)
                    {
                        //2015-08-11修改
                        cmd.CommandText = cmd.CommandText.Replace("@" + p.ParameterName.Substring(1), p.ParameterName);
                        cmd.CommandText = cmd.CommandText.Replace("?" + p.ParameterName.Substring(1), p.ParameterName);
                        cmd.CommandText = cmd.CommandText.Replace(":" + p.ParameterName.Substring(1), p.ParameterName);
                        //if (p.ParameterName.Substring(0, 1) == "?" || p.ParameterName.Substring(0, 1) == ":"
                        //        || p.ParameterName.Substring(0, 1) == "@")
                        //    cmd.CommandText = cmd.CommandText.Replace(paramPrefixToken + p.ParameterName.Substring(1), p.ParameterName);
                        //else
                        //    cmd.CommandText = cmd.CommandText.Replace(p.ParameterName.Substring(1), p.ParameterName);
                    }
                }

                if (p.Direction == ParameterDirection.Output || p.Direction == ParameterDirection.ReturnValue)
                {
                    continue;
                }

                object value  = p.Value;
                DbType dbType = p.DbType;

                if (value == DBNull.Value)
                {
                    continue;
                }

                if (value == null)
                {
                    p.Value = DBNull.Value;
                    continue;
                }

                Type type = value.GetType();

                if (type.IsEnum)
                {
                    p.DbType = DbType.Int32;
                    p.Value  = Convert.ToInt32(value);
                    continue;
                }

                if (dbType == DbType.Guid && type != typeof(Guid))
                {
                    p.Value = new Guid(value.ToString());
                    continue;
                }
                #region 2015-09-08注释
                ////2015-09-07 写
                //var v = value.ToString();
                //if (DatabaseType == DatabaseType.MsAccess
                //    && (dbType == DbType.AnsiString || dbType == DbType.String)
                //    && !string.IsNullOrWhiteSpace(v)
                //    && cmd.CommandText.ToLower()
                //    .IndexOf("like " + p.ParameterName.ToLower(), StringComparison.Ordinal) > -1)
                //{
                //    if (v[0] == '%')
                //    {
                //        v = "*" + v.Substring(1);
                //    }
                //    if (v[v.Length-1] == '%')
                //    {
                //        v = v.TrimEnd('%') + "*";
                //    }
                //    p.Value = v;
                //}
                #endregion
                //if ((dbType == DbType.AnsiString || dbType == DbType.String ||
                //    dbType == DbType.AnsiStringFixedLength || dbType == DbType.StringFixedLength) && (!(value is string)))
                //{
                //    p.Value = SerializationManager.Serialize(value);
                //    continue;
                //}

                if (type == typeof(Boolean))
                {
                    p.Value = (((bool)value) ? 1 : 0);
                    continue;
                }
            }
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="be"></param>
        /// <param name="co"></param>
        /// <param name="wtype"></param>
        /// <returns></returns>
        private static WhereClip LeftAndRight(BinaryExpression be, QueryOperator co, WhereType wtype = WhereType.Where)
        {
            ColumnFunction   leftFunction;
            ColumnFunction   rightFunction;
            MemberExpression leftMe = null;
            MemberExpression rightMe;

            System.Linq.Expressions.Expression expLeft  = be.Left;
            System.Linq.Expressions.Expression expRight = be.Right;
            if (be.Left.NodeType == ExpressionType.Convert)
            {
                expLeft = ((UnaryExpression)be.Left).Operand;
            }
            if (be.Right.NodeType == ExpressionType.Convert)
            {
                expRight = ((UnaryExpression)be.Right).Operand;
            }
            var isAgain = false;

Again:
            if (expLeft.NodeType == ExpressionType.Constant ||
                (expLeft.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expLeft).Expression == null) || isAgain)
            {
                if (expRight.NodeType == ExpressionType.Constant ||
                    (expRight.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expRight).Expression == null))
                {
                    return(DataUtils.ConvertValue <bool>(fastEvaluator.Eval(be))
                        ? new WhereClip(" 1=2 ")
                        : new WhereClip(" 1=1 "));
                }
                else
                {
                    var keyRightName = GetMemberName(expRight, out rightFunction, out rightMe);

                    if (expLeft.NodeType == ExpressionType.MemberAccess)
                    {
                        var left = (MemberExpression)expLeft;
                        if (left.Expression != null && (wtype == WhereType.JoinWhere || left.Expression.ToString() == rightMe.Expression.ToString()))
                        {
                            ColumnFunction functionLeft;
                            var            keyLeft = GetMemberName(expLeft, out functionLeft, out left);
                            if (keyRightName[0].Contains("$"))
                            {
                                return(new WhereClip(CreateField(keyLeft, left.Expression.Type), GetValue(expRight), co));
                            }
                            else
                            {
                                return(new WhereClip(CreateField(keyRightName, rightMe.Expression.Type), CreateField(keyLeft, left.Expression.Type), co));
                            }
                        }
                    }
                    object value = GetValue(expLeft);
                    if (keyRightName[0].Contains("$"))
                    {
                        if (DataUtils.ConvertValue <bool>(fastEvaluator.Eval(be)))
                        {
                            return(new WhereClip(" 1=2 "));
                        }
                        return(new WhereClip(" 1=1 "));
                    }
                    if (value != null)
                    {
                        return(new WhereClip(CreateField(keyRightName, rightMe.Expression.Type), value, co));
                    }
                    switch (co)
                    {
                    case QueryOperator.Equal:
                        return(CreateField(keyRightName, rightMe.Expression.Type).IsNull());

                    case QueryOperator.NotEqual:
                        return(CreateField(keyRightName, rightMe.Expression.Type).IsNotNull());
                    }
                    throw new Exception("null值只支持等于或不等于!出错比较符:" + co.ToString());
                }
            }
            else
            {
                string[] key;
                try
                {
                    key = GetMemberName(expLeft, out leftFunction, out leftMe);
                    if (key[0].Contains("$"))
                    {
                        isAgain = true;
                        goto Again;
                    }
                }
                catch (Exception)
                {
                    isAgain = true;
                    goto Again;
                }
                if (expRight.NodeType == ExpressionType.MemberAccess)
                {
                    var right = (MemberExpression)expRight;
                    if (right.Expression != null && (wtype == WhereType.JoinWhere || right.Expression == leftMe.Expression))
                    {
                        ColumnFunction functionRight;
                        var            keyRight = GetMemberName(expRight, out functionRight, out right);
                        return(new WhereClip(
                                   CreateField(key, leftMe.Expression.Type),
                                   CreateField(keyRight, right.Expression.Type)
                                   , co));
                    }
                }
                object value = GetValue(expRight);
                if (value == null)
                {
                    if (co == QueryOperator.Equal)
                    {
                        return(CreateField(key, leftMe.Expression.Type).IsNull());
                    }
                    if (co == QueryOperator.NotEqual)
                    {
                        return(CreateField(key, leftMe.Expression.Type).IsNotNull());
                    }
                    throw new Exception("null值只支持等于或不等于!");
                }
                return(new WhereClip(CreateField(key, leftMe.Expression.Type), value, co));
            }
        }