DataSourceView.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. owner.DataSourceChanged += new EventHandler (OnDataSourceChanged);
  48. }
  49. void OnDataSourceChanged (object sender, EventArgs e) {
  50. OnDataSourceViewChanged (EventArgs.Empty);
  51. }
  52. public virtual void Delete (IDictionary keys, IDictionary values,
  53. DataSourceViewOperationCallback callBack)
  54. {
  55. if (callBack == null)
  56. throw new ArgumentNullException ("callBack");
  57. int rowAffected;
  58. try {
  59. rowAffected = ExecuteDelete (keys, values);
  60. }
  61. catch (Exception e) {
  62. if (!callBack (0, e))
  63. throw;
  64. return;
  65. }
  66. callBack (rowAffected, null);
  67. }
  68. protected virtual int ExecuteDelete(IDictionary keys, IDictionary values)
  69. {
  70. throw new NotSupportedException ();
  71. }
  72. protected virtual int ExecuteInsert (IDictionary keys)
  73. {
  74. throw new NotSupportedException();
  75. }
  76. protected internal abstract IEnumerable ExecuteSelect (
  77. DataSourceSelectArguments arguments);
  78. protected virtual int ExecuteUpdate (IDictionary keys, IDictionary values,
  79. IDictionary oldValues )
  80. {
  81. throw new NotSupportedException ();
  82. }
  83. public virtual void Insert (IDictionary values,
  84. DataSourceViewOperationCallback callBack)
  85. {
  86. if (callBack == null)
  87. throw new ArgumentNullException("callBack");
  88. int rowAffected;
  89. try {
  90. rowAffected = ExecuteInsert (values);
  91. } catch (Exception e) {
  92. if (!callBack (0, e))
  93. throw;
  94. return;
  95. }
  96. callBack (rowAffected, null);
  97. }
  98. protected virtual void OnDataSourceViewChanged (EventArgs eventArgs)
  99. {
  100. if (eventsList != null) {
  101. EventHandler evtHandler = eventsList [EventDataSourceViewChanged] as EventHandler;
  102. if (evtHandler != null)
  103. evtHandler(this, eventArgs);
  104. }
  105. }
  106. protected internal virtual void RaiseUnsupportedCapabilityError (
  107. DataSourceCapabilities capability)
  108. {
  109. if ((capability & DataSourceCapabilities.Sort) != 0)
  110. if (!CanSort)
  111. throw new NotSupportedException ("Sort Capabilites");
  112. if ((capability & DataSourceCapabilities.Page) != 0)
  113. if (!CanPage)
  114. throw new NotSupportedException("Page Capabilites");
  115. if ((capability & DataSourceCapabilities.RetrieveTotalRowCount) != 0)
  116. if (!CanRetrieveTotalRowCount)
  117. throw new NotSupportedException("RetrieveTotalRowCount Capabilites");
  118. return;
  119. }
  120. public virtual void Select (DataSourceSelectArguments selectArgs,
  121. DataSourceViewSelectCallback callBack)
  122. {
  123. if (callBack == null)
  124. throw new ArgumentNullException("callBack");
  125. selectArgs.RaiseUnsupportedCapabilitiesError (this);
  126. IEnumerable selectList = ExecuteSelect (selectArgs);
  127. callBack (selectList);
  128. }
  129. public virtual void Update(IDictionary keys, IDictionary values,
  130. IDictionary oldValues, DataSourceViewOperationCallback callBack)
  131. {
  132. if (callBack == null)
  133. throw new ArgumentNullException ("callBack");
  134. int rowAffected;
  135. try {
  136. rowAffected = ExecuteUpdate (keys, values, oldValues);
  137. } catch (Exception e) {
  138. if (!callBack (0, e))
  139. throw;
  140. return;
  141. }
  142. callBack (rowAffected, null);
  143. }
  144. public virtual bool CanDelete { get { return false; } }
  145. public virtual bool CanInsert { get { return false; } }
  146. public virtual bool CanPage { get { return false; } }
  147. public virtual bool CanRetrieveTotalRowCount { get { return false; } }
  148. public virtual bool CanSort { get { return false; } }
  149. public virtual bool CanUpdate { get { return false; } }
  150. EventHandlerList eventsList;
  151. protected EventHandlerList Events {
  152. get {
  153. if (eventsList == null)
  154. eventsList = new EventHandlerList();
  155. return eventsList;
  156. }
  157. }
  158. internal bool HasEvents ()
  159. {
  160. return eventsList != null;
  161. }
  162. public string Name {
  163. get { return viewName; }
  164. }
  165. static readonly object EventDataSourceViewChanged = new object ();
  166. public event EventHandler DataSourceViewChanged
  167. {
  168. add { Events.AddHandler (EventDataSourceViewChanged, value); }
  169. remove { Events.RemoveHandler (EventDataSourceViewChanged, value); }
  170. }
  171. }
  172. }
  173. #endif