/// <summary>
        /// 尝试将 exp 转换成 DbParameterExpression。
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static DbExpression ParseDbExpression(this DbExpression exp)
        {
            DbExpression stripedExp = DbExpressionHelper.StripInvalidConvert(exp);

            DbExpression tempExp = stripedExp;

            List <DbConvertExpression> cList = null;

            while (tempExp.NodeType == DbExpressionType.Convert)
            {
                if (cList == null)
                {
                    cList = new List <DbConvertExpression>();
                }

                DbConvertExpression c = (DbConvertExpression)tempExp;
                cList.Add(c);
                tempExp = c.Operand;
            }

            if (tempExp.NodeType == DbExpressionType.Constant || tempExp.NodeType == DbExpressionType.Parameter)
            {
                return(stripedExp);
            }

            if (tempExp.NodeType == DbExpressionType.MemberAccess)
            {
                DbMemberExpression dbMemberExp = (DbMemberExpression)tempExp;

                if (ExistDateTime_NowOrDateTime_UtcNow(dbMemberExp))
                {
                    return(stripedExp);
                }

                DbParameterExpression val;
                if (DbExpressionExtensions.TryParseToParameterExpression(dbMemberExp, out val))
                {
                    if (cList != null)
                    {
                        if (val.Value == DBNull.Value)//如果是 null,则不需要 Convert 了,在数据库里没意义
                        {
                            return(val);
                        }

                        DbConvertExpression c = null;
                        for (int i = cList.Count - 1; i > -1; i--)
                        {
                            DbConvertExpression item = cList[i];
                            c = new DbConvertExpression(item.Type, val);
                        }

                        return(c);
                    }

                    return(val);
                }
            }

            return(stripedExp);
        }
Exemple #2
0
        public override DbExpression Visit(DbConvertExpression exp)
        {
            DbExpression stripedExp = DbExpressionHelper.StripInvalidConvert(exp);

            if (stripedExp.NodeType != DbExpressionType.Convert)
            {
                EnsureDbExpressionReturnCSharpBoolean(stripedExp).Accept(this);
                return(exp);
            }

            exp = (DbConvertExpression)stripedExp;

            if (exp.Type == UtilConstants.TypeOfString)
            {
                this._sqlBuilder.Append("TO_CHAR(");
                exp.Operand.Accept(this);
                this._sqlBuilder.Append(")");
                return(exp);
            }

            if (exp.Type == UtilConstants.TypeOfDateTime)
            {
                this._sqlBuilder.Append("TO_TIMESTAMP(");
                exp.Operand.Accept(this);
                this._sqlBuilder.Append(",'yyyy-mm-dd hh24:mi:ssxff')");
                return(exp);
            }

            string dbTypeString;

            if (TryGetCastTargetDbTypeString(exp.Operand.Type, exp.Type, out dbTypeString, false))
            {
                this.BuildCastState(EnsureDbExpressionReturnCSharpBoolean(exp.Operand), dbTypeString);
            }
            else
            {
                EnsureDbExpressionReturnCSharpBoolean(exp.Operand).Accept(this);
            }

            return(exp);
        }