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 void RequireCompatibleType( DbExpression expression, PrimitiveTypeKind requiredResultType, string argumentName) { ArgumentValidation.RequireCompatibleType(expression, requiredResultType, argumentName, -1); }
internal static TypeUsage ValidateFilter( DbExpressionBinding input, DbExpression predicate) { ArgumentValidation.RequireCompatibleType(predicate, PrimitiveTypeKind.Boolean, nameof(predicate)); return(input.Expression.ResultType); }
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); }
internal static TypeUsage ValidateRefFromKey( EntitySet entitySet, DbExpression keyValues, EntityType entityType) { ArgumentValidation.CheckEntitySet((EntitySetBase)entitySet, nameof(entitySet)); ArgumentValidation.CheckType((EdmType)entityType); if (!TypeSemantics.IsValidPolymorphicCast((EdmType)entitySet.ElementType, (EdmType)entityType)) { throw new ArgumentException(Strings.Cqt_Ref_PolymorphicArgRequired); } TypeUsage resultType = ArgumentValidation.CreateResultType((EdmType)TypeHelpers.CreateKeyRowType((EntityTypeBase)entitySet.ElementType)); ArgumentValidation.RequireCompatibleType(keyValues, resultType, nameof(keyValues)); return(ArgumentValidation.CreateReferenceResultType((EntityTypeBase)entityType)); }
internal static TypeUsage ValidateJoin( DbExpressionBinding left, DbExpressionBinding right, DbExpression joinCondition) { if (left.VariableName.Equals(right.VariableName, StringComparison.Ordinal)) { throw new ArgumentException(Strings.Cqt_Join_DuplicateVariableNames); } ArgumentValidation.RequireCompatibleType(joinCondition, PrimitiveTypeKind.Boolean, nameof(joinCondition)); return(ArgumentValidation.CreateCollectionOfRowResultType(new List <KeyValuePair <string, TypeUsage> >(2) { new KeyValuePair <string, TypeUsage>(left.VariableName, left.VariableType), new KeyValuePair <string, TypeUsage>(right.VariableName, right.VariableType) })); }
internal static TypeUsage ValidateNavigate( DbExpression navigateFrom, RelationshipEndMember fromEnd, RelationshipEndMember toEnd, out RelationshipType relType, bool allowAllRelationshipsInSameTypeHierarchy) { ArgumentValidation.CheckMember((EdmMember)fromEnd, nameof(fromEnd)); ArgumentValidation.CheckMember((EdmMember)toEnd, nameof(toEnd)); relType = fromEnd.DeclaringType as RelationshipType; ArgumentValidation.CheckType((EdmType)relType); if (!relType.Equals((object)toEnd.DeclaringType)) { throw new ArgumentException(Strings.Cqt_Factory_IncompatibleRelationEnds, nameof(toEnd)); } ArgumentValidation.RequireCompatibleType(navigateFrom, fromEnd, allowAllRelationshipsInSameTypeHierarchy); return(ArgumentValidation.CreateResultType(toEnd)); }
internal static TypeUsage ValidateNavigate( DbExpression navigateFrom, RelationshipType type, string fromEndName, string toEndName, out RelationshipEndMember fromEnd, out RelationshipEndMember toEnd) { ArgumentValidation.CheckType((EdmType)type); if (!type.RelationshipEndMembers.TryGetValue(fromEndName, false, out fromEnd)) { throw new ArgumentOutOfRangeException(fromEndName, Strings.Cqt_Factory_NoSuchRelationEnd); } if (!type.RelationshipEndMembers.TryGetValue(toEndName, false, out toEnd)) { throw new ArgumentOutOfRangeException(toEndName, Strings.Cqt_Factory_NoSuchRelationEnd); } ArgumentValidation.RequireCompatibleType(navigateFrom, fromEnd, false); return(ArgumentValidation.CreateResultType(toEnd)); }
internal static DbExpressionList ValidateFunctionAggregate( EdmFunction function, IEnumerable <DbExpression> args) { ArgumentValidation.CheckFunction(function); if (!TypeSemantics.IsAggregateFunction(function) || function.ReturnParameter == null) { throw new ArgumentException(Strings.Cqt_Aggregate_InvalidFunction, nameof(function)); } FunctionParameter[] expectedParams = ArgumentValidation.GetExpectedParameters(function); return(ArgumentValidation.CreateExpressionList(args, "argument", expectedParams.Length, (Action <DbExpression, int>)((exp, idx) => { TypeUsage typeUsage = expectedParams[idx].TypeUsage; TypeUsage elementType = (TypeUsage)null; if (TypeHelpers.TryGetCollectionElementType(typeUsage, out elementType)) { typeUsage = elementType; } ArgumentValidation.RequireCompatibleType(exp, typeUsage, "argument"); }))); }
internal static TypeUsage ValidateNew( TypeUsage instanceType, IEnumerable <DbExpression> arguments, out DbExpressionList validArguments) { ArgumentValidation.CheckType(instanceType, nameof(instanceType)); CollectionType type = (CollectionType)null; if (TypeHelpers.TryGetEdmType <CollectionType>(instanceType, out type) && type != null) { TypeUsage elementType = type.TypeUsage; validArguments = ArgumentValidation.CreateExpressionList(arguments, nameof(arguments), true, (Action <DbExpression, int>)((exp, idx) => ArgumentValidation.RequireCompatibleType(exp, elementType, nameof(arguments), idx))); } else { List <TypeUsage> expectedTypes = ArgumentValidation.GetStructuralMemberTypes(instanceType); int pos = 0; validArguments = ArgumentValidation.CreateExpressionList(arguments, nameof(arguments), expectedTypes.Count, (Action <DbExpression, int>)((exp, idx) => ArgumentValidation.RequireCompatibleType(exp, expectedTypes[pos++], nameof(arguments), idx))); } return(instanceType); }
internal static TypeUsage ValidateFunction( EdmFunction function, IEnumerable <DbExpression> arguments, out DbExpressionList validArgs) { ArgumentValidation.CheckFunction(function); if (!function.IsComposableAttribute) { throw new ArgumentException(Strings.Cqt_Function_NonComposableInExpression, nameof(function)); } if (!string.IsNullOrEmpty(function.CommandTextAttribute) && !function.HasUserDefinedBody) { throw new ArgumentException(Strings.Cqt_Function_CommandTextInExpression, nameof(function)); } if (function.ReturnParameter == null) { throw new ArgumentException(Strings.Cqt_Function_VoidResultInvalid, nameof(function)); } FunctionParameter[] expectedParams = ArgumentValidation.GetExpectedParameters(function); validArgs = ArgumentValidation.CreateExpressionList(arguments, nameof(arguments), expectedParams.Length, (Action <DbExpression, int>)((exp, idx) => ArgumentValidation.RequireCompatibleType(exp, expectedParams[idx].TypeUsage, nameof(arguments), idx))); return(function.ReturnParameter.TypeUsage); }
internal static TypeUsage ValidateCase( IEnumerable <DbExpression> whenExpressions, IEnumerable <DbExpression> thenExpressions, DbExpression elseExpression, out DbExpressionList validWhens, out DbExpressionList validThens) { validWhens = ArgumentValidation.CreateExpressionList(whenExpressions, nameof(whenExpressions), (Action <DbExpression, int>)((exp, idx) => ArgumentValidation.RequireCompatibleType(exp, PrimitiveTypeKind.Boolean, nameof(whenExpressions), idx))); TypeUsage commonResultType = (TypeUsage)null; validThens = ArgumentValidation.CreateExpressionList(thenExpressions, nameof(thenExpressions), (Action <DbExpression, int>)((exp, idx) => { if (commonResultType == null) { commonResultType = exp.ResultType; } else { commonResultType = TypeHelpers.GetCommonTypeUsage(exp.ResultType, commonResultType); if (commonResultType == null) { throw new ArgumentException(Strings.Cqt_Case_InvalidResultType); } } })); commonResultType = TypeHelpers.GetCommonTypeUsage(elseExpression.ResultType, commonResultType); if (commonResultType == null) { throw new ArgumentException(Strings.Cqt_Case_InvalidResultType); } if (validWhens.Count != validThens.Count) { throw new ArgumentException(Strings.Cqt_Case_WhensMustEqualThens); } return(commonResultType); }
internal static TypeUsage ValidateQuantifier(DbExpression predicate) { ArgumentValidation.RequireCompatibleType(predicate, PrimitiveTypeKind.Boolean, nameof(predicate)); return(predicate.ResultType); }