Пример #1
0
        public static IExpression Where(this IStatement statment, ICondition criteria)
        {
            if (criteria == null)
            {
                return(null);
            }

            if (criteria is Condition c)
            {
                return(ConditionExtension.ToExpression(c, field => From(statment, field, out var property).CreateField(property), parameter => statment.Parameters.Add(parameter)));
            }

            if (criteria is ConditionCollection cc)
            {
                return(ConditionExtension.ToExpression(cc, field => From(statment, field, out var property).CreateField(property), parameter => statment.Parameters.Add(parameter)));
            }

            throw new NotSupportedException($"The '{criteria.GetType().FullName}' type is an unsupported condition type.");
        }
Пример #2
0
		public static IExpression Where(this IStatement statement, ICondition criteria)
		{
			if(criteria == null)
				return null;

			if(criteria is Condition c)
				return ConditionExtension.ToExpression(c,
					field => GetField(statement, field),
					parameter => statement.Parameters.Add(parameter));

			if(criteria is ConditionCollection cc)
				return ConditionExtension.ToExpression(cc,
					field => GetField(statement, field),
					parameter => statement.Parameters.Add(parameter));

			throw new NotSupportedException($"The '{criteria.GetType().FullName}' type is an unsupported condition type.");

			FieldIdentifier GetField(IStatement host, Condition condition)
			{
				var source = From(statement, condition.Name, (src, complex) => CreateSubquery(host, src, complex, condition.Value as ICondition), out var property);

				if(property.IsSimplex)
					return source.CreateField(property);

				if(condition.Operator == ConditionOperator.Exists || condition.Operator == ConditionOperator.NotExists)
				{
					condition.Value = source;
					return null;
				}

				throw new DataException($"The specified '{condition.Name}' condition is associated with a one-to-many composite(navigation) property and does not support the {condition.Operator} operation.");
			}

			ISource CreateSubquery(IStatement host, ISource source, IDataEntityComplexProperty complex, ICondition condition)
			{
				var subquery = host.Subquery(complex.Foreign);
				var where = ConditionExpression.And();

				foreach(var link in complex.Links)
				{
					subquery.Select.Members.Add(subquery.Table.CreateField(link.Foreign));

					where.Add(Expression.Equal(
						subquery.Table.CreateField(link.Foreign),
						source.CreateField(link.Principal)
					));
				}

				if(complex.HasConstraints())
				{
					foreach(var constraint in complex.Constraints)
					{
						where.Add(Expression.Equal(
							subquery.Table.CreateField(constraint.Name),
							complex.GetConstraintValue(constraint)
						));
					}
				}

				if(condition != null)
					where.Add(Where(subquery, condition));

				subquery.Where = where;
				return subquery;
			}
		}