private bool ShouldConvertValues(BinaryCriteria criteria, out Field field, out object value)
        {
            field = null;
            value = null;

            if (ReferenceEquals(null, criteria) ||
                criteria.Operator < CriteriaOperator.EQ ||
                criteria.Operator > CriteriaOperator.NotIn)
                return false;

            var left = criteria.LeftOperand as Criteria;
            if (ReferenceEquals(null, left))
                return false;

            field = FindField(left.Expression);
            if (ReferenceEquals(null, field))
                return false;

            if (field is StringField)
                return false;

            var right = criteria.RightOperand as ValueCriteria;
            if (ReferenceEquals(null, right))
                return false;

            value = right.Value;
            return value != null;
        }
Пример #2
0
        protected override BaseCriteria VisitBinary(BinaryCriteria criteria)
        {
            if (ShouldConvertValues(criteria, out Field field, out object value))
            {
                try
                {
                    var str = value as string;
                    if (str == null && value is IEnumerable)
                    {
                        var values = new List <object>();
                        foreach (var v in value as IEnumerable)
                        {
                            values.Add(field.ConvertValue(v, CultureInfo.InvariantCulture));
                        }

                        return(new BinaryCriteria(new Criteria(field), criteria.Operator, new ValueCriteria(values)));
                    }

                    if (str == null || str.Length != 0)
                    {
                        value = field.ConvertValue(value, CultureInfo.InvariantCulture);
                        return(new BinaryCriteria(new Criteria(field), criteria.Operator, new ValueCriteria(value)));
                    }
                }
                catch
                {
                    // swallow exceptions for backward compability
                }
            }

            return(base.VisitBinary(criteria));
        }
        protected override BaseCriteria VisitBinary(BinaryCriteria criteria)
        {
            Field field;
            object value;
            if (ShouldConvertValues(criteria, out field, out value))
            try
            {
                var str = value as string;
                if (str == null && value is IEnumerable)
                {
                    var values = new List<object>();
                    foreach (var v in value as IEnumerable)
                        values.Add(field.ConvertValue(v, CultureInfo.InvariantCulture));

                    return new BinaryCriteria(new Criteria(field), criteria.Operator, new ValueCriteria(values));
                }

                if (str == null || str.Length != 0)
                {
                    value = field.ConvertValue(value, CultureInfo.InvariantCulture);
                    return new BinaryCriteria(new Criteria(field), criteria.Operator, new ValueCriteria(value));
                }
            }
            catch
            {
                // swallow exceptions for backward compability
            }

            return base.VisitBinary(criteria);
        }
Пример #4
0
        protected virtual BaseCriteria VisitBinary(BinaryCriteria criteria)
        {
            var left = this.Visit(criteria.LeftOperand);
            var right = this.Visit(criteria.RightOperand);

            if (!Object.ReferenceEquals(left, criteria.LeftOperand) ||
                !Object.ReferenceEquals(right, criteria.RightOperand))
            {
                return new BinaryCriteria(left, criteria.Operator, right);
            }

            return criteria;
        }
Пример #5
0
        /// <summary>
        /// Visits the binary criteria. Binary criteria is one with
        /// two operands and an operator between.
        /// </summary>
        /// <param name="criteria">The binary criteria.</param>
        /// <returns></returns>
        protected virtual BaseCriteria VisitBinary(BinaryCriteria criteria)
        {
            var left  = Visit(criteria.LeftOperand);
            var right = Visit(criteria.RightOperand);

            if (!Object.ReferenceEquals(left, criteria.LeftOperand) ||
                !Object.ReferenceEquals(right, criteria.RightOperand))
            {
                return(new BinaryCriteria(left, criteria.Operator, right));
            }

            return(criteria);
        }
        private bool ShouldConvertValues(BinaryCriteria criteria, out Field field, out object value)
        {
            field = null;
            value = null;

            if (ReferenceEquals(null, criteria) ||
                criteria.Operator < CriteriaOperator.EQ ||
                criteria.Operator > CriteriaOperator.NotIn)
            {
                return(false);
            }

            var left = criteria.LeftOperand as Criteria;

            if (ReferenceEquals(null, left))
            {
                return(false);
            }

            field = FindField(left.Expression);
            if (ReferenceEquals(null, field))
            {
                return(false);
            }

            if (field is StringField)
            {
                return(false);
            }

            var right = criteria.RightOperand as ValueCriteria;

            if (ReferenceEquals(null, right))
            {
                return(false);
            }

            value = right.Value;
            return(value != null);
        }
Пример #7
0
        private bool ShouldConvertValues(BinaryCriteria criteria, out Field field, out object value)
        {
            field = null;
            value = null;

            if (criteria is null ||
                criteria.Operator < CriteriaOperator.EQ ||
                criteria.Operator > CriteriaOperator.NotIn)
            {
                return(false);
            }

            if (!(criteria.LeftOperand is Criteria left))
            {
                return(false);
            }

            field = FindField(left.Expression);
            if (field is null)
            {
                return(false);
            }

            if (field is StringField)
            {
                return(false);
            }

            if (!(criteria.RightOperand is ValueCriteria right))
            {
                return(false);
            }

            value = right.Value;
            return(value != null);
        }
Пример #8
0
        public static TFields As <TFields>(this TFields fields, string alias)
            where TFields : RowFieldsBase, new()
        {
            Check.NotNullOrWhiteSpace(alias, "alias");

            if ((alias == "t0" || alias == "T0") &&
                (fields.alias == "t0" || fields.alias == "T0"))
            {
                return(fields);
            }

            if (fields.AliasName != "t0" && fields.AliasName != "T0")
            {
                throw new ArgumentException("You can't alias fields that are already aliased once!");
            }

            var key = new Tuple <Type, string>(typeof(TFields), alias);

            RowFieldsBase cached;

            if (cache.TryGetValue(key, out cached))
            {
                return((TFields)cached);
            }

            var result = new TFields();

            result.Initialize();

            var aliasPrefix = alias + "_";

            var joinByKey = new HashSet <string>(result.joins.Keys, StringComparer.OrdinalIgnoreCase);

            Func <string, string> mapAlias = x =>
            {
                if (x == "t0" || x == "T0")
                {
                    return(alias);
                }

                if (!joinByKey.Contains(x))
                {
                    return(x);
                }

                return(aliasPrefix + x);
            };

            Func <string, string> mapExpression = x =>
            {
                if (x == null)
                {
                    return(null);
                }

                return(JoinAliasLocator.ReplaceAliases(x, mapAlias));
            };

            foreach (var field in result)
            {
                field.expression = mapExpression(field.expression);

                if (field.referencedAliases != null && field.ReferencedAliases.Count > 0)
                {
                    var old = field.ReferencedAliases.ToArray();
                    field.ReferencedAliases.Clear();
                    foreach (var x in old)
                    {
                        field.ReferencedAliases.Add(mapAlias(x));
                    }
                }

                field.join      = null;
                field.joinAlias = field.joinAlias == null ? null : mapAlias(field.joinAlias);
            }

            var oldJoins = result.joins.ToArray();

            result.joins.Clear();
            foreach (var join in oldJoins)
            {
                BaseCriteria onCriteria;
                var          bc = join.Value.OnCriteria as BinaryCriteria;
                if (!ReferenceEquals(null, bc))
                {
                    onCriteria = new BinaryCriteria(
                        new Criteria(mapExpression(bc.LeftOperand.ToString())),
                        bc.Operator,
                        new Criteria(mapExpression(bc.RightOperand.ToString())));
                }
                else
                {
                    if (ReferenceEquals(null, join.Value.OnCriteria))
                    {
                        onCriteria = null;
                    }
                    else
                    {
                        onCriteria = new Criteria(mapExpression(join.Value.OnCriteria.ToString()));
                    }
                }

                new ReplacedJoin(result.Joins,
                                 mapExpression(join.Value.Table),
                                 mapAlias(join.Value.Name),
                                 onCriteria,
                                 join.Value.GetKeyword());
            }

            result.alias    = alias;
            result.aliasDot = alias + ".";

            cache[key] = result;
            return(result);
        }