diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/Common/AdapterUtil.SqlClient.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/Common/AdapterUtil.SqlClient.cs index 2c66613ca0..da6a069c90 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/Common/AdapterUtil.SqlClient.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/Common/AdapterUtil.SqlClient.cs @@ -609,11 +609,6 @@ internal static Delegate FindBuilder(MulticastDelegate mcd) return null; } - internal static void TimerCurrent(out long ticks) - { - ticks = DateTime.UtcNow.ToFileTimeUtc(); - } - internal static long TimerCurrent() { return DateTime.UtcNow.ToFileTimeUtc(); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBuffer.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBuffer.cs index 891d788925..1099c974ba 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBuffer.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBuffer.cs @@ -41,38 +41,38 @@ internal enum StorageType internal struct DateTimeInfo { // This is used to store DateTime - internal int daypart; - internal int timepart; + internal int _daypart; + internal int _timepart; } internal struct NumericInfo { // This is used to store Decimal data - internal int data1; - internal int data2; - internal int data3; - internal int data4; - internal byte precision; - internal byte scale; - internal bool positive; + internal int _data1; + internal int _data2; + internal int _data3; + internal int _data4; + internal byte _precision; + internal byte _scale; + internal bool _positive; } internal struct TimeInfo { - internal long ticks; - internal byte scale; + internal long _ticks; + internal byte _scale; } internal struct DateTime2Info { - internal int date; - internal TimeInfo timeInfo; + internal int _date; + internal TimeInfo _timeInfo; } internal struct DateTimeOffsetInfo { - internal DateTime2Info dateTime2Info; - internal short offset; + internal DateTime2Info _dateTime2Info; + internal short _offset; } [StructLayout(LayoutKind.Explicit)] @@ -126,26 +126,11 @@ private SqlBuffer(SqlBuffer value) _object = value._object; } - internal bool IsEmpty - { - get - { - return (StorageType.Empty == _type); - } - } + internal bool IsEmpty => (StorageType.Empty == _type); - internal bool IsNull - { - get - { - return _isNull; - } - } + internal bool IsNull => _isNull; - internal StorageType VariantInternalStorageType - { - get { return _type; } - } + internal StorageType VariantInternalStorageType => _type; internal bool Boolean { @@ -157,7 +142,7 @@ internal bool Boolean { return _value._boolean; } - return (bool)this.Value; // anything else we haven't thought of goes through boxing. + return (bool)Value; // anything else we haven't thought of goes through boxing. } set { @@ -178,7 +163,7 @@ internal byte Byte { return _value._byte; } - return (byte)this.Value; // anything else we haven't thought of goes through boxing. + return (byte)Value; // anything else we haven't thought of goes through boxing. } set { @@ -194,7 +179,7 @@ internal byte[] ByteArray get { ThrowIfNull(); - return this.SqlBinary.Value; + return SqlBinary.Value; } } @@ -214,9 +199,9 @@ internal DateTime DateTime } if (StorageType.DateTime == _type) { - return SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo.daypart, _value._dateTimeInfo.timepart); + return SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo._daypart, _value._dateTimeInfo._timepart); } - return (DateTime)this.Value; // anything else we haven't thought of goes through boxing. + return (DateTime)Value; // anything else we haven't thought of goes through boxing. } } @@ -228,11 +213,11 @@ internal decimal Decimal if (StorageType.Decimal == _type) { - if (_value._numericInfo.data4 != 0 || _value._numericInfo.scale > 28) + if (_value._numericInfo._data4 != 0 || _value._numericInfo._scale > 28) { throw new OverflowException(SQLResource.ConversionOverflowMessage); } - return new decimal(_value._numericInfo.data1, _value._numericInfo.data2, _value._numericInfo.data3, !_value._numericInfo.positive, _value._numericInfo.scale); + return new decimal(_value._numericInfo._data1, _value._numericInfo._data2, _value._numericInfo._data3, !_value._numericInfo._positive, _value._numericInfo._scale); } if (StorageType.Money == _type) { @@ -245,7 +230,7 @@ internal decimal Decimal } return new decimal((int)(l & 0xffffffff), (int)(l >> 32), 0, isNegative, 4); } - return (decimal)this.Value; // anything else we haven't thought of goes through boxing. + return (decimal)Value; // anything else we haven't thought of goes through boxing. } } @@ -259,7 +244,7 @@ internal double Double { return _value._double; } - return (double)this.Value; // anything else we haven't thought of goes through boxing. + return (double)Value; // anything else we haven't thought of goes through boxing. } set { @@ -283,7 +268,7 @@ internal Guid Guid { return ((SqlGuid)_object).Value; } - return (Guid)this.Value; + return (Guid)Value; } set @@ -305,7 +290,7 @@ internal short Int16 { return _value._int16; } - return (short)this.Value; // anything else we haven't thought of goes through boxing. + return (short)Value; // anything else we haven't thought of goes through boxing. } set { @@ -326,7 +311,7 @@ internal int Int32 { return _value._int32; } - return (int)this.Value; // anything else we haven't thought of goes through boxing. + return (int)Value; // anything else we haven't thought of goes through boxing. } set { @@ -347,7 +332,7 @@ internal long Int64 { return _value._int64; } - return (long)this.Value; // anything else we haven't thought of goes through boxing. + return (long)Value; // anything else we haven't thought of goes through boxing. } set { @@ -368,7 +353,7 @@ internal float Single { return _value._single; } - return (float)this.Value; // anything else we haven't thought of goes through boxing. + return (float)Value; // anything else we haven't thought of goes through boxing. } set { @@ -393,12 +378,12 @@ internal string String { return ((SqlCachedBuffer)(_object)).ToString(); } - return (string)this.Value; // anything else we haven't thought of goes through boxing. + return (string)Value; // anything else we haven't thought of goes through boxing. } } // use static list of format strings indexed by scale for perf - private static string[] s_katmaiDateTimeOffsetFormatByScale = new string[] { + private static readonly string[] s_katmaiDateTimeOffsetFormatByScale = new string[] { "yyyy-MM-dd HH:mm:ss zzz", "yyyy-MM-dd HH:mm:ss.f zzz", "yyyy-MM-dd HH:mm:ss.ff zzz", @@ -409,7 +394,7 @@ internal string String "yyyy-MM-dd HH:mm:ss.fffffff zzz", }; - private static string[] s_katmaiDateTime2FormatByScale = new string[] { + private static readonly string[] s_katmaiDateTime2FormatByScale = new string[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.f", "yyyy-MM-dd HH:mm:ss.ff", @@ -420,7 +405,7 @@ internal string String "yyyy-MM-dd HH:mm:ss.fffffff", }; - private static string[] s_katmaiTimeFormatByScale = new string[] { + private static readonly string[] s_katmaiTimeFormatByScale = new string[] { "HH:mm:ss", "HH:mm:ss.f", "HH:mm:ss.ff", @@ -439,25 +424,25 @@ internal string KatmaiDateTimeString if (StorageType.Date == _type) { - return this.DateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo); + return DateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo); } if (StorageType.Time == _type) { - byte scale = _value._timeInfo.scale; - return new DateTime(_value._timeInfo.ticks).ToString(s_katmaiTimeFormatByScale[scale], DateTimeFormatInfo.InvariantInfo); + byte scale = _value._timeInfo._scale; + return new DateTime(_value._timeInfo._ticks).ToString(s_katmaiTimeFormatByScale[scale], DateTimeFormatInfo.InvariantInfo); } if (StorageType.DateTime2 == _type) { - byte scale = _value._dateTime2Info.timeInfo.scale; - return this.DateTime.ToString(s_katmaiDateTime2FormatByScale[scale], DateTimeFormatInfo.InvariantInfo); + byte scale = _value._dateTime2Info._timeInfo._scale; + return DateTime.ToString(s_katmaiDateTime2FormatByScale[scale], DateTimeFormatInfo.InvariantInfo); } if (StorageType.DateTimeOffset == _type) { - DateTimeOffset dto = this.DateTimeOffset; - byte scale = _value._dateTimeOffsetInfo.dateTime2Info.timeInfo.scale; + DateTimeOffset dto = DateTimeOffset; + byte scale = _value._dateTimeOffsetInfo._dateTime2Info._timeInfo._scale; return dto.ToString(s_katmaiDateTimeOffsetFormatByScale[scale], DateTimeFormatInfo.InvariantInfo); } - return (string)this.Value; // anything else we haven't thought of goes through boxing. + return (string)Value; // anything else we haven't thought of goes through boxing. } } @@ -476,7 +461,7 @@ internal SqlString KatmaiDateTimeSqlString } return new SqlString(KatmaiDateTimeString); } - return (SqlString)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlString)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -488,10 +473,10 @@ internal TimeSpan Time if (StorageType.Time == _type) { - return new TimeSpan(_value._timeInfo.ticks); + return new TimeSpan(_value._timeInfo._ticks); } - return (TimeSpan)this.Value; // anything else we haven't thought of goes through boxing. + return (TimeSpan)Value; // anything else we haven't thought of goes through boxing. } } @@ -503,18 +488,18 @@ internal DateTimeOffset DateTimeOffset if (StorageType.DateTimeOffset == _type) { - TimeSpan offset = new TimeSpan(0, _value._dateTimeOffsetInfo.offset, 0); + TimeSpan offset = new TimeSpan(0, _value._dateTimeOffsetInfo._offset, 0); // datetime part presents time in UTC - return new DateTimeOffset(GetTicksFromDateTime2Info(_value._dateTimeOffsetInfo.dateTime2Info) + offset.Ticks, offset); + return new DateTimeOffset(GetTicksFromDateTime2Info(_value._dateTimeOffsetInfo._dateTime2Info) + offset.Ticks, offset); } - return (DateTimeOffset)this.Value; // anything else we haven't thought of goes through boxing. + return (DateTimeOffset)Value; // anything else we haven't thought of goes through boxing. } } private static long GetTicksFromDateTime2Info(DateTime2Info dateTime2Info) { - return (dateTime2Info.date * TimeSpan.TicksPerDay + dateTime2Info.timeInfo.ticks); + return (dateTime2Info._date * TimeSpan.TicksPerDay + dateTime2Info._timeInfo._ticks); } internal SqlBinary SqlBinary @@ -525,7 +510,7 @@ internal SqlBinary SqlBinary { return (SqlBinary)_object; } - return (SqlBinary)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlBinary)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -548,7 +533,7 @@ internal SqlBoolean SqlBoolean } return new SqlBoolean(_value._boolean); } - return (SqlBoolean)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlBoolean)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -564,7 +549,7 @@ internal SqlByte SqlByte } return new SqlByte(_value._byte); } - return (SqlByte)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlByte)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -580,7 +565,7 @@ internal SqlCachedBuffer SqlCachedBuffer } return (SqlCachedBuffer)_object; } - return (SqlCachedBuffer)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlCachedBuffer)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -603,7 +588,7 @@ internal SqlXml SqlXml } return (SqlXml)_object; } - return (SqlXml)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlXml)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -624,7 +609,7 @@ internal SqlDateTime SqlDateTime { return SqlDateTime.Null; } - return new SqlDateTime(_value._dateTimeInfo.daypart, _value._dateTimeInfo.timepart); + return new SqlDateTime(_value._dateTimeInfo._daypart, _value._dateTimeInfo._timepart); } return (SqlDateTime)SqlValue; // anything else we haven't thought of goes through boxing. } @@ -640,16 +625,16 @@ internal SqlDecimal SqlDecimal { return SqlDecimal.Null; } - return new SqlDecimal(_value._numericInfo.precision, - _value._numericInfo.scale, - _value._numericInfo.positive, - _value._numericInfo.data1, - _value._numericInfo.data2, - _value._numericInfo.data3, - _value._numericInfo.data4 + return new SqlDecimal(_value._numericInfo._precision, + _value._numericInfo._scale, + _value._numericInfo._positive, + _value._numericInfo._data1, + _value._numericInfo._data2, + _value._numericInfo._data3, + _value._numericInfo._data4 ); } - return (SqlDecimal)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlDecimal)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -665,7 +650,7 @@ internal SqlDouble SqlDouble } return new SqlDouble(_value._double); } - return (SqlDouble)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlDouble)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -681,7 +666,7 @@ internal SqlGuid SqlGuid { return IsNull ? SqlGuid.Null : (SqlGuid)_object; } - return (SqlGuid)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlGuid)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -704,7 +689,7 @@ internal SqlInt16 SqlInt16 } return new SqlInt16(_value._int16); } - return (SqlInt16)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlInt16)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -720,7 +705,7 @@ internal SqlInt32 SqlInt32 } return new SqlInt32(_value._int32); } - return (SqlInt32)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlInt32)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -736,7 +721,7 @@ internal SqlInt64 SqlInt64 } return new SqlInt64(_value._int64); } - return (SqlInt64)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlInt64)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -752,7 +737,7 @@ internal SqlMoney SqlMoney } return SqlTypeWorkarounds.SqlMoneyCtor(_value._int64, 1/*ignored*/); } - return (SqlMoney)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlMoney)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -768,7 +753,7 @@ internal SqlSingle SqlSingle } return new SqlSingle(_value._single); } - return (SqlSingle)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlSingle)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -793,7 +778,7 @@ internal SqlString SqlString } return data.ToSqlString(); } - return (SqlString)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlString)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -878,6 +863,10 @@ internal object SqlValue } } + + // these variables store pre-boxed bool values to be used when returning a boolean + // in a object typed location, if these are not used a new value is boxed each time + // one is needed which leads to a lot of garbage which needs to be collected private static readonly object s_cachedTrueObject = true; private static readonly object s_cachedFalseObject = false; @@ -894,7 +883,7 @@ internal object Value case StorageType.Empty: return DBNull.Value; case StorageType.Boolean: - return Boolean ? s_cachedTrueObject : s_cachedFalseObject; + return Boolean ? s_cachedTrueObject : s_cachedFalseObject; // return pre-boxed values for perf case StorageType.Byte: return Byte; case StorageType.DateTime: @@ -1082,8 +1071,8 @@ internal void Clear() internal void SetToDateTime(int daypart, int timepart) { Debug.Assert(IsEmpty, "setting value a second time?"); - _value._dateTimeInfo.daypart = daypart; - _value._dateTimeInfo.timepart = timepart; + _value._dateTimeInfo._daypart = daypart; + _value._dateTimeInfo._timepart = timepart; _type = StorageType.DateTime; _isNull = false; } @@ -1091,13 +1080,13 @@ internal void SetToDateTime(int daypart, int timepart) internal void SetToDecimal(byte precision, byte scale, bool positive, int[] bits) { Debug.Assert(IsEmpty, "setting value a second time?"); - _value._numericInfo.precision = precision; - _value._numericInfo.scale = scale; - _value._numericInfo.positive = positive; - _value._numericInfo.data1 = bits[0]; - _value._numericInfo.data2 = bits[1]; - _value._numericInfo.data3 = bits[2]; - _value._numericInfo.data4 = bits[3]; + _value._numericInfo._precision = precision; + _value._numericInfo._scale = scale; + _value._numericInfo._positive = positive; + _value._numericInfo._data1 = bits[0]; + _value._numericInfo._data2 = bits[1]; + _value._numericInfo._data3 = bits[2]; + _value._numericInfo._data4 = bits[3]; _type = StorageType.Decimal; _isNull = false; } @@ -1149,8 +1138,8 @@ internal void SetToTime(TimeSpan timeSpan, byte scale) Debug.Assert(IsEmpty, "setting value a second time?"); _type = StorageType.Time; - _value._timeInfo.ticks = timeSpan.Ticks; - _value._timeInfo.scale = scale; + _value._timeInfo._ticks = timeSpan.Ticks; + _value._timeInfo._scale = scale; _isNull = false; } @@ -1159,8 +1148,8 @@ internal void SetToDateTime2(ReadOnlySpan bytes, byte scale, byte denormal Debug.Assert(IsEmpty, "setting value a second time?"); int length = bytes.Length; _type = StorageType.DateTime2; - FillInTimeInfo(ref _value._dateTime2Info.timeInfo, bytes.Slice(0, length - 3), scale, denormalizedScale); // remaining 3 bytes is for date - _value._dateTime2Info.date = GetDateFromByteArray(bytes.Slice(length - 3)); // 3 bytes for date + FillInTimeInfo(ref _value._dateTime2Info._timeInfo, bytes.Slice(0, length - 3), scale, denormalizedScale); // remaining 3 bytes is for date + _value._dateTime2Info._date = GetDateFromByteArray(bytes.Slice(length - 3)); // 3 bytes for date _isNull = false; } @@ -1169,9 +1158,9 @@ internal void SetToDateTimeOffset(ReadOnlySpan bytes, byte scale, byte den Debug.Assert(IsEmpty, "setting value a second time?"); int length = bytes.Length; _type = StorageType.DateTimeOffset; - FillInTimeInfo(ref _value._dateTimeOffsetInfo.dateTime2Info.timeInfo, bytes.Slice(0, length - 5), scale, denormalizedScale); // remaining 5 bytes are for date and offset - _value._dateTimeOffsetInfo.dateTime2Info.date = GetDateFromByteArray(bytes.Slice(length - 5)); // 3 bytes for date - _value._dateTimeOffsetInfo.offset = (Int16)(bytes[length - 2] + (bytes[length - 1] << 8)); // 2 bytes for offset (Int16) + FillInTimeInfo(ref _value._dateTimeOffsetInfo._dateTime2Info._timeInfo, bytes.Slice(0, length - 5), scale, denormalizedScale); // remaining 5 bytes are for date and offset + _value._dateTimeOffsetInfo._dateTime2Info._date = GetDateFromByteArray(bytes.Slice(length - 5)); // 3 bytes for date + _value._dateTimeOffsetInfo._offset = (short)(bytes[length - 2] + (bytes[length - 1] << 8)); // 2 bytes for offset (Int16) _isNull = false; } @@ -1181,10 +1170,10 @@ internal void SetToDateTimeOffset(DateTimeOffset dateTimeOffset, byte scale) _type = StorageType.DateTimeOffset; DateTime utcDateTime = dateTimeOffset.UtcDateTime; // timeInfo stores the utc datetime of a datatimeoffset - _value._dateTimeOffsetInfo.dateTime2Info.timeInfo.ticks = utcDateTime.TimeOfDay.Ticks; - _value._dateTimeOffsetInfo.dateTime2Info.timeInfo.scale = scale; - _value._dateTimeOffsetInfo.dateTime2Info.date = utcDateTime.Subtract(DateTime.MinValue).Days; - _value._dateTimeOffsetInfo.offset = (short)dateTimeOffset.Offset.TotalMinutes; + _value._dateTimeOffsetInfo._dateTime2Info._timeInfo._ticks = utcDateTime.TimeOfDay.Ticks; + _value._dateTimeOffsetInfo._dateTime2Info._timeInfo._scale = scale; + _value._dateTimeOffsetInfo._dateTime2Info._date = utcDateTime.Subtract(DateTime.MinValue).Days; + _value._dateTimeOffsetInfo._offset = (short)dateTimeOffset.Offset.TotalMinutes; _isNull = false; } @@ -1195,21 +1184,21 @@ private static void FillInTimeInfo(ref TimeInfo timeInfo, ReadOnlySpan tim Debug.Assert(0 <= scale && scale <= 7, "invalid scale: " + scale); Debug.Assert(0 <= denormalizedScale && denormalizedScale <= 7, "invalid denormalized scale: " + denormalizedScale); - Int64 tickUnits = (Int64)timeBytes[0] + ((Int64)timeBytes[1] << 8) + ((Int64)timeBytes[2] << 16); + long tickUnits = timeBytes[0] + ((long)timeBytes[1] << 8) + ((long)timeBytes[2] << 16); if (length > 3) { - tickUnits += ((Int64)timeBytes[3] << 24); + tickUnits += ((long)timeBytes[3] << 24); } if (length > 4) { - tickUnits += ((Int64)timeBytes[4] << 32); + tickUnits += ((long)timeBytes[4] << 32); } - timeInfo.ticks = tickUnits * TdsEnums.TICKS_FROM_SCALE[scale]; + timeInfo._ticks = tickUnits * TdsEnums.TICKS_FROM_SCALE[scale]; // Once the deserialization has been completed using the value scale, we need to set the actual denormalized scale, // coming from the data type, on the original result, so that it has the proper scale setting. // This only applies for values that got serialized/deserialized for encryption. Otherwise, both scales should be equal. - timeInfo.scale = denormalizedScale; + timeInfo._scale = denormalizedScale; } private static int GetDateFromByteArray(ReadOnlySpan buf) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs index 082bec5c21..ea01f463c6 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -365,7 +365,7 @@ public bool StatisticsEnabled if (null == _statistics) { _statistics = new SqlStatistics(); - ADP.TimerCurrent(out _statistics._openTimestamp); + _statistics._openTimestamp = ADP.TimerCurrent(); } // set statistics on the parser // update timestamp; @@ -385,7 +385,7 @@ public bool StatisticsEnabled TdsParser parser = Parser; Debug.Assert(parser != null, "Where's the parser?"); parser.Statistics = null; - ADP.TimerCurrent(out _statistics._closeTimestamp); + _statistics._closeTimestamp = ADP.TimerCurrent(); } } } @@ -1095,7 +1095,7 @@ public override void Close() if (null != Statistics) { - ADP.TimerCurrent(out _statistics._closeTimestamp); + _statistics._closeTimestamp = ADP.TimerCurrent(); } } catch (Exception ex) @@ -1709,7 +1709,7 @@ private bool TryOpen(TaskCompletionSource retry, SqlConnec if (StatisticsEnabled || (s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterExecuteCommand) && statistics != null)) { - ADP.TimerCurrent(out _statistics._openTimestamp); + _statistics._openTimestamp = ADP.TimerCurrent(); tdsInnerConnection.Parser.Statistics = _statistics; } else @@ -2070,7 +2070,7 @@ public void ResetStatistics() if (ConnectionState.Open == State) { // update timestamp; - ADP.TimerCurrent(out _statistics._openTimestamp); + _statistics._openTimestamp = ADP.TimerCurrent(); } } } @@ -2094,7 +2094,7 @@ private void UpdateStatistics() if (ConnectionState.Open == State) { // update timestamp - ADP.TimerCurrent(out _statistics._closeTimestamp); + _statistics._closeTimestamp = ADP.TimerCurrent(); } // delegate the rest of the work to the SqlStatistics class Statistics.UpdateStatistics(); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlStatistics.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlStatistics.cs index f6abfd7354..4aeeb28322 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlStatistics.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlStatistics.cs @@ -127,7 +127,7 @@ internal bool RequestExecutionTimer() { if (_startExecutionTimestamp == 0) { - ADP.TimerCurrent(out _startExecutionTimestamp); + _startExecutionTimestamp = ADP.TimerCurrent(); return true; } return false; @@ -138,7 +138,7 @@ internal void RequestNetworkServerTimer() Debug.Assert(_startExecutionTimestamp != 0, "No network time expected outside execution period"); if (_startNetworkServerTimestamp == 0) { - ADP.TimerCurrent(out _startNetworkServerTimestamp); + _startNetworkServerTimestamp = ADP.TimerCurrent(); } _waitForReply = true; } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs index 706064e856..e17a65be95 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs @@ -119,7 +119,6 @@ internal static int GetTimeoutMilliseconds(long timeoutTime) return (int)msecRemaining; } - internal static long GetTimeout(long timeoutMilliseconds) { long result; diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/AdapterUtil.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/AdapterUtil.cs index e52a60e1e0..e995114aca 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/AdapterUtil.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/AdapterUtil.cs @@ -2271,11 +2271,6 @@ static internal bool NeedManualEnlistment() return false; } - static internal void TimerCurrent(out long ticks) - { - ticks = DateTime.UtcNow.ToFileTimeUtc(); - } - static internal long TimerCurrent() { return DateTime.UtcNow.ToFileTimeUtc(); diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBuffer.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBuffer.cs index c395c3cc2d..66071812dc 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBuffer.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBuffer.cs @@ -11,10 +11,8 @@ namespace Microsoft.Data.SqlClient { - - internal sealed class SqlBuffer + internal sealed partial class SqlBuffer { - internal enum StorageType { Empty = 0, @@ -42,55 +40,67 @@ internal enum StorageType internal struct DateTimeInfo { // This is used to store DateTime - internal Int32 daypart; - internal Int32 timepart; + internal int _daypart; + internal int _timepart; } internal struct NumericInfo { // This is used to store Decimal data - internal Int32 data1; - internal Int32 data2; - internal Int32 data3; - internal Int32 data4; - internal Byte precision; - internal Byte scale; - internal Boolean positive; + internal int _data1; + internal int _data2; + internal int _data3; + internal int _data4; + internal byte _precision; + internal byte _scale; + internal bool _positive; } internal struct TimeInfo { - internal Int64 ticks; - internal byte scale; + internal long _ticks; + internal byte _scale; } internal struct DateTime2Info { - internal Int32 date; - internal TimeInfo timeInfo; + internal int _date; + internal TimeInfo _timeInfo; } internal struct DateTimeOffsetInfo { - internal DateTime2Info dateTime2Info; - internal Int16 offset; + internal DateTime2Info _dateTime2Info; + internal short _offset; } [StructLayout(LayoutKind.Explicit)] internal struct Storage { - [FieldOffset(0)] internal Boolean _boolean; - [FieldOffset(0)] internal Byte _byte; - [FieldOffset(0)] internal DateTimeInfo _dateTimeInfo; - [FieldOffset(0)] internal Double _double; - [FieldOffset(0)] internal NumericInfo _numericInfo; - [FieldOffset(0)] internal Int16 _int16; - [FieldOffset(0)] internal Int32 _int32; - [FieldOffset(0)] internal Int64 _int64; // also used to store Money, UtcDateTime, Date , and Time - [FieldOffset(0)] internal Single _single; - [FieldOffset(0)] internal TimeInfo _timeInfo; - [FieldOffset(0)] internal DateTime2Info _dateTime2Info; - [FieldOffset(0)] internal DateTimeOffsetInfo _dateTimeOffsetInfo; + [FieldOffset(0)] + internal bool _boolean; + [FieldOffset(0)] + internal byte _byte; + [FieldOffset(0)] + internal DateTimeInfo _dateTimeInfo; + [FieldOffset(0)] + internal double _double; + [FieldOffset(0)] + internal NumericInfo _numericInfo; + [FieldOffset(0)] + internal short _int16; + [FieldOffset(0)] + internal int _int32; + [FieldOffset(0)] + internal long _int64; // also used to store Money, UtcDateTime, Date , and Time + [FieldOffset(0)] + internal float _single; + [FieldOffset(0)] + internal TimeInfo _timeInfo; + [FieldOffset(0)] + internal DateTime2Info _dateTime2Info; + [FieldOffset(0)] + internal DateTimeOffsetInfo _dateTimeOffsetInfo; } private bool _isNull; @@ -113,28 +123,13 @@ private SqlBuffer(SqlBuffer value) _object = value._object; } - internal bool IsEmpty - { - get - { - return (StorageType.Empty == _type); - } - } + internal bool IsEmpty => StorageType.Empty == _type; - internal bool IsNull - { - get - { - return _isNull; - } - } + internal bool IsNull => _isNull; - internal StorageType VariantInternalStorageType - { - get { return _type; } - } + internal StorageType VariantInternalStorageType => _type; - internal Boolean Boolean + internal bool Boolean { get { @@ -144,7 +139,7 @@ internal Boolean Boolean { return _value._boolean; } - return (Boolean)this.Value; // anything else we haven't thought of goes through boxing. + return (bool)Value; // anything else we haven't thought of goes through boxing. } set { @@ -155,7 +150,7 @@ internal Boolean Boolean } } - internal Byte Byte + internal byte Byte { get { @@ -165,7 +160,7 @@ internal Byte Byte { return _value._byte; } - return (Byte)this.Value; // anything else we haven't thought of goes through boxing. + return (byte)Value; // anything else we haven't thought of goes through boxing. } set { @@ -176,12 +171,12 @@ internal Byte Byte } } - internal Byte[] ByteArray + internal byte[] ByteArray { get { ThrowIfNull(); - return this.SqlBinary.Value; // TODO: avoid the copy that this entails, without sacrificing the immutability of SqlBinary, if we've handed it out. + return SqlBinary.Value; } } @@ -201,13 +196,13 @@ internal DateTime DateTime } if (StorageType.DateTime == _type) { - return SqlTypes.SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo.daypart, _value._dateTimeInfo.timepart); + return SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo._daypart, _value._dateTimeInfo._timepart); } - return (DateTime)this.Value; // anything else we haven't thought of goes through boxing. + return (DateTime)Value; // anything else we haven't thought of goes through boxing. } } - internal Decimal Decimal + internal decimal Decimal { get { @@ -215,11 +210,11 @@ internal Decimal Decimal if (StorageType.Decimal == _type) { - if (_value._numericInfo.data4 != 0 || _value._numericInfo.scale > 28) + if (_value._numericInfo._data4 != 0 || _value._numericInfo._scale > 28) { throw new OverflowException(SQLResource.ConversionOverflowMessage); } - return new Decimal(_value._numericInfo.data1, _value._numericInfo.data2, _value._numericInfo.data3, !_value._numericInfo.positive, _value._numericInfo.scale); + return new decimal(_value._numericInfo._data1, _value._numericInfo._data2, _value._numericInfo._data3, !_value._numericInfo._positive, _value._numericInfo._scale); } if (StorageType.Money == _type) { @@ -230,13 +225,13 @@ internal Decimal Decimal isNegative = true; l = -l; } - return new Decimal((int)(l & 0xffffffff), (int)(l >> 32), 0, isNegative, 4); + return new decimal((int)(l & 0xffffffff), (int)(l >> 32), 0, isNegative, 4); } - return (Decimal)this.Value; // anything else we haven't thought of goes through boxing. + return (decimal)Value; // anything else we haven't thought of goes through boxing. } } - internal Double Double + internal double Double { get { @@ -246,7 +241,7 @@ internal Double Double { return _value._double; } - return (Double)this.Value; // anything else we haven't thought of goes through boxing. + return (double)Value; // anything else we haven't thought of goes through boxing. } set { @@ -263,11 +258,11 @@ internal Guid Guid { // TODO: It is possible to further optimize this, by storing the data from the wire without constructing a SqlGuid first, however we're already twice as fast! ThrowIfNull(); - return this.SqlGuid.Value; + return SqlGuid.Value; } } - internal Int16 Int16 + internal short Int16 { get { @@ -277,7 +272,7 @@ internal Int16 Int16 { return _value._int16; } - return (Int16)this.Value; // anything else we haven't thought of goes through boxing. + return (short)Value; // anything else we haven't thought of goes through boxing. } set { @@ -288,7 +283,7 @@ internal Int16 Int16 } } - internal Int32 Int32 + internal int Int32 { get { @@ -298,7 +293,7 @@ internal Int32 Int32 { return _value._int32; } - return (Int32)this.Value; // anything else we haven't thought of goes through boxing. + return (int)Value; // anything else we haven't thought of goes through boxing. } set { @@ -309,7 +304,7 @@ internal Int32 Int32 } } - internal Int64 Int64 + internal long Int64 { get { @@ -319,7 +314,7 @@ internal Int64 Int64 { return _value._int64; } - return (Int64)this.Value; // anything else we haven't thought of goes through boxing. + return (long)Value; // anything else we haven't thought of goes through boxing. } set { @@ -330,7 +325,7 @@ internal Int64 Int64 } } - internal Single Single + internal float Single { get { @@ -340,7 +335,7 @@ internal Single Single { return _value._single; } - return (Single)this.Value; // anything else we haven't thought of goes through boxing. + return (float)Value; // anything else we haven't thought of goes through boxing. } set { @@ -351,7 +346,7 @@ internal Single Single } } - internal String String + internal string String { get { @@ -359,18 +354,18 @@ internal String String if (StorageType.String == _type) { - return (String)_object; + return (string)_object; } else if (StorageType.SqlCachedBuffer == _type) { return ((SqlCachedBuffer)(_object)).ToString(); } - return (String)this.Value; // anything else we haven't thought of goes through boxing. + return (string)Value; // anything else we haven't thought of goes through boxing. } } // use static list of format strings indexed by scale for perf! - private static string[] __katmaiDateTimeOffsetFormatByScale = new string[] { + private static readonly string[] s_katmaiDateTimeOffsetFormatByScale = new string[] { "yyyy-MM-dd HH:mm:ss zzz", "yyyy-MM-dd HH:mm:ss.f zzz", "yyyy-MM-dd HH:mm:ss.ff zzz", @@ -381,7 +376,7 @@ internal String String "yyyy-MM-dd HH:mm:ss.fffffff zzz", }; - private static string[] __katmaiDateTime2FormatByScale = new string[] { + private static readonly string[] s_katmaiDateTime2FormatByScale = new string[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.f", "yyyy-MM-dd HH:mm:ss.ff", @@ -392,7 +387,7 @@ internal String String "yyyy-MM-dd HH:mm:ss.fffffff", }; - private static string[] __katmaiTimeFormatByScale = new string[] { + private static readonly string[] s_katmaiTimeFormatByScale = new string[] { "HH:mm:ss", "HH:mm:ss.f", "HH:mm:ss.ff", @@ -411,25 +406,25 @@ internal string KatmaiDateTimeString if (StorageType.Date == _type) { - return this.DateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo); + return DateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo); } if (StorageType.Time == _type) { - byte scale = _value._timeInfo.scale; - return new DateTime(_value._timeInfo.ticks).ToString(__katmaiTimeFormatByScale[scale], DateTimeFormatInfo.InvariantInfo); + byte scale = _value._timeInfo._scale; + return new DateTime(_value._timeInfo._ticks).ToString(s_katmaiTimeFormatByScale[scale], DateTimeFormatInfo.InvariantInfo); } if (StorageType.DateTime2 == _type) { - byte scale = _value._dateTime2Info.timeInfo.scale; - return this.DateTime.ToString(__katmaiDateTime2FormatByScale[scale], DateTimeFormatInfo.InvariantInfo); + byte scale = _value._dateTime2Info._timeInfo._scale; + return DateTime.ToString(s_katmaiDateTime2FormatByScale[scale], DateTimeFormatInfo.InvariantInfo); } if (StorageType.DateTimeOffset == _type) { - DateTimeOffset dto = this.DateTimeOffset; - byte scale = _value._dateTimeOffsetInfo.dateTime2Info.timeInfo.scale; - return dto.ToString(__katmaiDateTimeOffsetFormatByScale[scale], DateTimeFormatInfo.InvariantInfo); + DateTimeOffset dto = DateTimeOffset; + byte scale = _value._dateTimeOffsetInfo._dateTime2Info._timeInfo._scale; + return dto.ToString(s_katmaiDateTimeOffsetFormatByScale[scale], DateTimeFormatInfo.InvariantInfo); } - return (String)this.Value; // anything else we haven't thought of goes through boxing. + return (string)Value; // anything else we haven't thought of goes through boxing. } } @@ -448,7 +443,7 @@ internal SqlString KatmaiDateTimeSqlString } return new SqlString(KatmaiDateTimeString); } - return (SqlString)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlString)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -460,10 +455,10 @@ internal TimeSpan Time if (StorageType.Time == _type) { - return new TimeSpan(_value._timeInfo.ticks); + return new TimeSpan(_value._timeInfo._ticks); } - return (TimeSpan)this.Value; // anything else we haven't thought of goes through boxing. + return (TimeSpan)Value; // anything else we haven't thought of goes through boxing. } } @@ -475,18 +470,18 @@ internal DateTimeOffset DateTimeOffset if (StorageType.DateTimeOffset == _type) { - TimeSpan offset = new TimeSpan(0, _value._dateTimeOffsetInfo.offset, 0); + TimeSpan offset = new TimeSpan(0, _value._dateTimeOffsetInfo._offset, 0); // datetime part presents time in UTC - return new DateTimeOffset(GetTicksFromDateTime2Info(_value._dateTimeOffsetInfo.dateTime2Info) + offset.Ticks, offset); + return new DateTimeOffset(GetTicksFromDateTime2Info(_value._dateTimeOffsetInfo._dateTime2Info) + offset.Ticks, offset); } - return (DateTimeOffset)this.Value; // anything else we haven't thought of goes through boxing. + return (DateTimeOffset)Value; // anything else we haven't thought of goes through boxing. } } private static long GetTicksFromDateTime2Info(DateTime2Info dateTime2Info) { - return (dateTime2Info.date * TimeSpan.TicksPerDay + dateTime2Info.timeInfo.ticks); + return (dateTime2Info._date * TimeSpan.TicksPerDay + dateTime2Info._timeInfo._ticks); } internal SqlBinary SqlBinary @@ -497,7 +492,7 @@ internal SqlBinary SqlBinary { return (SqlBinary)_object; } - return (SqlBinary)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlBinary)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -520,7 +515,7 @@ internal SqlBoolean SqlBoolean } return new SqlBoolean(_value._boolean); } - return (SqlBoolean)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlBoolean)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -536,7 +531,7 @@ internal SqlByte SqlByte } return new SqlByte(_value._byte); } - return (SqlByte)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlByte)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -552,7 +547,7 @@ internal SqlCachedBuffer SqlCachedBuffer } return (SqlCachedBuffer)_object; } - return (SqlCachedBuffer)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlCachedBuffer)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -575,7 +570,7 @@ internal SqlXml SqlXml } return (SqlXml)_object; } - return (SqlXml)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlXml)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -596,7 +591,7 @@ internal SqlDateTime SqlDateTime { return SqlDateTime.Null; } - return new SqlDateTime(_value._dateTimeInfo.daypart, _value._dateTimeInfo.timepart); + return new SqlDateTime(_value._dateTimeInfo._daypart, _value._dateTimeInfo._timepart); } return (SqlDateTime)SqlValue; // anything else we haven't thought of goes through boxing. } @@ -612,16 +607,16 @@ internal SqlDecimal SqlDecimal { return SqlDecimal.Null; } - return new SqlDecimal(_value._numericInfo.precision, - _value._numericInfo.scale, - _value._numericInfo.positive, - _value._numericInfo.data1, - _value._numericInfo.data2, - _value._numericInfo.data3, - _value._numericInfo.data4 + return new SqlDecimal(_value._numericInfo._precision, + _value._numericInfo._scale, + _value._numericInfo._positive, + _value._numericInfo._data1, + _value._numericInfo._data2, + _value._numericInfo._data3, + _value._numericInfo._data4 ); } - return (SqlDecimal)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlDecimal)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -637,7 +632,7 @@ internal SqlDouble SqlDouble } return new SqlDouble(_value._double); } - return (SqlDouble)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlDouble)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -649,7 +644,7 @@ internal SqlGuid SqlGuid { return (SqlGuid)_object; } - return (SqlGuid)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlGuid)SqlValue; // anything else we haven't thought of goes through boxing. } set { @@ -672,7 +667,7 @@ internal SqlInt16 SqlInt16 } return new SqlInt16(_value._int16); } - return (SqlInt16)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlInt16)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -688,7 +683,7 @@ internal SqlInt32 SqlInt32 } return new SqlInt32(_value._int32); } - return (SqlInt32)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlInt32)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -704,7 +699,7 @@ internal SqlInt64 SqlInt64 } return new SqlInt64(_value._int64); } - return (SqlInt64)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlInt64)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -720,7 +715,7 @@ internal SqlMoney SqlMoney } return SqlTypeWorkarounds.SqlMoneyCtor(_value._int64, 1/*ignored*/); } - return (SqlMoney)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlMoney)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -736,7 +731,7 @@ internal SqlSingle SqlSingle } return new SqlSingle(_value._single); } - return (SqlSingle)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlSingle)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -750,8 +745,7 @@ internal SqlString SqlString { return SqlString.Null; } - return new SqlString((String)_object); - + return new SqlString((string)_object); } else if (StorageType.SqlCachedBuffer == _type) { @@ -762,7 +756,7 @@ internal SqlString SqlString } return data.ToSqlString(); } - return (SqlString)this.SqlValue; // anything else we haven't thought of goes through boxing. + return (SqlString)SqlValue; // anything else we haven't thought of goes through boxing. } } @@ -796,6 +790,7 @@ internal object SqlValue return SqlSingle; case StorageType.String: return SqlString; + case StorageType.SqlCachedBuffer: { SqlCachedBuffer data = (SqlCachedBuffer)(_object); @@ -811,14 +806,13 @@ internal object SqlValue return _object; case StorageType.SqlXml: + if (_isNull) { - if (_isNull) - { - return SqlXml.Null; - } - Debug.Assert(null != _object); - return (SqlXml)_object; + return SqlXml.Null; } + Debug.Assert(null != _object); + return (SqlXml)_object; + case StorageType.Date: case StorageType.DateTime2: if (_isNull) @@ -826,12 +820,14 @@ internal object SqlValue return DBNull.Value; } return DateTime; + case StorageType.DateTimeOffset: if (_isNull) { return DBNull.Value; } return DateTimeOffset; + case StorageType.Time: if (_isNull) { @@ -843,6 +839,9 @@ internal object SqlValue } } + private static readonly object s_cachedTrueObject = true; + private static readonly object s_cachedFalseObject = false; + internal object Value { get @@ -856,7 +855,7 @@ internal object Value case StorageType.Empty: return DBNull.Value; case StorageType.Boolean: - return Boolean; + return Boolean ? s_cachedTrueObject : s_cachedFalseObject; case StorageType.Byte: return Byte; case StorageType.DateTime: @@ -914,75 +913,76 @@ internal Type GetTypeFromStorageType(bool isSqlType) { switch (_type) { - case SqlBuffer.StorageType.Empty: + case StorageType.Empty: return null; - case SqlBuffer.StorageType.Boolean: + case StorageType.Boolean: return typeof(SqlBoolean); - case SqlBuffer.StorageType.Byte: + case StorageType.Byte: return typeof(SqlByte); - case SqlBuffer.StorageType.DateTime: + case StorageType.DateTime: return typeof(SqlDateTime); - case SqlBuffer.StorageType.Decimal: + case StorageType.Decimal: return typeof(SqlDecimal); - case SqlBuffer.StorageType.Double: + case StorageType.Double: return typeof(SqlDouble); - case SqlBuffer.StorageType.Int16: + case StorageType.Int16: return typeof(SqlInt16); - case SqlBuffer.StorageType.Int32: + case StorageType.Int32: return typeof(SqlInt32); - case SqlBuffer.StorageType.Int64: + case StorageType.Int64: return typeof(SqlInt64); - case SqlBuffer.StorageType.Money: + case StorageType.Money: return typeof(SqlMoney); - case SqlBuffer.StorageType.Single: + case StorageType.Single: return typeof(SqlSingle); - case SqlBuffer.StorageType.String: + case StorageType.String: return typeof(SqlString); - case SqlBuffer.StorageType.SqlCachedBuffer: + case StorageType.SqlCachedBuffer: return typeof(SqlString); - case SqlBuffer.StorageType.SqlBinary: - return typeof(object); - case SqlBuffer.StorageType.SqlGuid: + case StorageType.SqlBinary: return typeof(object); - case SqlBuffer.StorageType.SqlXml: + case StorageType.SqlGuid: + return typeof(SqlGuid); + case StorageType.SqlXml: return typeof(SqlXml); + // Date DateTime2 and DateTimeOffset have no direct Sql type to contain them } } else { //Is CLR Type switch (_type) { - case SqlBuffer.StorageType.Empty: + case StorageType.Empty: return null; - case SqlBuffer.StorageType.Boolean: - return typeof(Boolean); - case SqlBuffer.StorageType.Byte: - return typeof(Byte); - case SqlBuffer.StorageType.DateTime: + case StorageType.Boolean: + return typeof(bool); + case StorageType.Byte: + return typeof(byte); + case StorageType.DateTime: return typeof(DateTime); - case SqlBuffer.StorageType.Decimal: - return typeof(Decimal); - case SqlBuffer.StorageType.Double: - return typeof(Double); - case SqlBuffer.StorageType.Int16: - return typeof(Int16); - case SqlBuffer.StorageType.Int32: - return typeof(Int32); - case SqlBuffer.StorageType.Int64: - return typeof(Int64); - case SqlBuffer.StorageType.Money: - return typeof(Decimal); - case SqlBuffer.StorageType.Single: - return typeof(Single); - case SqlBuffer.StorageType.String: - return typeof(String); - case SqlBuffer.StorageType.SqlBinary: - return typeof(Byte[]); - case SqlBuffer.StorageType.SqlCachedBuffer: + case StorageType.Decimal: + return typeof(decimal); + case StorageType.Double: + return typeof(double); + case StorageType.Int16: + return typeof(short); + case StorageType.Int32: + return typeof(int); + case StorageType.Int64: + return typeof(long); + case StorageType.Money: + return typeof(decimal); + case StorageType.Single: + return typeof(float); + case StorageType.String: return typeof(string); - case SqlBuffer.StorageType.SqlGuid: + case StorageType.SqlBinary: + return typeof(byte[]); + case StorageType.SqlCachedBuffer: + return typeof(string); + case StorageType.SqlGuid: return typeof(Guid); - case SqlBuffer.StorageType.SqlXml: + case StorageType.SqlXml: return typeof(string); } } @@ -1031,8 +1031,8 @@ internal void Clear() internal void SetToDateTime(int daypart, int timepart) { Debug.Assert(IsEmpty, "setting value a second time?"); - _value._dateTimeInfo.daypart = daypart; - _value._dateTimeInfo.timepart = timepart; + _value._dateTimeInfo._daypart = daypart; + _value._dateTimeInfo._timepart = timepart; _type = StorageType.DateTime; _isNull = false; } @@ -1040,13 +1040,13 @@ internal void SetToDateTime(int daypart, int timepart) internal void SetToDecimal(byte precision, byte scale, bool positive, int[] bits) { Debug.Assert(IsEmpty, "setting value a second time?"); - _value._numericInfo.precision = precision; - _value._numericInfo.scale = scale; - _value._numericInfo.positive = positive; - _value._numericInfo.data1 = bits[0]; - _value._numericInfo.data2 = bits[1]; - _value._numericInfo.data3 = bits[2]; - _value._numericInfo.data4 = bits[3]; + _value._numericInfo._precision = precision; + _value._numericInfo._scale = scale; + _value._numericInfo._positive = positive; + _value._numericInfo._data1 = bits[0]; + _value._numericInfo._data2 = bits[1]; + _value._numericInfo._data3 = bits[2]; + _value._numericInfo._data4 = bits[3]; _type = StorageType.Decimal; _isNull = false; } @@ -1107,8 +1107,8 @@ internal void SetToTime(TimeSpan timeSpan, byte scale) Debug.Assert(IsEmpty, "setting value a second time?"); _type = StorageType.Time; - _value._timeInfo.ticks = timeSpan.Ticks; - _value._timeInfo.scale = scale; + _value._timeInfo._ticks = timeSpan.Ticks; + _value._timeInfo._scale = scale; _isNull = false; } @@ -1117,8 +1117,8 @@ internal void SetToDateTime2(byte[] bytes, int length, byte scale, byte denormal Debug.Assert(IsEmpty, "setting value a second time?"); _type = StorageType.DateTime2; - FillInTimeInfo(ref _value._dateTime2Info.timeInfo, bytes, length - 3, scale, denormalizedScale); // remaining 3 bytes is for date - _value._dateTime2Info.date = GetDateFromByteArray(bytes, length - 3); // 3 bytes for date + FillInTimeInfo(ref _value._dateTime2Info._timeInfo, bytes, length - 3, scale, denormalizedScale); // remaining 3 bytes is for date + _value._dateTime2Info._date = GetDateFromByteArray(bytes, length - 3); // 3 bytes for date _isNull = false; } @@ -1127,9 +1127,9 @@ internal void SetToDateTime2(DateTime dateTime, byte scale) Debug.Assert(IsEmpty, "setting value a second time?"); _type = StorageType.DateTime2; - _value._dateTime2Info.timeInfo.ticks = dateTime.TimeOfDay.Ticks; - _value._dateTime2Info.timeInfo.scale = scale; - _value._dateTime2Info.date = dateTime.Subtract(DateTime.MinValue).Days; + _value._dateTime2Info._timeInfo._ticks = dateTime.TimeOfDay.Ticks; + _value._dateTime2Info._timeInfo._scale = scale; + _value._dateTime2Info._date = dateTime.Subtract(DateTime.MinValue).Days; _isNull = false; } @@ -1138,9 +1138,9 @@ internal void SetToDateTimeOffset(byte[] bytes, int length, byte scale, byte den Debug.Assert(IsEmpty, "setting value a second time?"); _type = StorageType.DateTimeOffset; - FillInTimeInfo(ref _value._dateTimeOffsetInfo.dateTime2Info.timeInfo, bytes, length - 5, scale, denormalizedScale); // remaining 5 bytes are for date and offset - _value._dateTimeOffsetInfo.dateTime2Info.date = GetDateFromByteArray(bytes, length - 5); // 3 bytes for date - _value._dateTimeOffsetInfo.offset = (Int16)(bytes[length - 2] + (bytes[length - 1] << 8)); // 2 bytes for offset (Int16) + FillInTimeInfo(ref _value._dateTimeOffsetInfo._dateTime2Info._timeInfo, bytes, length - 5, scale, denormalizedScale); // remaining 5 bytes are for date and offset + _value._dateTimeOffsetInfo._dateTime2Info._date = GetDateFromByteArray(bytes, length - 5); // 3 bytes for date + _value._dateTimeOffsetInfo._offset = (short)(bytes[length - 2] + (bytes[length - 1] << 8)); // 2 bytes for offset (Int16) _isNull = false; } @@ -1150,10 +1150,10 @@ internal void SetToDateTimeOffset(DateTimeOffset dateTimeOffset, byte scale) _type = StorageType.DateTimeOffset; DateTime utcDateTime = dateTimeOffset.UtcDateTime; // timeInfo stores the utc datetime of a datatimeoffset - _value._dateTimeOffsetInfo.dateTime2Info.timeInfo.ticks = utcDateTime.TimeOfDay.Ticks; - _value._dateTimeOffsetInfo.dateTime2Info.timeInfo.scale = scale; - _value._dateTimeOffsetInfo.dateTime2Info.date = utcDateTime.Subtract(DateTime.MinValue).Days; - _value._dateTimeOffsetInfo.offset = (Int16)dateTimeOffset.Offset.TotalMinutes; + _value._dateTimeOffsetInfo._dateTime2Info._timeInfo._ticks = utcDateTime.TimeOfDay.Ticks; + _value._dateTimeOffsetInfo._dateTime2Info._timeInfo._scale = scale; + _value._dateTimeOffsetInfo._dateTime2Info._date = utcDateTime.Subtract(DateTime.MinValue).Days; + _value._dateTimeOffsetInfo._offset = (short)dateTimeOffset.Offset.TotalMinutes; _isNull = false; } @@ -1163,24 +1163,24 @@ private static void FillInTimeInfo(ref TimeInfo timeInfo, byte[] timeBytes, int Debug.Assert(0 <= scale && scale <= 7, "invalid scale: " + scale); Debug.Assert(0 <= denormalizedScale && denormalizedScale <= 7, "invalid denormalized scale: " + denormalizedScale); - Int64 tickUnits = (Int64)timeBytes[0] + ((Int64)timeBytes[1] << 8) + ((Int64)timeBytes[2] << 16); + long tickUnits = timeBytes[0] + ((long)timeBytes[1] << 8) + ((long)timeBytes[2] << 16); if (length > 3) { - tickUnits += ((Int64)timeBytes[3] << 24); + tickUnits += ((long)timeBytes[3] << 24); } if (length > 4) { - tickUnits += ((Int64)timeBytes[4] << 32); + tickUnits += ((long)timeBytes[4] << 32); } - timeInfo.ticks = tickUnits * TdsEnums.TICKS_FROM_SCALE[scale]; + timeInfo._ticks = tickUnits * TdsEnums.TICKS_FROM_SCALE[scale]; // Once the deserialization has been completed using the value scale, we need to set the actual denormalized scale, // coming from the data type, on the original result, so that it has the proper scale setting. // This only applies for values that got serialized/deserialized for encryption. Otherwise, both scales should be equal. - timeInfo.scale = denormalizedScale; + timeInfo._scale = denormalizedScale; } - private static Int32 GetDateFromByteArray(byte[] buf, int offset) + private static int GetDateFromByteArray(byte[] buf, int offset) { return buf[offset] + (buf[offset + 1] << 8) + (buf[offset + 2] << 16); } @@ -1193,4 +1193,4 @@ private void ThrowIfNull() } } } -}// namespace +} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs index c48a540746..d4455461c6 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -426,7 +426,7 @@ public bool StatisticsEnabled if (null == _statistics) { _statistics = new SqlStatistics(); - ADP.TimerCurrent(out _statistics._openTimestamp); + _statistics._openTimestamp = ADP.TimerCurrent(); } // set statistics on the parser // update timestamp; @@ -446,7 +446,7 @@ public bool StatisticsEnabled TdsParser parser = Parser; Debug.Assert(parser != null, "Where's the parser?"); parser.Statistics = null; - ADP.TimerCurrent(out _statistics._closeTimestamp); + _statistics._closeTimestamp = ADP.TimerCurrent(); } } } @@ -1450,7 +1450,7 @@ override public void Close() if (null != Statistics) { - ADP.TimerCurrent(out _statistics._closeTimestamp); + _statistics._closeTimestamp = ADP.TimerCurrent(); } } #if DEBUG @@ -2075,7 +2075,7 @@ private bool TryOpenInner(TaskCompletionSource retry) if (StatisticsEnabled) { - ADP.TimerCurrent(out _statistics._openTimestamp); + _statistics._openTimestamp = ADP.TimerCurrent(); tdsInnerConnection.Parser.Statistics = _statistics; } else @@ -2770,7 +2770,7 @@ public void ResetStatistics() if (ConnectionState.Open == State) { // update timestamp; - ADP.TimerCurrent(out _statistics._openTimestamp); + _statistics._openTimestamp = ADP.TimerCurrent(); } } } @@ -2799,7 +2799,7 @@ private void UpdateStatistics() if (ConnectionState.Open == State) { // update timestamp - ADP.TimerCurrent(out _statistics._closeTimestamp); + _statistics._closeTimestamp = ADP.TimerCurrent(); } // delegate the rest of the work to the SqlStatistics class Statistics.UpdateStatistics(); diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStatistics.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStatistics.cs index 28dfee86f0..7c72711cd2 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStatistics.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStatistics.cs @@ -95,7 +95,7 @@ internal void ContinueOnNewConnection() internal IDictionary GetHashtable() { - Hashtable ht = new Hashtable(); + Hashtable ht = new Hashtable(18); ht.Add("BuffersReceived", _buffersReceived); ht.Add("BuffersSent", _buffersSent); @@ -124,7 +124,7 @@ internal bool RequestExecutionTimer() { if (_startExecutionTimestamp == 0) { - ADP.TimerCurrent(out _startExecutionTimestamp); + _startExecutionTimestamp = ADP.TimerCurrent(); return true; } return false; @@ -135,7 +135,7 @@ internal void RequestNetworkServerTimer() Debug.Assert(_startExecutionTimestamp != 0, "No network time expected outside execution period"); if (_startNetworkServerTimestamp == 0) { - ADP.TimerCurrent(out _startNetworkServerTimestamp); + _startNetworkServerTimestamp = ADP.TimerCurrent(); } _waitForReply = true; } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index bb4ab023ef..bd5f08f8ea 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -8762,7 +8762,7 @@ internal void TdsLogin(SqlLogin rec, else { userName = rec.userName; - encryptedPassword = TdsParserStaticMethods.EncryptPassword(rec.password); + encryptedPassword = TdsParserStaticMethods.ObfuscatePassword(rec.password); encryptedPasswordLengthInBytes = encryptedPassword.Length; // password in clear text is already encrypted and its length is in byte } @@ -8772,7 +8772,7 @@ internal void TdsLogin(SqlLogin rec, } else { - encryptedChangePassword = TdsParserStaticMethods.EncryptPassword(rec.newPassword); + encryptedChangePassword = TdsParserStaticMethods.ObfuscatePassword(rec.newPassword); encryptedChangePasswordLengthInBytes = encryptedChangePassword.Length; } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs index 0e3f0c385a..025ea7d020 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStaticMethods.cs @@ -3,14 +3,13 @@ // See the LICENSE file in the project root for more information. using System; +using Microsoft.Data.Common; using System.Globalization; using System.Runtime.Versioning; using System.Security.Permissions; namespace Microsoft.Data.SqlClient { - using Microsoft.Data.Common; - internal sealed class TdsParserStaticMethods { @@ -92,9 +91,9 @@ static internal void AliasRegistryLookup(ref string host, ref string protocol) // Encrypt password to be sent to SQL Server // Note: The same logic is used in SNIPacketSetData (SniManagedWrapper) to encrypt passwords stored in SecureString // If this logic changed, SNIPacketSetData needs to be changed as well - static internal Byte[] EncryptPassword(string password) + internal static byte[] ObfuscatePassword(string password) { - Byte[] bEnc = new Byte[password.Length << 1]; + byte[] bEnc = new byte[password.Length << 1]; int s; byte bLo; byte bHi; @@ -104,8 +103,8 @@ static internal Byte[] EncryptPassword(string password) s = (int)password[i]; bLo = (byte)(s & 0xff); bHi = (byte)((s >> 8) & 0xff); - bEnc[i << 1] = (Byte)((((bLo & 0x0f) << 4) | (bLo >> 4)) ^ 0xa5); - bEnc[(i << 1) + 1] = (Byte)((((bHi & 0x0f) << 4) | (bHi >> 4)) ^ 0xa5); + bEnc[i << 1] = (byte)((((bLo & 0x0f) << 4) | (bLo >> 4)) ^ 0xa5); + bEnc[(i << 1) + 1] = (byte)((((bHi & 0x0f) << 4) | (bHi >> 4)) ^ 0xa5); } return bEnc; } @@ -114,7 +113,7 @@ static internal Byte[] EncryptPassword(string password) [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] static internal int GetCurrentProcessIdForTdsLoginOnly() { - return SafeNativeMethods.GetCurrentProcessId(); + return Common.SafeNativeMethods.GetCurrentProcessId(); } @@ -172,8 +171,9 @@ static internal byte[] GetNetworkPhysicalAddressForTdsLoginOnly() return nicAddress; } + // translates remaining time in stateObj (from user specified timeout) to timeout value for SNI - static internal Int32 GetTimeoutMilliseconds(long timeoutTime) + internal static int GetTimeoutMilliseconds(long timeoutTime) { // User provided timeout t | timeout value for SNI | meaning // ------------------------+-----------------------+------------------------------ @@ -181,7 +181,7 @@ static internal Int32 GetTimeoutMilliseconds(long timeoutTime) // t>0 && tint.MaxValue | int.MaxValue | must not exceed int.MaxValue - if (Int64.MaxValue == timeoutTime) + if (long.MaxValue == timeoutTime) { return -1; // infinite timeout } @@ -192,24 +192,19 @@ static internal Int32 GetTimeoutMilliseconds(long timeoutTime) { return 0; } - if (msecRemaining > (long)Int32.MaxValue) + if (msecRemaining > (long)int.MaxValue) { - return Int32.MaxValue; + return int.MaxValue; } - return (Int32)msecRemaining; - } - - static internal long GetTimeoutSeconds(int timeout) - { - return GetTimeout((long)timeout * 1000L); + return (int)msecRemaining; } - static internal long GetTimeout(long timeoutMilliseconds) + internal static long GetTimeout(long timeoutMilliseconds) { long result; if (timeoutMilliseconds <= 0) { - result = Int64.MaxValue; // no timeout... + result = long.MaxValue; // no timeout... } else { @@ -220,24 +215,24 @@ static internal long GetTimeout(long timeoutMilliseconds) catch (OverflowException) { // In case of overflow, set to 'infinite' timeout - result = Int64.MaxValue; + result = long.MaxValue; } } return result; } - static internal bool TimeoutHasExpired(long timeoutTime) + internal static bool TimeoutHasExpired(long timeoutTime) { bool result = false; - if (0 != timeoutTime && Int64.MaxValue != timeoutTime) + if (0 != timeoutTime && long.MaxValue != timeoutTime) { result = ADP.TimerHasExpired(timeoutTime); } return result; } - static internal int NullAwareStringLength(string str) + internal static int NullAwareStringLength(string str) { if (str == null) { @@ -249,7 +244,7 @@ static internal int NullAwareStringLength(string str) } } - static internal int GetRemainingTimeout(int timeout, long start) + internal static int GetRemainingTimeout(int timeout, long start) { if (timeout <= 0) { @@ -265,6 +260,5 @@ static internal int GetRemainingTimeout(int timeout, long start) return checked((int)remaining); } } - } }