internal static void ValidateSortClause(DbExpression key, string collation) { ArgumentValidation.ValidateSortClause(key); Check.NotEmpty(collation, nameof(collation)); if (!TypeSemantics.IsPrimitiveType(key.ResultType, PrimitiveTypeKind.String)) { throw new ArgumentException(Strings.Cqt_Sort_NonStringCollationInvalid, nameof(collation)); } }
internal static TypeUsage ValidateConstant(Type type) { PrimitiveTypeKind primitiveTypeKind; if (!ArgumentValidation.TryGetPrimitiveTypeKind(type, out primitiveTypeKind)) { throw new ArgumentException(Strings.Cqt_Constant_InvalidType, nameof(type)); } return(TypeHelpers.GetLiteralTypeUsage(primitiveTypeKind)); }
internal static TypeUsage ValidateNewEmptyCollection( TypeUsage collectionType, out DbExpressionList validElements) { ArgumentValidation.CheckType(collectionType, nameof(collectionType)); if (!TypeSemantics.IsCollectionType(collectionType)) { throw new ArgumentException(Strings.Cqt_NewInstance_CollectionTypeRequired, nameof(collectionType)); } validElements = new DbExpressionList((IList <DbExpression>) new DbExpression[0]); return(collectionType); }
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 TypeUsage ValidateApply( DbExpressionBinding input, DbExpressionBinding apply) { if (input.VariableName.Equals(apply.VariableName, StringComparison.Ordinal)) { throw new ArgumentException(Strings.Cqt_Apply_DuplicateVariableNames); } return(ArgumentValidation.CreateCollectionOfRowResultType(new List <KeyValuePair <string, TypeUsage> >() { new KeyValuePair <string, TypeUsage>(input.VariableName, input.VariableType), new KeyValuePair <string, TypeUsage>(apply.VariableName, apply.VariableType) })); }
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 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 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 void CheckEntitySet(EntitySetBase entitySet, string varName) { ArgumentValidation.CheckReadOnly(entitySet, varName); if (entitySet.EntityContainer == null) { throw new ArgumentException(Strings.Cqt_Metadata_EntitySetEntityContainerNull, varName); } if (!ArgumentValidation.CheckDataSpace((GlobalItem)entitySet.EntityContainer)) { throw new ArgumentException(Strings.Cqt_Metadata_EntitySetIncorrectSpace, varName); } if (!ArgumentValidation.CheckDataSpace((GlobalItem)entitySet.ElementType)) { throw new ArgumentException(Strings.Cqt_Metadata_EntitySetIncorrectSpace, varName); } }
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 RequireComparableCollectionArguments <TExpressionType>( DbExpression left, DbExpression right) { TypeUsage typeUsage = ArgumentValidation.RequireCollectionArguments <TExpressionType>(left, right); if (!TypeHelpers.IsSetComparableOpType(TypeHelpers.GetElementTypeUsage(left.ResultType))) { throw new ArgumentException(Strings.Cqt_InvalidTypeForSetOperation((object)TypeHelpers.GetElementTypeUsage(left.ResultType).Identity, (object)typeof(TExpressionType).Name), nameof(left)); } if (!TypeHelpers.IsSetComparableOpType(TypeHelpers.GetElementTypeUsage(right.ResultType))) { throw new ArgumentException(Strings.Cqt_InvalidTypeForSetOperation((object)TypeHelpers.GetElementTypeUsage(right.ResultType).Identity, (object)typeof(TExpressionType).Name), nameof(right)); } return(typeUsage); }
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 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 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"); }))); }
private static void CheckFunction(EdmFunction function) { ArgumentValidation.CheckReadOnly((GlobalItem)function, nameof(function)); if (!ArgumentValidation.CheckDataSpace((GlobalItem)function)) { throw new ArgumentException(Strings.Cqt_Metadata_FunctionIncorrectSpace, nameof(function)); } if (function.IsComposableAttribute && function.ReturnParameter == null) { throw new ArgumentException(Strings.Cqt_Metadata_FunctionReturnParameterNull, nameof(function)); } if (function.ReturnParameter != null && !ArgumentValidation.CheckDataSpace(function.ReturnParameter.TypeUsage)) { throw new ArgumentException(Strings.Cqt_Metadata_FunctionParameterIncorrectSpace, "function.ReturnParameter"); } IList <FunctionParameter> parameters = (IList <FunctionParameter>)function.Parameters; for (int index = 0; index < parameters.Count; ++index) { ArgumentValidation.CheckParameter(parameters[index], StringUtil.FormatIndex("function.Parameters", index)); } }
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); }
internal static void CheckType(TypeUsage type) { ArgumentValidation.CheckType(type, nameof(type)); }
private static void CheckType(EdmType type, string argumentName) { ArgumentValidation.CheckReadOnly((GlobalItem)type, argumentName); }
private static void CheckType(EdmType type) { ArgumentValidation.CheckType(type, nameof(type)); }
internal static void ValidateConstant(TypeUsage constantType, object value) { ArgumentValidation.CheckType(constantType, nameof(constantType)); EnumType type1; if (TypeHelpers.TryGetEdmType <EnumType>(constantType, out type1)) { Type clrEquivalentType = type1.UnderlyingType.ClrEquivalentType; if (clrEquivalentType != value.GetType() && (!value.GetType().IsEnum() || !ArgumentValidation.ClrEdmEnumTypesMatch(type1, value.GetType()))) { throw new ArgumentException(Strings.Cqt_Constant_ClrEnumTypeDoesNotMatchEdmEnumType((object)value.GetType().Name, (object)type1.Name, (object)clrEquivalentType.Name), nameof(value)); } } else { PrimitiveType type2; if (!TypeHelpers.TryGetEdmType <PrimitiveType>(constantType, out type2)) { throw new ArgumentException(Strings.Cqt_Constant_InvalidConstantType((object)constantType.ToString()), nameof(constantType)); } PrimitiveTypeKind primitiveTypeKind; if ((!ArgumentValidation.TryGetPrimitiveTypeKind(value.GetType(), out primitiveTypeKind) || type2.PrimitiveTypeKind != primitiveTypeKind) && (!Helper.IsGeographicType(type2) || primitiveTypeKind != PrimitiveTypeKind.Geography) && (!Helper.IsGeometricType(type2) || primitiveTypeKind != PrimitiveTypeKind.Geometry)) { throw new ArgumentException(Strings.Cqt_Constant_InvalidValueForType((object)constantType.ToString()), nameof(value)); } } }
internal static ReadOnlyCollection <DbSortClause> ValidateSort( IEnumerable <DbSortClause> sortOrder) { return(ArgumentValidation.ValidateSortArguments(sortOrder)); }
internal static TypeUsage ValidateConstant(object value) { return(ArgumentValidation.ValidateConstant(value.GetType())); }
internal static TypeUsage ValidateElement(DbExpression argument) { ArgumentValidation.RequireCollectionArgument <DbElementExpression>(argument); return(TypeHelpers.GetEdmType <CollectionType>(argument.ResultType).TypeUsage); }
private static bool CheckDataSpace(TypeUsage type) { return(ArgumentValidation.CheckDataSpace((GlobalItem)type.EdmType)); }
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()); }
private static bool ClrEdmEnumTypesMatch(EnumType edmEnumType, Type clrEnumType) { PrimitiveTypeKind primitiveTypeKind; if (clrEnumType.Name != edmEnumType.Name || clrEnumType.GetEnumNames().Length < edmEnumType.Members.Count || (!ArgumentValidation.TryGetPrimitiveTypeKind(clrEnumType.GetEnumUnderlyingType(), out primitiveTypeKind) || primitiveTypeKind != edmEnumType.UnderlyingType.PrimitiveTypeKind)) { return(false); } foreach (EnumMember member in edmEnumType.Members) { if (!((IEnumerable <string>)clrEnumType.GetEnumNames()).Contains <string>(member.Name) || !member.Value.Equals(Convert.ChangeType(Enum.Parse(clrEnumType, member.Name), clrEnumType.GetEnumUnderlyingType(), (IFormatProvider)CultureInfo.InvariantCulture))) { return(false); } } return(true); }
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)); }