internal static TypeUsage ValidateInvoke( DbLambda lambda, IEnumerable <DbExpression> arguments, out DbExpressionList validArguments) { validArguments = (DbExpressionList)null; EnumerableValidator <DbExpression, DbExpression, DbExpressionList> validator = ArgumentValidation.CreateValidator <DbExpression, DbExpression, DbExpressionList>(arguments, nameof(arguments), (Func <DbExpression, int, DbExpression>)((exp, idx) => { ArgumentValidation.RequireCompatibleType(exp, lambda.Variables[idx].ResultType, nameof(arguments), idx); return(exp); }), (Func <List <DbExpression>, DbExpressionList>)(expList => new DbExpressionList((IList <DbExpression>)expList))); validator.ExpectedElementCount = lambda.Variables.Count; validArguments = validator.Validate(); return(lambda.Body.ResultType); }
private static DbExpressionList CreateExpressionList( IEnumerable <DbExpression> arguments, string argumentName, bool allowEmpty, Action <DbExpression, int> validationCallback) { EnumerableValidator <DbExpression, DbExpression, DbExpressionList> validator = ArgumentValidation.CreateValidator <DbExpression, DbExpression, DbExpressionList>(arguments, argumentName, (Func <DbExpression, int, DbExpression>)((exp, idx) => { if (validationCallback != null) { validationCallback(exp, idx); } return(exp); }), (Func <List <DbExpression>, DbExpressionList>)(expList => new DbExpressionList((IList <DbExpression>)expList))); validator.AllowEmpty = allowEmpty; return(validator.Validate()); }
internal TResult Validate() { return(EnumerableValidator <TElementIn, TElementOut, TResult> .Validate(this.target, this.argumentName, this.ExpectedElementCount, this.AllowEmpty, this.ConvertElement, this.CreateResult, this.GetName)); }
internal static TypeUsage ValidateNewRow( IEnumerable <KeyValuePair <string, DbExpression> > columnValues, out DbExpressionList validElements) { List <KeyValuePair <string, TypeUsage> > columnTypes = new List <KeyValuePair <string, TypeUsage> >(); EnumerableValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList> validator = ArgumentValidation.CreateValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList>(columnValues, nameof(columnValues), (Func <KeyValuePair <string, DbExpression>, int, DbExpression>)((columnValue, idx) => { ArgumentValidation.CheckNamed <DbExpression>(columnValue, nameof(columnValues), idx); columnTypes.Add(new KeyValuePair <string, TypeUsage>(columnValue.Key, columnValue.Value.ResultType)); return(columnValue.Value); }), (Func <List <DbExpression>, DbExpressionList>)(expList => new DbExpressionList((IList <DbExpression>)expList))); validator.GetName = (Func <KeyValuePair <string, DbExpression>, int, string>)((columnValue, idx) => columnValue.Key); validElements = validator.Validate(); return(ArgumentValidation.CreateResultType((EdmType)TypeHelpers.CreateRowType((IEnumerable <KeyValuePair <string, TypeUsage> >)columnTypes))); }
internal static TypeUsage ValidateCreateRef( EntitySet entitySet, EntityType entityType, IEnumerable <DbExpression> keyValues, out DbExpression keyConstructor) { ArgumentValidation.CheckEntitySet((EntitySetBase)entitySet, nameof(entitySet)); ArgumentValidation.CheckType((EdmType)entityType, nameof(entityType)); if (!TypeSemantics.IsValidPolymorphicCast((EdmType)entitySet.ElementType, (EdmType)entityType)) { throw new ArgumentException(Strings.Cqt_Ref_PolymorphicArgRequired); } IList <EdmMember> keyMembers = (IList <EdmMember>)entityType.KeyMembers; EnumerableValidator <DbExpression, KeyValuePair <string, DbExpression>, List <KeyValuePair <string, DbExpression> > > validator = ArgumentValidation.CreateValidator <DbExpression, KeyValuePair <string, DbExpression>, List <KeyValuePair <string, DbExpression> > >(keyValues, nameof(keyValues), (Func <DbExpression, int, KeyValuePair <string, DbExpression> >)((valueExp, idx) => { ArgumentValidation.RequireCompatibleType(valueExp, keyMembers[idx].TypeUsage, nameof(keyValues), idx); return(new KeyValuePair <string, DbExpression>(keyMembers[idx].Name, valueExp)); }), (Func <List <KeyValuePair <string, DbExpression> >, List <KeyValuePair <string, DbExpression> > >)(columnList => columnList)); validator.ExpectedElementCount = keyMembers.Count; List <KeyValuePair <string, DbExpression> > keyValuePairList = validator.Validate(); keyConstructor = (DbExpression)DbExpressionBuilder.NewRow((IEnumerable <KeyValuePair <string, DbExpression> >)keyValuePairList); return(ArgumentValidation.CreateReferenceResultType((EntityTypeBase)entityType)); }
internal static ReadOnlyCollection <DbSortClause> ValidateSortArguments( IEnumerable <DbSortClause> sortOrder) { EnumerableValidator <DbSortClause, DbSortClause, ReadOnlyCollection <DbSortClause> > validator = ArgumentValidation.CreateValidator <DbSortClause, DbSortClause, ReadOnlyCollection <DbSortClause> >(sortOrder, nameof(sortOrder), (Func <DbSortClause, int, DbSortClause>)((key, idx) => key), (Func <List <DbSortClause>, ReadOnlyCollection <DbSortClause> >)(keyList => ArgumentValidation.NewReadOnlyCollection <DbSortClause>((IList <DbSortClause>)keyList))); validator.AllowEmpty = false; return(validator.Validate()); }
internal static TypeUsage ValidateGroupBy( IEnumerable <KeyValuePair <string, DbExpression> > keys, IEnumerable <KeyValuePair <string, DbAggregate> > aggregates, out DbExpressionList validKeys, out ReadOnlyCollection <DbAggregate> validAggregates) { List <KeyValuePair <string, TypeUsage> > columns = new List <KeyValuePair <string, TypeUsage> >(); HashSet <string> keyNames = new HashSet <string>(); EnumerableValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList> validator1 = ArgumentValidation.CreateValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList>(keys, nameof(keys), (Func <KeyValuePair <string, DbExpression>, int, DbExpression>)((keyInfo, index) => { ArgumentValidation.CheckNamed <DbExpression>(keyInfo, nameof(keys), index); if (!TypeHelpers.IsValidGroupKeyType(keyInfo.Value.ResultType)) { throw new ArgumentException(Strings.Cqt_GroupBy_KeyNotEqualityComparable((object)keyInfo.Key)); } keyNames.Add(keyInfo.Key); columns.Add(new KeyValuePair <string, TypeUsage>(keyInfo.Key, keyInfo.Value.ResultType)); return(keyInfo.Value); }), (Func <List <DbExpression>, DbExpressionList>)(expList => new DbExpressionList((IList <DbExpression>)expList))); validator1.AllowEmpty = true; validator1.GetName = (Func <KeyValuePair <string, DbExpression>, int, string>)((keyInfo, idx) => keyInfo.Key); validKeys = validator1.Validate(); bool hasGroupAggregate = false; EnumerableValidator <KeyValuePair <string, DbAggregate>, DbAggregate, ReadOnlyCollection <DbAggregate> > validator2 = ArgumentValidation.CreateValidator <KeyValuePair <string, DbAggregate>, DbAggregate, ReadOnlyCollection <DbAggregate> >(aggregates, nameof(aggregates), (Func <KeyValuePair <string, DbAggregate>, int, DbAggregate>)((aggInfo, idx) => { ArgumentValidation.CheckNamed <DbAggregate>(aggInfo, nameof(aggregates), idx); if (keyNames.Contains(aggInfo.Key)) { throw new ArgumentException(Strings.Cqt_GroupBy_AggregateColumnExistsAsGroupColumn((object)aggInfo.Key)); } if (aggInfo.Value is DbGroupAggregate) { if (hasGroupAggregate) { throw new ArgumentException(Strings.Cqt_GroupBy_MoreThanOneGroupAggregate); } hasGroupAggregate = true; } columns.Add(new KeyValuePair <string, TypeUsage>(aggInfo.Key, aggInfo.Value.ResultType)); return(aggInfo.Value); }), (Func <List <DbAggregate>, ReadOnlyCollection <DbAggregate> >)(aggList => ArgumentValidation.NewReadOnlyCollection <DbAggregate>((IList <DbAggregate>)aggList))); validator2.AllowEmpty = true; validator2.GetName = (Func <KeyValuePair <string, DbAggregate>, int, string>)((aggInfo, idx) => aggInfo.Key); validAggregates = validator2.Validate(); if (validKeys.Count == 0 && validAggregates.Count == 0) { throw new ArgumentException(Strings.Cqt_GroupBy_AtLeastOneKeyOrAggregate); } return(ArgumentValidation.CreateCollectionOfRowResultType(columns)); }
internal static ReadOnlyCollection <DbVariableReferenceExpression> ValidateLambda( IEnumerable <DbVariableReferenceExpression> variables) { EnumerableValidator <DbVariableReferenceExpression, DbVariableReferenceExpression, ReadOnlyCollection <DbVariableReferenceExpression> > validator = ArgumentValidation.CreateValidator <DbVariableReferenceExpression, DbVariableReferenceExpression, ReadOnlyCollection <DbVariableReferenceExpression> >(variables, nameof(variables), (Func <DbVariableReferenceExpression, int, DbVariableReferenceExpression>)((varExp, idx) => { if (varExp == null) { throw new ArgumentNullException(StringUtil.FormatIndex(nameof(variables), idx)); } return(varExp); }), (Func <List <DbVariableReferenceExpression>, ReadOnlyCollection <DbVariableReferenceExpression> >)(varList => new ReadOnlyCollection <DbVariableReferenceExpression>((IList <DbVariableReferenceExpression>)varList))); validator.AllowEmpty = true; validator.GetName = (Func <DbVariableReferenceExpression, int, string>)((varDef, idx) => varDef.VariableName); return(validator.Validate()); }