From d2b8e8e466e3d6c6fdf3a1066844b437c9c2e62c Mon Sep 17 00:00:00 2001 From: Wraith2 Date: Sun, 17 Oct 2021 13:49:59 +0100 Subject: [PATCH] fix merge conflicts --- .../Data/SqlClient/Server/ValueUtilsSmi.cs | 492 +++++++++--------- 1 file changed, 246 insertions(+), 246 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs index f53396cdaa..1e7044f5db 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs +++ b/src/Microsoft.Data.SqlClient/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 partial class ValueUtilsSmi { - private const int __maxByteChunkSize = TdsEnums.MAXSIZE; - private const int __maxCharChunkSize = TdsEnums.MAXSIZE / sizeof(char); + 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 - 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 + private const int DefaultBinaryBufferSize = 4096; // Size of the buffer used to read input parameter of type Stream + private const int DefaultTextBufferSize = 4096; // Size of the buffer (in chars) user to read input parameter of type TextReader // // User-visible semantics-laden Getter/Setter support methods @@ -98,7 +98,7 @@ private static long GetBytesConversion(SmiEventSink_Default sink, ITypedGettersV , null #endif ); - if (null == obj) + if (obj == null) { throw ADP.InvalidCast(); } @@ -117,13 +117,14 @@ private static long GetBytesConversion(SmiEventSink_Default sink, ITypedGettersV } } - if (null == buffer) + if (buffer == null) { return value.Length; } - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength * sizeof(char), value.Length, - fieldOffset, buffer.Length, bufferOffset, length); + length = CheckXetParameters( + metaData.SqlDbType, metaData.MaxLength * sizeof(char), value.Length, fieldOffset, buffer.Length, bufferOffset, length + ); Array.Copy(value.Value, checked((int)fieldOffset), buffer, bufferOffset, length); return length; } @@ -131,12 +132,18 @@ private static long GetBytesConversion(SmiEventSink_Default sink, ITypedGettersV internal static long GetBytes(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiExtendedMetaData metaData, long fieldOffset, byte[] buffer, int bufferOffset, int length, bool throwOnNull) { // Additional exclusions not caught by GetBytesInternal - if ((SmiMetaData.UnlimitedMaxLengthIndicator != metaData.MaxLength && - (SqlDbType.VarChar == metaData.SqlDbType || - SqlDbType.NVarChar == metaData.SqlDbType || - SqlDbType.Char == metaData.SqlDbType || - SqlDbType.NChar == metaData.SqlDbType)) || - SqlDbType.Xml == metaData.SqlDbType) + if ( + ( + SmiMetaData.UnlimitedMaxLengthIndicator != metaData.MaxLength && + ( + metaData.SqlDbType == SqlDbType.VarChar || + metaData.SqlDbType == SqlDbType.NVarChar || + metaData.SqlDbType == SqlDbType.Char || + metaData.SqlDbType == SqlDbType.NChar + ) + ) || + SqlDbType.Xml == metaData.SqlDbType + ) { throw SQL.NonBlobColumn(metaData.Name); } @@ -166,14 +173,15 @@ internal static long GetBytesInternal(SmiEventSink_Default sink, ITypedGettersV3 } } long actualLength = GetBytesLength_Unchecked(sink, getters, ordinal); - if (null == buffer) + if (buffer == null) { return actualLength; } if (MetaDataUtilsSmi.IsCharOrXmlType(metaData.SqlDbType)) { - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength * sizeof(char), actualLength, - fieldOffset, buffer.Length, bufferOffset, length); + length = CheckXetParameters( + metaData.SqlDbType, metaData.MaxLength * sizeof(char), actualLength, fieldOffset, buffer.Length, bufferOffset, length + ); } else { @@ -196,7 +204,7 @@ internal static long GetChars(SmiEventSink_Default sink, ITypedGettersV3 getters if (CanAccessGetterDirectly(metaData, ExtendedClrTypeCode.CharArray)) { long actualLength = GetCharsLength_Unchecked(sink, getters, ordinal); - if (null == buffer) + if (buffer == null) { return actualLength; } @@ -218,12 +226,13 @@ internal static long GetChars(SmiEventSink_Default sink, ITypedGettersV3 getters { throw ADP.InvalidCast(); } - if (null == buffer) + if (buffer == null) { return value.Length; } - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength * sizeof(char), value.Length, - fieldOffset, buffer.Length, bufferOffset, length); + length = CheckXetParameters( + metaData.SqlDbType, metaData.MaxLength * sizeof(char), value.Length, fieldOffset, buffer.Length, bufferOffset, length + ); value.CopyTo(checked((int)fieldOffset), buffer, bufferOffset, length); return length; } @@ -294,7 +303,7 @@ internal static decimal GetDecimal(SmiEventSink_Default sink, ITypedGettersV3 ge , null #endif ); - if (null == result) + if (result == null) { throw ADP.InvalidCast(); } @@ -497,7 +506,7 @@ internal static SqlBytes GetSqlBytes(SmiEventSink_Default sink, ITypedGettersV3 else { long length = GetBytesLength_Unchecked(sink, getters, ordinal); - if (0 <= length && length < __maxByteChunkSize) + if (length >= 0 && length < MaxByteChunkSize) { byte[] byteBuffer = GetByteArray_Unchecked(sink, getters, ordinal); result = new SqlBytes(byteBuffer); @@ -647,7 +656,7 @@ internal static SqlDateTime GetSqlDateTime(SmiEventSink_Default sink, ITypedGett , null #endif ); - if (null == obj) + if (obj == null) { throw ADP.InvalidCast(); } @@ -1043,7 +1052,7 @@ SmiMetaData metaData #endif ) { - object result = null; + object result; if (IsDBNull_Unchecked(sink, getters, ordinal)) { result = DBNull.Value; @@ -1211,10 +1220,10 @@ SmiMetaData metaData #endif ) { - object result = null; + object result; if (IsDBNull_Unchecked(sink, getters, ordinal)) { - if (SqlDbType.Udt == metaData.SqlDbType) + if (metaData.SqlDbType == SqlDbType.Udt) { result = NullUdtInstance(metaData); } @@ -1423,9 +1432,9 @@ SmiMetaData metaData internal static object NullUdtInstance(SmiMetaData metaData) { - Type t = metaData.Type; - Debug.Assert(t != null, "Unexpected null of Udt type on NullUdtInstance!"); - return t.InvokeMember("Null", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static, null, null, new object[] { }, CultureInfo.InvariantCulture); + Type type = metaData.Type; + Debug.Assert(type != null, "Unexpected null of Udt type on NullUdtInstance!"); + return type.InvokeMember("Null", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static, null, null, Array.Empty(), CultureInfo.InvariantCulture); } // Strongly-typed setters are a bit simpler than their corresponding getters. @@ -1439,27 +1448,25 @@ internal static void SetDBNull(SmiEventSink_Default sink, ITypedSettersV3 setter 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) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Byte); - SetByte_Unchecked(sink, setters, ordinal, value); } internal static long SetBytes(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, long fieldOffset, byte[] buffer, int bufferOffset, int length) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.ByteArray); - if (null == buffer) + if (buffer == null) { throw ADP.ArgumentNull(nameof(buffer)); } - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, fieldOffset, buffer.Length, bufferOffset, length); + length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: fieldOffset, bufferLength: buffer.Length, bufferOffset: bufferOffset, length: length); Debug.Assert(length >= 0, "Buffer.Length was invalid!"); - if (0 == length) + if (length == 0) { // Front end semantics says to ignore fieldOffset and bufferOffset // if not doing any actual work. @@ -1494,13 +1501,13 @@ internal static long SetBytesLength(SmiEventSink_Default sink, ITypedSettersV3 s internal static long SetChars(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, long fieldOffset, char[] buffer, int bufferOffset, int length) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.CharArray); - if (null == buffer) + if (buffer == null) { throw ADP.ArgumentNull(nameof(buffer)); } - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, fieldOffset, buffer.Length, bufferOffset, length); + length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: fieldOffset, bufferLength: buffer.Length, bufferOffset: bufferOffset, length: length); Debug.Assert(length >= 0, "Buffer.Length was invalid!"); - if (0 == length) + if (length == 0) { // Front end semantics says to ignore fieldOffset and bufferOffset // if not doing any actual work. @@ -1515,7 +1522,6 @@ internal static long SetChars(SmiEventSink_Default sink, ITypedSettersV3 setters internal static void SetDateTime(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, DateTime value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.DateTime); - SetDateTime_Checked(sink, setters, ordinal, metaData, value); } @@ -1538,49 +1544,42 @@ internal static void SetDateTimeOffset(SmiEventSink_Default sink, ITypedSettersV 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) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Double); - SetDouble_Unchecked(sink, setters, ordinal, value); } internal static void SetGuid(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, Guid value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Guid); - SetGuid_Unchecked(sink, setters, ordinal, 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, 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, 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, float value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.Single); - SetSingle_Unchecked(sink, setters, ordinal, value); } @@ -1593,7 +1592,6 @@ internal static void SetSqlBinary(SmiEventSink_Default sink, ITypedSettersV3 set internal static void SetSqlBoolean(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlBoolean value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlBoolean); - SetSqlBoolean_Unchecked(sink, setters, ordinal, value); } @@ -1607,7 +1605,6 @@ internal static void SetSqlByte(SmiEventSink_Default sink, ITypedSettersV3 sette internal static void SetSqlBytes(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlBytes value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlBytes); - SetSqlBytes_LengthChecked(sink, setters, ordinal, metaData, value, 0); } @@ -1620,63 +1617,54 @@ internal static void SetSqlChars(SmiEventSink_Default sink, ITypedSettersV3 sett internal static void SetSqlDateTime(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlDateTime value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlDateTime); - SetSqlDateTime_Checked(sink, setters, ordinal, metaData, value); } internal static void SetSqlDecimal(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlDecimal value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlDecimal); - SetSqlDecimal_Unchecked(sink, setters, ordinal, value); } internal static void SetSqlDouble(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlDouble value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlDouble); - SetSqlDouble_Unchecked(sink, setters, ordinal, value); } internal static void SetSqlGuid(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlGuid value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlGuid); - SetSqlGuid_Unchecked(sink, setters, ordinal, value); } internal static void SetSqlInt16(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlInt16 value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlInt16); - SetSqlInt16_Unchecked(sink, setters, ordinal, value); } internal static void SetSqlInt32(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlInt32 value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlInt32); - SetSqlInt32_Unchecked(sink, setters, ordinal, value); } internal static void SetSqlInt64(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlInt64 value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlInt64); - SetSqlInt64_Unchecked(sink, setters, ordinal, value); } internal static void SetSqlMoney(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlMoney value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlMoney); - SetSqlMoney_Checked(sink, setters, ordinal, metaData, value); } internal static void SetSqlSingle(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlSingle value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlSingle); - SetSqlSingle_Unchecked(sink, setters, ordinal, value); } @@ -1689,14 +1677,12 @@ internal static void SetSqlString(SmiEventSink_Default sink, ITypedSettersV3 set internal static void SetSqlXml(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlXml value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.SqlXml); - SetSqlXml_Unchecked(sink, setters, ordinal, value); } internal static void SetString(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, string value) { ThrowIfInvalidSetterAccess(metaData, ExtendedClrTypeCode.String); - SetString_LengthChecked(sink, setters, ordinal, metaData, value, 0); } @@ -1726,19 +1712,21 @@ internal static void SetCompatibleValue( object value, ExtendedClrTypeCode typeCode, int offset - ) + ) { // Ensure either an invalid type, or caller validated compatibility // SqlDbType.Variant and have special handling - Debug.Assert(typeCode == ExtendedClrTypeCode.Invalid || - typeCode == ExtendedClrTypeCode.SByte || - typeCode == ExtendedClrTypeCode.UInt16 || - typeCode == ExtendedClrTypeCode.UInt32 || - typeCode == ExtendedClrTypeCode.UInt64 || - typeCode == ExtendedClrTypeCode.DBNull || - typeCode == ExtendedClrTypeCode.Empty || - CanAccessSetterDirectly(metaData, typeCode) || - value is DataFeed /* already validated */); + Debug.Assert( + typeCode == ExtendedClrTypeCode.Invalid || + typeCode == ExtendedClrTypeCode.SByte || + typeCode == ExtendedClrTypeCode.UInt16 || + typeCode == ExtendedClrTypeCode.UInt32 || + typeCode == ExtendedClrTypeCode.UInt64 || + typeCode == ExtendedClrTypeCode.DBNull || + typeCode == ExtendedClrTypeCode.Empty || + CanAccessSetterDirectly(metaData, typeCode) || + value is DataFeed + ); switch (typeCode) { @@ -1883,13 +1871,18 @@ internal static void SetCompatibleValueV200( int length, ParameterPeekAheadValue peekAhead, SqlBuffer.StorageType storageType - ) + ) { // Ensure caller validated compatibility for types handled directly in this method - Debug.Assert((ExtendedClrTypeCode.DataTable != typeCode && - ExtendedClrTypeCode.DbDataReader != typeCode && - ExtendedClrTypeCode.IEnumerableOfSqlDataRecord != typeCode) || - CanAccessSetterDirectly(metaData, typeCode), "Un-validated type '" + typeCode + "' for metaData: " + metaData.SqlDbType); + Debug.Assert( + ( + typeCode != ExtendedClrTypeCode.DataTable && + typeCode != ExtendedClrTypeCode.DbDataReader && + typeCode != ExtendedClrTypeCode.IEnumerableOfSqlDataRecord + ) || + CanAccessSetterDirectly(metaData, typeCode), + "Un-validated type '" + typeCode + "' for metaData: " + metaData.SqlDbType + ); if (typeCode == ExtendedClrTypeCode.DateTime) { @@ -1919,13 +1912,18 @@ internal static void SetCompatibleValueV200( int offset, int length, ParameterPeekAheadValue peekAhead - ) + ) { // Ensure caller validated compatibility for types handled directly in this method - Debug.Assert((ExtendedClrTypeCode.DataTable != typeCode && - ExtendedClrTypeCode.DbDataReader != typeCode && - ExtendedClrTypeCode.IEnumerableOfSqlDataRecord != typeCode) || - CanAccessSetterDirectly(metaData, typeCode), "Un-validated type '" + typeCode + "' for metaData: " + metaData.SqlDbType); + Debug.Assert( + ( + typeCode != ExtendedClrTypeCode.DataTable && + typeCode != ExtendedClrTypeCode.DbDataReader && + typeCode != ExtendedClrTypeCode.IEnumerableOfSqlDataRecord + ) || + CanAccessSetterDirectly(metaData, typeCode), + "Un-validated type '" + typeCode + "' for metaData: " + metaData.SqlDbType + ); switch (typeCode) { @@ -2225,8 +2223,11 @@ internal static void FillCompatibleSettersFromReader(SmiEventSink_Default sink, ); if ((storageType == SqlBuffer.StorageType.DateTime2) || (storageType == SqlBuffer.StorageType.Date)) SetCompatibleValueV200(sink, setters, i, metaData[i], o, typeCode, 0, 0, null, storageType); + } else + { SetCompatibleValueV200(sink, setters, i, metaData[i], o, typeCode, 0, 0, null); + } } break; @@ -2244,7 +2245,7 @@ internal static void FillCompatibleSettersFromReader(SmiEventSink_Default sink, SetDateTime_Checked(sink, setters, i, metaData[i], reader.GetDateTime(i)); break; case SqlDbType.Time: - { // block to scope sqlReader local and avoid conflicts + { Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.TimeSpan)); TimeSpan ts; if (reader is SqlDataReader sqlReader) @@ -2259,7 +2260,7 @@ internal static void FillCompatibleSettersFromReader(SmiEventSink_Default sink, } break; case SqlDbType.DateTimeOffset: - { // block to scope sqlReader local and avoid conflicts + { Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.DateTimeOffset)); DateTimeOffset dto; if (reader is SqlDataReader sqlReader) @@ -2290,7 +2291,7 @@ internal static void FillCompatibleSettersFromRecord(SmiEventSink_Default sink, { for (int i = 0; i < metaData.Length; ++i) { - if (null != useDefaultValues && useDefaultValues[i]) + if (useDefaultValues != null && useDefaultValues[i]) { continue; } @@ -2407,7 +2408,7 @@ internal static void FillCompatibleSettersFromRecord(SmiEventSink_Default sink, SetDateTime_Checked(sink, setters, i, metaData[i], record.GetDateTime(i)); break; case SqlDbType.Time: - { // block to scope sqlReader local and avoid conflicts + { Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.TimeSpan)); TimeSpan ts; if (record is SqlDataRecord sqlRecord) @@ -2422,7 +2423,7 @@ internal static void FillCompatibleSettersFromRecord(SmiEventSink_Default sink, } break; case SqlDbType.DateTimeOffset: - { // block to scope sqlReader local and avoid conflicts + { Debug.Assert(CanAccessSetterDirectly(metaData[i], ExtendedClrTypeCode.DateTimeOffset)); DateTimeOffset dto; if (record is SqlDataRecord sqlRecord) @@ -2453,67 +2454,63 @@ private static object GetUdt_LengthChecked(SmiEventSink_Default sink, ITypedGett object result; if (IsDBNull_Unchecked(sink, getters, ordinal)) { - 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, Array.Empty(), CultureInfo.InvariantCulture); + Type type = metaData.Type; + Debug.Assert(type != null, "Unexpected null of udtType on GetUdt_LengthChecked!"); + result = type.InvokeMember("Null", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static, null, null, Array.Empty(), CultureInfo.InvariantCulture); Debug.Assert(result != null); } else { // Note: do not need to copy getter stream, since it will not be used beyond // deserialization (valid lifetime of getters is limited). - Stream s = new SmiGettersStream(sink, getters, ordinal, metaData); - result = SerializationHelperSql9.Deserialize(s, metaData.Type); + Stream stream = new SmiGettersStream(sink, getters, ordinal, metaData); + result = SerializationHelperSql9.Deserialize(stream, metaData.Type); } return result; } private static decimal GetDecimal_PossiblyMoney(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal, SmiMetaData metaData) { - if (SqlDbType.Decimal == metaData.SqlDbType) + if (metaData.SqlDbType == SqlDbType.Decimal) { return GetSqlDecimal_Unchecked(sink, getters, ordinal).Value; } else { - Debug.Assert(SqlDbType.Money == metaData.SqlDbType || - SqlDbType.SmallMoney == metaData.SqlDbType, - "Unexpected sqldbtype=" + metaData.SqlDbType); + Debug.Assert(metaData.SqlDbType == SqlDbType.Money || metaData.SqlDbType == SqlDbType.SmallMoney, "Unexpected sqldbtype=" + metaData.SqlDbType); return GetSqlMoney_Unchecked(sink, getters, ordinal).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) + if (metaData.SqlDbType == SqlDbType.Decimal || metaData.SqlDbType == SqlDbType.Variant) { SetDecimal_Unchecked(sink, setters, ordinal, value); } else { - Debug.Assert(SqlDbType.Money == metaData.SqlDbType || - SqlDbType.SmallMoney == metaData.SqlDbType, - "Unexpected sqldbtype=" + metaData.SqlDbType); + Debug.Assert(metaData.SqlDbType == SqlDbType.Money || metaData.SqlDbType == SqlDbType.SmallMoney, "Unexpected sqldbtype=" + metaData.SqlDbType); SetSqlMoney_Checked(sink, setters, ordinal, metaData, new SqlMoney(value)); } } // Hard coding smalldatetime limits... - private static readonly DateTime s_dtSmallMax = new(2079, 06, 06, 23, 59, 29, 998); - private static readonly DateTime s_dtSmallMin = new(1899, 12, 31, 23, 59, 29, 999); + 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 void VerifyDateTimeRange(SqlDbType dbType, DateTime value) { - if (SqlDbType.SmallDateTime == dbType && (s_dtSmallMax < value || s_dtSmallMin > value)) + if (dbType == SqlDbType.SmallDateTime && (s_smallDateTimeMax < value || s_smallDateTimeMin > value)) { throw ADP.InvalidMetaDataValue(); } } - private static readonly TimeSpan s_timeMin = TimeSpan.Zero; - private static readonly TimeSpan s_timeMax = new(TimeSpan.TicksPerDay - 1); + private static readonly TimeSpan s_timeSpanMin = TimeSpan.Zero; + private static readonly TimeSpan s_timeSpanMax = new TimeSpan(TimeSpan.TicksPerDay - 1); private static void VerifyTimeRange(SqlDbType dbType, TimeSpan value) { - if (SqlDbType.Time == dbType && (s_timeMin > value || value > s_timeMax)) + if (dbType == SqlDbType.Time && (s_timeSpanMin > value || value > s_timeSpanMax)) { throw ADP.InvalidMetaDataValue(); } @@ -2522,7 +2519,7 @@ private static void VerifyTimeRange(SqlDbType dbType, TimeSpan value) private static void SetDateTime_Checked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, DateTime value) { VerifyDateTimeRange(metaData.SqlDbType, value); - SetDateTime_Unchecked(sink, setters, ordinal, ((SqlDbType.Date == metaData.SqlDbType) ? value.Date : value)); + SetDateTime_Unchecked(sink, setters, ordinal, (metaData.SqlDbType == SqlDbType.Date) ? value.Date : value); } private static void SetTimeSpan_Checked(SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, TimeSpan value) @@ -2554,10 +2551,10 @@ private static void SetDate_Checked(SmiEventSink_Default sink, ITypedSettersV3 s private static void SetSqlMoney_Checked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlMoney value) { - if (!value.IsNull && SqlDbType.SmallMoney == metaData.SqlDbType) + if (!value.IsNull && metaData.SqlDbType == SqlDbType.SmallMoney) { decimal decimalValue = value.Value; - if (TdsEnums.SQL_SMALL_MONEY_MIN > decimalValue || TdsEnums.SQL_SMALL_MONEY_MAX < decimalValue) + if (decimalValue < TdsEnums.SQL_SMALL_MONEY_MIN || decimalValue > TdsEnums.SQL_SMALL_MONEY_MAX) { throw SQL.MoneyOverflow(decimalValue.ToString(CultureInfo.InvariantCulture)); } @@ -2567,14 +2564,14 @@ private static void SetSqlMoney_Checked(SmiEventSink_Default sink, ITypedSetters private static void SetByteArray_LengthChecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, byte[] buffer, int offset) { - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, buffer.Length, offset, buffer.Length - offset); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: buffer.Length, bufferOffset: offset, length: buffer.Length - offset); Debug.Assert(length >= 0, "buffer.Length was invalid!"); SetByteArray_Unchecked(sink, setters, ordinal, buffer, offset, length); } private static void SetCharArray_LengthChecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, char[] buffer, int offset) { - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, buffer.Length, offset, buffer.Length - offset); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: buffer.Length, bufferOffset: offset, length: buffer.Length - offset); Debug.Assert(length >= 0, "buffer.Length was invalid!"); SetCharArray_Unchecked(sink, setters, ordinal, buffer, offset, length); } @@ -2584,7 +2581,7 @@ private static void SetSqlBinary_LengthChecked(SmiEventSink_Default sink, ITyped int length = 0; if (!value.IsNull) { - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, value.Length, offset, value.Length - offset); + length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: value.Length, bufferOffset: offset, length: value.Length - offset); Debug.Assert(length >= 0, "value.Length was invalid!"); } SetSqlBinary_Unchecked(sink, setters, ordinal, value, offset, length); @@ -2592,7 +2589,6 @@ private static void SetSqlBinary_LengthChecked(SmiEventSink_Default sink, ITyped private static void SetBytes_FromRecord(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlDataRecord record, int offset) { - // 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); @@ -2600,12 +2596,12 @@ private static void SetBytes_FromRecord(SmiEventSink_Default sink, ITypedSetters { bufferLength = NoLengthLimit; } - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, checked((int)bufferLength), offset, checked((int)bufferLength)); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: checked((int)bufferLength), bufferOffset: offset, length: checked((int)bufferLength)); int chunkSize; - if (length > __maxByteChunkSize || length < 0) + if (length > MaxByteChunkSize || length < 0) { - chunkSize = __maxByteChunkSize; + chunkSize = MaxByteChunkSize; } else { @@ -2618,18 +2614,17 @@ private static void SetBytes_FromRecord(SmiEventSink_Default sink, ITypedSetters long currentOffset = offset; long lengthWritten = 0; - while ((length < 0 || lengthWritten < length) && - 0 != (bytesRead = record.GetBytes(ordinal, currentOffset, buffer, 0, chunkSize)) && - 0 != bytesWritten) + while ( + (length < 0 || lengthWritten < length) && + (bytesRead = record.GetBytes(ordinal, currentOffset, buffer, 0, chunkSize)) != 0 && + bytesWritten != 0 + ) { bytesWritten = setters.SetBytes(sink, ordinal, currentOffset, buffer, 0, checked((int)bytesRead)); sink.ProcessMessagesAndThrow(); checked { currentOffset += bytesWritten; - } - checked - { lengthWritten += bytesWritten; } } @@ -2641,13 +2636,12 @@ private static void SetBytes_FromRecord(SmiEventSink_Default sink, ITypedSetters private static void SetBytes_FromReader(SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, DbDataReader reader, int offset) { - // Deal with large values by sending bufferLength of NoLengthLimit (== assume // CheckXetParameters will ignore requested-length checks in this case) - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, NoLengthLimit /* buffer length */, offset, NoLengthLimit /* requested length */ ); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: NoLengthLimit, bufferOffset: offset, length: NoLengthLimit); // Use fixed chunk size for all cases to avoid inquiring from reader. - int chunkSize = __maxByteChunkSize; + int chunkSize = MaxByteChunkSize; byte[] buffer = new byte[chunkSize]; long bytesRead; @@ -2655,18 +2649,17 @@ private static void SetBytes_FromReader(SmiEventSink_Default sink, SmiTypedGette long currentOffset = offset; long lengthWritten = 0; - while ((length < 0 || lengthWritten < length) && - 0 != (bytesRead = reader.GetBytes(ordinal, currentOffset, buffer, 0, chunkSize)) && - 0 != bytesWritten) + while ( + (length < 0 || lengthWritten < length) && + (bytesRead = reader.GetBytes(ordinal, currentOffset, buffer, 0, chunkSize)) != 0 && + bytesWritten != 0 + ) { bytesWritten = setters.SetBytes(sink, ordinal, currentOffset, buffer, 0, checked((int)bytesRead)); sink.ProcessMessagesAndThrow(); checked { currentOffset += bytesWritten; - } - checked - { lengthWritten += bytesWritten; } } @@ -2688,14 +2681,13 @@ private static void SetSqlBytes_LengthChecked(SmiEventSink_Default sink, ITypedS { bufferLength = NoLengthLimit; } - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, checked((int)bufferLength), offset, checked((int)bufferLength)); + length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: checked((int)bufferLength), bufferOffset: offset, length: checked((int)bufferLength)); } SetSqlBytes_Unchecked(sink, setters, ordinal, value, 0, length); } private static void SetChars_FromRecord(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, SqlDataRecord record, int offset) { - // 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); @@ -2703,18 +2695,18 @@ private static void SetChars_FromRecord(SmiEventSink_Default sink, ITypedSetters { bufferLength = NoLengthLimit; } - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, checked((int)bufferLength), offset, checked((int)bufferLength - offset)); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: checked((int)bufferLength), bufferOffset: offset, length: checked((int)bufferLength - offset)); int chunkSize; - if (length > __maxCharChunkSize || length < 0) + if (length > MaxCharChunkSize || length < 0) { if (MetaDataUtilsSmi.IsAnsiType(metaData.SqlDbType)) { - chunkSize = __maxByteChunkSize; + chunkSize = MaxByteChunkSize; } else { - chunkSize = __maxCharChunkSize; + chunkSize = MaxCharChunkSize; } } else @@ -2728,18 +2720,17 @@ private static void SetChars_FromRecord(SmiEventSink_Default sink, ITypedSetters long currentOffset = offset; long lengthWritten = 0; - while ((length < 0 || lengthWritten < length) && - 0 != (charsRead = record.GetChars(ordinal, currentOffset, buffer, 0, chunkSize)) && - 0 != charsWritten) + while ( + (length < 0 || lengthWritten < length) && + (charsRead = record.GetChars(ordinal, currentOffset, buffer, 0, chunkSize)) != 0 && + charsWritten != 0 + ) { charsWritten = setters.SetChars(sink, ordinal, currentOffset, buffer, 0, checked((int)charsRead)); sink.ProcessMessagesAndThrow(); checked { currentOffset += charsWritten; - } - checked - { lengthWritten += charsWritten; } } @@ -2777,20 +2768,19 @@ private static void SetCharsOrString_FromReader(SmiEventSink_Default sink, SmiTy // Use chunking via SetChars to transfer a value from a reader to a gettersetter private static void SetChars_FromReader(SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, DbDataReader reader, int offset) { - // Deal with large values by sending bufferLength of NoLengthLimit (== assume // CheckXetParameters will ignore requested-length checks in this case) - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, NoLengthLimit /* buffer length */, offset, NoLengthLimit /* requested length */ ); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: NoLengthLimit , bufferOffset: offset, length: NoLengthLimit ); // Use fixed chunk size for all cases to avoid inquiring from reader. int chunkSize; if (MetaDataUtilsSmi.IsAnsiType(metaData.SqlDbType)) { - chunkSize = __maxByteChunkSize; + chunkSize = MaxByteChunkSize; } else { - chunkSize = __maxCharChunkSize; + chunkSize = MaxCharChunkSize; } char[] buffer = new char[chunkSize]; @@ -2799,18 +2789,17 @@ private static void SetChars_FromReader(SmiEventSink_Default sink, SmiTypedGette long currentOffset = offset; long lengthWritten = 0; - while ((length < 0 || lengthWritten < length) && - 0 != (charsRead = reader.GetChars(ordinal, currentOffset, buffer, 0, chunkSize)) && - 0 != charsWritten) + while ( + (length < 0 || lengthWritten < length) && + (charsRead = reader.GetChars(ordinal, currentOffset, buffer, 0, chunkSize)) != 0 && + charsWritten != 0 + ) { charsWritten = setters.SetChars(sink, ordinal, currentOffset, buffer, 0, checked((int)charsRead)); sink.ProcessMessagesAndThrow(); checked { currentOffset += charsWritten; - } - checked - { lengthWritten += charsWritten; } } @@ -2823,7 +2812,7 @@ private static void SetChars_FromReader(SmiEventSink_Default sink, SmiTypedGette private static void SetString_FromReader(SmiEventSink_Default sink, SmiTypedGetterSetter setters, int ordinal, SmiMetaData metaData, DbDataReader reader, int offset) { string value = reader.GetString(ordinal); - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, value.Length, 0, NoLengthLimit /* buffer */, offset, NoLengthLimit /* request */); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, value.Length, fieldOffset: 0, bufferLength: NoLengthLimit, bufferOffset: offset, length: NoLengthLimit); setters.SetString(sink, ordinal, value, offset, length); sink.ProcessMessagesAndThrow(); @@ -2841,7 +2830,7 @@ private static void SetSqlChars_LengthChecked(SmiEventSink_Default sink, ITypedS { bufferLength = NoLengthLimit; } - length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, checked((int)bufferLength), offset, checked((int)bufferLength - offset)); + length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: checked((int)bufferLength), bufferOffset: offset, length: checked((int)bufferLength - offset)); } SetSqlChars_Unchecked(sink, setters, ordinal, value, 0, length); } @@ -2855,7 +2844,7 @@ private static void SetSqlString_LengthChecked(SmiEventSink_Default sink, ITyped else { string stringValue = value.Value; - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, stringValue.Length, offset, stringValue.Length - offset); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, bufferLength: stringValue.Length, bufferOffset: offset, length: stringValue.Length - offset); Debug.Assert(length >= 0, "value.Length was invalid!"); SetSqlString_Unchecked(sink, setters, ordinal, metaData, value, offset, length); } @@ -2863,7 +2852,7 @@ private static void SetSqlString_LengthChecked(SmiEventSink_Default sink, ITyped private static void SetString_LengthChecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, string value, int offset) { - int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, NoLengthLimit /* actual */, 0, value.Length, offset, checked(value.Length - offset)); + int length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength, actualLength: NoLengthLimit, fieldOffset: 0, value.Length, offset, checked(value.Length - offset)); Debug.Assert(length >= 0, "value.Length was invalid!"); SetString_Unchecked(sink, setters, ordinal, value, offset, length); } @@ -2913,10 +2902,14 @@ private static bool CanAccessGetterDirectly(SmiMetaData metaData, ExtendedClrTyp bool returnValue = s_canAccessGetterDirectly[(int)setterTypeCode, (int)metaData.SqlDbType]; // Additional restrictions to distinguish TVPs and Structured UDTs - if (returnValue && - (ExtendedClrTypeCode.DataTable == setterTypeCode || - ExtendedClrTypeCode.DbDataReader == setterTypeCode || - ExtendedClrTypeCode.IEnumerableOfSqlDataRecord == setterTypeCode)) + if ( + returnValue && + ( + setterTypeCode == ExtendedClrTypeCode.DataTable || + setterTypeCode == ExtendedClrTypeCode.DbDataReader || + setterTypeCode == ExtendedClrTypeCode.IEnumerableOfSqlDataRecord + ) + ) { returnValue = metaData.IsMultiValued; } @@ -2935,10 +2928,14 @@ private static bool CanAccessSetterDirectly(SmiMetaData metaData, ExtendedClrTyp bool returnValue = s_canAccessSetterDirectly[(int)setterTypeCode, (int)metaData.SqlDbType]; // Additional restrictions to distinguish TVPs and Structured UDTs - if (returnValue && - (ExtendedClrTypeCode.DataTable == setterTypeCode || - ExtendedClrTypeCode.DbDataReader == setterTypeCode || - ExtendedClrTypeCode.IEnumerableOfSqlDataRecord == setterTypeCode)) + if ( + returnValue && + ( + setterTypeCode == ExtendedClrTypeCode.DataTable || + setterTypeCode == ExtendedClrTypeCode.DbDataReader || + setterTypeCode == ExtendedClrTypeCode.IEnumerableOfSqlDataRecord + ) + ) { returnValue = metaData.IsMultiValued; } @@ -2963,18 +2960,20 @@ private static long PositiveMin(long first, long second) // Check Get Byte/Chars parameters, throw or adjust invalid values private static int CheckXetParameters( - SqlDbType dbType, - long maxLength, - long actualLength, - long fieldOffset, - int bufferLength, - int bufferOffset, - int length) - { - if (0 > fieldOffset) + SqlDbType dbType, + long maxLength, + long actualLength, + long fieldOffset, + int bufferLength, + int bufferOffset, + int length + ) + { + if (fieldOffset < 0) + { throw ADP.NegativeParameter(nameof(fieldOffset)); + } - // if negative buffer index, throw if (bufferOffset < 0) { throw ADP.InvalidDestinationBufferIndex(bufferLength, bufferOffset, nameof(bufferOffset)); @@ -2991,20 +2990,22 @@ private static int CheckXetParameters( return length; } - // if bad buffer index, throw if (bufferOffset > bufferLength) { throw ADP.InvalidDestinationBufferIndex(bufferLength, bufferOffset, nameof(bufferOffset)); } - // if there is not enough room in the buffer for data if (checked(length + bufferOffset) > bufferLength) + { throw ADP.InvalidBufferSizeOrIndex(length, bufferOffset); + } if (length < 0) + { throw ADP.InvalidDataLength(length); + } - if (0 <= actualLength && actualLength <= fieldOffset) + if (actualLength >=0 && actualLength <= fieldOffset) { return 0; } @@ -3017,21 +3018,20 @@ private static int CheckXetParameters( // special case for variants, since their maxLength is actually a bit bigger than // the actual data length allowed. - if (SqlDbType.Variant == dbType) + if (dbType == SqlDbType.Variant) { length = Math.Min(length, TdsEnums.TYPE_SIZE_LIMIT); } - Debug.Assert(0 > maxLength || 0 > actualLength || - maxLength >= actualLength, "Actual = " + actualLength + ", max = " + maxLength + ", sqldbtype=" + dbType); + Debug.Assert(0 > maxLength || 0 > actualLength || maxLength >= actualLength, "Actual = " + actualLength + ", max = " + maxLength + ", sqldbtype=" + dbType); - if (0 <= actualLength) + if (actualLength >= 0) { // Length is guaranteed to be >= 0 coming in and actualLength >= fieldOffset, so this operation guarantees result >= 0 length = (int)Math.Min((long)length, actualLength - fieldOffset); Debug.Assert(length >= 0, "result < 0, actualLength/fieldOffset problem?"); } - else if (SqlDbType.Udt != dbType && 0 <= maxLength) + else if (dbType != SqlDbType.Udt && maxLength >= 0) { length = (int)Math.Min((long)length, maxLength - fieldOffset); Debug.Assert(length >= 0, "Result < 0, maxlen/fieldoffset problem?"); @@ -3229,7 +3229,6 @@ private static long GetBytesLength_Unchecked(SmiEventSink_Default sink, ITypedGe return result; } - private static char[] GetCharArray_Unchecked(SmiEventSink_Default sink, ITypedGettersV3 getters, int ordinal) { Debug.Assert(!IsDBNull_Unchecked(sink, getters, ordinal)); @@ -3437,11 +3436,11 @@ private static void SetByteArray_Unchecked(SmiEventSink_Default sink, ITypedSett private static void SetStream_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metadata, StreamDataFeed feed) { long len = metadata.MaxLength; - byte[] buff = new byte[constBinBufferSize]; + byte[] buff = new byte[DefaultBinaryBufferSize]; int nWritten = 0; do { - int readSize = constBinBufferSize; + int readSize = DefaultBinaryBufferSize; if (len > 0 && nWritten + readSize > len) { readSize = (int)(len - nWritten); @@ -3469,11 +3468,11 @@ private static void SetStream_Unchecked(SmiEventSink_Default sink, ITypedSetters private static void SetTextReader_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metadata, TextDataFeed feed) { long len = metadata.MaxLength; - char[] buff = new char[constTextBufferSize]; + char[] buff = new char[DefaultTextBufferSize]; int nWritten = 0; do { - int readSize = constTextBufferSize; + int readSize = DefaultTextBufferSize; if (len > 0 && nWritten + readSize > len) { readSize = (int)(len - nWritten); @@ -3549,7 +3548,7 @@ private static void SetDateTime_Unchecked(SmiEventSink_Default sink, ITypedSette private static void SetDateTime2_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, DateTime value) { - Debug.Assert(SqlDbType.Variant == metaData.SqlDbType, "Invalid type. This should be called only when the type is variant."); + Debug.Assert(metaData.SqlDbType == SqlDbType.Variant, "Invalid type. This should be called only when the type is variant."); setters.SetVariantMetaData(sink, ordinal, SmiMetaData.DefaultDateTime2); setters.SetDateTime(sink, ordinal, value); sink.ProcessMessagesAndThrow(); @@ -3557,7 +3556,7 @@ private static void SetDateTime2_Unchecked(SmiEventSink_Default sink, ITypedSett private static void SetDate_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, SmiMetaData metaData, DateTime value) { - Debug.Assert(SqlDbType.Variant == metaData.SqlDbType, "Invalid type. This should be called only when the type is variant."); + Debug.Assert(metaData.SqlDbType == SqlDbType.Variant, "Invalid type. This should be called only when the type is variant."); setters.SetVariantMetaData(sink, ordinal, SmiMetaData.DefaultDate); setters.SetDateTime(sink, ordinal, value); sink.ProcessMessagesAndThrow(); @@ -3661,9 +3660,9 @@ private static void SetSqlBytes_Unchecked(SmiEventSink_Default sink, ITypedSette else { int chunkSize; - if (length > __maxByteChunkSize || length < 0) + if (length > MaxByteChunkSize || length < 0) { - chunkSize = __maxByteChunkSize; + chunkSize = MaxByteChunkSize; } else { @@ -3676,9 +3675,11 @@ private static void SetSqlBytes_Unchecked(SmiEventSink_Default sink, ITypedSette long currentOffset = offset; long lengthWritten = 0; - while ((length < 0 || lengthWritten < length) && - 0 != (bytesRead = value.Read(currentOffset, buffer, 0, chunkSize)) && - 0 != bytesWritten) + while ( + (length < 0 || lengthWritten < length) && + (bytesRead = value.Read(currentOffset, buffer, 0, chunkSize)) != 0 && + bytesWritten != 0 + ) { bytesWritten = setters.SetBytes(sink, ordinal, currentOffset, buffer, 0, checked((int)bytesRead)); sink.ProcessMessagesAndThrow(); @@ -3708,9 +3709,9 @@ private static void SetSqlChars_Unchecked(SmiEventSink_Default sink, ITypedSette else { int chunkSize; - if (length > __maxCharChunkSize || length < 0) + if (length > MaxCharChunkSize || length < 0) { - chunkSize = __maxCharChunkSize; + chunkSize = MaxCharChunkSize; } else { @@ -3723,18 +3724,17 @@ private static void SetSqlChars_Unchecked(SmiEventSink_Default sink, ITypedSette long currentOffset = offset; long lengthWritten = 0; - while ((length < 0 || lengthWritten < length) && - 0 != (charsRead = value.Read(currentOffset, buffer, 0, chunkSize)) && - 0 != charsWritten) + while ( + (length < 0 || lengthWritten < length) && + (charsRead = value.Read(currentOffset, buffer, 0, chunkSize)) != 0 && + charsWritten != 0 + ) { charsWritten = setters.SetChars(sink, ordinal, currentOffset, buffer, 0, checked((int)charsRead)); sink.ProcessMessagesAndThrow(); checked { currentOffset += charsWritten; - } - checked - { lengthWritten += charsWritten; } } @@ -3844,7 +3844,7 @@ private static void SetSqlMoney_Unchecked(SmiEventSink_Default sink, ITypedSette } else { - if (SqlDbType.Variant == metaData.SqlDbType) + if (metaData.SqlDbType == SqlDbType.Variant) { setters.SetVariantMetaData(sink, ordinal, SmiMetaData.DefaultMoney); sink.ProcessMessagesAndThrow(); @@ -3877,17 +3877,18 @@ private static void SetSqlString_Unchecked(SmiEventSink_Default sink, ITypedSett } else { - if (SqlDbType.Variant == metaData.SqlDbType) + if (metaData.SqlDbType == SqlDbType.Variant) { // Set up a NVarChar metadata with correct LCID/Collation metaData = new SmiMetaData( - SqlDbType.NVarChar, - SmiMetaData.MaxUnicodeCharacters, - 0, - 0, - value.LCID, - value.SqlCompareOptions, - null); + SqlDbType.NVarChar, + SmiMetaData.MaxUnicodeCharacters, + precision: 0, + scale: 0, + value.LCID, + value.SqlCompareOptions, + userDefinedType: null + ); setters.SetVariantMetaData(sink, ordinal, metaData); sink.ProcessMessagesAndThrow(); } @@ -3911,23 +3912,25 @@ private static void SetSqlXml_Unchecked(SmiEventSink_Default sink, ITypedSetters private static void SetXmlReader_Unchecked(SmiEventSink_Default sink, ITypedSettersV3 setters, int ordinal, XmlReader xmlReader) { // set up writer - XmlWriterSettings WriterSettings = new(); - WriterSettings.CloseOutput = false; // don't close the memory stream - WriterSettings.ConformanceLevel = ConformanceLevel.Fragment; - WriterSettings.Encoding = System.Text.Encoding.Unicode; - WriterSettings.OmitXmlDeclaration = true; - - System.IO.Stream target = new SmiSettersStream(sink, setters, ordinal, SmiMetaData.DefaultXml); - - XmlWriter xmlWriter = XmlWriter.Create(target, WriterSettings); + XmlWriterSettings WriterSettings = new XmlWriterSettings + { + CloseOutput = false, // don't close the memory stream + ConformanceLevel = ConformanceLevel.Fragment, + Encoding = System.Text.Encoding.Unicode, + OmitXmlDeclaration = true + }; - // now spool the data into the writer (WriteNode will call Read()) - xmlReader.Read(); - while (!xmlReader.EOF) + using (Stream target = new SmiSettersStream(sink, setters, ordinal, SmiMetaData.DefaultXml)) + using (XmlWriter xmlWriter = XmlWriter.Create(target, WriterSettings)) { - xmlWriter.WriteNode(xmlReader, true); + // now spool the data into the writer (WriteNode will call Read()) + xmlReader.Read(); + while (!xmlReader.EOF) + { + xmlWriter.WriteNode(xmlReader, true); + } + xmlWriter.Flush(); } - xmlWriter.Flush(); sink.ProcessMessagesAndThrow(); } @@ -3945,7 +3948,7 @@ private static void SetDbDataReader_Unchecked( int ordinal, SmiMetaData metaData, DbDataReader value - ) + ) { // Get the target gettersetter setters = setters.GetTypedGetterSetter(sink, ordinal); @@ -3971,7 +3974,7 @@ private static void SetIEnumerableOfSqlDataRecord_Unchecked( SmiMetaData metaData, IEnumerable value, ParameterPeekAheadValue peekAhead - ) + ) { // Get target gettersetter setters = setters.GetTypedGetterSetter(sink, ordinal); @@ -4005,39 +4008,36 @@ ParameterPeekAheadValue peekAhead enumerator = value.GetEnumerator(); } - using (enumerator) + while (enumerator.MoveNext()) { - while (enumerator.MoveNext()) - { - setters.NewElement(sink); - sink.ProcessMessagesAndThrow(); + setters.NewElement(sink); + sink.ProcessMessagesAndThrow(); - SqlDataRecord record = enumerator.Current; + SqlDataRecord record = enumerator.Current; - if (record.FieldCount != mdFields.Length) - { - throw SQL.EnumeratedRecordFieldCountChanged(recordNumber); - } + if (record.FieldCount != mdFields.Length) + { + throw SQL.EnumeratedRecordFieldCountChanged(recordNumber); + } - for (int i = 0; i < record.FieldCount; i++) + for (int i = 0; i < record.FieldCount; i++) + { + if (!MetaDataUtilsSmi.IsCompatible(metaData.FieldMetaData[i], record.GetSqlMetaData(i))) { - if (!MetaDataUtilsSmi.IsCompatible(metaData.FieldMetaData[i], record.GetSqlMetaData(i))) - { - throw SQL.EnumeratedRecordMetaDataChanged(record.GetName(i), recordNumber); - } + throw SQL.EnumeratedRecordMetaDataChanged(record.GetName(i), recordNumber); } - - FillCompatibleSettersFromRecord(sink, setters, mdFields, record, defaults); - recordNumber++; } - setters.EndElements(sink); - sink.ProcessMessagesAndThrow(); + FillCompatibleSettersFromRecord(sink, setters, mdFields, record, defaults); + recordNumber++; } + + setters.EndElements(sink); + sink.ProcessMessagesAndThrow(); + } finally { - // Clean up! if (enumerator is IDisposable disposable) { disposable.Dispose();