From e314d9a5acfd85c5beaff540bfce844084caec85 Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Thu, 3 Dec 2020 00:19:32 +0000 Subject: [PATCH 1/4] update similar files, merge and refomat shared files --- .../src/Microsoft.Data.SqlClient.csproj | 12 +- .../Data/SqlClient/Server/SqlNorm.cs | 43 +- .../Microsoft/Data/SqlClient/Server/SqlSer.cs | 20 +- .../Data/SqlClient/Server/ValueUtilsSmi.cs | 1 - .../netfx/src/Microsoft.Data.SqlClient.csproj | 34 +- .../Data/SqlClient/Server/SqlMetaData.cs | 1925 ----------------- .../Data/SqlClient/Server/SqlRecordBuffer.cs | 615 ------ .../SqlUserDefinedAggregateAttribute.cs | 140 -- .../Data/SqlClient/Server/ValueUtilsSmi.cs | 213 +- .../Microsoft/Data/SqlClient/Server/sqlser.cs | 128 +- .../Data/SqlClient/Server/SqlMetaData.cs | 1438 +++++++----- .../Data/SqlClient/Server/SqlRecordBuffer.cs | 0 .../SqlUserDefinedAggregateAttribute.cs | 20 +- 13 files changed, 1072 insertions(+), 3517 deletions(-) delete mode 100644 src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs delete mode 100644 src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs delete mode 100644 src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs (57%) rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs (100%) rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs (61%) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index e46d123395..e59a292f78 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -117,12 +117,18 @@ Microsoft\Data\SqlClient\Server\SqlFunctionAttribute.cs + + Microsoft\Data\SqlClient\Server\SqlMetaData.cs + Microsoft\Data\SqlClient\Server\SqlMethodAttribute.cs Microsoft\Data\SqlClient\Server\SqlUserDefinedTypeAttribute.cs + + Microsoft\Data\SqlClient\Server\SqlUserDefinedAggregateAttribute.cs + Microsoft\Data\SqlClient\ColumnEncryptionKeyInfo.cs @@ -237,6 +243,9 @@ Microsoft\Data\SqlClient\Server\InvalidUdtException.cs + + Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs + Microsoft\Data\SqlClient\SignatureVerificationCache.cs @@ -307,7 +316,6 @@ - @@ -364,10 +372,8 @@ - - diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs index 9ab047679c..d987f7a2b7 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs @@ -16,32 +16,34 @@ namespace Microsoft.Data.SqlClient.Server // a particular field. internal sealed class FieldInfoEx : IComparable { - internal readonly int Offset; - internal readonly FieldInfo FieldInfo; - internal readonly Normalizer Normalizer; + private readonly int _offset; internal FieldInfoEx(FieldInfo fi, int offset, Normalizer normalizer) { - FieldInfo = fi; - Offset = offset; Debug.Assert(normalizer != null, "normalizer argument should not be null!"); Normalizer = normalizer; + FieldInfo = fi; + _offset = offset; } + public FieldInfo FieldInfo { get; private set; } + public Normalizer Normalizer { get; private set; } // Sort fields by field offsets. public int CompareTo(object other) { FieldInfoEx otherF = other as FieldInfoEx; if (otherF == null) + { return -1; - return Offset.CompareTo(otherF.Offset); + } + return _offset.CompareTo(otherF._offset); } } // The most complex normalizer, a udt normalizer internal sealed class BinaryOrderedUdtNormalizer : Normalizer { - internal readonly FieldInfoEx[] FieldsToNormalize; + private readonly FieldInfoEx[] _fieldsToNormalize; private int _size; private byte[] _padBuffer; internal readonly object NullInstance; @@ -69,18 +71,18 @@ internal BinaryOrderedUdtNormalizer(Type t, bool isTopLevelUdt) // get all the fields FieldInfo[] fields = GetFields(t); - FieldsToNormalize = new FieldInfoEx[fields.Length]; + _fieldsToNormalize = new FieldInfoEx[fields.Length]; int i = 0; foreach (FieldInfo fi in fields) { int offset = Marshal.OffsetOf(fi.DeclaringType, fi.Name).ToInt32(); - FieldsToNormalize[i++] = new FieldInfoEx(fi, offset, GetNormalizer(fi.FieldType)); + _fieldsToNormalize[i++] = new FieldInfoEx(fi, offset, GetNormalizer(fi.FieldType)); } //sort by offset - Array.Sort(FieldsToNormalize); + Array.Sort(_fieldsToNormalize); //if this is not a top-level udt, do setup for null values. //null values need to compare less than all other values, //so prefix a null byte indicator. @@ -138,8 +140,10 @@ private object DeNormalizeInternal(Type t, Stream s) } } if (result == null) + { result = Activator.CreateInstance(t); - foreach (FieldInfoEx myField in FieldsToNormalize) + } + foreach (FieldInfoEx myField in _fieldsToNormalize) { myField.Normalizer.DeNormalize(myField.FieldInfo, result, s); } @@ -173,7 +177,7 @@ internal override void Normalize(FieldInfo fi, object obj, Stream s) } } - foreach (FieldInfoEx myField in FieldsToNormalize) + foreach (FieldInfoEx myField in _fieldsToNormalize) { myField.Normalizer.Normalize(myField.FieldInfo, inner, s); } @@ -189,10 +193,14 @@ internal override int Size get { if (_size != 0) + { return _size; + } if (IsNullable && !_isTopLevelUdt) + { _size = 1; - foreach (FieldInfoEx myField in FieldsToNormalize) + } + foreach (FieldInfoEx myField in _fieldsToNormalize) { _size += myField.Normalizer.Size; } @@ -238,7 +246,9 @@ internal static Normalizer GetNormalizer(Type t) n = new BinaryOrderedUdtNormalizer(t, false); } if (n == null) + { throw new Exception(StringsHelper.GetString(Strings.SQL_CannotCreateNormalizer, t.FullName)); + } n._skipNormalize = false; return n; } @@ -250,15 +260,14 @@ internal static Normalizer GetNormalizer(Type t) protected void FlipAllBits(byte[] b) { for (int i = 0; i < b.Length; i++) + { b[i] = (byte)~b[i]; + } } protected object GetValue(FieldInfo fi, object obj) => fi.GetValue(obj); - protected void SetValue(FieldInfo fi, object recvr, object value) - { - fi.SetValue(recvr, value); - } + protected void SetValue(FieldInfo fi, object recvr, object value) => fi.SetValue(recvr, value); internal abstract int Size { get; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs index a0bbc2a463..df063c99e1 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs @@ -197,10 +197,7 @@ internal NormalizedSerializer(Type t) : base(t) _maxSize = _normalizer.Size; } - public override void Serialize(Stream s, object o) - { - _normalizer.NormalizeTopObject(o, s); - } + public override void Serialize(Stream s, object o) => _normalizer.NormalizeTopObject(o, s); public override object Deserialize(Stream s) => _normalizer.DeNormalizeTopObject(_type, s); } @@ -253,22 +250,13 @@ private void DontDoIt() public override long Position { - get - { - return _size; - } - set - { - _size = value; - } + get => _size; + set =>_size = value; } public override long Length => _size; - public override void SetLength(long value) - { - _size = value; - } + public override void SetLength(long value) => _size = value; public override long Seek(long value, SeekOrigin loc) { diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs index 43a71f0eb5..cdf0802ef3 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs @@ -2729,7 +2729,6 @@ private static void SetUdt_LengthChecked(SmiEventSink_Default sink, ITypedSetter } } - // // Semantics support routines // diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index 8096839c85..cb0c9d0d13 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -172,12 +172,21 @@ Microsoft\Data\SqlClient\Server\SqlFunctionAttribute.cs + + Microsoft\Data\SqlClient\Server\SqlMetaData.cs + Microsoft\Data\SqlClient\Server\SqlMethodAttribute.cs Microsoft\Data\SqlClient\Server\SqlUserDefinedTypeAttribute.cs + + Microsoft\Data\SqlClient\Server\SqlUserDefinedAggregateAttribute.cs + + + Microsoft\Data\SqlClient\Server\SqlRecordBuffer.cs + Microsoft\Data\SqlClient\ColumnEncryptionKeyInfo.cs @@ -331,8 +340,6 @@ - - @@ -355,10 +362,16 @@ - - + + Component + + + Component + - + + Component + @@ -366,7 +379,9 @@ - + + Component + @@ -432,7 +447,9 @@ - + + Component + @@ -461,7 +478,6 @@ - @@ -535,4 +551,4 @@ - \ No newline at end of file + diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs deleted file mode 100644 index a3dd3edc4f..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs +++ /dev/null @@ -1,1925 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Data; -using System.Data.SqlTypes; -using System.Globalization; -using Microsoft.Data.Common; - -namespace Microsoft.Data.SqlClient.Server -{ - /// - // class SqlMetaData - // Simple immutable implementation of the a metadata-holding class. Only - // complexities are: - // 1) enforcing immutability. - // 2) Inferring type from a value. - // 3) Adjusting a value to match the metadata. - public sealed class SqlMetaData - { - private string m_strName; - private long m_lMaxLength; - private SqlDbType m_sqlDbType; - private byte m_bPrecision; - private byte m_bScale; - private long m_lLocale; - private SqlCompareOptions m_eCompareOptions; - private string m_XmlSchemaCollectionDatabase; - private string m_XmlSchemaCollectionOwningSchema; - private string m_XmlSchemaCollectionName; - private string m_serverTypeName; - private bool m_bPartialLength; - private Type m_udttype; - private bool m_useServerDefault; - private bool m_isUniqueKey; - private SortOrder m_columnSortOrder; - private int m_sortOrdinal; - - // unlimited (except by implementation) max-length. - private const long x_lMax = -1; - private const long x_lServerMaxUnicode = 4000; - private const long x_lServerMaxANSI = 8000; - private const long x_lServerMaxBinary = 8000; - private const bool x_defaultUseServerDefault = false; - private const bool x_defaultIsUniqueKey = false; - private const SortOrder x_defaultColumnSortOrder = SortOrder.Unspecified; - private const int x_defaultSortOrdinal = -1; - - private const SqlCompareOptions x_eDefaultStringCompareOptions = SqlCompareOptions.IgnoreCase - | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth; - - /// - // scalar types constructor without tvp extended properties - public SqlMetaData(String name, SqlDbType dbType) - { - Construct(name, dbType, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); - } - - /// - // scalar types constructor - public SqlMetaData(String name, SqlDbType dbType, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// - // binary or string constructor with only max length - // (for string types, locale and compare options will be picked up from the thread. - public SqlMetaData(String name, SqlDbType dbType, long maxLength) - { - Construct(name, dbType, maxLength, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); - } - - /// - // binary or string constructor with only max length and tvp extended properties - // (for string types, locale and compare options will be picked up from the thread. - public SqlMetaData(String name, SqlDbType dbType, long maxLength, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, maxLength, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// - // udt ctor without tvp extended properties - public SqlMetaData(String name, SqlDbType dbType, Type userDefinedType) - { - Construct(name, dbType, userDefinedType, null, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); - } - - /// - // udt ctor without tvp extended properties - public SqlMetaData(String name, SqlDbType dbType, Type userDefinedType, string serverTypeName) - { - Construct(name, dbType, userDefinedType, serverTypeName, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); - } - - /// - // udt ctor - public SqlMetaData(String name, SqlDbType dbType, Type userDefinedType, string serverTypeName, - bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, userDefinedType, serverTypeName, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// - // decimal ctor without tvp extended properties - public SqlMetaData(String name, SqlDbType dbType, byte precision, byte scale) - { - Construct(name, dbType, precision, scale, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); - } - - /// - // decimal ctor - public SqlMetaData(string name, SqlDbType dbType, byte precision, byte scale, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, precision, scale, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// - // string type constructor with locale and compare options, no tvp extended properties - public SqlMetaData(String name, SqlDbType dbType, long maxLength, long locale, - SqlCompareOptions compareOptions) - { - Construct(name, dbType, maxLength, locale, compareOptions, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); - } - - /// - // string type constructor with locale and compare options - public SqlMetaData(String name, SqlDbType dbType, long maxLength, long locale, - SqlCompareOptions compareOptions, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, maxLength, locale, compareOptions, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// - // typed xml ctor - public SqlMetaData(String name, SqlDbType dbType, string database, string owningSchema, - string objectName, bool useServerDefault, bool isUniqueKey, - SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, database, owningSchema, objectName, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// - // everything except xml schema and tvp properties ctor - public SqlMetaData(String name, SqlDbType dbType, long maxLength, byte precision, - byte scale, long locale, SqlCompareOptions compareOptions, - Type userDefinedType) : - this(name, dbType, maxLength, precision, scale, locale, compareOptions, - userDefinedType, x_defaultUseServerDefault, x_defaultIsUniqueKey, - x_defaultColumnSortOrder, x_defaultSortOrdinal) - { - } - - /// - // everything except xml schema ctor - public SqlMetaData(String name, SqlDbType dbType, long maxLength, byte precision, - byte scale, long localeId, SqlCompareOptions compareOptions, - Type userDefinedType, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - switch (dbType) - { - case SqlDbType.BigInt: - case SqlDbType.Image: - case SqlDbType.Timestamp: - case SqlDbType.Bit: - case SqlDbType.DateTime: - case SqlDbType.SmallDateTime: - case SqlDbType.Real: - case SqlDbType.Int: - case SqlDbType.Money: - case SqlDbType.SmallMoney: - case SqlDbType.Float: - case SqlDbType.UniqueIdentifier: - case SqlDbType.SmallInt: - case SqlDbType.TinyInt: - case SqlDbType.Xml: - case SqlDbType.Date: - Construct(name, dbType, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); - break; - case SqlDbType.Binary: - case SqlDbType.VarBinary: - Construct(name, dbType, maxLength, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); - break; - case SqlDbType.Char: - case SqlDbType.NChar: - case SqlDbType.NVarChar: - case SqlDbType.VarChar: - Construct(name, dbType, maxLength, localeId, compareOptions, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); - break; - case SqlDbType.NText: - case SqlDbType.Text: - // SQLBU # : We should ignore user's max length and use Max instead to avoid exception - Construct(name, dbType, Max, localeId, compareOptions, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); - break; - case SqlDbType.Decimal: - case SqlDbType.Time: - case SqlDbType.DateTime2: - case SqlDbType.DateTimeOffset: - Construct(name, dbType, precision, scale, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); - break; - case SqlDbType.Variant: - Construct(name, dbType, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); - break; - case SqlDbType.Udt: - Construct(name, dbType, userDefinedType, "", useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); - break; - default: - SQL.InvalidSqlDbTypeForConstructor(dbType); - break; - } - } - - /// - public SqlMetaData(String name, SqlDbType dbType, string database, string owningSchema, string objectName) - { - Construct(name, dbType, database, owningSchema, objectName, x_defaultUseServerDefault, x_defaultIsUniqueKey, - x_defaultColumnSortOrder, x_defaultSortOrdinal); - } - - // Most general constructor, should be able to initialize all SqlMetaData fields.(Used by SqlParameter) - internal SqlMetaData(String name, - SqlDbType sqlDBType, - long maxLength, - byte precision, - byte scale, - long localeId, - SqlCompareOptions compareOptions, - string xmlSchemaCollectionDatabase, - string xmlSchemaCollectionOwningSchema, - string xmlSchemaCollectionName, - bool partialLength, - Type udtType) - { - AssertNameIsValid(name); - - m_strName = name; - m_sqlDbType = sqlDBType; - m_lMaxLength = maxLength; - m_bPrecision = precision; - m_bScale = scale; - m_lLocale = localeId; - m_eCompareOptions = compareOptions; - m_XmlSchemaCollectionDatabase = xmlSchemaCollectionDatabase; - m_XmlSchemaCollectionOwningSchema = xmlSchemaCollectionOwningSchema; - m_XmlSchemaCollectionName = xmlSchemaCollectionName; - m_bPartialLength = partialLength; - - m_udttype = udtType; - } - - // Private constructor used to initialize default instance array elements. - // DO NOT EXPOSE OUTSIDE THIS CLASS! It performs no validation. - private SqlMetaData(String name, - SqlDbType sqlDbType, - long maxLength, - byte precision, - byte scale, - long localeId, - SqlCompareOptions compareOptions, - bool partialLength) - { - - AssertNameIsValid(name); - - m_strName = name; - m_sqlDbType = sqlDbType; - m_lMaxLength = maxLength; - m_bPrecision = precision; - m_bScale = scale; - m_lLocale = localeId; - m_eCompareOptions = compareOptions; - m_bPartialLength = partialLength; - m_udttype = null; - } - - /// - public SqlCompareOptions CompareOptions - { - get - { - return m_eCompareOptions; - } - } - - /// - public DbType DbType - { - get - { - return sxm_rgSqlDbTypeToDbType[(int)m_sqlDbType]; - } - } - - /// - public bool IsUniqueKey - { - get - { - return m_isUniqueKey; - } - } - - /// - public long LocaleId - { - get - { - return m_lLocale; - } - } - - /// - public static long Max - { - get - { - return x_lMax; - } - } - - /// - public long MaxLength - { - get - { - return m_lMaxLength; - } - } - - /// - public string Name - { - get - { - return m_strName; - } - } - - /// - public byte Precision - { - get - { - return m_bPrecision; - } - } - - /// - public byte Scale - { - get - { - return m_bScale; - } - } - - /// - public SortOrder SortOrder - { - get - { - return m_columnSortOrder; - } - } - - /// - public int SortOrdinal - { - get - { - return m_sortOrdinal; - } - } - - /// - public SqlDbType SqlDbType - { - get - { - return m_sqlDbType; - } - } - - /// - public Type Type - { - get - { - return m_udttype; - } - } - - /// - public string TypeName - { - get - { - if (m_serverTypeName != null) - { - return m_serverTypeName; - } - else if (SqlDbType == SqlDbType.Udt) - { - return UdtTypeName; - } - else - { - return sxm_rgDefaults[(int)SqlDbType].Name; - } - } - } - - internal string ServerTypeName - { - get - { - return m_serverTypeName; - } - } - - /// - public bool UseServerDefault - { - get - { - return m_useServerDefault; - } - } - - /// - public string XmlSchemaCollectionDatabase - { - get - { - return m_XmlSchemaCollectionDatabase; - } - } - - /// - public string XmlSchemaCollectionName - { - get - { - return m_XmlSchemaCollectionName; - } - } - - /// - public string XmlSchemaCollectionOwningSchema - { - get - { - return m_XmlSchemaCollectionOwningSchema; - } - } - - internal bool IsPartialLength - { - get - { - return m_bPartialLength; - } - } - - internal string UdtTypeName - { - get - { - if (SqlDbType != SqlDbType.Udt) - { - return null; - } - else if (m_udttype == null) - { - return null; - } - else - { - return m_udttype.FullName; - } - } - } - - // Construction for all types that do not have variable attributes - private void Construct(String name, SqlDbType dbType, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - AssertNameIsValid(name); - - ValidateSortOrder(columnSortOrder, sortOrdinal); - - // Check for absence of explicitly-allowed types to avoid unexpected additions when new types are added - if (!(SqlDbType.BigInt == dbType || - SqlDbType.Bit == dbType || - SqlDbType.DateTime == dbType || - SqlDbType.Date == dbType || - SqlDbType.DateTime2 == dbType || - SqlDbType.DateTimeOffset == dbType || - SqlDbType.Decimal == dbType || - SqlDbType.Float == dbType || - SqlDbType.Image == dbType || - SqlDbType.Int == dbType || - SqlDbType.Money == dbType || - SqlDbType.NText == dbType || - SqlDbType.Real == dbType || - SqlDbType.SmallDateTime == dbType || - SqlDbType.SmallInt == dbType || - SqlDbType.SmallMoney == dbType || - SqlDbType.Text == dbType || - SqlDbType.Time == dbType || - SqlDbType.Timestamp == dbType || - SqlDbType.TinyInt == dbType || - SqlDbType.UniqueIdentifier == dbType || - SqlDbType.Variant == dbType || - SqlDbType.Xml == dbType)) - throw SQL.InvalidSqlDbTypeForConstructor(dbType); - - SetDefaultsForType(dbType); - - if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) - { - m_lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID; - } - - - m_strName = name; - m_useServerDefault = useServerDefault; - m_isUniqueKey = isUniqueKey; - m_columnSortOrder = columnSortOrder; - m_sortOrdinal = sortOrdinal; - } - - // Construction for all types that vary by user-specified length (not Udts) - private void Construct(String name, SqlDbType dbType, long maxLength, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - AssertNameIsValid(name); - - ValidateSortOrder(columnSortOrder, sortOrdinal); - - long lLocale = 0; - if (SqlDbType.Char == dbType) - { - if (maxLength > x_lServerMaxANSI || maxLength < 0) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID; - } - else if (SqlDbType.VarChar == dbType) - { - if ((maxLength > x_lServerMaxANSI || maxLength < 0) && maxLength != Max) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID; - } - else if (SqlDbType.NChar == dbType) - { - if (maxLength > x_lServerMaxUnicode || maxLength < 0) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID; - } - else if (SqlDbType.NVarChar == dbType) - { - if ((maxLength > x_lServerMaxUnicode || maxLength < 0) && maxLength != Max) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID; - } - else if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) - { - // old-style lobs only allowed with Max length - if (SqlMetaData.Max != maxLength) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID; - } - else if (SqlDbType.Binary == dbType) - { - if (maxLength > x_lServerMaxBinary || maxLength < 0) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else if (SqlDbType.VarBinary == dbType) - { - if ((maxLength > x_lServerMaxBinary || maxLength < 0) && maxLength != Max) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else if (SqlDbType.Image == dbType) - { - // old-style lobs only allowed with Max length - if (SqlMetaData.Max != maxLength) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else - throw SQL.InvalidSqlDbTypeForConstructor(dbType); - - SetDefaultsForType(dbType); - - m_strName = name; - m_lMaxLength = maxLength; - m_lLocale = lLocale; - m_useServerDefault = useServerDefault; - m_isUniqueKey = isUniqueKey; - m_columnSortOrder = columnSortOrder; - m_sortOrdinal = sortOrdinal; - } - - // Construction for string types with specified locale/compare options - private void Construct(String name, - SqlDbType dbType, - long maxLength, - long locale, - SqlCompareOptions compareOptions, - bool useServerDefault, - bool isUniqueKey, - SortOrder columnSortOrder, - int sortOrdinal) - { - AssertNameIsValid(name); - - ValidateSortOrder(columnSortOrder, sortOrdinal); - - // Validate type and max length. - if (SqlDbType.Char == dbType) - { - if (maxLength > x_lServerMaxANSI || maxLength < 0) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else if (SqlDbType.VarChar == dbType) - { - if ((maxLength > x_lServerMaxANSI || maxLength < 0) && maxLength != Max) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else if (SqlDbType.NChar == dbType) - { - if (maxLength > x_lServerMaxUnicode || maxLength < 0) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else if (SqlDbType.NVarChar == dbType) - { - if ((maxLength > x_lServerMaxUnicode || maxLength < 0) && maxLength != Max) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) - { - // old-style lobs only allowed with Max length - if (SqlMetaData.Max != maxLength) - throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength"); - } - else - throw SQL.InvalidSqlDbTypeForConstructor(dbType); - - // Validate locale? - - // Validate compare options - // Binary sort must be by itself. - // Nothing else but the Ignore bits is allowed. - if (SqlCompareOptions.BinarySort != compareOptions && - 0 != (~((int)SqlCompareOptions.IgnoreCase | (int)SqlCompareOptions.IgnoreNonSpace | - (int)SqlCompareOptions.IgnoreKanaType | (int)SqlCompareOptions.IgnoreWidth) & - (int)compareOptions)) - throw ADP.InvalidEnumerationValue(typeof(SqlCompareOptions), (int)compareOptions); - - SetDefaultsForType(dbType); - - m_strName = name; - m_lMaxLength = maxLength; - m_lLocale = locale; - m_eCompareOptions = compareOptions; - m_useServerDefault = useServerDefault; - m_isUniqueKey = isUniqueKey; - m_columnSortOrder = columnSortOrder; - m_sortOrdinal = sortOrdinal; - } - - - private static byte[] __maxLenFromPrecision = new byte[] {5,5,5,5,5,5,5,5,5,9,9,9,9,9, - 9,9,9,9,9,13,13,13,13,13,13,13,13,13,17,17,17,17,17,17,17,17,17,17}; - - private const byte MaxTimeScale = 7; - - private static byte[] __maxVarTimeLenOffsetFromScale = new byte[] { 2, 2, 2, 1, 1, 0, 0, 0 }; - - // Construction for Decimal type and new Katmai Date/Time types - private void Construct(String name, SqlDbType dbType, byte precision, byte scale, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - AssertNameIsValid(name); - - ValidateSortOrder(columnSortOrder, sortOrdinal); - - if (SqlDbType.Decimal == dbType) - { - if (precision > SqlDecimal.MaxPrecision || scale > precision) - throw SQL.PrecisionValueOutOfRange(precision); - - if (scale > SqlDecimal.MaxScale) - throw SQL.ScaleValueOutOfRange(scale); - } - else if (SqlDbType.Time == dbType || SqlDbType.DateTime2 == dbType || SqlDbType.DateTimeOffset == dbType) - { - if (scale > MaxTimeScale) - { - throw SQL.TimeScaleValueOutOfRange(scale); - } - } - else - { - throw SQL.InvalidSqlDbTypeForConstructor(dbType); - } - SetDefaultsForType(dbType); - - m_strName = name; - m_bPrecision = precision; - m_bScale = scale; - if (SqlDbType.Decimal == dbType) - { - m_lMaxLength = __maxLenFromPrecision[precision - 1]; - } - else - { - m_lMaxLength -= __maxVarTimeLenOffsetFromScale[scale]; - } - m_useServerDefault = useServerDefault; - m_isUniqueKey = isUniqueKey; - m_columnSortOrder = columnSortOrder; - m_sortOrdinal = sortOrdinal; - } - - // Construction for Udt type - private void Construct(String name, SqlDbType dbType, Type userDefinedType, string serverTypeName, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - AssertNameIsValid(name); - - ValidateSortOrder(columnSortOrder, sortOrdinal); - - if (SqlDbType.Udt != dbType) - throw SQL.InvalidSqlDbTypeForConstructor(dbType); - - if (null == userDefinedType) - throw ADP.ArgumentNull("userDefinedType"); - - SetDefaultsForType(SqlDbType.Udt); - - m_strName = name; - m_lMaxLength = SerializationHelperSql9.GetUdtMaxLength(userDefinedType); - m_udttype = userDefinedType; - m_serverTypeName = serverTypeName; - m_useServerDefault = useServerDefault; - m_isUniqueKey = isUniqueKey; - m_columnSortOrder = columnSortOrder; - m_sortOrdinal = sortOrdinal; - } - - // Construction for Xml type - private void Construct(String name, SqlDbType dbType, string database, string owningSchema, - string objectName, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, - int sortOrdinal) - { - AssertNameIsValid(name); - - ValidateSortOrder(columnSortOrder, sortOrdinal); - - if (SqlDbType.Xml != dbType) - throw SQL.InvalidSqlDbTypeForConstructor(dbType); - - if (null != database || null != owningSchema) - { - if (null == objectName) - { - throw ADP.ArgumentNull("objectName"); - } - } - - SetDefaultsForType(SqlDbType.Xml); - m_strName = name; - - m_XmlSchemaCollectionDatabase = database; - m_XmlSchemaCollectionOwningSchema = owningSchema; - m_XmlSchemaCollectionName = objectName; - m_useServerDefault = useServerDefault; - m_isUniqueKey = isUniqueKey; - m_columnSortOrder = columnSortOrder; - m_sortOrdinal = sortOrdinal; - } - - private void AssertNameIsValid(string name) - { - if (null == name) - throw ADP.ArgumentNull("name"); - - if (Microsoft.Data.SqlClient.Server.SmiMetaData.MaxNameLength < name.Length) - throw SQL.NameTooLong("name"); - } - - private void ValidateSortOrder(SortOrder columnSortOrder, int sortOrdinal) - { - // Check that sort order is valid enum value. - if (SortOrder.Unspecified != columnSortOrder && - SortOrder.Ascending != columnSortOrder && - SortOrder.Descending != columnSortOrder) - { - throw SQL.InvalidSortOrder(columnSortOrder); - } - - // Must specify both sort order and ordinal, or neither - if ((SortOrder.Unspecified == columnSortOrder) != (x_defaultSortOrdinal == sortOrdinal)) - { - throw SQL.MustSpecifyBothSortOrderAndOrdinal(columnSortOrder, sortOrdinal); - } - } - - /// - public Int16 Adjust(Int16 value) - { - if (SqlDbType.SmallInt != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public Int32 Adjust(Int32 value) - { - if (SqlDbType.Int != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public Int64 Adjust(Int64 value) - { - if (SqlDbType.BigInt != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public float Adjust(float value) - { - if (SqlDbType.Real != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public double Adjust(double value) - { - if (SqlDbType.Float != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public string Adjust(string value) - { - if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) - { - //DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(string): Fixed-length type with Max length!"); - // Don't pad null values - if (null != value) - { - // Pad if necessary - if (value.Length < MaxLength) - value = value.PadRight((int)MaxLength); - } - } - else if (SqlDbType.VarChar != SqlDbType && - SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && - SqlDbType.NText != SqlDbType) - { - ThrowInvalidType(); - } - - // Handle null values after type check - if (null == value) - { - return null; - } - - if (value.Length > MaxLength && Max != MaxLength) - value = value.Remove((int)MaxLength, (int)(value.Length - MaxLength)); - - return value; - } - - /// - public decimal Adjust(decimal value) - { - if (SqlDbType.Decimal != SqlDbType && - SqlDbType.Money != SqlDbType && - SqlDbType.SmallMoney != SqlDbType) - { - ThrowInvalidType(); - } - - if (SqlDbType.Decimal != SqlDbType) - { - VerifyMoneyRange(new SqlMoney(value)); - return value; - } - else - { - SqlDecimal sdValue = InternalAdjustSqlDecimal(new SqlDecimal(value)); - return sdValue.Value; - } - } - - /// - public DateTime Adjust(DateTime value) - { - if (SqlDbType.DateTime == SqlDbType || SqlDbType.SmallDateTime == SqlDbType) - { - VerifyDateTimeRange(value); - } - else if (SqlDbType.DateTime2 == SqlDbType) - { - return new DateTime(InternalAdjustTimeTicks(value.Ticks)); - } - else if (SqlDbType.Date == SqlDbType) - { - return value.Date; - } - else - { - ThrowInvalidType(); - } - return value; - } - - /// - public Guid Adjust(Guid value) - { - if (SqlDbType.UniqueIdentifier != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlBoolean Adjust(SqlBoolean value) - { - if (SqlDbType.Bit != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlByte Adjust(SqlByte value) - { - if (SqlDbType.TinyInt != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlInt16 Adjust(SqlInt16 value) - { - if (SqlDbType.SmallInt != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlInt32 Adjust(SqlInt32 value) - { - if (SqlDbType.Int != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlInt64 Adjust(SqlInt64 value) - { - if (SqlDbType.BigInt != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlSingle Adjust(SqlSingle value) - { - if (SqlDbType.Real != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlDouble Adjust(SqlDouble value) - { - if (SqlDbType.Float != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlMoney Adjust(SqlMoney value) - { - if (SqlDbType.Money != SqlDbType && - SqlDbType.SmallMoney != SqlDbType) - ThrowInvalidType(); - - if (!value.IsNull) - VerifyMoneyRange(value); - - return value; - } - - /// - public SqlDateTime Adjust(SqlDateTime value) - { - if (SqlDbType.DateTime != SqlDbType && - SqlDbType.SmallDateTime != SqlDbType) - ThrowInvalidType(); - - if (!value.IsNull) - VerifyDateTimeRange(value.Value); - - return value; - } - - /// - public SqlDecimal Adjust(SqlDecimal value) - { - if (SqlDbType.Decimal != SqlDbType) - ThrowInvalidType(); - return InternalAdjustSqlDecimal(value); - } - - /// - public SqlString Adjust(SqlString value) - { - if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) - { - //DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(SqlString): Fixed-length type with Max length!"); - // Don't pad null values - if (!value.IsNull) - { - // Pad fixed-length types - if (value.Value.Length < MaxLength) - return new SqlString(value.Value.PadRight((int)MaxLength)); - } - } - else if (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) - ThrowInvalidType(); - - // Handle null values after type check - if (value.IsNull) - { - return value; - } - - // trim all types - if (value.Value.Length > MaxLength && Max != MaxLength) - value = new SqlString(value.Value.Remove((int)MaxLength, (int)(value.Value.Length - MaxLength))); - - return value; - } - - /// - public SqlBinary Adjust(SqlBinary value) - { - if (SqlDbType.Binary == SqlDbType || - SqlDbType.Timestamp == SqlDbType) - { - if (!value.IsNull) - { - // Pad fixed-length types - if (value.Length < MaxLength) - { - byte[] rgbValue = value.Value; - byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(rgbValue, rgbNewValue, rgbValue.Length); - Array.Clear(rgbNewValue, rgbValue.Length, rgbNewValue.Length - rgbValue.Length); - return new SqlBinary(rgbNewValue); - } - } - } - else if (SqlDbType.VarBinary != SqlDbType && - SqlDbType.Image != SqlDbType) - ThrowInvalidType(); - - // Handle null values - if (value.IsNull) - { - return value; - } - - // trim all types - if (value.Length > MaxLength && Max != MaxLength) - { - byte[] rgbValue = value.Value; - byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(rgbValue, rgbNewValue, (int)MaxLength); - value = new SqlBinary(rgbNewValue); - } - - return value; - } - - /// - public SqlGuid Adjust(SqlGuid value) - { - if (SqlDbType.UniqueIdentifier != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public SqlChars Adjust(SqlChars value) - { - if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) - { - //DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(SqlChars): Fixed-length type with Max length!"); - // Don't pad null values - if (null != value && !value.IsNull) - { - // Pad fixed-length types - long oldLength = value.Length; - if (oldLength < MaxLength) - { - // Make sure buffer is long enough - if (value.MaxLength < MaxLength) - { - char[] rgchNew = new char[(int)MaxLength]; - Array.Copy(value.Buffer, rgchNew, (int)oldLength); - value = new SqlChars(rgchNew); - } - - // pad extra space - char[] rgchTemp = value.Buffer; - for (long i = oldLength; i < MaxLength; i++) - rgchTemp[i] = ' '; - - value.SetLength(MaxLength); - return value; - } - } - } - else if (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) - ThrowInvalidType(); - - // Handle null values after type check. - if (null == value || value.IsNull) - { - return value; - } - - // trim all types - if (value.Length > MaxLength && Max != MaxLength) - value.SetLength(MaxLength); - - return value; - } - - /// - public SqlBytes Adjust(SqlBytes value) - { - if (SqlDbType.Binary == SqlDbType || SqlDbType.Timestamp == SqlDbType) - { - //DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(SqlBytes): Fixed-length type with Max length!"); - // Don't pad null values - if (null != value && !value.IsNull) - { - // Pad fixed-length types - int oldLength = (int)value.Length; - if (oldLength < MaxLength) - { - // Make sure buffer is long enough - if (value.MaxLength < MaxLength) - { - byte[] rgbNew = new byte[MaxLength]; - Array.Copy(value.Buffer, rgbNew, (int)oldLength); - value = new SqlBytes(rgbNew); - } - - // pad extra space - byte[] rgbTemp = value.Buffer; - Array.Clear(rgbTemp, oldLength, rgbTemp.Length - oldLength); - value.SetLength(MaxLength); - return value; - } - } - } - else if (SqlDbType.VarBinary != SqlDbType && - SqlDbType.Image != SqlDbType) - ThrowInvalidType(); - - // Handle null values after type check. - if (null == value || value.IsNull) - { - return value; - } - - // trim all types - if (value.Length > MaxLength && Max != MaxLength) - value.SetLength(MaxLength); - - return value; - } - - /// - public SqlXml Adjust(SqlXml value) - { - if (SqlDbType.Xml != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public TimeSpan Adjust(TimeSpan value) - { - if (SqlDbType.Time != SqlDbType) - ThrowInvalidType(); - VerifyTimeRange(value); - return new TimeSpan(InternalAdjustTimeTicks(value.Ticks)); - } - - /// - public DateTimeOffset Adjust(DateTimeOffset value) - { - if (SqlDbType.DateTimeOffset != SqlDbType) - ThrowInvalidType(); - return new DateTimeOffset(InternalAdjustTimeTicks(value.Ticks), value.Offset); - } - - /// - public object Adjust(object value) - { - // Pass null references through - if (null == value) - return null; - - Type dataType = value.GetType(); - switch (Type.GetTypeCode(dataType)) - { - case TypeCode.Boolean: - value = this.Adjust((Boolean)value); - break; - case TypeCode.Byte: - value = this.Adjust((Byte)value); - break; - case TypeCode.Char: - value = this.Adjust((Char)value); - break; - case TypeCode.DateTime: - value = this.Adjust((DateTime)value); - break; - case TypeCode.DBNull: /* DBNull passes through as is for all types */ - break; - case TypeCode.Decimal: - value = this.Adjust((Decimal)value); - break; - case TypeCode.Double: - value = this.Adjust((Double)value); - break; - case TypeCode.Empty: - throw ADP.InvalidDataType(TypeCode.Empty); - case TypeCode.Int16: - value = this.Adjust((Int16)value); - break; - case TypeCode.Int32: - value = this.Adjust((Int32)value); - break; - case TypeCode.Int64: - value = this.Adjust((Int64)value); - break; - case TypeCode.SByte: - throw ADP.InvalidDataType(TypeCode.SByte); - case TypeCode.Single: - value = this.Adjust((Single)value); - break; - case TypeCode.String: - value = this.Adjust((String)value); - break; - case TypeCode.UInt16: - throw ADP.InvalidDataType(TypeCode.UInt16); - case TypeCode.UInt32: - throw ADP.InvalidDataType(TypeCode.UInt32); - case TypeCode.UInt64: - throw ADP.InvalidDataType(TypeCode.UInt64); - case TypeCode.Object: - if (dataType == typeof(System.Byte[])) - value = this.Adjust((System.Byte[])value); - else if (dataType == typeof(System.Char[])) - value = this.Adjust((System.Char[])value); - else if (dataType == typeof(System.Guid)) - value = this.Adjust((System.Guid)value); - else if (dataType == typeof(System.Object)) - { - // - throw ADP.InvalidDataType(TypeCode.UInt64); - } - else if (dataType == typeof(SqlBinary)) - value = this.Adjust((SqlBinary)value); - else if (dataType == typeof(SqlBoolean)) - value = this.Adjust((SqlBoolean)value); - else if (dataType == typeof(SqlByte)) - value = this.Adjust((SqlByte)value); - else if (dataType == typeof(SqlDateTime)) - value = this.Adjust((SqlDateTime)value); - else if (dataType == typeof(SqlDouble)) - value = this.Adjust((SqlDouble)value); - else if (dataType == typeof(SqlGuid)) - value = this.Adjust((SqlGuid)value); - else if (dataType == typeof(SqlInt16)) - value = this.Adjust((SqlInt16)value); - else if (dataType == typeof(SqlInt32)) - value = this.Adjust((SqlInt32)value); - else if (dataType == typeof(SqlInt64)) - value = this.Adjust((SqlInt64)value); - else if (dataType == typeof(SqlMoney)) - value = this.Adjust((SqlMoney)value); - else if (dataType == typeof(SqlDecimal)) - value = this.Adjust((SqlDecimal)value); - else if (dataType == typeof(SqlSingle)) - value = this.Adjust((SqlSingle)value); - else if (dataType == typeof(SqlString)) - value = this.Adjust((SqlString)value); - else if (dataType == typeof(SqlChars)) - value = this.Adjust((SqlChars)value); - else if (dataType == typeof(SqlBytes)) - value = this.Adjust((SqlBytes)value); - else if (dataType == typeof(SqlXml)) - value = this.Adjust((SqlXml)value); - else if (dataType == typeof(TimeSpan)) - value = this.Adjust((TimeSpan)value); - else if (dataType == typeof(DateTimeOffset)) - value = this.Adjust((DateTimeOffset)value); - else - { - // Handle UDTs? - throw ADP.UnknownDataType(dataType); - } - break; - - - default: - throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType)); - } - - return value; - } - - /// - public static SqlMetaData InferFromValue(object value, String name) - { - if (value == null) - throw ADP.ArgumentNull("value"); - SqlMetaData smd = null; - Type dataType = value.GetType(); - switch (Type.GetTypeCode(dataType)) - { - case TypeCode.Boolean: - smd = new SqlMetaData(name, SqlDbType.Bit); - break; - case TypeCode.Byte: - smd = new SqlMetaData(name, SqlDbType.TinyInt); - break; - case TypeCode.Char: - smd = new SqlMetaData(name, SqlDbType.NVarChar, 1); - break; - case TypeCode.DateTime: - smd = new SqlMetaData(name, SqlDbType.DateTime); - break; - case TypeCode.DBNull: - throw ADP.InvalidDataType(TypeCode.DBNull); - case TypeCode.Decimal: - { // Add brackets in order to contain scope declare local variable "sd" - // use logic inside SqlDecimal to infer precision and scale. - SqlDecimal sd = new SqlDecimal((Decimal)value); - smd = new SqlMetaData(name, SqlDbType.Decimal, sd.Precision, sd.Scale); - } - break; - case TypeCode.Double: - smd = new SqlMetaData(name, SqlDbType.Float); - break; - case TypeCode.Empty: - throw ADP.InvalidDataType(TypeCode.Empty); - case TypeCode.Int16: - smd = new SqlMetaData(name, SqlDbType.SmallInt); - break; - case TypeCode.Int32: - smd = new SqlMetaData(name, SqlDbType.Int); - break; - case TypeCode.Int64: - smd = new SqlMetaData(name, SqlDbType.BigInt); - break; - case TypeCode.SByte: - throw ADP.InvalidDataType(TypeCode.SByte); - case TypeCode.Single: - smd = new SqlMetaData(name, SqlDbType.Real); - break; - case TypeCode.String: - { - long maxLen = ((String)value).Length; - if (maxLen < 1) - maxLen = 1; - - if (x_lServerMaxUnicode < maxLen) - maxLen = Max; - - smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen); - } - break; - case TypeCode.UInt16: - throw ADP.InvalidDataType(TypeCode.UInt16); - case TypeCode.UInt32: - throw ADP.InvalidDataType(TypeCode.UInt32); - case TypeCode.UInt64: - throw ADP.InvalidDataType(TypeCode.UInt64); - case TypeCode.Object: - if (dataType == typeof(System.Byte[])) - { - long maxLen = ((System.Byte[])value).Length; - if (maxLen < 1) - maxLen = 1; - - if (x_lServerMaxBinary < maxLen) - maxLen = Max; - - smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen); - } - else if (dataType == typeof(System.Char[])) - { - long maxLen = ((System.Char[])value).Length; - if (maxLen < 1) - maxLen = 1; - - if (x_lServerMaxUnicode < maxLen) - maxLen = Max; - - smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen); - } - else if (dataType == typeof(System.Guid)) - smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier); - else if (dataType == typeof(System.Object)) - smd = new SqlMetaData(name, SqlDbType.Variant); - else if (dataType == typeof(SqlBinary)) - { - long maxLen; - SqlBinary sb = ((SqlBinary)value); - if (!sb.IsNull) - { - maxLen = sb.Length; - if (maxLen < 1) - maxLen = 1; - - if (x_lServerMaxBinary < maxLen) - maxLen = Max; - } - else - maxLen = sxm_rgDefaults[(int)SqlDbType.VarBinary].MaxLength; - - smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen); - } - else if (dataType == typeof(SqlBoolean)) - smd = new SqlMetaData(name, SqlDbType.Bit); - else if (dataType == typeof(SqlByte)) - smd = new SqlMetaData(name, SqlDbType.TinyInt); - else if (dataType == typeof(SqlDateTime)) - smd = new SqlMetaData(name, SqlDbType.DateTime); - else if (dataType == typeof(SqlDouble)) - smd = new SqlMetaData(name, SqlDbType.Float); - else if (dataType == typeof(SqlGuid)) - smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier); - else if (dataType == typeof(SqlInt16)) - smd = new SqlMetaData(name, SqlDbType.SmallInt); - else if (dataType == typeof(SqlInt32)) - smd = new SqlMetaData(name, SqlDbType.Int); - else if (dataType == typeof(SqlInt64)) - smd = new SqlMetaData(name, SqlDbType.BigInt); - else if (dataType == typeof(SqlMoney)) - smd = new SqlMetaData(name, SqlDbType.Money); - else if (dataType == typeof(SqlDecimal)) - { - byte bPrec; - byte scale; - SqlDecimal sd = (SqlDecimal)value; - if (!sd.IsNull) - { - bPrec = sd.Precision; - scale = sd.Scale; - } - else - { - bPrec = sxm_rgDefaults[(int)SqlDbType.Decimal].Precision; - scale = sxm_rgDefaults[(int)SqlDbType.Decimal].Scale; - } - smd = new SqlMetaData(name, SqlDbType.Decimal, bPrec, scale); - } - else if (dataType == typeof(SqlSingle)) - smd = new SqlMetaData(name, SqlDbType.Real); - else if (dataType == typeof(SqlString)) - { - SqlString ss = (SqlString)value; - if (!ss.IsNull) - { - long maxLen = ss.Value.Length; - if (maxLen < 1) - maxLen = 1; - - if (maxLen > x_lServerMaxUnicode) - maxLen = Max; - - smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen, ss.LCID, ss.SqlCompareOptions); - } - else - { - smd = new SqlMetaData(name, SqlDbType.NVarChar, sxm_rgDefaults[(int)SqlDbType.NVarChar].MaxLength); - } - } - else if (dataType == typeof(SqlChars)) - { - long maxLen; - SqlChars sch = (SqlChars)value; - if (!sch.IsNull) - { - maxLen = sch.Length; - if (maxLen < 1) - maxLen = 1; - - if (maxLen > x_lServerMaxUnicode) - maxLen = Max; - } - else - maxLen = sxm_rgDefaults[(int)SqlDbType.NVarChar].MaxLength; - - smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen); - } - else if (dataType == typeof(SqlBytes)) - { - long maxLen; - SqlBytes sb = (SqlBytes)value; - if (!sb.IsNull) - { - maxLen = sb.Length; - if (maxLen < 1) - maxLen = 1; - else if (x_lServerMaxBinary < maxLen) - maxLen = Max; - } - else - maxLen = sxm_rgDefaults[(int)SqlDbType.VarBinary].MaxLength; - - smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen); - } - else if (dataType == typeof(SqlXml)) - smd = new SqlMetaData(name, SqlDbType.Xml); - else if (dataType == typeof(TimeSpan)) - smd = new SqlMetaData(name, SqlDbType.Time, 0, InferScaleFromTimeTicks(((TimeSpan)value).Ticks)); - else if (dataType == typeof(DateTimeOffset)) - smd = new SqlMetaData(name, SqlDbType.DateTimeOffset, 0, InferScaleFromTimeTicks(((DateTimeOffset)value).Ticks)); - else - throw ADP.UnknownDataType(dataType); - break; - - - default: - throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType)); - } - - return smd; - } - - /// - public bool Adjust(bool value) - { - if (SqlDbType.Bit != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public byte Adjust(byte value) - { - if (SqlDbType.TinyInt != SqlDbType) - ThrowInvalidType(); - return value; - } - - /// - public byte[] Adjust(byte[] value) - { - if (SqlDbType.Binary == SqlDbType || SqlDbType.Timestamp == SqlDbType) - { - //DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(byte[]): Fixed-length type with Max length!"); - // Don't pad null values - if (null != value) - { - // Pad fixed-length types - if (value.Length < MaxLength) - { - byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(value, rgbNewValue, value.Length); - Array.Clear(rgbNewValue, value.Length, (int)rgbNewValue.Length - value.Length); - return rgbNewValue; - } - } - } - else if (SqlDbType.VarBinary != SqlDbType && - SqlDbType.Image != SqlDbType) - ThrowInvalidType(); - - // Handle null values after type check - if (null == value) - { - return null; - } - - // trim all types - if (value.Length > MaxLength && Max != MaxLength) - { - byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(value, rgbNewValue, (int)MaxLength); - value = rgbNewValue; - } - - return value; - } - - /// - public char Adjust(char value) - { - if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) - { - if (1 != MaxLength) - ThrowInvalidType(); - } - else if ((1 > MaxLength) || // char must have max length of at least 1 - (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) - ) - ThrowInvalidType(); - - return value; - } - - /// - public char[] Adjust(char[] value) - { - if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) - { - //DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(byte[]): Fixed-length type with Max length!"); - // Don't pad null values - if (null != value) - { - // Pad fixed-length types - long oldLength = value.Length; - if (oldLength < MaxLength) - { - char[] rgchNew = new char[(int)MaxLength]; - Array.Copy(value, rgchNew, (int)oldLength); - - // pad extra space - for (long i = oldLength; i < rgchNew.Length; i++) - rgchNew[i] = ' '; - - return rgchNew; - } - } - } - else if (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) - ThrowInvalidType(); - - // Handle null values after type check - if (null == value) - { - return null; - } - - // trim all types - if (value.Length > MaxLength && Max != MaxLength) - { - char[] rgchNewValue = new char[MaxLength]; - Array.Copy(value, rgchNewValue, (int)MaxLength); - value = rgchNewValue; - } - - - return value; - } - - - internal static SqlMetaData GetPartialLengthMetaData(SqlMetaData md) - { - if (md.IsPartialLength == true) - { - return md; - } - if (md.SqlDbType == SqlDbType.Xml) - ThrowInvalidType(); //Xml should always have IsPartialLength = true - - if (md.SqlDbType == SqlDbType.NVarChar || md.SqlDbType == SqlDbType.VarChar || - md.SqlDbType == SqlDbType.VarBinary) - { - SqlMetaData mdnew = new SqlMetaData(md.Name, md.SqlDbType, SqlMetaData.Max, 0, 0, md.LocaleId, - md.CompareOptions, null, null, null, true, md.Type); - return mdnew; - } - else - return md; - } - - - private static void ThrowInvalidType() - { - throw ADP.InvalidMetaDataValue(); - } - - // Hard coding smalldatetime limits... - private static readonly DateTime x_dtSmallMax = new DateTime(2079, 06, 06, 23, 59, 29, 998); - private static readonly DateTime x_dtSmallMin = new DateTime(1899, 12, 31, 23, 59, 29, 999); - void VerifyDateTimeRange(DateTime value) - { - if (SqlDbType.SmallDateTime == SqlDbType && (x_dtSmallMax < value || x_dtSmallMin > value)) - ThrowInvalidType(); - } - - private static readonly SqlMoney x_smSmallMax = new SqlMoney(((Decimal)Int32.MaxValue) / 10000); - private static readonly SqlMoney x_smSmallMin = new SqlMoney(((Decimal)Int32.MinValue) / 10000); - void VerifyMoneyRange(SqlMoney value) - { - if (SqlDbType.SmallMoney == SqlDbType && ((x_smSmallMax < value).Value || (x_smSmallMin > value).Value)) - ThrowInvalidType(); - } - - private SqlDecimal InternalAdjustSqlDecimal(SqlDecimal value) - { - if (!value.IsNull && (value.Precision != Precision || value.Scale != Scale)) - { - // SQLBU 402377 Force truncation if target scale is smaller than actual scale. - if (value.Scale != Scale) - { - value = SqlDecimal.AdjustScale(value, Scale - value.Scale, false /* Don't round, truncate. */); - } - return SqlDecimal.ConvertToPrecScale(value, Precision, Scale); - } - - return value; - } - - private static readonly TimeSpan x_timeMin = TimeSpan.Zero; - private static readonly TimeSpan x_timeMax = new TimeSpan(TimeSpan.TicksPerDay - 1); - private void VerifyTimeRange(TimeSpan value) - { - if (SqlDbType.Time == SqlDbType && (x_timeMin > value || value > x_timeMax)) - { - ThrowInvalidType(); - } - } - - private static readonly Int64[] __unitTicksFromScale = { - 10000000, - 1000000, - 100000, - 10000, - 1000, - 100, - 10, - 1, - }; - - private Int64 InternalAdjustTimeTicks(Int64 ticks) - { - return (ticks / __unitTicksFromScale[Scale] * __unitTicksFromScale[Scale]); - } - - private static byte InferScaleFromTimeTicks(Int64 ticks) - { - for (byte scale = 0; scale < MaxTimeScale; ++scale) - { - if ((ticks / __unitTicksFromScale[scale] * __unitTicksFromScale[scale]) == ticks) - { - return scale; - } - } - return MaxTimeScale; - } - - private static DbType[] sxm_rgSqlDbTypeToDbType = { - DbType.Int64, // SqlDbType.BigInt - DbType.Binary, // SqlDbType.Binary - DbType.Boolean, // SqlDbType.Bit - DbType.AnsiString, // SqlDbType.Char - DbType.DateTime, // SqlDbType.DateTime - DbType.Decimal, // SqlDbType.Decimal - DbType.Double, // SqlDbType.Float - DbType.Binary, // SqlDbType.Image - DbType.Int32, // SqlDbType.Int - DbType.Currency, // SqlDbType.Money - DbType.String, // SqlDbType.NChar - DbType.String, // SqlDbType.NText - DbType.String, // SqlDbType.NVarChar - DbType.Single, // SqlDbType.Real - DbType.Guid, // SqlDbType.UniqueIdentifier - DbType.DateTime, // SqlDbType.SmallDateTime - DbType.Int16, // SqlDbType.SmallInt - DbType.Currency, // SqlDbType.SmallMoney - DbType.AnsiString, // SqlDbType.Text - DbType.Binary, // SqlDbType.Timestamp - DbType.Byte, // SqlDbType.TinyInt - DbType.Binary, // SqlDbType.VarBinary - DbType.AnsiString, // SqlDbType.VarChar - DbType.Object, // SqlDbType.Variant - DbType.Object, // SqlDbType.Row - DbType.Xml, // SqlDbType.Xml - DbType.String, // SqlDbType.NVarChar, place holder - DbType.String, // SqlDbType.NVarChar, place holder - DbType.String, // SqlDbType.NVarChar, place holder - DbType.Object, // SqlDbType.Udt - DbType.Object, // SqlDbType.Structured - DbType.Date, // SqlDbType.Date - DbType.Time, // SqlDbType.Time - DbType.DateTime2, // SqlDbType.DateTime2 - DbType.DateTimeOffset // SqlDbType.DateTimeOffset - }; - - private void SetDefaultsForType(SqlDbType dbType) - { - if (SqlDbType.BigInt <= dbType && SqlDbType.DateTimeOffset >= dbType) - { - SqlMetaData smdDflt = sxm_rgDefaults[(int)dbType]; - m_sqlDbType = dbType; - m_lMaxLength = smdDflt.MaxLength; - m_bPrecision = smdDflt.Precision; - m_bScale = smdDflt.Scale; - m_lLocale = smdDflt.LocaleId; - m_eCompareOptions = smdDflt.CompareOptions; - } - } - - // Array of default-valued metadata ordered by corresponding SqlDbType. - internal static SqlMetaData[] sxm_rgDefaults = - { - // new SqlMetaData(name, DbType, SqlDbType, MaxLen, Prec, Scale, Locale, DatabaseName, SchemaName, isPartialLength) - new SqlMetaData("bigint", SqlDbType.BigInt, - 8, 19, 0, 0, SqlCompareOptions.None, false), // SqlDbType.BigInt - new SqlMetaData("binary", SqlDbType.Binary, - 1, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Binary - new SqlMetaData("bit", SqlDbType.Bit, - 1, 1, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Bit - new SqlMetaData("char", SqlDbType.Char, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.Char - new SqlMetaData("datetime", SqlDbType.DateTime, - 8, 23, 3, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime - new SqlMetaData("decimal", SqlDbType.Decimal, - 9, 18, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Decimal - new SqlMetaData("float", SqlDbType.Float, - 8, 53, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Float - new SqlMetaData("image", SqlDbType.Image, - x_lMax, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Image - new SqlMetaData("int", SqlDbType.Int, - 4, 10, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Int - new SqlMetaData("money", SqlDbType.Money, - 8, 19, 4, 0, SqlCompareOptions.None, false), // SqlDbType.Money - new SqlMetaData("nchar", SqlDbType.NChar, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NChar - new SqlMetaData("ntext", SqlDbType.NText, - x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NText - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NVarChar - new SqlMetaData("real", SqlDbType.Real, - 4, 24, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Real - new SqlMetaData("uniqueidentifier", SqlDbType.UniqueIdentifier, - 16, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.UniqueIdentifier - new SqlMetaData("smalldatetime", SqlDbType.SmallDateTime, - 4, 16, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallDateTime - new SqlMetaData("smallint", SqlDbType.SmallInt, - 2, 5, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallInt - new SqlMetaData("smallmoney", SqlDbType.SmallMoney, - 4, 10, 4, 0, SqlCompareOptions.None, false), // SqlDbType.SmallMoney - new SqlMetaData("text", SqlDbType.Text, - x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.Text - new SqlMetaData("timestamp", SqlDbType.Timestamp, - 8, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Timestamp - new SqlMetaData("tinyint", SqlDbType.TinyInt, - 1, 3, 0, 0, SqlCompareOptions.None, false), // SqlDbType.TinyInt - new SqlMetaData("varbinary", SqlDbType.VarBinary, - x_lServerMaxBinary, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.VarBinary - new SqlMetaData("varchar", SqlDbType.VarChar, - x_lServerMaxANSI, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.VarChar - new SqlMetaData("sql_variant", SqlDbType.Variant, - 8016, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Variant - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 24 - new SqlMetaData("xml", SqlDbType.Xml, - x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, true), // SqlDbType.Xml - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 26 - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 27 - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 28 - new SqlMetaData("udt", SqlDbType.Udt, - 0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Udt = 29 Bug Fix: 302698 - new SqlMetaData("table", SqlDbType.Structured, - 0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Structured - new SqlMetaData("date", SqlDbType.Date, - 3, 10,0, 0, SqlCompareOptions.None, false), // SqlDbType.Date - new SqlMetaData("time", SqlDbType.Time, - 5, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.Time - new SqlMetaData("datetime2", SqlDbType.DateTime2, - 8, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime2 - new SqlMetaData("datetimeoffset", SqlDbType.DateTimeOffset, - 10, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTimeOffset - }; - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs deleted file mode 100644 index 2970fb2145..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs +++ /dev/null @@ -1,615 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Data; -using System.Data.SqlTypes; -using System.Diagnostics; -using System.Runtime.InteropServices; -using Microsoft.Data.Common; - -namespace Microsoft.Data.SqlClient.Server -{ - internal sealed class SqlRecordBuffer - { - - internal enum StorageType - { - Boolean, - Byte, - ByteArray, - CharArray, - DateTime, - DateTimeOffset, - Double, - Guid, - Int16, - Int32, - Int64, - Single, - String, - SqlDecimal, - TimeSpan, - } - - [StructLayout(LayoutKind.Explicit)] - internal struct Storage - { - [FieldOffset(0)] internal Boolean _boolean; - [FieldOffset(0)] internal Byte _byte; - [FieldOffset(0)] internal DateTime _dateTime; - [FieldOffset(0)] internal DateTimeOffset _dateTimeOffset; - [FieldOffset(0)] internal Double _double; - [FieldOffset(0)] internal Guid _guid; - [FieldOffset(0)] internal Int16 _int16; - [FieldOffset(0)] internal Int32 _int32; - [FieldOffset(0)] internal Int64 _int64; // also used to BytesLength and CharsLength - [FieldOffset(0)] internal Single _single; - [FieldOffset(0)] internal TimeSpan _timeSpan; - } - - private bool _isNull; - private StorageType _type; - private Storage _value; - private object _object; // String, SqlDecimal - private SmiMetaData _metadata; // for variant - private bool _isMetaSet; // flag to indicate whether we have set the variant metadata - - internal SqlRecordBuffer(SmiMetaData metaData) - { - _isNull = true; - } - - internal bool IsNull - { - get - { - return _isNull; - } - } - - internal Boolean Boolean - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Boolean == _type, "Wrong storage type: " + _type); - - return _value._boolean; - } - set - { - _value._boolean = value; - _type = StorageType.Boolean; - _isNull = false; - } - } - - internal Byte Byte - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Byte == _type, "Wrong storage type: " + _type); - - return _value._byte; - } - set - { - _value._byte = value; - _type = StorageType.Byte; - _isNull = false; - } - } - - internal DateTime DateTime - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.DateTime == _type, "Wrong storage type: " + _type); - - return _value._dateTime; - } - set - { - _value._dateTime = value; - _type = StorageType.DateTime; - _isNull = false; - // in case of variant types, the caller first sets metadata and than the value. we need to keep metadata after first value set - // if value is set second time without variant metadata, reset the metadata since it is not variant anymore - if (_isMetaSet) - { - _isMetaSet = false; - } - else - { - _metadata = null; // need to clear the variant metadata - } - } - } - - internal DateTimeOffset DateTimeOffset - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.DateTimeOffset == _type, "Wrong storage type: " + _type); - - return _value._dateTimeOffset; - } - set - { - _value._dateTimeOffset = value; - _type = StorageType.DateTimeOffset; - _isNull = false; - } - } - - internal Double Double - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Double == _type, "Wrong storage type: " + _type); - - return _value._double; - } - set - { - _value._double = value; - _type = StorageType.Double; - _isNull = false; - } - } - - internal Guid Guid - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Guid == _type, "Wrong storage type: " + _type); - - return _value._guid; - } - set - { - _value._guid = value; - _type = StorageType.Guid; - _isNull = false; - } - } - - internal Int16 Int16 - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Int16 == _type, "Wrong storage type: " + _type); - - return _value._int16; - } - set - { - _value._int16 = value; - _type = StorageType.Int16; - _isNull = false; - } - } - - internal Int32 Int32 - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Int32 == _type, "Wrong storage type: " + _type); - - return _value._int32; - } - set - { - _value._int32 = value; - _type = StorageType.Int32; - _isNull = false; - } - } - - internal Int64 Int64 - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Int64 == _type, "Wrong storage type: " + _type); - - return _value._int64; - } - set - { - _value._int64 = value; - _type = StorageType.Int64; - _isNull = false; - if (_isMetaSet) - { - _isMetaSet = false; - } - else - { - _metadata = null; // need to clear the variant metadata - } - } - } - - internal Single Single - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.Single == _type, "Wrong storage type: " + _type); - - return _value._single; - } - set - { - _value._single = value; - _type = StorageType.Single; - _isNull = false; - } - } - - internal String String - { - get - { - Debug.Assert(!_isNull, "Null data type"); - - if (StorageType.String == _type) - { - return (String)_object; - } - else if (StorageType.CharArray == _type) - { - return new String((char[])_object, 0, (int)CharsLength); - } - else - { - // Xml may be stored as byte array, yet have GetString called against it. - Debug.Assert(StorageType.ByteArray == _type, "Wrong storage type: " + _type); - System.IO.Stream byteStream = new System.IO.MemoryStream((byte[])_object, false); - return (new SqlXml(byteStream)).Value; - } - } - set - { - Debug.Assert(null != value, ""); - - _object = value; - _value._int64 = ((string)value).Length; - _type = StorageType.String; - _isNull = false; - if (_isMetaSet) - { - _isMetaSet = false; - } - else - { - _metadata = null; // need to clear the variant metadata - } - } - } - - internal SqlDecimal SqlDecimal - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.SqlDecimal == _type, "Wrong storage type: " + _type); - - return (SqlDecimal)_object; - } - set - { - Debug.Assert(!value.IsNull, "Null input"); - - _object = value; - _type = StorageType.SqlDecimal; - _isNull = false; - } - } - - internal TimeSpan TimeSpan - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.TimeSpan == _type, "Wrong storage type: " + _type); - - return _value._timeSpan; - } - set - { - _value._timeSpan = value; - _type = StorageType.TimeSpan; - _isNull = false; - } - } - - internal Int64 BytesLength - { - get - { - Debug.Assert(!_isNull, "Null data type"); - - // sometimes Xml is stored as string, but must support byte access - if (StorageType.String == _type) - { - ConvertXmlStringToByteArray(); - } - else - { - Debug.Assert(StorageType.ByteArray == _type, "Wrong storage type: " + _type); - } - - return _value._int64; - } - set - { - if (0 == value) - { - _value._int64 = value; - _object = new byte[0]; - _type = StorageType.ByteArray; - _isNull = false; - } - else - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.ByteArray == _type, "Wrong storage type: " + _type); - Debug.Assert(value > 0 && value <= ((byte[])_object).Length, "Invalid BytesLength"); - - _value._int64 = value; - } - } - } - - internal Int64 CharsLength - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.CharArray == _type || StorageType.String == _type, "Wrong storage type: " + _type); - - return _value._int64; - } - set - { - if (0 == value) - { - _value._int64 = value; - _object = new char[0]; - _type = StorageType.CharArray; - _isNull = false; - } - else - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.CharArray == _type || StorageType.String == _type, "Wrong storage type: " + _type); - Debug.Assert(value > 0 && - ((StorageType.CharArray == _type && value <= ((char[])_object).Length) || (StorageType.String == _type && value <= ((string)_object).Length)), - "Invalid CharsLength"); - - _value._int64 = value; - } - } - } - - internal SmiMetaData VariantType - { - get - { - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(_metadata == null - || _metadata.SqlDbType == SqlDbType.Money - || _metadata.SqlDbType == SqlDbType.NVarChar - || _metadata.SqlDbType == SqlDbType.DateTime - || _metadata.SqlDbType == SqlDbType.Date - || _metadata.SqlDbType == SqlDbType.DateTime2, - "Invalid metadata"); - - switch (_type) - { - case StorageType.Boolean: - return SmiMetaData.DefaultBit; - case StorageType.Byte: - return SmiMetaData.DefaultTinyInt; - case StorageType.ByteArray: - return SmiMetaData.DefaultVarBinary; - case StorageType.CharArray: - return SmiMetaData.DefaultNVarChar; - case StorageType.DateTime: - return _metadata ?? SmiMetaData.DefaultDateTime; - case StorageType.DateTimeOffset: - return SmiMetaData.DefaultDateTimeOffset; - case StorageType.Double: - return SmiMetaData.DefaultFloat; - case StorageType.Guid: - return SmiMetaData.DefaultUniqueIdentifier; - case StorageType.Int16: - return SmiMetaData.DefaultSmallInt; - case StorageType.Int32: - return SmiMetaData.DefaultInt; - case StorageType.Int64: - return _metadata ?? SmiMetaData.DefaultBigInt; - case StorageType.Single: - return SmiMetaData.DefaultReal; - case StorageType.String: - return _metadata ?? SmiMetaData.DefaultNVarChar; - case StorageType.SqlDecimal: - return new SmiMetaData(SqlDbType.Decimal, 17, ((SqlDecimal)_object).Precision, ((SqlDecimal)_object).Scale, 0, SqlCompareOptions.None, null); - case StorageType.TimeSpan: - return SmiMetaData.DefaultTime; - } - return null; - } - set - { - Debug.Assert(value != null && (value.SqlDbType == SqlDbType.Money || value.SqlDbType == SqlDbType.NVarChar), - "Invalid metadata"); - - _metadata = value; - _isMetaSet = true; - } - } - - internal int GetBytes(long fieldOffset, byte[] buffer, int bufferOffset, int length) - { - int ndataIndex = (int)fieldOffset; - - Debug.Assert(!_isNull, "Null data type"); - // sometimes Xml is stored as string, but must support byte access - if (StorageType.String == _type) - { - ConvertXmlStringToByteArray(); - } - else - { - Debug.Assert(StorageType.ByteArray == _type, "Wrong storage type: " + _type); - } - Debug.Assert(null != buffer, "Null buffer"); - Debug.Assert(ndataIndex + length <= BytesLength, "Invalid fieldOffset or length"); - - Buffer.BlockCopy((byte[])_object, ndataIndex, buffer, bufferOffset, length); - - return length; - } - - internal int GetChars(long fieldOffset, char[] buffer, int bufferOffset, int length) - { - int ndataIndex = (int)fieldOffset; - - Debug.Assert(!_isNull, "Null data type"); - Debug.Assert(StorageType.CharArray == _type || StorageType.String == _type, "Wrong storage type: " + _type); - Debug.Assert(null != buffer, "Null buffer"); - Debug.Assert(ndataIndex + length <= CharsLength, "Invalid fieldOffset or length"); - - if (StorageType.CharArray == _type) - { - Array.Copy((char[])_object, ndataIndex, buffer, bufferOffset, length); - } - else - { // String type - ((string)_object).CopyTo(ndataIndex, buffer, bufferOffset, length); - } - - return length; - } - - internal int SetBytes(long fieldOffset, byte[] buffer, int bufferOffset, int length) - { - int ndataIndex = (int)fieldOffset; - - if (IsNull || StorageType.ByteArray != _type) - { - if (ndataIndex != 0) - { // set the first time: should start from the beginning - throw ADP.ArgumentOutOfRange("fieldOffset"); - } - _object = new byte[length]; - _type = StorageType.ByteArray; - _isNull = false; - BytesLength = length; - } - else - { - if (ndataIndex > BytesLength) - { // no gap is allowed - throw ADP.ArgumentOutOfRange("fieldOffset"); - } - if (ndataIndex + length > BytesLength) - { // beyond the current length - int cbytes = ((byte[])_object).Length; - - if (ndataIndex + length > cbytes) - { // dynamic expansion - byte[] data = new byte[Math.Max(ndataIndex + length, 2 * cbytes)]; - Buffer.BlockCopy((byte[])_object, 0, data, 0, (int)BytesLength); - _object = data; - - } - BytesLength = ndataIndex + length; - } - } - - Buffer.BlockCopy(buffer, bufferOffset, (byte[])_object, ndataIndex, length); - - return length; - } - - internal int SetChars(long fieldOffset, char[] buffer, int bufferOffset, int length) - { - int ndataIndex = (int)fieldOffset; - - if (IsNull || (StorageType.CharArray != _type && StorageType.String != _type)) - { - if (ndataIndex != 0) - { // set the first time: should start from the beginning - throw ADP.ArgumentOutOfRange("fieldOffset"); - } - _object = new char[length]; - _type = StorageType.CharArray; - _isNull = false; - CharsLength = length; - } - else - { - if (ndataIndex > CharsLength) - { // no gap is allowed - throw ADP.ArgumentOutOfRange("fieldOffset"); - } - if (StorageType.String == _type) - { // convert string to char[] - _object = ((string)_object).ToCharArray(); - _type = StorageType.CharArray; - } - if (ndataIndex + length > CharsLength) - { // beyond the current length - int cchars = ((char[])_object).Length; - - if (ndataIndex + length > cchars) - { // dynamic expansion - char[] data = new char[Math.Max(ndataIndex + length, 2 * cchars)]; - Array.Copy((char[])_object, 0, data, 0, CharsLength); - _object = data; - } - CharsLength = ndataIndex + length; - } - } - - Array.Copy(buffer, bufferOffset, (char[])_object, ndataIndex, length); - - return length; - } - - internal void SetNull() - { - _isNull = true; - } - - // Handle case for Xml where SetString() was called, followed by GetBytes() - private void ConvertXmlStringToByteArray() - { - Debug.Assert(StorageType.String == _type, "ConvertXmlStringToByteArray: Invalid storage type for conversion: " + _type.ToString()); - - // Grab the unicode bytes, but prepend the XML unicode BOM - string value = (string)_object; - byte[] bytes = new byte[2 + System.Text.Encoding.Unicode.GetByteCount(value)]; - bytes[0] = 0xff; - bytes[1] = 0xfe; - System.Text.Encoding.Unicode.GetBytes(value, 0, value.Length, bytes, 2); - - _object = bytes; - _value._int64 = bytes.Length; - _type = StorageType.ByteArray; - } - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs deleted file mode 100644 index c5730bbb35..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs +++ /dev/null @@ -1,140 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using Microsoft.Data.Common; - -namespace Microsoft.Data.SqlClient.Server -{ - /// - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)] - public sealed class SqlUserDefinedAggregateAttribute : Attribute - { - private int m_MaxByteSize; - private bool m_fInvariantToDup; - private bool m_fInvariantToNulls; - private bool m_fInvariantToOrder = true; - private bool m_fNullIfEmpty; - private Format m_format; - private string m_fName; - - /// - // The maximum value for the maxbytesize field, in bytes. - public const int MaxByteSizeValue = 8000; - - /// - // A required attribute on all udaggs, used to indicate that the - // given type is a udagg, and its storage format. - public SqlUserDefinedAggregateAttribute(Format format) - { - switch (format) - { - case Format.Unknown: - throw ADP.NotSupportedUserDefinedTypeSerializationFormat((Microsoft.Data.SqlClient.Server.Format)format, "format"); - case Format.Native: - case Format.UserDefined: - this.m_format = format; - break; - default: - throw ADP.InvalidUserDefinedTypeSerializationFormat((Microsoft.Data.SqlClient.Server.Format)format); - } - } - - /// - // The maximum size of this instance, in bytes. Does not have to be - // specified for Native format serialization. The maximum value - // for this property is specified by MaxByteSizeValue. - public int MaxByteSize - { - get - { - return this.m_MaxByteSize; - } - set - { - // MaxByteSize of -1 means 2GB and is valid, as well as 0 to MaxByteSizeValue - if (value < -1 || value > MaxByteSizeValue) - { - throw ADP.ArgumentOutOfRange(StringsHelper.GetString(Strings.SQLUDT_MaxByteSizeValue), "MaxByteSize", value); - } - this.m_MaxByteSize = value; - } - } - - /// - public bool IsInvariantToDuplicates - { - get - { - return this.m_fInvariantToDup; - } - set - { - this.m_fInvariantToDup = value; - } - } - - /// - public bool IsInvariantToNulls - { - get - { - return this.m_fInvariantToNulls; - } - set - { - this.m_fInvariantToNulls = value; - } - } - - /// - public bool IsInvariantToOrder - { - get - { - return this.m_fInvariantToOrder; - } - set - { - this.m_fInvariantToOrder = value; - } - } - - /// - public bool IsNullIfEmpty - { - get - { - return this.m_fNullIfEmpty; - } - set - { - this.m_fNullIfEmpty = value; - } - } - - /// - // The on-disk format for this type. - public Format Format - { - get - { - return this.m_format; - } - } - - /// - public string Name - { - get - { - return m_fName; - } - set - { - m_fName = value; - } - } - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs index 42a59bb154..28bc1ae376 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs @@ -25,13 +25,13 @@ namespace Microsoft.Data.SqlClient.Server // as an ExtendedClrTypeCode enum for rapid access (lookup in static array is best, if possible). internal static class ValueUtilsSmi { - const int __maxByteChunkSize = TdsEnums.MAXSIZE; - const int __maxCharChunkSize = TdsEnums.MAXSIZE / sizeof(char); - const int NoLengthLimit = (int)SmiMetaData.UnlimitedMaxLengthIndicator; // make sure we use the same constant + private const int __maxByteChunkSize = TdsEnums.MAXSIZE; + private const int __maxCharChunkSize = TdsEnums.MAXSIZE / sizeof(char); + private const int NoLengthLimit = (int)SmiMetaData.UnlimitedMaxLengthIndicator; // make sure we use the same constant // Constants - const int constBinBufferSize = 4096; // Size of the buffer used to read input parameter of type Stream - const int constTextBufferSize = 4096; // Size of the buffer (in chars) user to read input parameter of type TextReader + private const int constBinBufferSize = 4096; // Size of the buffer used to read input parameter of type Stream + private const int constTextBufferSize = 4096; // Size of the buffer (in chars) user to read input parameter of type TextReader // // User-visible semantics-laden Getter/Setter support methods @@ -65,7 +65,7 @@ internal static bool GetBoolean(SmiEventSink_Default sink, ITypedGettersV3 gette { throw ADP.InvalidCast(); } - return (Boolean)result; + return (bool)result; } internal static byte GetByte(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) @@ -80,7 +80,7 @@ internal static byte GetByte(SmiEventSink_Default sink, ITypedGettersV3 getters, { throw ADP.InvalidCast(); } - return (Byte)result; + return (byte)result; } private static long GetBytesConversion(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData, long fieldOffset, byte[] buffer, int bufferOffset, int length, bool throwOnNull) @@ -197,7 +197,7 @@ internal static long GetChars(SmiEventSink_Default sink, ITypedGettersV3 getters return length; } - String value = ((String)GetValue(sink, getters, ordinal, metaData, null)); + string value = ((string)GetValue(sink, getters, ordinal, metaData, null)); if (null == value) { throw ADP.InvalidCast(); @@ -254,7 +254,7 @@ internal static DateTimeOffset GetDateTimeOffset(SmiEventSink_Default sink, SmiT return (DateTimeOffset)GetValue200(sink, getters, ordinal, metaData, null); } - internal static Decimal GetDecimal(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + internal static decimal GetDecimal(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { ThrowIfITypedGettersIsNull(sink, getters, ordinal); if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.Decimal)) @@ -266,10 +266,10 @@ internal static Decimal GetDecimal(SmiEventSink_Default sink, ITypedGettersV3 ge { throw ADP.InvalidCast(); } - return (Decimal)result; + return (decimal)result; } - internal static Double GetDouble(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + internal static double GetDouble(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { ThrowIfITypedGettersIsNull(sink, getters, ordinal); if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.Double)) @@ -281,7 +281,7 @@ internal static Double GetDouble(SmiEventSink_Default sink, ITypedGettersV3 gett { throw ADP.InvalidCast(); } - return (Double)result; + return (double)result; } internal static Guid GetGuid(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) @@ -299,7 +299,7 @@ internal static Guid GetGuid(SmiEventSink_Default sink, ITypedGettersV3 getters, return (Guid)result; } - internal static Int16 GetInt16(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + internal static short GetInt16(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { ThrowIfITypedGettersIsNull(sink, getters, ordinal); if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.Int16)) @@ -311,10 +311,10 @@ internal static Int16 GetInt16(SmiEventSink_Default sink, ITypedGettersV3 getter { throw ADP.InvalidCast(); } - return (Int16)obj; + return (short)obj; } - internal static Int32 GetInt32(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + internal static int GetInt32(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { ThrowIfITypedGettersIsNull(sink, getters, ordinal); if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.Int32)) @@ -326,10 +326,10 @@ internal static Int32 GetInt32(SmiEventSink_Default sink, ITypedGettersV3 getter { throw ADP.InvalidCast(); } - return (Int32)result; + return (int)result; } - internal static Int64 GetInt64(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + internal static long GetInt64(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { ThrowIfITypedGettersIsNull(sink, getters, ordinal); if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.Int64)) @@ -341,10 +341,10 @@ internal static Int64 GetInt64(SmiEventSink_Default sink, ITypedGettersV3 getter { throw ADP.InvalidCast(); } - return (Int64)result; + return (long)result; } - internal static Single GetSingle(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + internal static float GetSingle(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { ThrowIfITypedGettersIsNull(sink, getters, ordinal); if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.Single)) @@ -356,7 +356,7 @@ internal static Single GetSingle(SmiEventSink_Default sink, ITypedGettersV3 gett { throw ADP.InvalidCast(); } - return (Single)result; + return (float)result; } internal static SqlBinary GetSqlBinary(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) @@ -594,7 +594,7 @@ internal static SqlDouble GetSqlDouble(SmiEventSink_Default sink, ITypedGettersV } else { - Double temp = GetDouble_Unchecked(sink, getters, ordinal); + double temp = GetDouble_Unchecked(sink, getters, ordinal); result = new SqlDouble(temp); } } @@ -650,7 +650,7 @@ internal static SqlInt16 GetSqlInt16(SmiEventSink_Default sink, ITypedGettersV3 } else { - Int16 temp = GetInt16_Unchecked(sink, getters, ordinal); + short temp = GetInt16_Unchecked(sink, getters, ordinal); result = new SqlInt16(temp); } } @@ -678,7 +678,7 @@ internal static SqlInt32 GetSqlInt32(SmiEventSink_Default sink, ITypedGettersV3 } else { - Int32 temp = GetInt32_Unchecked(sink, getters, ordinal); + int temp = GetInt32_Unchecked(sink, getters, ordinal); result = new SqlInt32(temp); } } @@ -705,7 +705,7 @@ internal static SqlInt64 GetSqlInt64(SmiEventSink_Default sink, ITypedGettersV3 } else { - Int64 temp = GetInt64_Unchecked(sink, getters, ordinal); + long temp = GetInt64_Unchecked(sink, getters, ordinal); result = new SqlInt64(temp); } } @@ -760,7 +760,7 @@ internal static SqlSingle GetSqlSingle(SmiEventSink_Default sink, ITypedGettersV } else { - Single temp = GetSingle_Unchecked(sink, getters, ordinal); + float temp = GetSingle_Unchecked(sink, getters, ordinal); result = new SqlSingle(temp); } } @@ -788,7 +788,7 @@ internal static SqlString GetSqlString(SmiEventSink_Default sink, ITypedGettersV } else { - String temp = GetString_Unchecked(sink, getters, ordinal); + string temp = GetString_Unchecked(sink, getters, ordinal); result = new SqlString(temp); } } @@ -845,7 +845,7 @@ internal static SqlXml GetSqlXml(SmiEventSink_Default sink, ITypedGettersV3 gett return result; } - internal static String GetString(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + internal static string GetString(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { ThrowIfITypedGettersIsNull(sink, getters, ordinal); if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.String)) @@ -857,7 +857,7 @@ internal static String GetString(SmiEventSink_Default sink, ITypedGettersV3 gett { throw ADP.InvalidCast(); } - return (String)obj; + return (string)obj; } internal static SqlSequentialStreamSmi GetSequentialStream(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData, bool bypassTypeCheck = false) @@ -1146,7 +1146,7 @@ SmiContext context } else { - result = __typeSpecificNullForSqlValue[(int)metaData.SqlDbType]; + result = s_typeSpecificNullForSqlValue[(int)metaData.SqlDbType]; } } else @@ -1196,7 +1196,7 @@ SmiContext context } else { - result = __typeSpecificNullForSqlValue[(int)metaData.SqlDbType]; + result = s_typeSpecificNullForSqlValue[(int)metaData.SqlDbType]; } } else @@ -1291,7 +1291,7 @@ SmiContext context } // null return values for SqlClient 1.1-compatible GetSqlValue() - private static object[] __typeSpecificNullForSqlValue = { + private static object[] s_typeSpecificNullForSqlValue = { SqlInt64.Null, // SqlDbType.BigInt SqlBinary.Null, // SqlDbType.Binary SqlBoolean.Null, // SqlDbType.Bit @@ -1559,19 +1559,19 @@ SqlBuffer targetBuffer // destination // Strongly-typed setters are a bit simpler than their corresponding getters. // 1) check to make sure the type is compatible (exception if not) // 2) push the data - internal static void SetDBNull(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Boolean value) + internal static void SetDBNull(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, bool value) { SetDBNull_Unchecked(sink, setters, ordinal); } - internal static void SetBoolean(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Boolean value) + internal static void SetBoolean(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, bool value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Boolean); SetBoolean_Unchecked(sink, setters, ordinal, value); } - internal static void SetByte(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Byte value) + internal static void SetByte(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, byte value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Byte); @@ -1583,7 +1583,7 @@ internal static long SetBytes(SmiEventSink_Default sink, ITypedSettersV3 setters ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.ByteArray); if (null == buffer) { - throw ADP.ArgumentNull("buffer"); + throw ADP.ArgumentNull(nameof(buffer)); } length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, fieldOffset, buffer.Length, bufferOffset, length); Debug.Assert(length >= 0, "Buffer.Length was invalid!"); @@ -1624,7 +1624,7 @@ internal static long SetChars(SmiEventSink_Default sink, ITypedSettersV3 setters ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.CharArray); if (null == buffer) { - throw ADP.ArgumentNull("buffer"); + throw ADP.ArgumentNull(nameof(buffer)); } length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, fieldOffset, buffer.Length, bufferOffset, length); Debug.Assert(length >= 0, "Buffer.Length was invalid!"); @@ -1657,14 +1657,14 @@ internal static void SetDateTimeOffset(SmiEventSink_Default sink, ITypedSettersV SetDateTimeOffset_Unchecked(sink, (SmiTypedGetterSetter)setters, ordinal, value); } - internal static void SetDecimal(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Decimal value) + internal static void SetDecimal(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, decimal value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Decimal); SetDecimal_PossiblyMoney(sink, setters, ordinal, metaData, value); } - internal static void SetDouble(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Double value) + internal static void SetDouble(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, double value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Double); @@ -1678,28 +1678,28 @@ internal static void SetGuid(SmiEventSink_Default sink, ITypedSettersV3 setters, SetGuid_Unchecked(sink, setters, ordinal, value); } - internal static void SetInt16(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Int16 value) + internal static void SetInt16(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, short value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Int16); SetInt16_Unchecked(sink, setters, ordinal, value); } - internal static void SetInt32(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Int32 value) + internal static void SetInt32(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, int value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Int32); SetInt32_Unchecked(sink, setters, ordinal, value); } - internal static void SetInt64(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Int64 value) + internal static void SetInt64(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, long value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Int64); SetInt64_Unchecked(sink, setters, ordinal, value); } - internal static void SetSingle(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Single value) + internal static void SetSingle(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, float value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Single); @@ -1815,7 +1815,7 @@ internal static void SetSqlXml(SmiEventSink_Default sink, ITypedSettersV3 setter SetSqlXml_Unchecked(sink, setters, ordinal, value); } - internal static void SetString(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, String value) + internal static void SetString(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, string value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.String); @@ -1861,10 +1861,10 @@ int offset case ExtendedClrTypeCode.Invalid: throw ADP.UnknownDataType(value.GetType()); case ExtendedClrTypeCode.Boolean: - SetBoolean_Unchecked(sink, setters, ordinal, (Boolean)value); + SetBoolean_Unchecked(sink, setters, ordinal, (bool)value); break; case ExtendedClrTypeCode.Byte: - SetByte_Unchecked(sink, setters, ordinal, (Byte)value); + SetByte_Unchecked(sink, setters, ordinal, (byte)value); break; case ExtendedClrTypeCode.Char: { @@ -1880,27 +1880,27 @@ int offset SetDBNull_Unchecked(sink, setters, ordinal); break; case ExtendedClrTypeCode.Decimal: - SetDecimal_PossiblyMoney(sink, setters, ordinal, metaData, (Decimal)value); + SetDecimal_PossiblyMoney(sink, setters, ordinal, metaData, (decimal)value); break; case ExtendedClrTypeCode.Double: - SetDouble_Unchecked(sink, setters, ordinal, (Double)value); + SetDouble_Unchecked(sink, setters, ordinal, (double)value); break; case ExtendedClrTypeCode.Empty: SetDBNull_Unchecked(sink, setters, ordinal); break; case ExtendedClrTypeCode.Int16: - SetInt16_Unchecked(sink, setters, ordinal, (Int16)value); + SetInt16_Unchecked(sink, setters, ordinal, (short)value); break; case ExtendedClrTypeCode.Int32: - SetInt32_Unchecked(sink, setters, ordinal, (Int32)value); + SetInt32_Unchecked(sink, setters, ordinal, (int)value); break; case ExtendedClrTypeCode.Int64: - SetInt64_Unchecked(sink, setters, ordinal, (Int64)value); + SetInt64_Unchecked(sink, setters, ordinal, (long)value); break; case ExtendedClrTypeCode.SByte: throw ADP.InvalidCast(); case ExtendedClrTypeCode.Single: - SetSingle_Unchecked(sink, setters, ordinal, (Single)value); + SetSingle_Unchecked(sink, setters, ordinal, (float)value); break; case ExtendedClrTypeCode.String: SetString_LengthChecked(sink, setters, ordinal, metaData, (string)value, offset); @@ -1981,7 +1981,7 @@ int offset SetXmlReader_Unchecked(sink, setters, ordinal, ((XmlDataFeed)value)._source); break; default: - Debug.Assert(false, "Unvalidated extendedtypecode: " + typeCode); + Debug.Fail("Unvalidated extendedtypecode: " + typeCode); break; } } @@ -2185,11 +2185,9 @@ internal static void FillCompatibleITypedSettersFromReader(SmiEventSink_Default // In order for us to get here we would have to have an // invalid instance of SqlDbType, or one would have to add // new member to SqlDbType without adding a case in this - // switch, hence the assert - it must be bug in our code - // not invalid user parameters. - Debug.Assert(false, "unsupported DbType:" + metaData[i].SqlDbType.ToString()); + // switch, hence the assert. + Debug.Fail("unsupported DbType:" + metaData[i].SqlDbType.ToString()); throw ADP.NotSupported(); - } } } @@ -2399,11 +2397,9 @@ internal static void FillCompatibleSettersFromReader(SmiEventSink_Default sink, // In order for us to get here we would have to have an // invalid instance of SqlDbType, or one would have to add // new member to SqlDbType without adding a case in this - // switch, hence the assert - it must be bug in our code - // not invalid user parameters. + // switch, hence the assert. Debug.Fail("unsupported DbType:" + metaData[i].SqlDbType.ToString()); throw ADP.NotSupported(); - } } } @@ -2776,7 +2772,7 @@ private static object GetUdt_LengthChecked(SmiEventSink_Default sink, ITypedGett { Type t = metaData.Type; Debug.Assert(t != null, "Unexpected null of udtType on GetUdt_LengthChecked!"); - result = t.InvokeMember("Null", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static, null, null, new Object[] { }, CultureInfo.InvariantCulture); + result = t.InvokeMember("Null", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static, null, null, Array.Empty(), CultureInfo.InvariantCulture); Debug.Assert(result != null); } else @@ -2788,7 +2784,8 @@ private static object GetUdt_LengthChecked(SmiEventSink_Default sink, ITypedGett } return result; } - private static Decimal GetDecimal_PossiblyMoney(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) + + private static decimal GetDecimal_PossiblyMoney(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { if (SqlDbType.Decimal == metaData.SqlDbType) { @@ -2803,7 +2800,7 @@ private static Decimal GetDecimal_PossiblyMoney(SmiEventSink_Default sink, IType } } - private static void SetDecimal_PossiblyMoney(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Decimal value) + private static void SetDecimal_PossiblyMoney(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, decimal value) { if (SqlDbType.Decimal == metaData.SqlDbType || SqlDbType.Variant == metaData.SqlDbType) { @@ -2819,21 +2816,21 @@ private static void SetDecimal_PossiblyMoney(SmiEventSink_Default sink, ITypedSe } // Hard coding smalldatetime limits... - private static readonly DateTime x_dtSmallMax = new DateTime(2079, 06, 06, 23, 59, 29, 998); - private static readonly DateTime x_dtSmallMin = new DateTime(1899, 12, 31, 23, 59, 29, 999); + private static readonly DateTime s_dtSmallMax = new DateTime(2079, 06, 06, 23, 59, 29, 998); + private static readonly DateTime s_dtSmallMin = new DateTime(1899, 12, 31, 23, 59, 29, 999); private static void VerifyDateTimeRange(SqlDbType dbType, DateTime value) { - if (SqlDbType.SmallDateTime == dbType && (x_dtSmallMax < value || x_dtSmallMin > value)) + if (SqlDbType.SmallDateTime == dbType && (s_dtSmallMax < value || s_dtSmallMin > value)) { throw ADP.InvalidMetaDataValue(); } } - private static readonly TimeSpan x_timeMin = TimeSpan.Zero; - private static readonly TimeSpan x_timeMax = new TimeSpan(TimeSpan.TicksPerDay - 1); + private static readonly TimeSpan s_timeMin = TimeSpan.Zero; + private static readonly TimeSpan s_timeMax = new TimeSpan(TimeSpan.TicksPerDay - 1); private static void VerifyTimeRange(SqlDbType dbType, TimeSpan value) { - if (SqlDbType.Time == dbType && (x_timeMin > value || value > x_timeMax)) + if (SqlDbType.Time == dbType && (s_timeMin > value || value > s_timeMax)) { throw ADP.InvalidMetaDataValue(); } @@ -2917,7 +2914,7 @@ private static void SetBytes_FromRecord(SmiEventSink_Default sink, ITypedSetters // Deal with large values by sending bufferLength of NoLengthLimit (== assume // CheckXetParameters will ignore requested-length checks in this case long bufferLength = record.GetBytes(ordinal, 0, null, 0, 0); - if (bufferLength > Int32.MaxValue) + if (bufferLength > int.MaxValue) { bufferLength = NoLengthLimit; } @@ -3006,7 +3003,7 @@ private static void SetSqlBytes_LengthChecked(SmiEventSink_Default sink, ITypedS // Deal with large values by sending bufferLength of NoLengthLimit (== assume // CheckXetParameters will ignore requested-length checks in this case long bufferLength = value.Length; - if (bufferLength > Int32.MaxValue) + if (bufferLength > int.MaxValue) { bufferLength = NoLengthLimit; } @@ -3022,7 +3019,7 @@ private static void SetChars_FromRecord(SmiEventSink_Default sink, ITypedSetters // Deal with large values by sending bufferLength of NoLengthLimit // CheckXetParameters will ignore length checks in this case long bufferLength = record.GetChars(ordinal, 0, null, 0, 0); - if (bufferLength > Int32.MaxValue) + if (bufferLength > int.MaxValue) { bufferLength = NoLengthLimit; } @@ -3161,7 +3158,7 @@ private static void SetSqlChars_LengthChecked(SmiEventSink_Default sink, ITypedS // Deal with large values by sending bufferLength of NoLengthLimit // CheckXetParameters will ignore length checks in this case long bufferLength = value.Length; - if (bufferLength > Int32.MaxValue) + if (bufferLength > int.MaxValue) { bufferLength = NoLengthLimit; } @@ -3201,7 +3198,7 @@ private static void SetUdt_LengthChecked(SmiEventSink_Default sink, ITypedSetter } else { - System.IO.Stream target = new SmiSettersStream(sink, setters, ordinal, metaData); + Stream target = new SmiSettersStream(sink, setters, ordinal, metaData); SerializationHelperSql9.Serialize(target, value); } } @@ -3229,12 +3226,12 @@ private static void ThrowIfITypedGettersIsNull(SmiEventSink_Default sink, ITyped private static bool CanAccessGetterDirectly(SmiMetaData metaData, ExtendedClrTypeCode setterTypeCode) { // Make sure no-one adds new ExtendedType, nor SqlDbType without updating this file! - Debug.Assert(ExtendedClrTypeCode.First == 0 && (int)ExtendedClrTypeCode.Last == __canAccessGetterDirectly.GetLength(0) - 1, "ExtendedClrTypeCodes does not match with __canAccessGetterDirectly"); - Debug.Assert(SqlDbType.BigInt == 0 && (int)SqlDbType.DateTimeOffset == __canAccessGetterDirectly.GetLength(1) - 1, "SqlDbType does not match with __canAccessGetterDirectly"); + Debug.Assert(ExtendedClrTypeCode.First == 0 && (int)ExtendedClrTypeCode.Last == s_canAccessGetterDirectly.GetLength(0) - 1, "ExtendedClrTypeCodes does not match with __canAccessGetterDirectly"); + Debug.Assert(SqlDbType.BigInt == 0 && (int)SqlDbType.DateTimeOffset == s_canAccessGetterDirectly.GetLength(1) - 1, "SqlDbType does not match with __canAccessGetterDirectly"); Debug.Assert(ExtendedClrTypeCode.First <= setterTypeCode && ExtendedClrTypeCode.Last >= setterTypeCode); Debug.Assert(SqlDbType.BigInt <= metaData.SqlDbType && SqlDbType.DateTimeOffset >= metaData.SqlDbType); - bool returnValue = __canAccessGetterDirectly[(int)setterTypeCode, (int)metaData.SqlDbType]; + bool returnValue = s_canAccessGetterDirectly[(int)setterTypeCode, (int)metaData.SqlDbType]; // Additional restrictions to distinguish TVPs and Structured UDTs if (returnValue && @@ -3251,12 +3248,12 @@ private static bool CanAccessGetterDirectly(SmiMetaData metaData, ExtendedClrTyp private static bool CanAccessSetterDirectly(SmiMetaData metaData, ExtendedClrTypeCode setterTypeCode) { // Make sure no-one adds new ExtendedType, nor SqlDbType without updating this file! - Debug.Assert(ExtendedClrTypeCode.First == 0 && (int)ExtendedClrTypeCode.Last == __canAccessSetterDirectly.GetLength(0) - 1, "ExtendedClrTypeCodes does not match with __canAccessSetterDirectly"); - Debug.Assert(SqlDbType.BigInt == 0 && (int)SqlDbType.DateTimeOffset == __canAccessSetterDirectly.GetLength(1) - 1, "SqlDbType does not match with __canAccessSetterDirectly"); + Debug.Assert(ExtendedClrTypeCode.First == 0 && (int)ExtendedClrTypeCode.Last == s_canAccessSetterDirectly.GetLength(0) - 1, "ExtendedClrTypeCodes does not match with __canAccessSetterDirectly"); + Debug.Assert(SqlDbType.BigInt == 0 && (int)SqlDbType.DateTimeOffset == s_canAccessSetterDirectly.GetLength(1) - 1, "SqlDbType does not match with __canAccessSetterDirectly"); Debug.Assert(ExtendedClrTypeCode.First <= setterTypeCode && ExtendedClrTypeCode.Last >= setterTypeCode); Debug.Assert(SqlDbType.BigInt <= metaData.SqlDbType && SqlDbType.DateTimeOffset >= metaData.SqlDbType); - bool returnValue = __canAccessSetterDirectly[(int)setterTypeCode, (int)metaData.SqlDbType]; + bool returnValue = s_canAccessSetterDirectly[(int)setterTypeCode, (int)metaData.SqlDbType]; // Additional restrictions to distinguish TVPs and Structured UDTs if (returnValue && @@ -3296,12 +3293,12 @@ private static int CheckXetParameters( int length) { if (0 > fieldOffset) - throw ADP.NegativeParameter("fieldOffset"); + throw ADP.NegativeParameter(nameof(fieldOffset)); // if negative buffer index, throw if (bufferOffset < 0) { - throw ADP.InvalidDestinationBufferIndex(bufferLength, bufferOffset, "bufferOffset"); + throw ADP.InvalidDestinationBufferIndex(bufferLength, bufferOffset, nameof(bufferOffset)); } // skip further length checks for LOB buffer lengths @@ -3318,7 +3315,7 @@ private static int CheckXetParameters( // if bad buffer index, throw if (bufferOffset > bufferLength) { - throw ADP.InvalidDestinationBufferIndex(bufferLength, bufferOffset, "bufferOffset"); + throw ADP.InvalidDestinationBufferIndex(bufferLength, bufferOffset, nameof(bufferOffset)); } // if there is not enough room in the buffer for data @@ -3376,12 +3373,12 @@ private static int CheckXetParameters( // The tables should only be accessed from the CanAccessXetterDirectly methods. // - // A couple of private constants to increase the getter/setter access tables' contrast + // A couple of private constants to increase the getter/setter access tables' constrast private const bool X = true; private const bool _ = false; - private static bool[,] __canAccessGetterDirectly = { - // SqlDbTypes as columns (abreviated, but in order) + private static bool[,] s_canAccessGetterDirectly = { + // SqlDbTypes as columns (abbreviated, but in order) // ExtendedClrTypeCodes as rows // BI, Bin, Bit, Ch, DT, Dec, Fl, Im, Int, Mny, NCh, NTx, NVC, Rl, UI, SDT, SI, SMn, Txt, TS, TI, VBn, VCh, Var, 24, Xml, 26, 27, 28, Udt, St, Dat, Tm, DT2, DTO @@ -3433,7 +3430,7 @@ private static int CheckXetParameters( // BI, Bin, Bit, Ch, DT, Dec, Fl, Im, Int, Mny, NCh, NTx, NVC, Rl, UI, SDT, SI, SMn, Txt, TS, TI, VBn, VCh, Var, 24, Xml, 26, 27, 28, Udt, St, Dat, Tm, DT2, DTO }; - private static bool[,] __canAccessSetterDirectly = { + private static bool[,] s_canAccessSetterDirectly = { // Setters as columns (labels are abreviated from ExtendedClrTypeCode names) // SqlDbTypes as rows // BI, Bin, Bit, Ch, DT, Dec, Fl, Im, Int, Mny, NCh, NTx, NVC, Rl, UI, SDT, SI, SMn, Txt, TS, TI, VBn, VCh, Var, 24, Xml, 26, 27, 28, Udt, St, Dat, Tm, DT2, DTO @@ -3610,11 +3607,11 @@ private static DateTimeOffset GetDateTimeOffset_Unchecked(SmiEventSink_Default s return result; } - private static Double GetDouble_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) + private static double GetDouble_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) { Debug.Assert(!IsDBNull_Unchecked(sink, getters, ordinal)); - Double result = getters.GetDouble(sink, ordinal); + double result = getters.GetDouble(sink, ordinal); sink.ProcessMessagesAndThrow(); return result; } @@ -3628,38 +3625,38 @@ private static Guid GetGuid_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 return result; } - private static Int16 GetInt16_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) + private static short GetInt16_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) { Debug.Assert(!IsDBNull_Unchecked(sink, getters, ordinal)); - Int16 result = getters.GetInt16(sink, ordinal); + short result = getters.GetInt16(sink, ordinal); sink.ProcessMessagesAndThrow(); return result; } - private static Int32 GetInt32_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) + private static int GetInt32_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) { Debug.Assert(!IsDBNull_Unchecked(sink, getters, ordinal)); - Int32 result = getters.GetInt32(sink, ordinal); + int result = getters.GetInt32(sink, ordinal); sink.ProcessMessagesAndThrow(); return result; } - private static Int64 GetInt64_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) + private static long GetInt64_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) { Debug.Assert(!IsDBNull_Unchecked(sink, getters, ordinal)); - Int64 result = getters.GetInt64(sink, ordinal); + long result = getters.GetInt64(sink, ordinal); sink.ProcessMessagesAndThrow(); return result; } - private static Single GetSingle_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) + private static float GetSingle_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) { Debug.Assert(!IsDBNull_Unchecked(sink, getters, ordinal)); - Single result = getters.GetSingle(sink, ordinal); + float result = getters.GetSingle(sink, ordinal); sink.ProcessMessagesAndThrow(); return result; } @@ -3685,7 +3682,7 @@ private static SqlMoney GetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedG { Debug.Assert(!IsDBNull_Unchecked(sink, getters, ordinal)); - Int64 temp = getters.GetInt64(sink, ordinal); + long temp = getters.GetInt64(sink, ordinal); sink.ProcessMessagesAndThrow(); return SqlTypeWorkarounds.SqlMoneyCtor(temp, 1 /* ignored */ ); } @@ -3733,7 +3730,7 @@ private static TimeSpan GetTimeSpan_Unchecked(SmiEventSink_Default sink, SmiType return result; } - private static void SetBoolean_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Boolean value) + private static void SetBoolean_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, bool value) { setters.SetBoolean(sink, ordinal, value); sink.ProcessMessagesAndThrow(); @@ -3816,7 +3813,7 @@ private static void SetTextReader_Unchecked(SmiEventSink_Default sink, ITypedSet sink.ProcessMessagesAndThrow(); } - private static void SetByte_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Byte value) + private static void SetByte_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, byte value) { setters.SetByte(sink, ordinal, value); sink.ProcessMessagesAndThrow(); @@ -3853,7 +3850,7 @@ private static void SetDBNull_Unchecked(SmiEventSink_Default sink, ITypedSetters sink.ProcessMessagesAndThrow(); } - private static void SetDecimal_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Decimal value) + private static void SetDecimal_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, decimal value) { setters.SetSqlDecimal(sink, ordinal, new SqlDecimal(value)); sink.ProcessMessagesAndThrow(); @@ -3893,7 +3890,7 @@ private static void SetDateTimeOffset_Unchecked(SmiEventSink_Default sink, SmiTy sink.ProcessMessagesAndThrow(); } - private static void SetDouble_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Double value) + private static void SetDouble_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, double value) { setters.SetDouble(sink, ordinal, value); sink.ProcessMessagesAndThrow(); @@ -3905,25 +3902,25 @@ private static void SetGuid_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 sink.ProcessMessagesAndThrow(); } - private static void SetInt16_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Int16 value) + private static void SetInt16_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, short value) { setters.SetInt16(sink, ordinal, value); sink.ProcessMessagesAndThrow(); } - private static void SetInt32_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Int32 value) + private static void SetInt32_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, int value) { setters.SetInt32(sink, ordinal, value); sink.ProcessMessagesAndThrow(); } - private static void SetInt64_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Int64 value) + private static void SetInt64_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, long value) { setters.SetInt64(sink, ordinal, value); sink.ProcessMessagesAndThrow(); } - private static void SetSingle_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, Single value) + private static void SetSingle_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, float value) { setters.SetSingle(sink, ordinal, value); sink.ProcessMessagesAndThrow(); @@ -4168,8 +4165,7 @@ private static void SetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedSette sink.ProcessMessagesAndThrow(); } - long data = SqlTypeWorkarounds.SqlMoneyToSqlInternalRepresentation(value); - setters.SetInt64(sink, ordinal, data); + setters.SetInt64(sink, ordinal, SqlTypeWorkarounds.SqlMoneyToSqlInternalRepresentation(value)); } sink.ProcessMessagesAndThrow(); } @@ -4250,7 +4246,7 @@ private static void SetXmlReader_Unchecked(SmiEventSink_Default sink, ITypedSett sink.ProcessMessagesAndThrow(); } - private static void SetString_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, String value, int offset, int length) + private static void SetString_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, string value, int offset, int length) { setters.SetString(sink, ordinal, value, offset, length); sink.ProcessMessagesAndThrow(); @@ -4419,7 +4415,6 @@ ParameterPeekAheadValue peekAhead } } } - } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs index 4c07a40437..e003734764 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs @@ -11,7 +11,6 @@ namespace Microsoft.Data.SqlClient.Server { - internal class SerializationHelperSql9 { // Don't let anyone create an instance of this class. @@ -23,10 +22,7 @@ private SerializationHelperSql9() { } // in bytes. // Prevent inlining so that reflection calls are not moved to caller that may be in a different assembly that may have a different grant set. [MethodImpl(MethodImplOptions.NoInlining)] - internal static int SizeInBytes(Type t) - { - return SizeInBytes(Activator.CreateInstance(t)); - } + internal static int SizeInBytes(Type t) => SizeInBytes(Activator.CreateInstance(t)); // Get the m_size of the serialized stream for this type, in bytes. internal static int SizeInBytes(object instance) @@ -44,35 +40,29 @@ internal static void Serialize(Stream s, object instance) GetSerializer(instance.GetType()).Serialize(s, instance); } - internal static object Deserialize(Stream s, Type resultType) - { - return GetSerializer(resultType).Deserialize(s); - } + internal static object Deserialize(Stream s, Type resultType) => GetSerializer(resultType).Deserialize(s); - private static Format GetFormat(Type t) - { - return GetUdtAttribute(t).Format; - } + private static Format GetFormat(Type t) => GetUdtAttribute(t).Format; - //cache the relationship between a type and its serializer - //this is expensive to compute since it involves traversing the - //custom attributes of the type using reflection. + // Cache the relationship between a type and its serializer. + // This is expensive to compute since it involves traversing the + // custom attributes of the type using reflection. // - //use a per-thread cache, so that there are no synchronization - //issues when accessing cache entries from multiple threads. + // Use a per-thread cache, so that there are no synchronization + // issues when accessing cache entries from multiple threads. [ThreadStatic] - private static Hashtable m_types2Serializers; + private static Hashtable s_types2Serializers; private static Serializer GetSerializer(Type t) { - if (m_types2Serializers == null) - m_types2Serializers = new Hashtable(); + if (s_types2Serializers == null) + s_types2Serializers = new Hashtable(); - Serializer s = (Serializer)m_types2Serializers[t]; + Serializer s = (Serializer)s_types2Serializers[t]; if (s == null) { - s = (Serializer)GetNewSerializer(t); - m_types2Serializers[t] = s; + s = GetNewSerializer(t); + s_types2Serializers[t] = s; } return s; } @@ -83,13 +73,13 @@ internal static int GetUdtMaxLength(Type t) if (Format.Native == udtInfo.SerializationFormat) { - //In the native format, the user does not specify the - //max byte size, it is computed from the type definition + // In the native format, the user does not specify the + // max byte size, it is computed from the type definition return SerializationHelperSql9.SizeInBytes(t); } else { - //In all other formats, the user specifies the maximum size in bytes. + // In all other formats, the user specifies the maximum size in bytes. return udtInfo.MaxByteSize; } } @@ -187,38 +177,31 @@ internal abstract class Serializer { public abstract object Deserialize(Stream s); public abstract void Serialize(Stream s, object o); - protected Type m_type; + protected Type _type; protected Serializer(Type t) { - this.m_type = t; + _type = t; } } internal sealed class NormalizedSerializer : Serializer { - BinaryOrderedUdtNormalizer m_normalizer; - bool m_isFixedSize; - int m_maxSize; + private BinaryOrderedUdtNormalizer _normalizer; + private bool _isFixedSize; + private int _maxSize; internal NormalizedSerializer(Type t) : base(t) { SqlUserDefinedTypeAttribute udtAttr = SerializationHelperSql9.GetUdtAttribute(t); - this.m_normalizer = new BinaryOrderedUdtNormalizer(t, true); - this.m_isFixedSize = udtAttr.IsFixedLength; - this.m_maxSize = this.m_normalizer.Size; + _normalizer = new BinaryOrderedUdtNormalizer(t, true); + _isFixedSize = udtAttr.IsFixedLength; + _maxSize = _normalizer.Size; } - public override void Serialize(Stream s, object o) - { - m_normalizer.NormalizeTopObject(o, s); - } + public override void Serialize(Stream s, object o) => _normalizer.NormalizeTopObject(o, s); - public override object Deserialize(Stream s) - { - object result = m_normalizer.DeNormalizeTopObject(this.m_type, s); - return result; - } + public override object Deserialize(Stream s) => _normalizer.DeNormalizeTopObject(_type, s); } internal sealed class BinarySerializeSerializer : Serializer @@ -240,11 +223,13 @@ public override void Serialize(Stream s, object o) } } - // Prevent inlining so that reflection calls are not moved to caller that may be in a different assembly that may have a different grant set. + // Prevent inlining so that reflection calls are not moved + // to a caller that may be in a different assembly that may + // have a different grant set. [MethodImpl(MethodImplOptions.NoInlining)] public override object Deserialize(Stream s) { - object instance = Activator.CreateInstance(m_type); + object instance = Activator.CreateInstance(_type); BinaryReader r = new BinaryReader(s); if (instance is Microsoft.SqlServer.Server.IBinarySerialize) { @@ -262,7 +247,7 @@ public override object Deserialize(Stream s) // to the stream. internal sealed class DummyStream : Stream { - private long m_size; + private long _size; public DummyStream() { @@ -273,54 +258,21 @@ private void DontDoIt() throw new Exception(StringsHelper.GetString(Strings.Sql_InternalError)); } - public override bool CanRead - { - get - { - return false; - } - } + public override bool CanRead => false; - public override bool CanWrite - { - get - { - return true; - } - } + public override bool CanWrite => true; - public override bool CanSeek - { - get - { - return false; - } - } + public override bool CanSeek => false; public override long Position { - get - { - return this.m_size; - } - set - { - this.m_size = value; - } + get => _size; + set =>_size = value; } - public override long Length - { - get - { - return this.m_size; - } - } + public override long Length => _size; - public override void SetLength(long value) - { - this.m_size = value; - } + public override void SetLength(long value) => _size = value; public override long Seek(long value, SeekOrigin loc) { @@ -340,7 +292,7 @@ public override int Read(byte[] buffer, int offset, int count) public override void Write(byte[] buffer, int offset, int count) { - this.m_size += count; + _size += count; } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs similarity index 57% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs index 8a5b0a722a..e9f6b44065 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -10,7 +10,7 @@ namespace Microsoft.Data.SqlClient.Server { - /// + /// // class SqlMetaData // Simple immutable implementation of the a metadata-holding class. Only // complexities are: @@ -19,161 +19,366 @@ namespace Microsoft.Data.SqlClient.Server // 3) Adjusting a value to match the metadata. public sealed partial class SqlMetaData { - private string _strName; - private long _lMaxLength; + private const long MaxUnicodeLength = 4000; + private const long MaxANSILength = 8000; + private const long MaxBinaryLength = 8000; + private const long UnlimitedMaxLength = -1; // unlimited (except by implementation) max-length. + private const bool DefaultUseServerDefault = false; + private const bool DefaultIsUniqueKey = false; + private const SortOrder DefaultColumnSortOrder = SortOrder.Unspecified; + private const int DefaultSortOrdinal = -1; + private const byte MaxTimeScale = 7; + + private const SqlCompareOptions DefaultStringCompareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth; + + private static readonly SqlMoney s_smallMoneyMax = new SqlMoney(((decimal)int.MaxValue) / 10000); + private static readonly SqlMoney s_smallMoneyMin = new SqlMoney(((decimal)int.MinValue) / 10000); + + private static readonly DateTime s_smallDateTimeMax = new DateTime(2079, 06, 06, 23, 59, 29, 998); + private static readonly DateTime s_smallDateTimeMin = new DateTime(1899, 12, 31, 23, 59, 29, 999); + + private static readonly TimeSpan s_timeMin = TimeSpan.Zero; + private static readonly TimeSpan s_timeMax = new TimeSpan(TimeSpan.TicksPerDay - 1); + + private static readonly byte[] s_maxLenFromPrecision = new byte[] {5,5,5,5,5,5,5,5,5,9,9,9,9,9, + 9,9,9,9,9,13,13,13,13,13,13,13,13,13,17,17,17,17,17,17,17,17,17,17}; + + private static readonly byte[] s_maxVarTimeLenOffsetFromScale = new byte[] { 2, 2, 2, 1, 1, 0, 0, 0 }; + + private static readonly long[] s_unitTicksFromScale = { + 10000000, + 1000000, + 100000, + 10000, + 1000, + 100, + 10, + 1, + }; + + private static readonly DbType[] s_sqlDbTypeToDbType = { + DbType.Int64, // SqlDbType.BigInt + DbType.Binary, // SqlDbType.Binary + DbType.Boolean, // SqlDbType.Bit + DbType.AnsiString, // SqlDbType.Char + DbType.DateTime, // SqlDbType.DateTime + DbType.Decimal, // SqlDbType.Decimal + DbType.Double, // SqlDbType.Float + DbType.Binary, // SqlDbType.Image + DbType.Int32, // SqlDbType.Int + DbType.Currency, // SqlDbType.Money + DbType.String, // SqlDbType.NChar + DbType.String, // SqlDbType.NText + DbType.String, // SqlDbType.NVarChar + DbType.Single, // SqlDbType.Real + DbType.Guid, // SqlDbType.UniqueIdentifier + DbType.DateTime, // SqlDbType.SmallDateTime + DbType.Int16, // SqlDbType.SmallInt + DbType.Currency, // SqlDbType.SmallMoney + DbType.AnsiString, // SqlDbType.Text + DbType.Binary, // SqlDbType.Timestamp + DbType.Byte, // SqlDbType.TinyInt + DbType.Binary, // SqlDbType.VarBinary + DbType.AnsiString, // SqlDbType.VarChar + DbType.Object, // SqlDbType.Variant + DbType.Object, // SqlDbType.Row + DbType.Xml, // SqlDbType.Xml + DbType.String, // SqlDbType.NVarChar, place holder + DbType.String, // SqlDbType.NVarChar, place holder + DbType.String, // SqlDbType.NVarChar, place holder + DbType.Object, // SqlDbType.Udt + DbType.Object, // SqlDbType.Structured + DbType.Date, // SqlDbType.Date + DbType.Time, // SqlDbType.Time + DbType.DateTime2, // SqlDbType.DateTime2 + DbType.DateTimeOffset // SqlDbType.DateTimeOffset + }; + + + // Array of default-valued metadata ordered by corresponding SqlDbType. + internal static SqlMetaData[] s_defaults = + { + // new SqlMetaData(name, DbType, SqlDbType, MaxLen, Prec, Scale, Locale, DatabaseName, SchemaName, isPartialLength) + new SqlMetaData("bigint", SqlDbType.BigInt, + 8, 19, 0, 0, SqlCompareOptions.None, false), // SqlDbType.BigInt + new SqlMetaData("binary", SqlDbType.Binary, + 1, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Binary + new SqlMetaData("bit", SqlDbType.Bit, + 1, 1, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Bit + new SqlMetaData("char", SqlDbType.Char, + 1, 0, 0, 0, DefaultStringCompareOptions, false), // SqlDbType.Char + new SqlMetaData("datetime", SqlDbType.DateTime, + 8, 23, 3, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime + new SqlMetaData("decimal", SqlDbType.Decimal, + 9, 18, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Decimal + new SqlMetaData("float", SqlDbType.Float, + 8, 53, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Float + new SqlMetaData("image", SqlDbType.Image, + UnlimitedMaxLength, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Image + new SqlMetaData("int", SqlDbType.Int, + 4, 10, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Int + new SqlMetaData("money", SqlDbType.Money, + 8, 19, 4, 0, SqlCompareOptions.None, false), // SqlDbType.Money + new SqlMetaData("nchar", SqlDbType.NChar, + 1, 0, 0, 0, DefaultStringCompareOptions, false), // SqlDbType.NChar + new SqlMetaData("ntext", SqlDbType.NText, + UnlimitedMaxLength, 0, 0, 0, DefaultStringCompareOptions, false), // SqlDbType.NText + new SqlMetaData("nvarchar", SqlDbType.NVarChar, + MaxUnicodeLength, 0, 0, 0, DefaultStringCompareOptions, false), // SqlDbType.NVarChar + new SqlMetaData("real", SqlDbType.Real, + 4, 24, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Real + new SqlMetaData("uniqueidentifier", SqlDbType.UniqueIdentifier, + 16, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.UniqueIdentifier + new SqlMetaData("smalldatetime", SqlDbType.SmallDateTime, + 4, 16, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallDateTime + new SqlMetaData("smallint", SqlDbType.SmallInt, + 2, 5, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallInt + new SqlMetaData("smallmoney", SqlDbType.SmallMoney, + 4, 10, 4, 0, SqlCompareOptions.None, false), // SqlDbType.SmallMoney + new SqlMetaData("text", SqlDbType.Text, + UnlimitedMaxLength, 0, 0, 0, DefaultStringCompareOptions, false), // SqlDbType.Text + new SqlMetaData("timestamp", SqlDbType.Timestamp, + 8, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Timestamp + new SqlMetaData("tinyint", SqlDbType.TinyInt, + 1, 3, 0, 0, SqlCompareOptions.None, false), // SqlDbType.TinyInt + new SqlMetaData("varbinary", SqlDbType.VarBinary, + MaxBinaryLength, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.VarBinary + new SqlMetaData("varchar", SqlDbType.VarChar, + MaxANSILength, 0, 0, 0, DefaultStringCompareOptions, false), // SqlDbType.VarChar + new SqlMetaData("sql_variant", SqlDbType.Variant, + 8016, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Variant + new SqlMetaData("nvarchar", SqlDbType.NVarChar, + 1, 0, 0, 0, DefaultStringCompareOptions, false), // Placeholder for value 24 + new SqlMetaData("xml", SqlDbType.Xml, + UnlimitedMaxLength, 0, 0, 0, DefaultStringCompareOptions, true), // SqlDbType.Xml + new SqlMetaData("nvarchar", SqlDbType.NVarChar, + 1, 0, 0, 0, DefaultStringCompareOptions, false), // Placeholder for value 26 + new SqlMetaData("nvarchar", SqlDbType.NVarChar, + MaxUnicodeLength, 0, 0, 0, DefaultStringCompareOptions, false), // Placeholder for value 27 + new SqlMetaData("nvarchar", SqlDbType.NVarChar, + MaxUnicodeLength, 0, 0, 0, DefaultStringCompareOptions, false), // Placeholder for value 28 + new SqlMetaData("udt", SqlDbType.Udt, + 0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Udt = 29 + new SqlMetaData("table", SqlDbType.Structured, + 0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Structured + new SqlMetaData("date", SqlDbType.Date, + 3, 10,0, 0, SqlCompareOptions.None, false), // SqlDbType.Date + new SqlMetaData("time", SqlDbType.Time, + 5, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.Time + new SqlMetaData("datetime2", SqlDbType.DateTime2, + 8, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime2 + new SqlMetaData("datetimeoffset", SqlDbType.DateTimeOffset, + 10, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTimeOffset + }; + + private string _name; + private long _maxLength; private SqlDbType _sqlDbType; - private byte _bPrecision; - private byte _bScale; - private long _lLocale; - private SqlCompareOptions _eCompareOptions; + private byte _precision; + private byte _scale; + private long _locale; + private SqlCompareOptions _compareOptions; private string _xmlSchemaCollectionDatabase; private string _xmlSchemaCollectionOwningSchema; private string _xmlSchemaCollectionName; private string _serverTypeName; - private bool _bPartialLength; + private bool _partialLength; private Type _udtType; private bool _useServerDefault; private bool _isUniqueKey; private SortOrder _columnSortOrder; private int _sortOrdinal; - // unlimited (except by implementation) max-length. - private const long x_lMax = -1; - private const long x_lServerMaxUnicode = 4000; - private const long x_lServerMaxANSI = 8000; - private const long x_lServerMaxBinary = 8000; - private const bool x_defaultUseServerDefault = false; - private const bool x_defaultIsUniqueKey = false; - private const SortOrder x_defaultColumnSortOrder = SortOrder.Unspecified; - private const int x_defaultSortOrdinal = -1; - private const SqlCompareOptions x_eDefaultStringCompareOptions = SqlCompareOptions.IgnoreCase - | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth; - - /// + /// // scalar types constructor without tvp extended properties public SqlMetaData(string name, SqlDbType dbType) { - Construct(name, dbType, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); + Construct(name, dbType, DefaultUseServerDefault, DefaultIsUniqueKey, DefaultColumnSortOrder, DefaultSortOrdinal); } - /// + /// // scalar types constructor - public SqlMetaData(string name, SqlDbType dbType, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + public SqlMetaData( + string name, + SqlDbType dbType, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { - Construct(name, dbType, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); + Construct(name, dbType, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); } - /// + /// // binary or string constructor with only max length // (for string types, locale and compare options will be picked up from the thread. public SqlMetaData(string name, SqlDbType dbType, long maxLength) { - Construct(name, dbType, maxLength, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); + Construct(name, dbType, maxLength, DefaultUseServerDefault, DefaultIsUniqueKey, DefaultColumnSortOrder, DefaultSortOrdinal); } - /// + /// // binary or string constructor with only max length and tvp extended properties // (for string types, locale and compare options will be picked up from the thread. - public SqlMetaData(string name, SqlDbType dbType, long maxLength, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + public SqlMetaData( + string name, + SqlDbType dbType, + long maxLength, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { - Construct(name, dbType, maxLength, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); + Construct(name, dbType, maxLength, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); } - /// + /// // udt ctor without tvp extended properties public SqlMetaData(string name, SqlDbType dbType, Type userDefinedType) { - Construct(name, dbType, userDefinedType, null, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); + Construct(name, dbType, userDefinedType, null, DefaultUseServerDefault, DefaultIsUniqueKey, DefaultColumnSortOrder, DefaultSortOrdinal); } - /// + /// // udt ctor without tvp extended properties public SqlMetaData(string name, SqlDbType dbType, Type userDefinedType, string serverTypeName) { - Construct(name, dbType, userDefinedType, serverTypeName, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); + Construct(name, dbType, userDefinedType, serverTypeName, DefaultUseServerDefault, DefaultIsUniqueKey, DefaultColumnSortOrder, DefaultSortOrdinal); } - /// + /// // udt ctor - public SqlMetaData(string name, SqlDbType dbType, Type userDefinedType, string serverTypeName, - bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + public SqlMetaData( + string name, + SqlDbType dbType, + Type userDefinedType, + string serverTypeName, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { - Construct(name, dbType, userDefinedType, serverTypeName, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); + Construct(name, dbType, userDefinedType, serverTypeName, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); } - /// + /// // decimal ctor without tvp extended properties public SqlMetaData(string name, SqlDbType dbType, byte precision, byte scale) { - Construct(name, dbType, precision, scale, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); + Construct(name, dbType, precision, scale, DefaultUseServerDefault, DefaultIsUniqueKey, DefaultColumnSortOrder, DefaultSortOrdinal); } - /// + /// // decimal ctor - public SqlMetaData(string name, SqlDbType dbType, byte precision, byte scale, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + public SqlMetaData( + string name, + SqlDbType dbType, + byte precision, + byte scale, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { - Construct(name, dbType, precision, scale, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); + Construct(name, dbType, precision, scale, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); } - /// + /// // string type constructor with locale and compare options, no tvp extended properties - public SqlMetaData(string name, SqlDbType dbType, long maxLength, long locale, - SqlCompareOptions compareOptions) + public SqlMetaData( + string name, + SqlDbType dbType, + long maxLength, + long locale, + SqlCompareOptions compareOptions + ) { - Construct(name, dbType, maxLength, locale, compareOptions, x_defaultUseServerDefault, - x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal); + Construct(name, dbType, maxLength, locale, compareOptions, DefaultUseServerDefault, DefaultIsUniqueKey, DefaultColumnSortOrder, DefaultSortOrdinal); } - /// + /// // string type constructor with locale and compare options - public SqlMetaData(string name, SqlDbType dbType, long maxLength, long locale, - SqlCompareOptions compareOptions, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, maxLength, locale, compareOptions, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// + public SqlMetaData( + string name, + SqlDbType dbType, + long maxLength, + long locale, + SqlCompareOptions compareOptions, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) + { + Construct(name, dbType, maxLength, locale, compareOptions, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); + } + + /// // typed xml ctor - public SqlMetaData(string name, SqlDbType dbType, string database, string owningSchema, - string objectName, bool useServerDefault, bool isUniqueKey, - SortOrder columnSortOrder, int sortOrdinal) - { - Construct(name, dbType, database, owningSchema, objectName, useServerDefault, - isUniqueKey, columnSortOrder, sortOrdinal); - } - - /// + public SqlMetaData( + string name, + SqlDbType dbType, + string database, + string owningSchema, + string objectName, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) + { + Construct(name, dbType, database, owningSchema, objectName, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); + } + + /// // everything except xml schema and tvp properties ctor - public SqlMetaData(string name, SqlDbType dbType, long maxLength, byte precision, - byte scale, long locale, SqlCompareOptions compareOptions, - Type userDefinedType) : - this(name, dbType, maxLength, precision, scale, locale, compareOptions, - userDefinedType, x_defaultUseServerDefault, x_defaultIsUniqueKey, - x_defaultColumnSortOrder, x_defaultSortOrdinal) - { - } - - /// + public SqlMetaData( + string name, + SqlDbType dbType, + long maxLength, + byte precision, + byte scale, + long locale, + SqlCompareOptions compareOptions, + Type userDefinedType + ) : this( + name, + dbType, + maxLength, + precision, + scale, + locale, + compareOptions, + userDefinedType, + DefaultUseServerDefault, + DefaultIsUniqueKey, + DefaultColumnSortOrder, + DefaultSortOrdinal + ) + { + } + + /// // everything except xml schema ctor - public SqlMetaData(string name, SqlDbType dbType, long maxLength, byte precision, - byte scale, long localeId, SqlCompareOptions compareOptions, - Type userDefinedType, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + public SqlMetaData( + string name, + SqlDbType dbType, + long maxLength, + byte precision, + byte scale, + long localeId, + SqlCompareOptions compareOptions, + Type userDefinedType, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { switch (dbType) { @@ -220,7 +425,7 @@ public SqlMetaData(string name, SqlDbType dbType, long maxLength, byte precision Construct(name, dbType, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); break; case SqlDbType.Udt: - Construct(name, dbType, userDefinedType, "", useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); + Construct(name, dbType, userDefinedType, string.Empty, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal); break; default: SQL.InvalidSqlDbTypeForConstructor(dbType); @@ -228,147 +433,110 @@ public SqlMetaData(string name, SqlDbType dbType, long maxLength, byte precision } } - /// + /// public SqlMetaData(string name, SqlDbType dbType, string database, string owningSchema, string objectName) { - Construct(name, dbType, database, owningSchema, objectName, x_defaultUseServerDefault, x_defaultIsUniqueKey, - x_defaultColumnSortOrder, x_defaultSortOrdinal); + Construct(name, dbType, database, owningSchema, objectName, DefaultUseServerDefault, DefaultIsUniqueKey, DefaultColumnSortOrder, DefaultSortOrdinal); } // Most general constructor, should be able to initialize all SqlMetaData fields.(Used by SqlParameter) - internal SqlMetaData(string name, - SqlDbType sqlDBType, - long maxLength, - byte precision, - byte scale, - long localeId, - SqlCompareOptions compareOptions, - string xmlSchemaCollectionDatabase, - string xmlSchemaCollectionOwningSchema, - string xmlSchemaCollectionName, - bool partialLength, - Type udtType) + internal SqlMetaData( + string name, + SqlDbType sqlDBType, + long maxLength, + byte precision, + byte scale, + long localeId, + SqlCompareOptions compareOptions, + string xmlSchemaCollectionDatabase, + string xmlSchemaCollectionOwningSchema, + string xmlSchemaCollectionName, + bool partialLength, + Type udtType + ) { AssertNameIsValid(name); - _strName = name; + _name = name; _sqlDbType = sqlDBType; - _lMaxLength = maxLength; - _bPrecision = precision; - _bScale = scale; - _lLocale = localeId; - _eCompareOptions = compareOptions; + _maxLength = maxLength; + _precision = precision; + _scale = scale; + _locale = localeId; + _compareOptions = compareOptions; _xmlSchemaCollectionDatabase = xmlSchemaCollectionDatabase; _xmlSchemaCollectionOwningSchema = xmlSchemaCollectionOwningSchema; _xmlSchemaCollectionName = xmlSchemaCollectionName; - _bPartialLength = partialLength; - + _partialLength = partialLength; _udtType = udtType; } // Private constructor used to initialize default instance array elements. // DO NOT EXPOSE OUTSIDE THIS CLASS! It performs no validation. - private SqlMetaData(string name, - SqlDbType sqlDbType, - long maxLength, - byte precision, - byte scale, - long localeId, - SqlCompareOptions compareOptions, - bool partialLength) + private SqlMetaData( + string name, + SqlDbType sqlDbType, + long maxLength, + byte precision, + byte scale, + long localeId, + SqlCompareOptions compareOptions, + bool partialLength + ) { AssertNameIsValid(name); - _strName = name; + _name = name; _sqlDbType = sqlDbType; - _lMaxLength = maxLength; - _bPrecision = precision; - _bScale = scale; - _lLocale = localeId; - _eCompareOptions = compareOptions; - _bPartialLength = partialLength; + _maxLength = maxLength; + _precision = precision; + _scale = scale; + _locale = localeId; + _compareOptions = compareOptions; + _partialLength = partialLength; _udtType = null; } - /// - public SqlCompareOptions CompareOptions - { - get => _eCompareOptions; - } + /// + public SqlCompareOptions CompareOptions => _compareOptions; - /// - public DbType DbType - { - get => sxm_rgSqlDbTypeToDbType[(int)_sqlDbType]; - } + /// + public DbType DbType => s_sqlDbTypeToDbType[(int)_sqlDbType]; - /// - public bool IsUniqueKey - { - get => _isUniqueKey; - } + /// + public bool IsUniqueKey => _isUniqueKey; - /// - public long LocaleId - { - get => _lLocale; - } + /// + public long LocaleId => _locale; - /// - public static long Max - { - get => x_lMax; - } + /// + public static long Max => UnlimitedMaxLength; - /// - public long MaxLength - { - get => _lMaxLength; - } + /// + public long MaxLength => _maxLength; - /// - public string Name - { - get => _strName; - } + /// + public string Name => _name; - /// - public byte Precision - { - get => _bPrecision; - } + /// + public byte Precision => _precision; - /// - public byte Scale - { - get => _bScale; - } + /// + public byte Scale => _scale; - /// - public SortOrder SortOrder - { - get => _columnSortOrder; - } + /// + public SortOrder SortOrder => _columnSortOrder; - /// - public int SortOrdinal - { - get => _sortOrdinal; - } + /// + public int SortOrdinal => _sortOrdinal; - /// - public SqlDbType SqlDbType - { - get => _sqlDbType; - } + /// + public SqlDbType SqlDbType => _sqlDbType; - /// - public Type Type - { - get => _udtType; - } + /// + public Type Type => _udtType; - /// + /// public string TypeName { get @@ -383,44 +551,26 @@ public string TypeName } else { - return sxm_rgDefaults[(int)SqlDbType].Name; + return s_defaults[(int)SqlDbType].Name; } } } - internal string ServerTypeName - { - get => _serverTypeName; - } + internal string ServerTypeName => _serverTypeName; - /// - public bool UseServerDefault - { - get => _useServerDefault; - } + /// + public bool UseServerDefault => _useServerDefault; - /// - public string XmlSchemaCollectionDatabase - { - get => _xmlSchemaCollectionDatabase; - } + /// + public string XmlSchemaCollectionDatabase => _xmlSchemaCollectionDatabase; - /// - public string XmlSchemaCollectionName - { - get => _xmlSchemaCollectionName; - } + /// + public string XmlSchemaCollectionName => _xmlSchemaCollectionName; - /// - public string XmlSchemaCollectionOwningSchema - { - get => _xmlSchemaCollectionOwningSchema; - } + /// + public string XmlSchemaCollectionOwningSchema => _xmlSchemaCollectionOwningSchema; - internal bool IsPartialLength - { - get => _bPartialLength; - } + internal bool IsPartialLength => _partialLength; internal string UdtTypeName { @@ -442,8 +592,7 @@ internal string UdtTypeName } // Construction for all types that do not have variable attributes - private void Construct(string name, SqlDbType dbType, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + private void Construct(string name, SqlDbType dbType, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) { AssertNameIsValid(name); @@ -472,18 +621,20 @@ private void Construct(string name, SqlDbType dbType, bool useServerDefault, SqlDbType.TinyInt == dbType || SqlDbType.UniqueIdentifier == dbType || SqlDbType.Variant == dbType || - SqlDbType.Xml == dbType)) + SqlDbType.Xml == dbType) + ) + { throw SQL.InvalidSqlDbTypeForConstructor(dbType); + } SetDefaultsForType(dbType); if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) { - _lLocale = CultureInfo.CurrentCulture.LCID; + _locale = System.Globalization.CultureInfo.CurrentCulture.LCID; } - - _strName = name; + _name = name; _useServerDefault = useServerDefault; _isUniqueKey = isUniqueKey; _columnSortOrder = columnSortOrder; @@ -491,8 +642,7 @@ private void Construct(string name, SqlDbType dbType, bool useServerDefault, } // Construction for all types that vary by user-specified length (not Udts) - private void Construct(string name, SqlDbType dbType, long maxLength, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + private void Construct(string name, SqlDbType dbType, long maxLength, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) { AssertNameIsValid(name); @@ -501,59 +651,77 @@ private void Construct(string name, SqlDbType dbType, long maxLength, bool useSe long lLocale = 0; if (SqlDbType.Char == dbType) { - if (maxLength > x_lServerMaxANSI || maxLength < 0) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if (maxLength > MaxANSILength || maxLength < 0) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } lLocale = CultureInfo.CurrentCulture.LCID; } else if (SqlDbType.VarChar == dbType) { - if ((maxLength > x_lServerMaxANSI || maxLength < 0) && maxLength != Max) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if ((maxLength > MaxANSILength || maxLength < 0) && maxLength != Max) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } lLocale = CultureInfo.CurrentCulture.LCID; } else if (SqlDbType.NChar == dbType) { - if (maxLength > x_lServerMaxUnicode || maxLength < 0) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if (maxLength > MaxUnicodeLength || maxLength < 0) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } lLocale = CultureInfo.CurrentCulture.LCID; } else if (SqlDbType.NVarChar == dbType) { - if ((maxLength > x_lServerMaxUnicode || maxLength < 0) && maxLength != Max) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if ((maxLength > MaxUnicodeLength || maxLength < 0) && maxLength != Max) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } lLocale = CultureInfo.CurrentCulture.LCID; } else if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) { // old-style lobs only allowed with Max length if (SqlMetaData.Max != maxLength) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } lLocale = CultureInfo.CurrentCulture.LCID; } else if (SqlDbType.Binary == dbType) { - if (maxLength > x_lServerMaxBinary || maxLength < 0) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if (maxLength > MaxBinaryLength || maxLength < 0) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else if (SqlDbType.VarBinary == dbType) { - if ((maxLength > x_lServerMaxBinary || maxLength < 0) && maxLength != Max) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if ((maxLength > MaxBinaryLength || maxLength < 0) && maxLength != Max) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else if (SqlDbType.Image == dbType) { // old-style lobs only allowed with Max length if (SqlMetaData.Max != maxLength) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else + { throw SQL.InvalidSqlDbTypeForConstructor(dbType); + } SetDefaultsForType(dbType); - _strName = name; - _lMaxLength = maxLength; - _lLocale = lLocale; + _name = name; + _maxLength = maxLength; + _locale = lLocale; _useServerDefault = useServerDefault; _isUniqueKey = isUniqueKey; _columnSortOrder = columnSortOrder; @@ -561,15 +729,17 @@ private void Construct(string name, SqlDbType dbType, long maxLength, bool useSe } // Construction for string types with specified locale/compare options - private void Construct(string name, - SqlDbType dbType, - long maxLength, - long locale, - SqlCompareOptions compareOptions, - bool useServerDefault, - bool isUniqueKey, - SortOrder columnSortOrder, - int sortOrdinal) + private void Construct( + string name, + SqlDbType dbType, + long maxLength, + long locale, + SqlCompareOptions compareOptions, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { AssertNameIsValid(name); @@ -578,32 +748,44 @@ private void Construct(string name, // Validate type and max length. if (SqlDbType.Char == dbType) { - if (maxLength > x_lServerMaxANSI || maxLength < 0) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if (maxLength > MaxANSILength || maxLength < 0) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else if (SqlDbType.VarChar == dbType) { - if ((maxLength > x_lServerMaxANSI || maxLength < 0) && maxLength != Max) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if ((maxLength > MaxANSILength || maxLength < 0) && maxLength != Max) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else if (SqlDbType.NChar == dbType) { - if (maxLength > x_lServerMaxUnicode || maxLength < 0) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if (maxLength > MaxUnicodeLength || maxLength < 0) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else if (SqlDbType.NVarChar == dbType) { - if ((maxLength > x_lServerMaxUnicode || maxLength < 0) && maxLength != Max) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + if ((maxLength > MaxUnicodeLength || maxLength < 0) && maxLength != Max) + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) { // old-style lobs only allowed with Max length if (SqlMetaData.Max != maxLength) - throw ADP.Argument(System.StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + { + throw ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), nameof(maxLength)); + } } else + { throw SQL.InvalidSqlDbTypeForConstructor(dbType); + } // Validate locale? @@ -613,32 +795,35 @@ private void Construct(string name, if (SqlCompareOptions.BinarySort != compareOptions && 0 != (~((int)SqlCompareOptions.IgnoreCase | (int)SqlCompareOptions.IgnoreNonSpace | (int)SqlCompareOptions.IgnoreKanaType | (int)SqlCompareOptions.IgnoreWidth) & - (int)compareOptions)) + (int)compareOptions) + ) + { throw ADP.InvalidEnumerationValue(typeof(SqlCompareOptions), (int)compareOptions); + } SetDefaultsForType(dbType); - _strName = name; - _lMaxLength = maxLength; - _lLocale = locale; - _eCompareOptions = compareOptions; + _name = name; + _maxLength = maxLength; + _locale = locale; + _compareOptions = compareOptions; _useServerDefault = useServerDefault; _isUniqueKey = isUniqueKey; _columnSortOrder = columnSortOrder; _sortOrdinal = sortOrdinal; } - - private static byte[] s_maxLenFromPrecision = new byte[] {5,5,5,5,5,5,5,5,5,9,9,9,9,9, - 9,9,9,9,9,13,13,13,13,13,13,13,13,13,17,17,17,17,17,17,17,17,17,17}; - - private const byte MaxTimeScale = 7; - - private static byte[] s_maxVarTimeLenOffsetFromScale = new byte[] { 2, 2, 2, 1, 1, 0, 0, 0 }; - // Construction for Decimal type and new Katmai Date/Time types - private void Construct(string name, SqlDbType dbType, byte precision, byte scale, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + private void Construct( + string name, + SqlDbType dbType, + byte precision, + byte scale, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { AssertNameIsValid(name); @@ -647,10 +832,14 @@ private void Construct(string name, SqlDbType dbType, byte precision, byte scale if (SqlDbType.Decimal == dbType) { if (precision > SqlDecimal.MaxPrecision || scale > precision) + { throw SQL.PrecisionValueOutOfRange(precision); + } if (scale > SqlDecimal.MaxScale) + { throw SQL.ScaleValueOutOfRange(scale); + } } else if (SqlDbType.Time == dbType || SqlDbType.DateTime2 == dbType || SqlDbType.DateTimeOffset == dbType) { @@ -663,18 +852,19 @@ private void Construct(string name, SqlDbType dbType, byte precision, byte scale { throw SQL.InvalidSqlDbTypeForConstructor(dbType); } + SetDefaultsForType(dbType); - _strName = name; - _bPrecision = precision; - _bScale = scale; + _name = name; + _precision = precision; + _scale = scale; if (SqlDbType.Decimal == dbType) { - _lMaxLength = s_maxLenFromPrecision[precision - 1]; + _maxLength = s_maxLenFromPrecision[precision - 1]; } else { - _lMaxLength -= s_maxVarTimeLenOffsetFromScale[scale]; + _maxLength -= s_maxVarTimeLenOffsetFromScale[scale]; } _useServerDefault = useServerDefault; _isUniqueKey = isUniqueKey; @@ -683,23 +873,35 @@ private void Construct(string name, SqlDbType dbType, byte precision, byte scale } // Construction for Udt type - private void Construct(string name, SqlDbType dbType, Type userDefinedType, string serverTypeName, bool useServerDefault, - bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) + private void Construct( + string name, + SqlDbType dbType, + Type userDefinedType, + string serverTypeName, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { AssertNameIsValid(name); ValidateSortOrder(columnSortOrder, sortOrdinal); if (SqlDbType.Udt != dbType) + { throw SQL.InvalidSqlDbTypeForConstructor(dbType); + } if (null == userDefinedType) + { throw ADP.ArgumentNull(nameof(userDefinedType)); + } SetDefaultsForType(SqlDbType.Udt); - _strName = name; - _lMaxLength = SerializationHelperSql9.GetUdtMaxLength(userDefinedType); + _name = name; + _maxLength = SerializationHelperSql9.GetUdtMaxLength(userDefinedType); _udtType = userDefinedType; _serverTypeName = serverTypeName; _useServerDefault = useServerDefault; @@ -709,16 +911,26 @@ private void Construct(string name, SqlDbType dbType, Type userDefinedType, stri } // Construction for Xml type - private void Construct(string name, SqlDbType dbType, string database, string owningSchema, - string objectName, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, - int sortOrdinal) + private void Construct( + string name, + SqlDbType dbType, + string database, + string owningSchema, + string objectName, + bool useServerDefault, + bool isUniqueKey, + SortOrder columnSortOrder, + int sortOrdinal + ) { AssertNameIsValid(name); ValidateSortOrder(columnSortOrder, sortOrdinal); if (SqlDbType.Xml != dbType) + { throw SQL.InvalidSqlDbTypeForConstructor(dbType); + } if (null != database || null != owningSchema) { @@ -729,8 +941,8 @@ private void Construct(string name, SqlDbType dbType, string database, string ow } SetDefaultsForType(SqlDbType.Xml); - _strName = name; + _name = name; _xmlSchemaCollectionDatabase = database; _xmlSchemaCollectionOwningSchema = owningSchema; _xmlSchemaCollectionName = objectName; @@ -743,70 +955,86 @@ private void Construct(string name, SqlDbType dbType, string database, string ow private void AssertNameIsValid(string name) { if (null == name) + { throw ADP.ArgumentNull(nameof(name)); + } - if (Microsoft.Data.SqlClient.Server.SmiMetaData.MaxNameLength < name.Length) + if (SmiMetaData.MaxNameLength < name.Length) + { throw SQL.NameTooLong(nameof(name)); + } } private void ValidateSortOrder(SortOrder columnSortOrder, int sortOrdinal) { // Check that sort order is valid enum value. - if (SortOrder.Unspecified != columnSortOrder && - SortOrder.Ascending != columnSortOrder && - SortOrder.Descending != columnSortOrder) + if ( + SortOrder.Unspecified != columnSortOrder && + SortOrder.Ascending != columnSortOrder && + SortOrder.Descending != columnSortOrder + ) { throw SQL.InvalidSortOrder(columnSortOrder); } // Must specify both sort order and ordinal, or neither - if ((SortOrder.Unspecified == columnSortOrder) != (x_defaultSortOrdinal == sortOrdinal)) + if ((SortOrder.Unspecified == columnSortOrder) != (DefaultSortOrdinal == sortOrdinal)) { throw SQL.MustSpecifyBothSortOrderAndOrdinal(columnSortOrder, sortOrdinal); } } - /// + /// public short Adjust(short value) { if (SqlDbType.SmallInt != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public int Adjust(int value) { if (SqlDbType.Int != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public long Adjust(long value) { if (SqlDbType.BigInt != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public float Adjust(float value) { if (SqlDbType.Real != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public double Adjust(double value) { if (SqlDbType.Float != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public string Adjust(string value) { if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) @@ -816,7 +1044,9 @@ public string Adjust(string value) { // Pad if necessary if (value.Length < MaxLength) + { value = value.PadRight((int)MaxLength); + } } } else if (SqlDbType.VarChar != SqlDbType && @@ -834,12 +1064,14 @@ public string Adjust(string value) } if (value.Length > MaxLength && Max != MaxLength) + { value = value.Remove((int)MaxLength, (int)(value.Length - MaxLength)); + } return value; } - /// + /// public decimal Adjust(decimal value) { if (SqlDbType.Decimal != SqlDbType && @@ -861,7 +1093,7 @@ public decimal Adjust(decimal value) } } - /// + /// public DateTime Adjust(DateTime value) { if (SqlDbType.DateTime == SqlDbType || SqlDbType.SmallDateTime == SqlDbType) @@ -883,105 +1115,125 @@ public DateTime Adjust(DateTime value) return value; } - /// + /// public Guid Adjust(Guid value) { if (SqlDbType.UniqueIdentifier != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlBoolean Adjust(SqlBoolean value) { if (SqlDbType.Bit != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlByte Adjust(SqlByte value) { if (SqlDbType.TinyInt != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlInt16 Adjust(SqlInt16 value) { if (SqlDbType.SmallInt != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlInt32 Adjust(SqlInt32 value) { if (SqlDbType.Int != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlInt64 Adjust(SqlInt64 value) { if (SqlDbType.BigInt != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlSingle Adjust(SqlSingle value) { if (SqlDbType.Real != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlDouble Adjust(SqlDouble value) { if (SqlDbType.Float != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlMoney Adjust(SqlMoney value) { - if (SqlDbType.Money != SqlDbType && - SqlDbType.SmallMoney != SqlDbType) + if (SqlDbType.Money != SqlDbType && SqlDbType.SmallMoney != SqlDbType) + { ThrowInvalidType(); - + } if (!value.IsNull) + { VerifyMoneyRange(value); - + } return value; } - /// + /// public SqlDateTime Adjust(SqlDateTime value) { - if (SqlDbType.DateTime != SqlDbType && - SqlDbType.SmallDateTime != SqlDbType) + if (SqlDbType.DateTime != SqlDbType && SqlDbType.SmallDateTime != SqlDbType) + { ThrowInvalidType(); - + } if (!value.IsNull) + { VerifyDateTimeRange(value.Value); - + } return value; } - /// + /// public SqlDecimal Adjust(SqlDecimal value) { if (SqlDbType.Decimal != SqlDbType) + { ThrowInvalidType(); + } return InternalAdjustSqlDecimal(value); } - /// + /// public SqlString Adjust(SqlString value) { if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) @@ -992,12 +1244,20 @@ public SqlString Adjust(SqlString value) { // Pad fixed-length types if (value.Value.Length < MaxLength) + { return new SqlString(value.Value.PadRight((int)MaxLength)); + } } } - else if (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) + else if ( + SqlDbType.VarChar != SqlDbType && + SqlDbType.NVarChar != SqlDbType && + SqlDbType.Text != SqlDbType && + SqlDbType.NText != SqlDbType + ) + { ThrowInvalidType(); + } // Handle null values after type check if (value.IsNull) @@ -1007,16 +1267,17 @@ public SqlString Adjust(SqlString value) // trim all types if (value.Value.Length > MaxLength && Max != MaxLength) + { value = new SqlString(value.Value.Remove((int)MaxLength, (int)(value.Value.Length - MaxLength))); + } return value; } - /// + /// public SqlBinary Adjust(SqlBinary value) { - if (SqlDbType.Binary == SqlDbType || - SqlDbType.Timestamp == SqlDbType) + if (SqlDbType.Binary == SqlDbType || SqlDbType.Timestamp == SqlDbType) { if (!value.IsNull) { @@ -1025,15 +1286,16 @@ public SqlBinary Adjust(SqlBinary value) { byte[] rgbValue = value.Value; byte[] rgbNewValue = new byte[MaxLength]; - Buffer.BlockCopy(rgbValue, 0, rgbNewValue, 0, rgbValue.Length); + Array.Copy(rgbValue, rgbNewValue, rgbValue.Length); Array.Clear(rgbNewValue, rgbValue.Length, rgbNewValue.Length - rgbValue.Length); return new SqlBinary(rgbNewValue); } } } - else if (SqlDbType.VarBinary != SqlDbType && - SqlDbType.Image != SqlDbType) + else if (SqlDbType.VarBinary != SqlDbType && SqlDbType.Image != SqlDbType) + { ThrowInvalidType(); + } // Handle null values if (value.IsNull) @@ -1046,22 +1308,24 @@ public SqlBinary Adjust(SqlBinary value) { byte[] rgbValue = value.Value; byte[] rgbNewValue = new byte[MaxLength]; - Buffer.BlockCopy(rgbValue, 0, rgbNewValue, 0, (int)MaxLength); + Array.Copy(rgbValue, rgbNewValue, (int)MaxLength); value = new SqlBinary(rgbNewValue); } return value; } - /// + /// public SqlGuid Adjust(SqlGuid value) { if (SqlDbType.UniqueIdentifier != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public SqlChars Adjust(SqlChars value) { if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) @@ -1078,23 +1342,31 @@ public SqlChars Adjust(SqlChars value) if (value.MaxLength < MaxLength) { char[] rgchNew = new char[(int)MaxLength]; - Buffer.BlockCopy(value.Buffer, 0, rgchNew, 0, (int)oldLength); + Array.Copy(value.Buffer, rgchNew, (int)oldLength); value = new SqlChars(rgchNew); } // pad extra space char[] rgchTemp = value.Buffer; for (long i = oldLength; i < MaxLength; i++) + { rgchTemp[i] = ' '; + } value.SetLength(MaxLength); return value; } } } - else if (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) + else if ( + SqlDbType.VarChar != SqlDbType && + SqlDbType.NVarChar != SqlDbType && + SqlDbType.Text != SqlDbType && + SqlDbType.NText != SqlDbType + ) + { ThrowInvalidType(); + } // Handle null values after type check. if (null == value || value.IsNull) @@ -1104,12 +1376,14 @@ public SqlChars Adjust(SqlChars value) // trim all types if (value.Length > MaxLength && Max != MaxLength) + { value.SetLength(MaxLength); + } return value; } - /// + /// public SqlBytes Adjust(SqlBytes value) { if (SqlDbType.Binary == SqlDbType || SqlDbType.Timestamp == SqlDbType) @@ -1126,7 +1400,7 @@ public SqlBytes Adjust(SqlBytes value) if (value.MaxLength < MaxLength) { byte[] rgbNew = new byte[MaxLength]; - Buffer.BlockCopy(value.Buffer, 0, rgbNew, 0, (int)oldLength); + Array.Copy(value.Buffer, rgbNew, (int)oldLength); value = new SqlBytes(rgbNew); } @@ -1138,9 +1412,10 @@ public SqlBytes Adjust(SqlBytes value) } } } - else if (SqlDbType.VarBinary != SqlDbType && - SqlDbType.Image != SqlDbType) + else if (SqlDbType.VarBinary != SqlDbType && SqlDbType.Image != SqlDbType) + { ThrowInvalidType(); + } // Handle null values after type check. if (null == value || value.IsNull) @@ -1150,84 +1425,94 @@ public SqlBytes Adjust(SqlBytes value) // trim all types if (value.Length > MaxLength && Max != MaxLength) + { value.SetLength(MaxLength); + } return value; } - /// + /// public SqlXml Adjust(SqlXml value) { if (SqlDbType.Xml != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public TimeSpan Adjust(TimeSpan value) { if (SqlDbType.Time != SqlDbType) + { ThrowInvalidType(); + } VerifyTimeRange(value); return new TimeSpan(InternalAdjustTimeTicks(value.Ticks)); } - /// + /// public DateTimeOffset Adjust(DateTimeOffset value) { if (SqlDbType.DateTimeOffset != SqlDbType) + { ThrowInvalidType(); + } return new DateTimeOffset(InternalAdjustTimeTicks(value.Ticks), value.Offset); } - /// + /// public object Adjust(object value) { // Pass null references through if (null == value) + { return null; + } Type dataType = value.GetType(); switch (Type.GetTypeCode(dataType)) { case TypeCode.Boolean: - value = this.Adjust((bool)value); + value = Adjust((bool)value); break; case TypeCode.Byte: - value = this.Adjust((byte)value); + value = Adjust((byte)value); break; case TypeCode.Char: - value = this.Adjust((char)value); + value = Adjust((char)value); break; case TypeCode.DateTime: - value = this.Adjust((DateTime)value); + value = Adjust((DateTime)value); break; case TypeCode.DBNull: /* DBNull passes through as is for all types */ break; case TypeCode.Decimal: - value = this.Adjust((decimal)value); + value = Adjust((decimal)value); break; case TypeCode.Double: - value = this.Adjust((double)value); + value = Adjust((double)value); break; case TypeCode.Empty: throw ADP.InvalidDataType(TypeCode.Empty); case TypeCode.Int16: - value = this.Adjust((short)value); + value = Adjust((short)value); break; case TypeCode.Int32: - value = this.Adjust((int)value); + value = Adjust((int)value); break; case TypeCode.Int64: - value = this.Adjust((long)value); + value = Adjust((long)value); break; case TypeCode.SByte: throw ADP.InvalidDataType(TypeCode.SByte); case TypeCode.Single: - value = this.Adjust((float)value); + value = Adjust((float)value); break; case TypeCode.String: - value = this.Adjust((string)value); + value = Adjust((string)value); break; case TypeCode.UInt16: throw ADP.InvalidDataType(TypeCode.UInt16); @@ -1237,51 +1522,93 @@ public object Adjust(object value) throw ADP.InvalidDataType(TypeCode.UInt64); case TypeCode.Object: if (dataType == typeof(byte[])) - value = this.Adjust((byte[])value); + { + value = Adjust((byte[])value); + } else if (dataType == typeof(char[])) - value = this.Adjust((char[])value); - else if (dataType == typeof(System.Guid)) - value = this.Adjust((System.Guid)value); + { + value = Adjust((char[])value); + } + else if (dataType == typeof(Guid)) + { + value = Adjust((Guid)value); + } else if (dataType == typeof(object)) { throw ADP.InvalidDataType(TypeCode.UInt64); } else if (dataType == typeof(SqlBinary)) - value = this.Adjust((SqlBinary)value); + { + value = Adjust((SqlBinary)value); + } else if (dataType == typeof(SqlBoolean)) - value = this.Adjust((SqlBoolean)value); + { + value = Adjust((SqlBoolean)value); + } else if (dataType == typeof(SqlByte)) - value = this.Adjust((SqlByte)value); + { + value = Adjust((SqlByte)value); + } else if (dataType == typeof(SqlDateTime)) - value = this.Adjust((SqlDateTime)value); + { + value = Adjust((SqlDateTime)value); + } else if (dataType == typeof(SqlDouble)) - value = this.Adjust((SqlDouble)value); + { + value = Adjust((SqlDouble)value); + } else if (dataType == typeof(SqlGuid)) - value = this.Adjust((SqlGuid)value); + { + value = Adjust((SqlGuid)value); + } else if (dataType == typeof(SqlInt16)) - value = this.Adjust((SqlInt16)value); + { + value = Adjust((SqlInt16)value); + } else if (dataType == typeof(SqlInt32)) - value = this.Adjust((SqlInt32)value); + { + value = Adjust((SqlInt32)value); + } else if (dataType == typeof(SqlInt64)) - value = this.Adjust((SqlInt64)value); + { + value = Adjust((SqlInt64)value); + } else if (dataType == typeof(SqlMoney)) - value = this.Adjust((SqlMoney)value); + { + value = Adjust((SqlMoney)value); + } else if (dataType == typeof(SqlDecimal)) - value = this.Adjust((SqlDecimal)value); + { + value = Adjust((SqlDecimal)value); + } else if (dataType == typeof(SqlSingle)) - value = this.Adjust((SqlSingle)value); + { + value = Adjust((SqlSingle)value); + } else if (dataType == typeof(SqlString)) - value = this.Adjust((SqlString)value); + { + value = Adjust((SqlString)value); + } else if (dataType == typeof(SqlChars)) - value = this.Adjust((SqlChars)value); + { + value = Adjust((SqlChars)value); + } else if (dataType == typeof(SqlBytes)) - value = this.Adjust((SqlBytes)value); + { + value = Adjust((SqlBytes)value); + } else if (dataType == typeof(SqlXml)) - value = this.Adjust((SqlXml)value); + { + value = Adjust((SqlXml)value); + } else if (dataType == typeof(TimeSpan)) - value = this.Adjust((TimeSpan)value); + { + value = Adjust((TimeSpan)value); + } else if (dataType == typeof(DateTimeOffset)) - value = this.Adjust((DateTimeOffset)value); + { + value = Adjust((DateTimeOffset)value); + } else { // Handle UDTs? @@ -1289,7 +1616,6 @@ public object Adjust(object value) } break; - default: throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType)); } @@ -1297,12 +1623,14 @@ public object Adjust(object value) return value; } - /// + /// public static SqlMetaData InferFromValue(object value, string name) { if (value == null) + { throw ADP.ArgumentNull(nameof(value)); - SqlMetaData smd = null; + } + SqlMetaData smd; Type dataType = value.GetType(); switch (Type.GetTypeCode(dataType)) { @@ -1321,8 +1649,7 @@ public static SqlMetaData InferFromValue(object value, string name) case TypeCode.DBNull: throw ADP.InvalidDataType(TypeCode.DBNull); case TypeCode.Decimal: - { // Add brackets in order to contain scope declare local variable "sd" - // use logic inside SqlDecimal to infer precision and scale. + { SqlDecimal sd = new SqlDecimal((decimal)value); smd = new SqlMetaData(name, SqlDbType.Decimal, sd.Precision, sd.Scale); } @@ -1348,13 +1675,15 @@ public static SqlMetaData InferFromValue(object value, string name) break; case TypeCode.String: { - long maxLen = ((String)value).Length; + long maxLen = ((string)value).Length; if (maxLen < 1) + { maxLen = 1; - - if (x_lServerMaxUnicode < maxLen) + } + if (MaxUnicodeLength < maxLen) + { maxLen = Max; - + } smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen); } break; @@ -1367,30 +1696,38 @@ public static SqlMetaData InferFromValue(object value, string name) case TypeCode.Object: if (dataType == typeof(byte[])) { - long maxLen = ((System.Byte[])value).Length; + long maxLen = ((byte[])value).Length; if (maxLen < 1) + { maxLen = 1; - - if (x_lServerMaxBinary < maxLen) + } + if (MaxBinaryLength < maxLen) + { maxLen = Max; - + } smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen); } else if (dataType == typeof(char[])) { - long maxLen = ((System.Char[])value).Length; + long maxLen = ((char[])value).Length; if (maxLen < 1) + { maxLen = 1; - - if (x_lServerMaxUnicode < maxLen) + } + if (MaxUnicodeLength < maxLen) + { maxLen = Max; - + } smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen); } - else if (dataType == typeof(System.Guid)) + else if (dataType == typeof(Guid)) + { smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier); + } else if (dataType == typeof(object)) + { smd = new SqlMetaData(name, SqlDbType.Variant); + } else if (dataType == typeof(SqlBinary)) { long maxLen; @@ -1399,34 +1736,56 @@ public static SqlMetaData InferFromValue(object value, string name) { maxLen = sb.Length; if (maxLen < 1) + { maxLen = 1; - - if (x_lServerMaxBinary < maxLen) + } + if (MaxBinaryLength < maxLen) + { maxLen = Max; + } } else - maxLen = sxm_rgDefaults[(int)SqlDbType.VarBinary].MaxLength; - + { + maxLen = s_defaults[(int)SqlDbType.VarBinary].MaxLength; + } smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen); } else if (dataType == typeof(SqlBoolean)) + { smd = new SqlMetaData(name, SqlDbType.Bit); + } else if (dataType == typeof(SqlByte)) + { smd = new SqlMetaData(name, SqlDbType.TinyInt); + } else if (dataType == typeof(SqlDateTime)) + { smd = new SqlMetaData(name, SqlDbType.DateTime); + } else if (dataType == typeof(SqlDouble)) + { smd = new SqlMetaData(name, SqlDbType.Float); + } else if (dataType == typeof(SqlGuid)) + { smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier); + } else if (dataType == typeof(SqlInt16)) + { smd = new SqlMetaData(name, SqlDbType.SmallInt); + } else if (dataType == typeof(SqlInt32)) + { smd = new SqlMetaData(name, SqlDbType.Int); + } else if (dataType == typeof(SqlInt64)) + { smd = new SqlMetaData(name, SqlDbType.BigInt); + } else if (dataType == typeof(SqlMoney)) + { smd = new SqlMetaData(name, SqlDbType.Money); + } else if (dataType == typeof(SqlDecimal)) { byte bPrec; @@ -1439,13 +1798,15 @@ public static SqlMetaData InferFromValue(object value, string name) } else { - bPrec = sxm_rgDefaults[(int)SqlDbType.Decimal].Precision; - scale = sxm_rgDefaults[(int)SqlDbType.Decimal].Scale; + bPrec = s_defaults[(int)SqlDbType.Decimal].Precision; + scale = s_defaults[(int)SqlDbType.Decimal].Scale; } smd = new SqlMetaData(name, SqlDbType.Decimal, bPrec, scale); } else if (dataType == typeof(SqlSingle)) + { smd = new SqlMetaData(name, SqlDbType.Real); + } else if (dataType == typeof(SqlString)) { SqlString ss = (SqlString)value; @@ -1453,16 +1814,18 @@ public static SqlMetaData InferFromValue(object value, string name) { long maxLen = ss.Value.Length; if (maxLen < 1) + { maxLen = 1; - - if (maxLen > x_lServerMaxUnicode) + } + if (maxLen > MaxUnicodeLength) + { maxLen = Max; - + } smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen, ss.LCID, ss.SqlCompareOptions); } else { - smd = new SqlMetaData(name, SqlDbType.NVarChar, sxm_rgDefaults[(int)SqlDbType.NVarChar].MaxLength); + smd = new SqlMetaData(name, SqlDbType.NVarChar, s_defaults[(int)SqlDbType.NVarChar].MaxLength); } } else if (dataType == typeof(SqlChars)) @@ -1473,14 +1836,18 @@ public static SqlMetaData InferFromValue(object value, string name) { maxLen = sch.Length; if (maxLen < 1) + { maxLen = 1; - - if (maxLen > x_lServerMaxUnicode) + } + if (maxLen > MaxUnicodeLength) + { maxLen = Max; + } } else - maxLen = sxm_rgDefaults[(int)SqlDbType.NVarChar].MaxLength; - + { + maxLen = s_defaults[(int)SqlDbType.NVarChar].MaxLength; + } smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen); } else if (dataType == typeof(SqlBytes)) @@ -1491,23 +1858,36 @@ public static SqlMetaData InferFromValue(object value, string name) { maxLen = sb.Length; if (maxLen < 1) + { maxLen = 1; - else if (x_lServerMaxBinary < maxLen) + } + else if (MaxBinaryLength < maxLen) + { maxLen = Max; + } } else - maxLen = sxm_rgDefaults[(int)SqlDbType.VarBinary].MaxLength; - + { + maxLen = s_defaults[(int)SqlDbType.VarBinary].MaxLength; + } smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen); } else if (dataType == typeof(SqlXml)) + { smd = new SqlMetaData(name, SqlDbType.Xml); + } else if (dataType == typeof(TimeSpan)) + { smd = new SqlMetaData(name, SqlDbType.Time, 0, InferScaleFromTimeTicks(((TimeSpan)value).Ticks)); + } else if (dataType == typeof(DateTimeOffset)) + { smd = new SqlMetaData(name, SqlDbType.DateTimeOffset, 0, InferScaleFromTimeTicks(((DateTimeOffset)value).Ticks)); + } else + { throw ADP.UnknownDataType(dataType); + } break; default: @@ -1517,23 +1897,27 @@ public static SqlMetaData InferFromValue(object value, string name) return smd; } - /// + /// public bool Adjust(bool value) { if (SqlDbType.Bit != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public byte Adjust(byte value) { if (SqlDbType.TinyInt != SqlDbType) + { ThrowInvalidType(); + } return value; } - /// + /// public byte[] Adjust(byte[] value) { if (SqlDbType.Binary == SqlDbType || SqlDbType.Timestamp == SqlDbType) @@ -1545,15 +1929,16 @@ public byte[] Adjust(byte[] value) if (value.Length < MaxLength) { byte[] rgbNewValue = new byte[MaxLength]; - Buffer.BlockCopy(value, 0, rgbNewValue, 0, value.Length); + Array.Copy(value, rgbNewValue, value.Length); Array.Clear(rgbNewValue, value.Length, (int)rgbNewValue.Length - value.Length); return rgbNewValue; } } } - else if (SqlDbType.VarBinary != SqlDbType && - SqlDbType.Image != SqlDbType) + else if (SqlDbType.VarBinary != SqlDbType && SqlDbType.Image != SqlDbType) + { ThrowInvalidType(); + } // Handle null values after type check if (null == value) @@ -1565,31 +1950,34 @@ public byte[] Adjust(byte[] value) if (value.Length > MaxLength && Max != MaxLength) { byte[] rgbNewValue = new byte[MaxLength]; - Buffer.BlockCopy(value, 0, rgbNewValue, 0, (int)MaxLength); + Array.Copy(value, rgbNewValue, (int)MaxLength); value = rgbNewValue; } return value; } - /// + /// public char Adjust(char value) { if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) { if (1 != MaxLength) + { ThrowInvalidType(); + } } else if ((1 > MaxLength) || // char must have max length of at least 1 (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) ) + { ThrowInvalidType(); - + } return value; } - /// + /// public char[] Adjust(char[] value) { if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) @@ -1602,19 +1990,26 @@ public char[] Adjust(char[] value) if (oldLength < MaxLength) { char[] rgchNew = new char[(int)MaxLength]; - Buffer.BlockCopy(value, 0, rgchNew, 0, (int)oldLength); + Array.Copy(value, rgchNew, (int)oldLength); // pad extra space for (long i = oldLength; i < rgchNew.Length; i++) + { rgchNew[i] = ' '; - + } return rgchNew; } } } - else if (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType && - SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType) + else if ( + SqlDbType.VarChar != SqlDbType && + SqlDbType.NVarChar != SqlDbType && + SqlDbType.Text != SqlDbType && + SqlDbType.NText != SqlDbType + ) + { ThrowInvalidType(); + } // Handle null values after type check if (null == value) @@ -1626,11 +2021,10 @@ public char[] Adjust(char[] value) if (value.Length > MaxLength && Max != MaxLength) { char[] rgchNewValue = new char[MaxLength]; - Buffer.BlockCopy(value, 0, rgchNewValue, 0, (int)MaxLength); + Array.Copy(value, rgchNewValue, (int)MaxLength); value = rgchNewValue; } - return value; } @@ -1642,40 +2036,43 @@ internal static SqlMetaData GetPartialLengthMetaData(SqlMetaData md) return md; } if (md.SqlDbType == SqlDbType.Xml) + { ThrowInvalidType(); //Xml should always have IsPartialLength = true - - if (md.SqlDbType == SqlDbType.NVarChar || md.SqlDbType == SqlDbType.VarChar || - md.SqlDbType == SqlDbType.VarBinary) + } + if ( + md.SqlDbType == SqlDbType.NVarChar || + md.SqlDbType == SqlDbType.VarChar || + md.SqlDbType == SqlDbType.VarBinary + ) { - SqlMetaData mdnew = new SqlMetaData(md.Name, md.SqlDbType, SqlMetaData.Max, 0, 0, md.LocaleId, + return new SqlMetaData(md.Name, md.SqlDbType, SqlMetaData.Max, 0, 0, md.LocaleId, md.CompareOptions, null, null, null, true, md.Type); - return mdnew; } else + { return md; + } } - private static void ThrowInvalidType() { throw ADP.InvalidMetaDataValue(); } - // Hard coding smalldatetime limits... - private static readonly DateTime s_dtSmallMax = new DateTime(2079, 06, 06, 23, 59, 29, 998); - private static readonly DateTime s_dtSmallMin = new DateTime(1899, 12, 31, 23, 59, 29, 999); private void VerifyDateTimeRange(DateTime value) { - if (SqlDbType.SmallDateTime == SqlDbType && (s_dtSmallMax < value || s_dtSmallMin > value)) + if (SqlDbType.SmallDateTime == SqlDbType && (s_smallDateTimeMax < value || s_smallDateTimeMin > value)) + { ThrowInvalidType(); + } } - private static readonly SqlMoney s_smSmallMax = new SqlMoney(((decimal)int.MaxValue) / 10000); - private static readonly SqlMoney s_smSmallMin = new SqlMoney(((decimal)int.MinValue) / 10000); private void VerifyMoneyRange(SqlMoney value) { - if (SqlDbType.SmallMoney == SqlDbType && ((s_smSmallMax < value).Value || (s_smSmallMin > value).Value)) + if (SqlDbType.SmallMoney == SqlDbType && ((s_smallMoneyMax < value).Value || (s_smallMoneyMin > value).Value)) + { ThrowInvalidType(); + } } private SqlDecimal InternalAdjustSqlDecimal(SqlDecimal value) @@ -1693,8 +2090,6 @@ private SqlDecimal InternalAdjustSqlDecimal(SqlDecimal value) return value; } - private static readonly TimeSpan s_timeMin = TimeSpan.Zero; - private static readonly TimeSpan s_timeMax = new TimeSpan(TimeSpan.TicksPerDay - 1); private void VerifyTimeRange(TimeSpan value) { if (SqlDbType.Time == SqlDbType && (s_timeMin > value || value > s_timeMax)) @@ -1703,17 +2098,6 @@ private void VerifyTimeRange(TimeSpan value) } } - private static readonly long[] s_unitTicksFromScale = { - 10000000, - 1000000, - 100000, - 10000, - 1000, - 100, - 10, - 1, - }; - private long InternalAdjustTimeTicks(long ticks) { return (ticks / s_unitTicksFromScale[Scale] * s_unitTicksFromScale[Scale]); @@ -1731,132 +2115,18 @@ private static byte InferScaleFromTimeTicks(long ticks) return MaxTimeScale; } - private static DbType[] sxm_rgSqlDbTypeToDbType = { - DbType.Int64, // SqlDbType.BigInt - DbType.Binary, // SqlDbType.Binary - DbType.Boolean, // SqlDbType.Bit - DbType.AnsiString, // SqlDbType.Char - DbType.DateTime, // SqlDbType.DateTime - DbType.Decimal, // SqlDbType.Decimal - DbType.Double, // SqlDbType.Float - DbType.Binary, // SqlDbType.Image - DbType.Int32, // SqlDbType.Int - DbType.Currency, // SqlDbType.Money - DbType.String, // SqlDbType.NChar - DbType.String, // SqlDbType.NText - DbType.String, // SqlDbType.NVarChar - DbType.Single, // SqlDbType.Real - DbType.Guid, // SqlDbType.UniqueIdentifier - DbType.DateTime, // SqlDbType.SmallDateTime - DbType.Int16, // SqlDbType.SmallInt - DbType.Currency, // SqlDbType.SmallMoney - DbType.AnsiString, // SqlDbType.Text - DbType.Binary, // SqlDbType.Timestamp - DbType.Byte, // SqlDbType.TinyInt - DbType.Binary, // SqlDbType.VarBinary - DbType.AnsiString, // SqlDbType.VarChar - DbType.Object, // SqlDbType.Variant - DbType.Object, // SqlDbType.Row - DbType.Xml, // SqlDbType.Xml - DbType.String, // SqlDbType.NVarChar, place holder - DbType.String, // SqlDbType.NVarChar, place holder - DbType.String, // SqlDbType.NVarChar, place holder - DbType.Object, // SqlDbType.Udt - DbType.Object, // SqlDbType.Structured - DbType.Date, // SqlDbType.Date - DbType.Time, // SqlDbType.Time - DbType.DateTime2, // SqlDbType.DateTime2 - DbType.DateTimeOffset // SqlDbType.DateTimeOffset - }; - private void SetDefaultsForType(SqlDbType dbType) { if (SqlDbType.BigInt <= dbType && SqlDbType.DateTimeOffset >= dbType) { - SqlMetaData smdDflt = sxm_rgDefaults[(int)dbType]; + SqlMetaData smdDflt = s_defaults[(int)dbType]; _sqlDbType = dbType; - _lMaxLength = smdDflt.MaxLength; - _bPrecision = smdDflt.Precision; - _bScale = smdDflt.Scale; - _lLocale = smdDflt.LocaleId; - _eCompareOptions = smdDflt.CompareOptions; + _maxLength = smdDflt.MaxLength; + _precision = smdDflt.Precision; + _scale = smdDflt.Scale; + _locale = smdDflt.LocaleId; + _compareOptions = smdDflt.CompareOptions; } } - - // Array of default-valued metadata ordered by corresponding SqlDbType. - internal static SqlMetaData[] sxm_rgDefaults = - { - // new SqlMetaData(name, DbType, SqlDbType, MaxLen, Prec, Scale, Locale, DatabaseName, SchemaName, isPartialLength) - new SqlMetaData("bigint", SqlDbType.BigInt, - 8, 19, 0, 0, SqlCompareOptions.None, false), // SqlDbType.BigInt - new SqlMetaData("binary", SqlDbType.Binary, - 1, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Binary - new SqlMetaData("bit", SqlDbType.Bit, - 1, 1, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Bit - new SqlMetaData("char", SqlDbType.Char, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.Char - new SqlMetaData("datetime", SqlDbType.DateTime, - 8, 23, 3, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime - new SqlMetaData("decimal", SqlDbType.Decimal, - 9, 18, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Decimal - new SqlMetaData("float", SqlDbType.Float, - 8, 53, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Float - new SqlMetaData("image", SqlDbType.Image, - x_lMax, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Image - new SqlMetaData("int", SqlDbType.Int, - 4, 10, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Int - new SqlMetaData("money", SqlDbType.Money, - 8, 19, 4, 0, SqlCompareOptions.None, false), // SqlDbType.Money - new SqlMetaData("nchar", SqlDbType.NChar, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NChar - new SqlMetaData("ntext", SqlDbType.NText, - x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NText - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NVarChar - new SqlMetaData("real", SqlDbType.Real, - 4, 24, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Real - new SqlMetaData("uniqueidentifier", SqlDbType.UniqueIdentifier, - 16, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.UniqueIdentifier - new SqlMetaData("smalldatetime", SqlDbType.SmallDateTime, - 4, 16, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallDateTime - new SqlMetaData("smallint", SqlDbType.SmallInt, - 2, 5, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallInt - new SqlMetaData("smallmoney", SqlDbType.SmallMoney, - 4, 10, 4, 0, SqlCompareOptions.None, false), // SqlDbType.SmallMoney - new SqlMetaData("text", SqlDbType.Text, - x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.Text - new SqlMetaData("timestamp", SqlDbType.Timestamp, - 8, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Timestamp - new SqlMetaData("tinyint", SqlDbType.TinyInt, - 1, 3, 0, 0, SqlCompareOptions.None, false), // SqlDbType.TinyInt - new SqlMetaData("varbinary", SqlDbType.VarBinary, - x_lServerMaxBinary, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.VarBinary - new SqlMetaData("varchar", SqlDbType.VarChar, - x_lServerMaxANSI, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.VarChar - new SqlMetaData("sql_variant", SqlDbType.Variant, - 8016, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Variant - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 24 - new SqlMetaData("xml", SqlDbType.Xml, - x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, true), // SqlDbType.Xml - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - 1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 26 - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 27 - new SqlMetaData("nvarchar", SqlDbType.NVarChar, - x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 28 - new SqlMetaData("udt", SqlDbType.Udt, - 0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Udt = 29 - new SqlMetaData("table", SqlDbType.Structured, - 0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Structured - new SqlMetaData("date", SqlDbType.Date, - 3, 10,0, 0, SqlCompareOptions.None, false), // SqlDbType.Date - new SqlMetaData("time", SqlDbType.Time, - 5, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.Time - new SqlMetaData("datetime2", SqlDbType.DateTime2, - 8, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime2 - new SqlMetaData("datetimeoffset", SqlDbType.DateTimeOffset, - 10, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTimeOffset - }; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlRecordBuffer.cs diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs similarity index 61% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs index 12c1149428..861cc8cef0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlUserDefinedAggregateAttribute.cs @@ -7,7 +7,7 @@ namespace Microsoft.Data.SqlClient.Server { - /// + /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)] public sealed class SqlUserDefinedAggregateAttribute : Attribute { @@ -19,11 +19,11 @@ public sealed class SqlUserDefinedAggregateAttribute : Attribute private Format _format; private string _name; - /// + /// // The maximum value for the maxbytesize field, in bytes. public const int MaxByteSizeValue = 8000; - /// + /// // A required attribute on all UD Aggs, used to indicate that the // given type is a UD Agg, and its storage format. public SqlUserDefinedAggregateAttribute(Format format) @@ -41,7 +41,7 @@ public SqlUserDefinedAggregateAttribute(Format format) } } - /// + /// // The maximum size of this instance, in bytes. Does not have to be // specified for Native format serialization. The maximum value // for this property is specified by MaxByteSizeValue. @@ -62,7 +62,7 @@ public int MaxByteSize } } - /// + /// public bool IsInvariantToDuplicates { get @@ -75,7 +75,7 @@ public bool IsInvariantToDuplicates } } - /// + /// public bool IsInvariantToNulls { get @@ -88,7 +88,7 @@ public bool IsInvariantToNulls } } - /// + /// public bool IsInvariantToOrder { get @@ -101,7 +101,7 @@ public bool IsInvariantToOrder } } - /// + /// public bool IsNullIfEmpty { get @@ -114,11 +114,11 @@ public bool IsNullIfEmpty } } - /// + /// // The on-disk format for this type. public Format Format => _format; - /// + /// public string Name { get From 2c4a3927389ca2429be8dbe682efa70a79cb6a7d Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Fri, 11 Dec 2020 09:57:35 +0000 Subject: [PATCH 2/4] address feedback --- .../Microsoft/Data/SqlClient/Server/SqlSer.cs | 2 +- .../netfx/src/Microsoft.Data.SqlClient.csproj | 20 +++++-------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs index df063c99e1..89c8ed94b5 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs @@ -251,7 +251,7 @@ private void DontDoIt() public override long Position { get => _size; - set =>_size = value; + set => _size = value; } public override long Length => _size; diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index cb0c9d0d13..ca67131a9a 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -362,16 +362,10 @@ - - Component - - - Component - + + - - Component - + @@ -379,9 +373,7 @@ - - Component - + @@ -447,9 +439,7 @@ - - Component - + From d638856ba4fb1ba6ebdf590ee38eb80da04412cf Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Fri, 11 Dec 2020 20:59:07 +0000 Subject: [PATCH 3/4] remove unneeded fully qualified name --- .../src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs index e9f6b44065..051b31d79b 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs @@ -631,7 +631,7 @@ private void Construct(string name, SqlDbType dbType, bool useServerDefault, boo if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) { - _locale = System.Globalization.CultureInfo.CurrentCulture.LCID; + _locale = CultureInfo.CurrentCulture.LCID; } _name = name; From 146019fe508e232020fd61f41e0e9791e5a75503 Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Tue, 22 Dec 2020 17:44:52 +0000 Subject: [PATCH 4/4] address feedback and misc vs suggestions --- .../src/Microsoft/Data/SqlClient/Server/SqlNorm.cs | 4 ++-- .../src/Microsoft/Data/SqlClient/Server/SqlSer.cs | 8 +++----- .../netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs | 9 +++------ 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs index d987f7a2b7..e0c24c87fb 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlNorm.cs @@ -25,8 +25,8 @@ internal FieldInfoEx(FieldInfo fi, int offset, Normalizer normalizer) FieldInfo = fi; _offset = offset; } - public FieldInfo FieldInfo { get; private set; } - public Normalizer Normalizer { get; private set; } + internal FieldInfo FieldInfo { get; private set; } + internal Normalizer Normalizer { get; private set; } // Sort fields by field offsets. public int CompareTo(object other) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs index 89c8ed94b5..c9f1536f50 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/Server/SqlSer.cs @@ -173,14 +173,12 @@ private static Serializer GetNewSerializer(Type t) // The base serializer class. internal abstract class Serializer { + protected Type _type; + public abstract object Deserialize(Stream s); public abstract void Serialize(Stream s, object o); - protected Type _type; - protected Serializer(Type t) - { - _type = t; - } + protected Serializer(Type t) => _type = t; } internal sealed class NormalizedSerializer : Serializer diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs index e003734764..2967aaaa55 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/Server/sqlser.cs @@ -215,7 +215,7 @@ public override void Serialize(Stream s, object o) BinaryWriter w = new BinaryWriter(s); if (o is Microsoft.SqlServer.Server.IBinarySerialize) { - ((Microsoft.SqlServer.Server.IBinarySerialize)o).Write(w); + ((SqlServer.Server.IBinarySerialize)o).Write(w); } else { @@ -233,7 +233,7 @@ public override object Deserialize(Stream s) BinaryReader r = new BinaryReader(s); if (instance is Microsoft.SqlServer.Server.IBinarySerialize) { - ((Microsoft.SqlServer.Server.IBinarySerialize)instance).Read(r); + ((SqlServer.Server.IBinarySerialize)instance).Read(r); } else { @@ -290,9 +290,6 @@ public override int Read(byte[] buffer, int offset, int count) return -1; } - public override void Write(byte[] buffer, int offset, int count) - { - _size += count; - } + public override void Write(byte[] buffer, int offset, int count) => _size += count; } }