DeleteResult.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. namespace MongoDB.Driver
  20. {
  21. /// <summary>
  22. /// The result of a delete operation.
  23. /// </summary>
  24. public abstract class DeleteResult
  25. {
  26. // static
  27. internal static DeleteResult FromCore(BulkWriteResult result)
  28. {
  29. if (result.IsAcknowledged)
  30. {
  31. return new Acknowledged(result.DeletedCount);
  32. }
  33. return Unacknowledged.Instance;
  34. }
  35. // properties
  36. /// <summary>
  37. /// Gets the deleted count. If IsAcknowledged is false, this will throw an exception.
  38. /// </summary>
  39. public abstract long DeletedCount { get; }
  40. /// <summary>
  41. /// Gets a value indicating whether the result is acknowledged.
  42. /// </summary>
  43. public abstract bool IsAcknowledged { get; }
  44. // constructors
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="DeleteResult"/> class.
  47. /// </summary>
  48. protected DeleteResult()
  49. {
  50. }
  51. // nested classes
  52. /// <summary>
  53. /// The result of an acknowledged delete operation.
  54. /// </summary>
  55. public class Acknowledged : DeleteResult
  56. {
  57. private readonly long _deletedCount;
  58. /// <summary>
  59. /// Initializes a new instance of the
  60. /// </summary>
  61. /// <param name="deletedCount">The deleted count.</param>
  62. public Acknowledged(long deletedCount)
  63. {
  64. _deletedCount = deletedCount;
  65. }
  66. /// <inheritdoc/>
  67. public override long DeletedCount
  68. {
  69. get { return _deletedCount; }
  70. }
  71. /// <inheritdoc/>
  72. public override bool IsAcknowledged
  73. {
  74. get { return true; }
  75. }
  76. }
  77. /// <summary>
  78. /// The result of an unacknowledged delete operation.
  79. /// </summary>
  80. public class Unacknowledged : DeleteResult
  81. {
  82. private static Unacknowledged __instance = new Unacknowledged();
  83. /// <summary>
  84. /// Gets the instance.
  85. /// </summary>
  86. public static Unacknowledged Instance
  87. {
  88. get { return __instance; }
  89. }
  90. private Unacknowledged()
  91. {
  92. }
  93. /// <inheritdoc/>
  94. public override long DeletedCount
  95. {
  96. get { throw new NotSupportedException("Only acknowledged writes support the DeletedCount property."); }
  97. }
  98. /// <inheritdoc/>
  99. public override bool IsAcknowledged
  100. {
  101. get { return false; }
  102. }
  103. }
  104. }
  105. }