DataSourceView.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // System.Web.UI.DataSourceView
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. using System.ComponentModel;
  36. namespace System.Web.UI {
  37. public abstract class DataSourceView
  38. {
  39. //IDataSource dataSourceOwner;
  40. string viewName = String.Empty;
  41. protected DataSourceView (IDataSource owner, string viewName)
  42. {
  43. if (owner == null)
  44. throw new ArgumentNullException ("owner");
  45. //this.dataSourceOwner = owner;
  46. this.viewName = viewName;
  47. }
  48. public virtual void Delete (IDictionary keys, IDictionary values,
  49. DataSourceViewOperationCallback callBack)
  50. {
  51. if (callBack == null)
  52. throw new ArgumentNullException ("callBack");
  53. int rowAffected;
  54. try {
  55. rowAffected = ExecuteDelete (keys, values);
  56. }
  57. catch (Exception e) {
  58. if (!callBack (0, e))
  59. throw;
  60. return;
  61. }
  62. callBack (rowAffected, null);
  63. }
  64. protected virtual int ExecuteDelete(IDictionary keys, IDictionary values)
  65. {
  66. throw new NotSupportedException ();
  67. }
  68. protected virtual int ExecuteInsert (IDictionary keys)
  69. {
  70. throw new NotSupportedException();
  71. }
  72. protected internal abstract IEnumerable ExecuteSelect (
  73. DataSourceSelectArguments arguments);
  74. protected virtual int ExecuteUpdate (IDictionary keys, IDictionary values,
  75. IDictionary oldValues )
  76. {
  77. throw new NotSupportedException ();
  78. }
  79. public virtual void Insert (IDictionary values,
  80. DataSourceViewOperationCallback callBack)
  81. {
  82. if (callBack == null)
  83. throw new ArgumentNullException("callBack");
  84. int rowAffected;
  85. try {
  86. rowAffected = ExecuteInsert (values);
  87. } catch (Exception e) {
  88. if (!callBack (0, e))
  89. throw;
  90. return;
  91. }
  92. callBack (rowAffected, null);
  93. }
  94. protected virtual void OnDataSourceViewChanged (EventArgs eventArgs)
  95. {
  96. if (eventsList != null) {
  97. EventHandler evtHandler = eventsList [EventDataSourceViewChanged] as EventHandler;
  98. if (evtHandler != null)
  99. evtHandler(this, eventArgs);
  100. }
  101. }
  102. protected internal virtual void RaiseUnsupportedCapabilityError (
  103. DataSourceCapabilities capability)
  104. {
  105. if ((capability & DataSourceCapabilities.Sort) != 0)
  106. if (!CanSort)
  107. throw new NotSupportedException ("Sort Capabilites");
  108. if ((capability & DataSourceCapabilities.Page) != 0)
  109. if (!CanPage)
  110. throw new NotSupportedException("Page Capabilites");
  111. if ((capability & DataSourceCapabilities.RetrieveTotalRowCount) != 0)
  112. if (!CanRetrieveTotalRowCount)
  113. throw new NotSupportedException("RetrieveTotalRowCount Capabilites");
  114. return;
  115. }
  116. public virtual void Select (DataSourceSelectArguments selectArgs,
  117. DataSourceViewSelectCallback callBack)
  118. {
  119. if (callBack == null)
  120. throw new ArgumentNullException("callBack");
  121. selectArgs.RaiseUnsupportedCapabilitiesError (this);
  122. IEnumerable selectList = ExecuteSelect (selectArgs);
  123. callBack (selectList);
  124. }
  125. public virtual void Update(IDictionary keys, IDictionary values,
  126. IDictionary oldValues, DataSourceViewOperationCallback callBack)
  127. {
  128. if (callBack == null)
  129. throw new ArgumentNullException ("callBack");
  130. int rowAffected;
  131. try {
  132. rowAffected = ExecuteUpdate (keys, values, oldValues);
  133. } catch (Exception e) {
  134. if (!callBack (0, e))
  135. throw;
  136. return;
  137. }
  138. callBack (rowAffected, null);
  139. }
  140. public virtual bool CanDelete { get { return false; } }
  141. public virtual bool CanInsert { get { return false; } }
  142. public virtual bool CanPage { get { return false; } }
  143. public virtual bool CanRetrieveTotalRowCount { get { return false; } }
  144. public virtual bool CanSort { get { return false; } }
  145. public virtual bool CanUpdate { get { return false; } }
  146. EventHandlerList eventsList;
  147. protected EventHandlerList Events {
  148. get {
  149. if (eventsList == null)
  150. eventsList = new EventHandlerList();
  151. return eventsList;
  152. }
  153. }
  154. internal bool HasEvents ()
  155. {
  156. return eventsList != null;
  157. }
  158. public string Name {
  159. get { return viewName; }
  160. }
  161. static readonly object EventDataSourceViewChanged = new object ();
  162. public event EventHandler DataSourceViewChanged
  163. {
  164. add { Events.AddHandler (EventDataSourceViewChanged, value); }
  165. remove { Events.RemoveHandler (EventDataSourceViewChanged, value); }
  166. }
  167. }
  168. }
  169. #endif