SqlReferenceCollection.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SqlReferenceCollection.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. //------------------------------------------------------------------------------
  8. using System;
  9. using System.Data;
  10. using System.Data.Common;
  11. using System.Diagnostics;
  12. using System.Data.ProviderBase;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace System.Data.SqlClient {
  16. sealed internal class SqlReferenceCollection : DbReferenceCollection {
  17. internal const int DataReaderTag = 1;
  18. internal const int CommandTag = 2;
  19. internal const int BulkCopyTag = 3;
  20. override public void Add(object value, int tag) {
  21. Debug.Assert(DataReaderTag == tag || CommandTag == tag || BulkCopyTag == tag, "unexpected tag?");
  22. Debug.Assert(DataReaderTag != tag || value is SqlDataReader, "tag doesn't match object type: SqlDataReader");
  23. Debug.Assert(CommandTag != tag || value is SqlCommand, "tag doesn't match object type: SqlCommand");
  24. Debug.Assert(BulkCopyTag != tag || value is SqlBulkCopy, "tag doesn't match object type: SqlBulkCopy");
  25. base.AddItem(value, tag);
  26. }
  27. internal void Deactivate() {
  28. base.Notify(0);
  29. }
  30. internal SqlDataReader FindLiveReader(SqlCommand command) {
  31. if (command == null) {
  32. // if null == command, will find first live datareader
  33. return FindItem<SqlDataReader>(DataReaderTag, (dataReader) => (!dataReader.IsClosed));
  34. }
  35. else {
  36. // else will find live datareader assocated with the command
  37. return FindItem<SqlDataReader>(DataReaderTag, (dataReader) => ((!dataReader.IsClosed) && (command == dataReader.Command)));
  38. }
  39. }
  40. // Finds a SqlCommand associated with the given StateObject
  41. internal SqlCommand FindLiveCommand(TdsParserStateObject stateObj) {
  42. return FindItem<SqlCommand>(CommandTag, (command) => (command.StateObject == stateObj));
  43. }
  44. override protected void NotifyItem(int message, int tag, object value) {
  45. Debug.Assert(0 == message, "unexpected message?");
  46. Debug.Assert(DataReaderTag == tag || CommandTag == tag || BulkCopyTag == tag, "unexpected tag?");
  47. if (tag == DataReaderTag) {
  48. Debug.Assert(value is SqlDataReader, "Incorrect object type");
  49. var rdr = (SqlDataReader)value;
  50. if (!rdr.IsClosed) {
  51. rdr.CloseReaderFromConnection();
  52. }
  53. }
  54. else if (tag == CommandTag) {
  55. Debug.Assert(value is SqlCommand, "Incorrect object type");
  56. ((SqlCommand)value).OnConnectionClosed();
  57. }
  58. else if (tag == BulkCopyTag) {
  59. Debug.Assert(value is SqlBulkCopy, "Incorrect object type");
  60. ((SqlBulkCopy)value).OnConnectionClosed();
  61. }
  62. }
  63. override public void Remove(object value) {
  64. Debug.Assert(value is SqlDataReader || value is SqlCommand || value is SqlBulkCopy, "SqlReferenceCollection.Remove expected a SqlDataReader or SqlCommand or SqlBulkCopy");
  65. base.RemoveItem(value);
  66. }
  67. }
  68. }