Ejemplo n.º 1
0
        protected override SqlColumnConstraint VisitSqlColumnConstraint(SqlColumnConstraint node)
        {
            var op    = node.Operator;
            var value = node.Value;

            #region 处理一些特殊的值

            switch (op)
            {
            case SqlColumnConstraintOperator.Like:
            case SqlColumnConstraintOperator.Contains:
            case SqlColumnConstraintOperator.StartWith:
            case SqlColumnConstraintOperator.EndWith:
                //如果是空字符串的模糊对比操作,直接认为是真。
                var strValue = value as string;
                if (string.IsNullOrEmpty(strValue))
                {
                    _sql.Append("1 = 1");
                    return(node);
                }
                break;

            case SqlColumnConstraintOperator.In:
            case SqlColumnConstraintOperator.NotIn:
                //对于 In、NotIn 操作,如果传入的是空列表时,需要特殊处理:
                //In(Empty) 表示 false,NotIn(Empty) 表示 true。
                if (value is IEnumerable)
                {
                    bool hasValue = false;
                    foreach (var item in value as IEnumerable)
                    {
                        hasValue = true;
                        break;
                    }
                    if (!hasValue)
                    {
                        if (op == SqlColumnConstraintOperator.In)
                        {
                            _sql.Append("0 = 1");
                        }
                        else
                        {
                            _sql.Append("1 = 1");
                        }

                        return(node);
                    }
                }
                break;

            default:
                break;
            }

            #endregion

            this.AppendColumnUsage(node.Column);

            //根据不同的操作符,来生成不同的_sql。
            switch (op)
            {
            case SqlColumnConstraintOperator.Equal:
                if (value == null || value == DBNull.Value)
                {
                    _sql.Append(" IS NULL");
                }
                else
                {
                    _sql.Append(" = ");
                    _sql.AppendParameter(value);
                }
                break;

            case SqlColumnConstraintOperator.NotEqual:
                if (value == null || value == DBNull.Value)
                {
                    _sql.Append(" IS NOT NULL");
                }
                else
                {
                    _sql.Append(" != ");
                    _sql.AppendParameter(value);
                }
                break;

            case SqlColumnConstraintOperator.Greater:
                _sql.Append(" > ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.GreaterEqual:
                _sql.Append(" >= ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.Less:
                _sql.Append(" < ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.LessEqual:
                _sql.Append(" <= ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.Like:
                _sql.Append(" LIKE ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.Contains:
                _sql.Append(" LIKE ");
                _sql.AppendParameter("%" + value + "%");
                break;

            case SqlColumnConstraintOperator.StartWith:
                _sql.Append(" LIKE ");
                _sql.AppendParameter(value + "%");
                break;

            case SqlColumnConstraintOperator.EndWith:
                _sql.Append(" LIKE ");
                _sql.AppendParameter("%" + value);
                break;

            case SqlColumnConstraintOperator.In:
            case SqlColumnConstraintOperator.NotIn:
                var opSql = op == SqlColumnConstraintOperator.In ? "IN" : "NOT IN";
                _sql.Append(" ").Append(opSql).Append(" (");
                if (value is IEnumerable)
                {
                    bool first = true;
                    foreach (var item in value as IEnumerable)
                    {
                        if (!first)
                        {
                            _sql.Append(',');
                        }
                        _sql.AppendParameter(item);
                        first = false;
                    }
                }
                else if (value is SqlNode)
                {
                    _sql.AppendLine();
                    this.Indent++;
                    this.Visit(value as SqlNode);
                    this.Indent--;
                    _sql.AppendLine();
                }
                _sql.Append(')');
                break;

            default:
                throw new NotSupportedException();
            }

            return(node);
        }
Ejemplo n.º 2
0
        protected override SqlColumnConstraint VisitSqlColumnConstraint(SqlColumnConstraint node)
        {
            var op    = node.Operator;
            var value = node.Value;

            value = this.PrepareConstraintValue(value);

            #region 处理一些特殊的值

            switch (op)
            {
            case SqlColumnConstraintOperator.Like:
            case SqlColumnConstraintOperator.Contains:
            case SqlColumnConstraintOperator.StartsWith:
            case SqlColumnConstraintOperator.EndsWith:
                //如果是空字符串的模糊对比操作,直接认为是真。
                var strValue = value as string;
                if (string.IsNullOrEmpty(strValue))
                {
                    _sql.Append("1 = 1");
                    return(node);
                }
                break;

            case SqlColumnConstraintOperator.NotLike:
            case SqlColumnConstraintOperator.NotContains:
            case SqlColumnConstraintOperator.NotStartsWith:
            case SqlColumnConstraintOperator.NotEndsWith:
                //如果是空字符串的模糊对比操作,直接认为是假。
                var strValue2 = value as string;
                if (string.IsNullOrEmpty(strValue2))
                {
                    _sql.Append("1 != 1");
                    return(node);
                }
                break;

            case SqlColumnConstraintOperator.In:
            case SqlColumnConstraintOperator.NotIn:
                //对于 In、NotIn 操作,如果传入的是空列表时,需要特殊处理:
                //In(Empty) 表示 false,NotIn(Empty) 表示 true。
                if (value is IEnumerable)
                {
                    bool hasValue = false;
                    foreach (var item in value as IEnumerable)
                    {
                        hasValue = true;
                        break;
                    }
                    if (!hasValue)
                    {
                        if (op == SqlColumnConstraintOperator.In)
                        {
                            _sql.Append("0 = 1");
                        }
                        else
                        {
                            _sql.Append("1 = 1");
                        }

                        return(node);
                    }
                }
                break;

            default:
                break;
            }

            #endregion

            this.AppendColumnUsage(node.Column);

            //根据不同的操作符,来生成不同的_sql。
            switch (op)
            {
            case SqlColumnConstraintOperator.Equal:
                if (value == null || value == DBNull.Value)
                {
                    _sql.Append(" IS NULL");
                }
                else
                {
                    _sql.Append(" = ");
                    _sql.AppendParameter(value);
                }
                break;

            case SqlColumnConstraintOperator.NotEqual:
                if (value == null || value == DBNull.Value)
                {
                    _sql.Append(" IS NOT NULL");
                }
                else
                {
                    _sql.Append(" != ");
                    _sql.AppendParameter(value);
                }
                break;

            case SqlColumnConstraintOperator.Greater:
                _sql.Append(" > ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.GreaterEqual:
                _sql.Append(" >= ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.Less:
                _sql.Append(" < ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.LessEqual:
                _sql.Append(" <= ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.Like:
                _sql.Append(" LIKE ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.NotLike:
                _sql.Append(" NOT LIKE ");
                _sql.AppendParameter(value);
                break;

            case SqlColumnConstraintOperator.Contains:
                _sql.Append(" LIKE ");
                _sql.AppendParameter(WILDCARD_ALL + this.Escape(value) + WILDCARD_ALL);
                this.AppendEscapePlause(value);
                break;

            case SqlColumnConstraintOperator.NotContains:
                _sql.Append(" NOT LIKE ");
                _sql.AppendParameter(WILDCARD_ALL + this.Escape(value) + WILDCARD_ALL);
                this.AppendEscapePlause(value);
                break;

            case SqlColumnConstraintOperator.StartsWith:
                _sql.Append(" LIKE ");
                _sql.AppendParameter(this.Escape(value) + WILDCARD_ALL);
                this.AppendEscapePlause(value);
                break;

            case SqlColumnConstraintOperator.NotStartsWith:
                _sql.Append(" NOT LIKE ");
                _sql.AppendParameter(this.Escape(value) + WILDCARD_ALL);
                this.AppendEscapePlause(value);
                break;

            case SqlColumnConstraintOperator.EndsWith:
                _sql.Append(" LIKE ");
                _sql.AppendParameter(WILDCARD_ALL + this.Escape(value));
                this.AppendEscapePlause(value);
                break;

            case SqlColumnConstraintOperator.NotEndsWith:
                _sql.Append(" NOT LIKE ");
                _sql.AppendParameter(WILDCARD_ALL + this.Escape(value));
                this.AppendEscapePlause(value);
                break;

            case SqlColumnConstraintOperator.In:
            case SqlColumnConstraintOperator.NotIn:
                var opSql = op == SqlColumnConstraintOperator.In ? "IN" : "NOT IN";
                _sql.Append(" ").Append(opSql).Append(" (");
                if (value is IEnumerable)
                {
                    bool first         = true;
                    bool needDelimiter = false;
                    int  i             = 0;
                    foreach (var item in value as IEnumerable)
                    {
                        if (++i > this.MaxItemsInInClause)
                        {
                            throw new TooManyItemsInInClauseException();
                        }

                        if (first)
                        {
                            first         = false;
                            needDelimiter = item is string || item is DateTime || item is Guid;
                        }
                        else
                        {
                            _sql.Append(',');
                        }

                        if (needDelimiter)
                        {
                            _sql.Append('\'').Append(item).Append('\'');
                        }
                        else
                        {
                            _sql.Append(item);
                        }

                        //由于集合中的数据可能过多,所以这里不要使用参数化的查询。
                        //_sql.AppendParameter(item);
                    }
                }
                else if (value is SqlNode)
                {
                    _sql.AppendLine();
                    this.Indent++;
                    this.Visit(value as SqlNode);
                    this.Indent--;
                    _sql.AppendLine();
                }
                _sql.Append(')');
                break;

            default:
                throw new NotSupportedException();
            }

            return(node);
        }