Inheritance: BaseCriteria
        protected override BaseCriteria VisitCriteria(Criteria criteria)
        {
            var result = base.VisitCriteria(criteria);
            
            criteria = result as Criteria;

            if (!Object.ReferenceEquals(null, criteria))
            {
                var field = FindField(criteria.Expression);
                if (ReferenceEquals(null, field))
                {
                    throw new ValidationError("InvalidCriteriaField", criteria.Expression,
                        String.Format("'{0}' criteria field is not found!", criteria.Expression));
                }

                if (!CanFilterField(field))
                {
                    throw new ValidationError("CantFilterField", criteria.Expression,
                        String.Format("Can't filter on field '{0}'!", criteria.Expression));
                }

                return new Criteria(field);
            }

            return result;
        }
        public void BaseCriteria_ShortCircuitAnd_WithNullRightCriteria_ReturnsLeftCriteria()
        {
            var a = new Criteria("x == 1");
            Criteria b = null;

            var c = a && b;
            Assert.Equal(a, c);
        }
        public void BaseCriteria_ShortCircuitOr_WithEmptyLeftCriteria_ReturnsRightCriteria()
        {
            var a = Criteria.Empty;
            var b = new Criteria("y = 2");

            var c = a || b;
            Assert.Equal(b, c);
        }
        public void BaseCriteria_And_WithEmptyLeftCriteria_ReturnsRightCriteria()
        {
            var a = Criteria.Empty;
            var b = new Criteria("y = 2");

            var c = a & b;
            Assert.Equal(b, c);
        }
        public void BaseCriteria_ShortCircuitAnd_WithEmptyRightCriteria_ReturnsLeftCriteria()
        {
            var a = new Criteria("x == 1");
            var b = Criteria.Empty;

            var c = a && b;
            Assert.Equal(a, c);
        }
        public void BaseCriteria_ShortCircuitAnd_WithNullLeftCriteria_ReturnsRightCriteria()
        {
            Criteria a = null;
            var b = new Criteria("y = 2");

            var c = a && b;
            Assert.Equal(b, c);
        }
        public void BaseCriteria_ShortCircuitAnd_WithTwoCriteria_GeneratesBinaryCriteria()
        {
            var a = new Criteria("x = 1");
            var b = new Criteria("y = 2");

            var c = a && b;
            var actual = Assert.IsType<BinaryCriteria>(c);
            Assert.Equal(CriteriaOperator.AND, actual.Operator);
            Assert.Equal(a, actual.LeftOperand);
            Assert.Equal(b, actual.RightOperand);
        }
        protected override BaseCriteria VisitCriteria(Criteria criteria)
        {
            if (criteria.Expression.IsEmptyOrNull())
                return base.VisitCriteria(criteria);

            if (!SqlSyntax.IsValidIdentifier(criteria.Expression))
                throw new ValidationError("InvalidCriteriaField",
                    String.Format("{0} is not a valid field name!", criteria.Expression));

            return base.VisitCriteria(criteria);
        }
        public void BaseCriteria_GreaterThanOrEqualOperatorOverload_WorksWithEnums()
        {
            var left = new Criteria("x");
            var actual = left >= MyEnum.Value1;

            Assert.NotNull(actual);
            Assert.IsType(typeof(BinaryCriteria), actual);

            var binary = actual as BinaryCriteria;

            Assert.Equal(CriteriaOperator.GE, binary.Operator);
            Assert.Equal(binary.LeftOperand, left);
            Assert.IsType(typeof(ValueCriteria), binary.RightOperand);

            var value = binary.RightOperand as ValueCriteria;

            Assert.IsType(typeof(MyEnum), value.Value);
            Assert.Equal(MyEnum.Value1, value.Value);
        }
        public void BaseCriteria_Or_WithNullRightCriteria_ReturnsLeftCriteria()
        {
            var a = new Criteria("x == 1");
            Criteria b = null;

            var c = a | b;
            Assert.Equal(a, c);
        }
Exemple #11
0
 /// <summary>
 /// Visits the criteria returning potentially reworked version.
 /// </summary>
 /// <param name="criteria">The criteria.</param>
 /// <returns></returns>
 protected virtual BaseCriteria VisitCriteria(Criteria criteria)
 {
     return(criteria);
 }
 protected virtual BaseCriteria VisitCriteria(Criteria criteria)
 {
     return criteria;
 }
        public void BaseCriteria_Or_WithEmptyRightCriteria_ReturnsLeftCriteria()
        {
            var a = new Criteria("x == 1");
            var b = Criteria.Empty;

            var c = a | b;
            Assert.Equal(a, c);
        }
        public bool ActivateFor(Row row)
        {
            if (ReferenceEquals(null, Target))
                return false;

            attr = Target.GetAttribute<LinkingSetRelationAttribute>();
            if (attr == null)
                return false;

            if (!(row is IIdRow))
            {
                throw new ArgumentException(String.Format("Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                    "but it doesn't implement IIdRow!",
                    Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }


            var listType = Target.ValueType;
            if (!listType.GetIsGenericType() ||
                listType.GetGenericTypeDefinition() != typeof(List<>))
            {
                throw new ArgumentException(String.Format("Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                    "but its property type is not a generic List (e.g. List<int>)!",
                    Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            var rowType = attr.RowType;
            if (rowType.GetIsAbstract() ||
                !typeof(Row).IsAssignableFrom(rowType))
            {
                throw new ArgumentException(String.Format(
                    "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                    "but specified row type is not valid row class!",
                        Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            if (!typeof(IIdRow).IsAssignableFrom(rowType))
            {
                throw new ArgumentException(String.Format(
                    "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                    "but specified row type doesn't implement IIdRow!",
                        Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            listFactory = FastReflection.DelegateForConstructor<IList>(listType);
            rowFactory = FastReflection.DelegateForConstructor<Row>(rowType);

            listHandlerFactory = FastReflection.DelegateForConstructor<IListRequestProcessor>(
                typeof(ListRequestHandler<>).MakeGenericType(rowType));

            saveHandlerFactory = FastReflection.DelegateForConstructor<ISaveRequestProcessor>(
                typeof(SaveRequestHandler<>).MakeGenericType(rowType));

            saveRequestFactory = FastReflection.DelegateForConstructor<ISaveRequest>(
                typeof(SaveRequest<>).MakeGenericType(rowType));

            deleteHandlerFactory = FastReflection.DelegateForConstructor<IDeleteRequestProcessor>(
                typeof(DeleteRequestHandler<>).MakeGenericType(rowType));

            var detailRow = rowFactory();

            thisKeyField = detailRow.FindFieldByPropertyName(attr.ThisKey) ??
                detailRow.FindField(attr.ThisKey);

            if (ReferenceEquals(thisKeyField, null))
                throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                    "This field is specified for a linking set relation in field '{2}' of row type '{3}'.",
                    attr.ThisKey, detailRow.GetType().FullName,
                    Target.PropertyName ?? Target.Name, row.GetType().FullName));

            this.thisKeyCriteria = new Criteria(thisKeyField.PropertyName ?? thisKeyField.Name);

            itemKeyField = detailRow.FindFieldByPropertyName(attr.ItemKey) ??
                detailRow.FindField(attr.ItemKey);

            if (ReferenceEquals(itemKeyField, null))
                throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                    "This field is specified for a linking set relation in field '{2}' of row type '{3}'.",
                    attr.ItemKey, detailRow.GetType().FullName,
                    Target.PropertyName ?? Target.Name, row.GetType().FullName));

            if (!string.IsNullOrEmpty(attr.FilterField))
            {
                this.filterField = detailRow.FindFieldByPropertyName(attr.FilterField) ?? detailRow.FindField(attr.FilterField);
                if (ReferenceEquals(null, this.filterField))
                    throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                        "This field is specified for a linking set relation as FilterField in field '{2}' of row type '{3}'.",
                        attr.FilterField, detailRow.GetType().FullName,
                        Target.PropertyName ?? Target.Name, row.GetType().FullName));

                this.filterCriteria = new Criteria(filterField.PropertyName ?? filterField.Name);
                this.filterValue = filterField.ConvertValue(attr.FilterValue, CultureInfo.InvariantCulture);
                if (this.filterValue == null)
                {
                    this.filterCriteria = this.filterCriteria.IsNull();
                    this.filterCriteriaT0 = this.filterField.IsNull();
                }
                else
                {
                    this.filterCriteria = this.filterCriteria == new ValueCriteria(this.filterValue);
                    this.filterCriteriaT0 = this.filterField == new ValueCriteria(this.filterValue);
                }
            }

            return true;
        }
        public void BaseCriteria_Or_WithNullLeftCriteria_ReturnsRightCriteria()
        {
            Criteria a = null;
            var b = new Criteria("y = 2");

            var c = a | b;
            Assert.Equal(b, c);
        }
        public static BaseCriteria GetContainsTextFilter(string containsText, Criteria[] textFields)
        {
            containsText = containsText.TrimToNull();
            if (containsText != null && textFields.Length > 0)
            {
                var flt = Criteria.Empty;
                foreach (var field in textFields)
                    flt |= field.Contains(containsText);
                flt = ~(flt);

                return flt;
            }
            return null;
        }
        private object GetExistingID(IDbConnection connection, RelationInfo info,
            object thisKey)
        {
            var criteria = new Criteria(info.OtherKeyField.PropertyName ?? info.OtherKeyField.Name) ==
                new ValueCriteria(thisKey);

            if (!ReferenceEquals(null, info.FilterField))
            {
                var flt = new Criteria(info.FilterField.PropertyName ?? info.FilterField.Name);
                if (info.FilterValue == null)
                    criteria &= flt.IsNull();
                else
                    criteria &= flt == new ValueCriteria(info.FilterValue);
            }

            var existing = info.ListHandlerFactory().Process(connection, new ListRequest
            {
                ColumnSelection = ColumnSelection.KeyOnly,
                Criteria = criteria
            }).Entities;

            if (existing.Count > 1)
                throw new Exception(String.Format("Found multiple extension rows for UpdatableExtension '{0}'", 
                    info.Attr.Alias));

            if (existing.Count == 0)
                return null;

            return ((Field)((IIdRow)existing[0]).IdField).AsObject((Row)existing[0]);
        }
        public void OnReturn(IListRequestHandler handler)
        {
            if (ReferenceEquals(null, Target) ||
                !handler.ShouldSelectField(Target) ||
                handler.Response.Entities.IsEmptyOrNull())
                return;

            var idField = (Field)((handler.Row as IIdRow).IdField);

            var listHandler = listHandlerFactory();

            var listRequest = new ListRequest
            {
                ColumnSelection = ColumnSelection.KeyOnly,
                IncludeColumns = new HashSet<string>
                {
                    itemKeyField.PropertyName ?? itemKeyField.Name,
                    thisKeyField.PropertyName ?? thisKeyField.Name
                }
            };

            var thisKeyCriteria = new Criteria(thisKeyField.PropertyName ?? thisKeyField.Name);

            var enumerator = handler.Response.Entities.Cast<Row>();
            while (true)
            {
                var part = enumerator.Take(1000);
                if (!part.Any())
                    break;

                enumerator = enumerator.Skip(1000);

                listRequest.Criteria = thisKeyCriteria.In(
                    part.Select(x => idField.AsObject(x)));

                IListResponse response = listHandler.Process(
                    handler.Connection, listRequest);

                var lookup = response.Entities.Cast<Row>()
                    .ToLookup(x => thisKeyField.AsObject(x).ToString());

                foreach (var row in part)
                {
                    var list = listFactory();
                    var matching = lookup[idField.AsObject(row).ToString()];
                    foreach (var x in matching)
                        list.Add(itemKeyField.AsObject(x));

                    Target.AsObject(row, list);
                }
            }
        }