public static string BuildMultiValueTypeName(this DataType elementType) { ExceptionUtilities.CheckArgumentNotNull(elementType, "elementType"); PrimitiveDataType primitiveDataType = elementType as PrimitiveDataType; ComplexDataType complexDataType = elementType as ComplexDataType; if (primitiveDataType != null) { string edmTypeName = primitiveDataType.GetFacetValue <EdmTypeNameFacet, string>(null); string edmNamespace = primitiveDataType.GetFacetValue <EdmNamespaceFacet, string>(null); if (!string.IsNullOrEmpty(edmTypeName) && !string.IsNullOrEmpty(edmNamespace)) { edmTypeName = edmNamespace + '.' + edmTypeName; } if (!string.IsNullOrEmpty(edmTypeName)) { edmTypeName = "Collection(" + edmTypeName + ")"; } return(edmTypeName); } else { ExceptionUtilities.Assert(complexDataType != null, "Unexpected TypeName to create for a Collection '{0}'", elementType); return("Collection(" + complexDataType.Definition.FullName + ")"); } }
/// <summary> /// Resolves hints for data generation for the given primitive type. /// </summary> /// <param name="dataType">Primitive data type to resolve data generator for.</param> /// <returns>Data generation hints.</returns> public IEnumerable <DataGenerationHint> ResolveDataGenerationHints(PrimitiveDataType dataType) { var hints = new List <DataGenerationHint>(); int maxLength = dataType.GetFacetValue <MaxLengthFacet, int>(-1); if (maxLength >= 0) { hints.Add(DataGenerationHints.MaxLength(maxLength)); } int precision = dataType.GetFacetValue <NumericPrecisionFacet, int>(-1); if (precision > 0 && precision <= DataGenerationUtilities.MaxNumericPrecision) { hints.Add(DataGenerationHints.NumericPrecision(precision)); } int scale = dataType.GetFacetValue <NumericScaleFacet, int>(-1); if (scale >= 0 && scale <= DataGenerationUtilities.MaxNumericScale) { hints.Add(DataGenerationHints.NumericScale(scale)); } bool isUnicode = dataType.GetFacetValue <IsUnicodeFacet, bool>(true); if (!isUnicode) { hints.Add(DataGenerationHints.AnsiString); } int timePrecision = dataType.GetFacetValue <TimePrecisionFacet, int>(-1); if (timePrecision >= 0 && timePrecision <= DataGenerationUtilities.MaxFractionalSeconds) { hints.Add(DataGenerationHints.FractionalSeconds(timePrecision)); } else { timePrecision = DataGenerationUtilities.MaxFractionalSeconds; } if (dataType is TimeOfDayDataType) { TimeSpan maxValue = new TimeSpan(23, 59, 59); long factor = (long)Math.Pow(10, DataGenerationUtilities.MaxFractionalSeconds - timePrecision); maxValue += new TimeSpan(((TimeSpan.TicksPerSecond - 1) / factor) * factor); hints.Add(DataGenerationHints.MaxValue <TimeSpan>(maxValue)); hints.Add(DataGenerationHints.MinValue <TimeSpan>(TimeSpan.Zero)); } return(hints); }
private static int GetCommonPrecision(PrimitiveDataType type1, PrimitiveDataType type2) { // Determine the precision using the following formula: max(s1, s2) + max(p1-s1, p2-s2) int scale1 = type1.GetFacetValue <NumericScaleFacet, int>(0); int scale2 = type2.GetFacetValue <NumericScaleFacet, int>(0); int scale = Math.Max(scale1, scale2); int precision1 = type1.GetFacetValue <NumericPrecisionFacet, int>(GetMaxPrecision(type1)); int precision2 = type2.GetFacetValue <NumericPrecisionFacet, int>(GetMaxPrecision(type2)); return(scale + Math.Max(precision1 - scale1, precision2 - scale2)); }
/// <summary> /// Returns the full EDM name (namespace and type name) for the specified primitive type. /// </summary> /// <param name="primitiveDataType">The primitive type to get the full name for.</param> /// <returns>The full EDM name, namespace and type name for the specified <paramref name="primitiveDataType"/>.</returns> public static string FullEdmName(this PrimitiveDataType primitiveDataType) { ExceptionUtilities.CheckArgumentNotNull(primitiveDataType, "primitiveDataType"); string edmTypeName = primitiveDataType.GetFacetValue <EdmTypeNameFacet, string>(null); string edmNamespace = primitiveDataType.GetFacetValue <EdmNamespaceFacet, string>(null); if (!string.IsNullOrEmpty(edmTypeName) && !string.IsNullOrEmpty(edmNamespace)) { edmTypeName = edmNamespace + '.' + edmTypeName; } return(edmTypeName); }
/// <summary> /// Resolves hints for data generation for the given primitive type. /// </summary> /// <param name="dataType">Primitive data type to resolve data generator for.</param> /// <returns>Data generation hints.</returns> public IEnumerable<DataGenerationHint> ResolveDataGenerationHints(PrimitiveDataType dataType) { var hints = new List<DataGenerationHint>(); int maxLength = dataType.GetFacetValue<MaxLengthFacet, int>(-1); if (maxLength >= 0) { hints.Add(DataGenerationHints.MaxLength(maxLength)); } int precision = dataType.GetFacetValue<NumericPrecisionFacet, int>(-1); if (precision > 0 && precision <= DataGenerationUtilities.MaxNumericPrecision) { hints.Add(DataGenerationHints.NumericPrecision(precision)); } int scale = dataType.GetFacetValue<NumericScaleFacet, int>(-1); if (scale >= 0 && scale <= DataGenerationUtilities.MaxNumericScale) { hints.Add(DataGenerationHints.NumericScale(scale)); } bool isUnicode = dataType.GetFacetValue<IsUnicodeFacet, bool>(true); if (!isUnicode) { hints.Add(DataGenerationHints.AnsiString); } int timePrecision = dataType.GetFacetValue<TimePrecisionFacet, int>(-1); if (timePrecision >= 0 && timePrecision <= DataGenerationUtilities.MaxFractionalSeconds) { hints.Add(DataGenerationHints.FractionalSeconds(timePrecision)); } else { timePrecision = DataGenerationUtilities.MaxFractionalSeconds; } if (dataType is TimeOfDayDataType) { TimeSpan maxValue = new TimeSpan(23, 59, 59); long factor = (long)Math.Pow(10, DataGenerationUtilities.MaxFractionalSeconds - timePrecision); maxValue += new TimeSpan(((TimeSpan.TicksPerSecond - 1) / factor) * factor); hints.Add(DataGenerationHints.MaxValue<TimeSpan>(maxValue)); hints.Add(DataGenerationHints.MinValue<TimeSpan>(TimeSpan.Zero)); } return hints; }
private static void Fixup(NamedStructuralType structuralType, List <NamedStructuralType> visited) { if (visited.Contains(structuralType)) { return; } visited.Add(structuralType); foreach (MemberProperty property in structuralType.Properties) { PrimitiveDataType primitiveDataType = property.PropertyType as PrimitiveDataType; ComplexDataType complexDataType = property.PropertyType as ComplexDataType; if (complexDataType != null) { Fixup(complexDataType.Definition, visited); } else if (primitiveDataType != null) { Type clrType = primitiveDataType.GetFacetValue <PrimitiveClrTypeFacet, Type>(null); ExceptionUtilities.CheckObjectNotNull(clrType, "PrimitiveClrTypeFacet has not been defined for the property: '{0}.{1}'.", structuralType.Name, property.Name); AddDataGenerationHints(property, clrType); property.PropertyType = FixupType(primitiveDataType, clrType); } } }
private PrimitiveDataType CompensatePrimitiveDefaultFacets(PrimitiveDataType inputDataType) { var inputBinary = inputDataType as BinaryDataType; var inputString = inputDataType as StringDataType; if (inputBinary == null && inputString == null) { return(inputDataType); } int?maxLength = null; if (inputDataType.HasFacet <MaxLengthFacet>()) { maxLength = inputDataType.GetFacet <MaxLengthFacet>().Value; } bool isUnicode = inputDataType.GetFacetValue <IsUnicodeFacet, bool>(true); if (inputBinary != null) { return(EdmDataTypes.Binary(maxLength) .Nullable(inputDataType.IsNullable)); } else { return(EdmDataTypes.String(maxLength, isUnicode) .Nullable(inputDataType.IsNullable)); } }
private static int GetCommonScale(PrimitiveDataType type1, PrimitiveDataType type2) { int scale1 = type1.GetFacetValue <NumericScaleFacet, int>(0); int scale2 = type2.GetFacetValue <NumericScaleFacet, int>(0); return(Math.Max(scale1, scale2)); }
private IDataGenerator ResolveNonCollectionDataGenerator(DataType dataType, bool isUnique, IList <DataGenerationHint> dataGenHints) { ComplexDataType complexDataType = dataType as ComplexDataType; if (complexDataType != null) { var complexGenerator = this.GetOrCreateAndRegisterStructuralDataGeneratorForComplexType(complexDataType.Definition); if (dataType.IsNullable) { return(new NullableNamedValuesGeneratorProxy(complexGenerator, this.Random, dataGenHints)); } return(complexGenerator); } EnumDataType enumDataType = dataType as EnumDataType; if (enumDataType != null) { return(this.GetOrCreateAndRegisterNonCollectionDataGeneratorForEnumType(enumDataType, isUnique, dataGenHints)); } PrimitiveDataType primitiveDataType = dataType as PrimitiveDataType; SpatialDataType spatialType = dataType as SpatialDataType; if (primitiveDataType == null) { throw new TaupoNotSupportedException( string.Format(CultureInfo.InvariantCulture, "Data generator creation is not supported for this data type: '{0}'.", dataType.ToString())); } else if (spatialType != null) { ExceptionUtilities.CheckObjectNotNull( this.SpatialDataGeneratorResolver, "Cannot generate value for spatial data type '{0}' without SpatialDataGeneratorResolver being set", dataType); var isUniqueHint = dataGenHints.OfType <AllUniqueHint>().SingleOrDefault(); if (isUniqueHint != null) { isUnique = true; } return(this.SpatialDataGeneratorResolver.GetDataGenerator(spatialType, isUnique, this.Random, dataGenHints.ToArray())); } else { Type clrType = null; bool isNullable = true; clrType = primitiveDataType.GetFacetValue <PrimitiveClrTypeFacet, Type>(null); ExceptionUtilities.CheckObjectNotNull(clrType, "Facet of type '{0}' not defined on a property type '{1}'.", typeof(PrimitiveClrTypeFacet).Name, dataType); isNullable = primitiveDataType.IsNullable; return(this.ResolvePrimitiveDataGeneratorBasedOnClrType(clrType, isUnique, isNullable, dataGenHints)); } }
/// <summary> /// Returns the type name defined. By defualt returns the value of EdmTypeNameFacet. If it is not defined, returns the vlaue of /// SpatialShapeKindFacet or UnqualifiedDataBaseTypeNameFacet. If none is defined, returns an empty string. /// </summary> /// <param name="spatialDataType">The spatial data type</param> /// <returns>the type name defined in the spatial type.</returns> public static string GetSpatialTypeName(PrimitiveDataType spatialDataType) { ExceptionUtilities.CheckArgumentNotNull(spatialDataType, "spatialDataType"); string dataTypeName = spatialDataType.GetFacetValue <EdmTypeNameFacet, string>(string.Empty); if (string.IsNullOrEmpty(dataTypeName)) { var shapeKind = spatialDataType.GetFacetValue <SpatialShapeKindFacet, SpatialShapeKind>(SpatialShapeKind.Unspecified); dataTypeName = (shapeKind == SpatialShapeKind.Unspecified) ? spatialDataType.GetFacetValue <UnqualifiedDatabaseTypeNameFacet, string>(string.Empty) : shapeKind.ToString(); } ExceptionUtilities.Assert( !string.IsNullOrEmpty(dataTypeName), "the data type is not supported as spatial data type."); return(dataTypeName); }
/// <summary> /// Initializes a new instance of the QueryMappedScalarType class. /// </summary> /// <param name="modelType">The model type.</param> /// <param name="storeType">The store type.</param> /// <param name="evaluationStrategy">The evaluation strategy.</param> public QueryMappedScalarType(PrimitiveDataType modelType, PrimitiveDataType storeType, IQueryEvaluationStrategy evaluationStrategy) : base(evaluationStrategy) { ExceptionUtilities.CheckArgumentNotNull(modelType, "modelType"); ExceptionUtilities.CheckArgumentNotNull(storeType, "storeType"); this.ModelType = modelType; this.StoreType = storeType; var clrType = modelType.GetFacetValue <PrimitiveClrTypeFacet, Type>(null); if (clrType != null && modelType.IsNullable && clrType.IsValueType()) { clrType = typeof(Nullable <>).MakeGenericType(clrType); } this.ClrType = clrType; }
private static int GetCommonPrecision(PrimitiveDataType type1, PrimitiveDataType type2) { // Determine the precision using the following formula: max(s1, s2) + max(p1-s1, p2-s2) int scale1 = type1.GetFacetValue<NumericScaleFacet, int>(0); int scale2 = type2.GetFacetValue<NumericScaleFacet, int>(0); int scale = Math.Max(scale1, scale2); int precision1 = type1.GetFacetValue<NumericPrecisionFacet, int>(GetMaxPrecision(type1)); int precision2 = type2.GetFacetValue<NumericPrecisionFacet, int>(GetMaxPrecision(type2)); return scale + Math.Max(precision1 - scale1, precision2 - scale2); }
/// <summary> /// Creates a data generator for the given data type from the repository. /// </summary> /// <param name="dataType">The data type from the repository to get data generator for.</param> /// <param name="random">The random number generator.</param> /// <param name="isUnique">The value indicating whether the generator should produce unique data.</param> /// <returns>The data generator for the given data type.</returns> public virtual IDataGenerator CreateDataGenerator(PrimitiveDataType dataType, IRandomNumberGenerator random, bool isUnique) { ExceptionUtilities.CheckArgumentNotNull(dataType, "dataType"); ExceptionUtilities.CheckArgumentNotNull(random, "random"); if (this.primitiveDataTypes == null) { this.primitiveDataTypes = new List<PrimitiveDataType>(); this.primitiveDataTypes.AddRange(this.GetPrimitiveDataTypesOverride()); } if (!this.primitiveDataTypes.Contains(dataType)) { throw new TaupoInvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Given type does not belong to this repository. Type: '{0}'.", dataType.ToString())); } var enumDataType = dataType as EnumDataType; var clrType = enumDataType != null ? enumDataType.Definition.UnderlyingType : dataType.GetFacetValue<PrimitiveClrTypeFacet, Type>(null); ExceptionUtilities.CheckObjectNotNull(clrType, "{0} has to be defined on the type: '{1}'.", enumDataType != null ? "UnderlyingType" : "PrimitiveClrTypeFacet", dataType); if (dataType.IsNullable && clrType.IsValueType() && !(clrType.IsGenericType() && clrType.GetGenericTypeDefinition() == typeof(Nullable<>))) { clrType = typeof(Nullable<>).MakeGenericType(clrType); } var hints = this.DataGenerationHintsResolver.ResolveDataGenerationHints(dataType); return this.ResolveDataGenerator(clrType, random, isUnique, hints.ToArray()); }
/// <summary> /// Creates a data generator for the given data type from the repository. /// </summary> /// <param name="dataType">The data type from the repository to get data generator for.</param> /// <param name="random">The random number generator.</param> /// <param name="isUnique">The value indicating whether the generator should produce unique data.</param> /// <returns>The data generator for the given data type.</returns> public virtual IDataGenerator CreateDataGenerator(PrimitiveDataType dataType, IRandomNumberGenerator random, bool isUnique) { ExceptionUtilities.CheckArgumentNotNull(dataType, "dataType"); ExceptionUtilities.CheckArgumentNotNull(random, "random"); if (this.primitiveDataTypes == null) { this.primitiveDataTypes = new List <PrimitiveDataType>(); this.primitiveDataTypes.AddRange(this.GetPrimitiveDataTypesOverride()); } if (!this.primitiveDataTypes.Contains(dataType)) { throw new TaupoInvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Given type does not belong to this repository. Type: '{0}'.", dataType.ToString())); } var enumDataType = dataType as EnumDataType; var clrType = enumDataType != null ? enumDataType.Definition.UnderlyingType : dataType.GetFacetValue <PrimitiveClrTypeFacet, Type>(null); ExceptionUtilities.CheckObjectNotNull(clrType, "{0} has to be defined on the type: '{1}'.", enumDataType != null ? "UnderlyingType" : "PrimitiveClrTypeFacet", dataType); if (dataType.IsNullable && clrType.IsValueType() && !(clrType.IsGenericType() && clrType.GetGenericTypeDefinition() == typeof(Nullable <>))) { clrType = typeof(Nullable <>).MakeGenericType(clrType); } var hints = this.DataGenerationHintsResolver.ResolveDataGenerationHints(dataType); return(this.ResolveDataGenerator(clrType, random, isUnique, hints.ToArray())); }
private PrimitiveDataType CompensatePrimitiveDefaultFacets(PrimitiveDataType inputDataType) { var inputBinary = inputDataType as BinaryDataType; var inputString = inputDataType as StringDataType; if (inputBinary == null && inputString == null) { return inputDataType; } int? maxLength = null; if (inputDataType.HasFacet<MaxLengthFacet>()) { maxLength = inputDataType.GetFacet<MaxLengthFacet>().Value; } bool isUnicode = inputDataType.GetFacetValue<IsUnicodeFacet, bool>(true); if (inputBinary != null) { return EdmDataTypes.Binary(maxLength) .Nullable(inputDataType.IsNullable); } else { return EdmDataTypes.String(maxLength, isUnicode) .Nullable(inputDataType.IsNullable); } }
private static int GetCommonScale(PrimitiveDataType type1, PrimitiveDataType type2) { int scale1 = type1.GetFacetValue<NumericScaleFacet, int>(0); int scale2 = type2.GetFacetValue<NumericScaleFacet, int>(0); return Math.Max(scale1, scale2); }
private bool DoesClrTypeMatch(PrimitiveDataType primitiveDataType, Type clrType) { var clrTypeFromDataType = primitiveDataType.GetFacetValue <PrimitiveClrTypeFacet, Type>(null); return(clrType == clrTypeFromDataType); }