Exemplo n.º 1
0
        /// <summary>
        /// 解析非表达式,生成sql条件表达式节点及其附属的sql参数
        /// </summary>
        /// <param name="parameterExpression">Lambda表达式的参数</param>
        /// <param name="bodyExpression">Lambda表达式的主体(或主体的一部分)</param>
        /// <param name="parameterNames">记录不允许重复的sql参数名称</param>
        /// <returns>sql条件表达式节点及其附属的sql参数</returns>
        public override KeyValuePair <INodeBuilder, IDbDataParameter[]> Visit(ParameterExpression parameterExpression, Expression bodyExpression, HashSet <string> parameterNames)
        {
            //获取一元表达式并检查该表达式是否满足解析条件
            UnaryExpression unaryExpression = bodyExpression as UnaryExpression;

            if (unaryExpression.Operand == null)
            {
                throw new Exception(string.Format("Unknow Expression:{0}", unaryExpression.ToString()));
            }
            switch (unaryExpression.Operand.NodeType)
            {
            case ExpressionType.Equal:
            case ExpressionType.NotEqual:
            case ExpressionType.Call:
                break;

            default:
                throw new Exception(string.Format("Unknow Expression:{0}", unaryExpression.ToString()));
            }
            //获取sql语句模板
            WhereVisitor whereVisitor = this._factory.GetVisitor(unaryExpression.Operand.NodeType);
            KeyValuePair <INodeBuilder, IDbDataParameter[]> sqlBuilderPair = whereVisitor.Visit(parameterExpression, unaryExpression.Operand, parameterNames);
            StringBuilder sqlTemplate = new StringBuilder();

            sqlBuilderPair.Key.Build(sqlTemplate);
            //替换sql语句模板中的操作符为相反类型的操作符(如LIKE 替换为NOT LIKE)
            string baseSqlTemplate = sqlTemplate.ToString();

            if (baseSqlTemplate.Contains("LIKE"))
            {
                sqlTemplate.Replace("LIKE", "NOT LIKE");
            }
            if (baseSqlTemplate.Contains(" = "))
            {
                sqlTemplate.Replace(" = ", " <> ");
            }
            if (baseSqlTemplate.Contains(" != "))
            {
                sqlTemplate.Replace(" != ", " = ");
            }
            if (baseSqlTemplate.Contains("IS NULL"))
            {
                sqlTemplate.Replace("IS NULL", "IS NOT NULL");
            }
            if (baseSqlTemplate.Contains("IS NOT NULL"))
            {
                sqlTemplate.Replace("IS NOT NULL", "IS NULL");
            }
            if (baseSqlTemplate.Contains("IN"))
            {
                sqlTemplate.Replace("IN", "NOT IN");
            }
            //获取sql表达式
            INodeBuilder nodeBuilder = new NodeBuilder(SqlType.Where, sqlTemplate.ToString());

            //返回sql表达式及其附属的sql参数集合
            return(new KeyValuePair <INodeBuilder, IDbDataParameter[]>(nodeBuilder, sqlBuilderPair.Value));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 解析查询条件表达式,生成sql条件表达式节点及其附属的sql参数
        /// </summary>
        /// <param name="parameterExpression">Lambda表达式的参数</param>
        /// <param name="bodyExpression">Lambda表达式的主体(或主体的一部分)</param>
        /// <param name="parameterNames">记录不允许重复的sql参数名称</param>
        /// <returns>sql条件表达式节点及其附属的sql参数</returns>
        public override KeyValuePair <INodeBuilder, IDbDataParameter[]> Visit(ParameterExpression parameterExpression, Expression bodyExpression, HashSet <string> parameterNames)
        {
            //获取二叉树表达式
            BinaryExpression binaryExpression = bodyExpression as BinaryExpression;
            //获取左sql表达式及其附属参数
            WhereVisitor leftVisitor = this._factory.GetVisitor(binaryExpression.Left.NodeType);
            KeyValuePair <INodeBuilder, IDbDataParameter[]> leftPair = leftVisitor.Visit(parameterExpression, binaryExpression.Left, parameterNames);
            //获取右sql表达式及其附属参数
            WhereVisitor rightVisitor = this._factory.GetVisitor(binaryExpression.Right.NodeType);
            KeyValuePair <INodeBuilder, IDbDataParameter[]> rightPair = rightVisitor.Visit(parameterExpression, binaryExpression.Right, parameterNames);
            //获取最终的sql条件表达式节点
            INodeBuilder nodeBuilder = new BinaryBuilder()
            {
                LeftBuilder  = leftPair.Key,
                NodeType     = binaryExpression.NodeType,
                RightBuilder = rightPair.Key
            };

            //获取其附属参数
            IDbDataParameter[] sqlParameters = leftPair.Value.Concat(rightPair.Value).ToArray();
            //返回最终的sql表达式及其附属参数
            return(new KeyValuePair <INodeBuilder, IDbDataParameter[]>(nodeBuilder, sqlParameters));
        }