DataSourceView.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. [MonoTODO ("Extra method to keep things compiling")]
  42. protected DataSourceView()
  43. {
  44. }
  45. protected DataSourceView(IDataSource owner, string viewName)
  46. {
  47. this.dataSourceOwner = owner;
  48. this.viewName = viewName;
  49. }
  50. public virtual void Delete (IDictionary keys, IDictionary values,
  51. DataSourceViewOperationCallback callBack)
  52. {
  53. if (callBack == null)
  54. throw new ArgumentNullException ("callBack");
  55. int rowAffected = 0;
  56. Exception passOn = null;
  57. try {
  58. rowAffected = ExecuteDelete (keys, values);
  59. } catch (Exception e) {
  60. passOn = e;
  61. }
  62. callBack (rowAffected, passOn);
  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 = 0;
  85. Exception passOn = null;
  86. try {
  87. rowAffected = ExecuteInsert (values);
  88. } catch (Exception e) {
  89. passOn = e;
  90. }
  91. callBack (rowAffected, passOn);
  92. }
  93. protected virtual void OnDataSourceViewChanged (EventArgs eventArgs)
  94. {
  95. EventHandler evtHandler = eventsList [EventDataSourceViewChanged] as EventHandler;
  96. if (evtHandler != null)
  97. evtHandler(this, eventArgs);
  98. }
  99. protected internal virtual void RaiseUnsupportedCapabilityError (
  100. DataSourceCapabilities capability)
  101. {
  102. if (capability == DataSourceCapabilities.Sort)
  103. if (!CanSort)
  104. throw new NotSupportedException ("Sort Capabilites");
  105. if (capability == DataSourceCapabilities.Page)
  106. if (!CanPage)
  107. throw new NotSupportedException("Page Capabilites");
  108. if (capability == DataSourceCapabilities.RetrieveTotalRowCount)
  109. if (!CanRetrieveTotalRowCount)
  110. throw new NotSupportedException("RetrieveTotalRowCount Capabilites");
  111. if (capability == (DataSourceCapabilities.Sort &
  112. DataSourceCapabilities.Page))
  113. if (!(CanSort && CanPage))
  114. throw new NotSupportedException ("Sort Capabilites");
  115. if (capability == (DataSourceCapabilities.Sort &
  116. DataSourceCapabilities.RetrieveTotalRowCount))
  117. if (!(CanSort && CanRetrieveTotalRowCount))
  118. throw new NotSupportedException("Page Capabilites");
  119. if (capability == (DataSourceCapabilities.Page &
  120. DataSourceCapabilities.RetrieveTotalRowCount))
  121. if (!(CanPage && CanRetrieveTotalRowCount))
  122. throw new NotSupportedException("RetrieveTotalRowCount Capabilites");
  123. if (capability == (DataSourceCapabilities.Sort &
  124. DataSourceCapabilities.Page &
  125. DataSourceCapabilities.RetrieveTotalRowCount))
  126. if (!(CanSort && CanPage && CanRetrieveTotalRowCount))
  127. throw new NotSupportedException ("Sort Capabilites");
  128. return;
  129. }
  130. [MonoTODO ("Extra method to keep things compiling, need to remove later")]
  131. public abstract IEnumerable Select ();
  132. public virtual void Select (DataSourceSelectArguments selectArgs,
  133. DataSourceViewSelectCallback callBack)
  134. {
  135. if (callBack == null)
  136. throw new ArgumentNullException("callBack");
  137. selectArgs.RaiseUnsupportedCapabilitiesError (this);
  138. IEnumerable selectList = ExecuteSelect (selectArgs);
  139. callBack (selectList);
  140. }
  141. public virtual int Update(IDictionary keys, IDictionary values,
  142. IDictionary oldValues, DataSourceViewOperationCallback callBack)
  143. {
  144. if (callBack == null)
  145. throw new ArgumentNullException ("callBack");
  146. int rowAffected = 0;
  147. Exception passOn = null;
  148. try {
  149. rowAffected = ExecuteUpdate (keys, values, oldValues);
  150. } catch (Exception e) {
  151. passOn = e;
  152. }
  153. callBack (rowAffected, passOn);
  154. return rowAffected;
  155. }
  156. public virtual bool CanDelete { get { return false; } }
  157. public virtual bool CanInsert { get { return false; } }
  158. public virtual bool CanPage { get { return false; } }
  159. public virtual bool CanRetrieveTotalRowCount { get { return false; } }
  160. public virtual bool CanSort { get { return false; } }
  161. public virtual bool CanUpdate { get { return false; } }
  162. EventHandlerList eventsList;
  163. protected EventHandlerList Events {
  164. get {
  165. if (eventsList == null)
  166. eventsList = new EventHandlerList();
  167. return eventsList;
  168. }
  169. }
  170. internal bool HasEvents ()
  171. {
  172. return eventsList != null;
  173. }
  174. public virtual string Name {
  175. get { return viewName; }
  176. }
  177. static readonly object EventDataSourceViewChanged = new object ();
  178. public event EventHandler DataSourceViewChanged
  179. {
  180. add { Events.AddHandler (EventDataSourceViewChanged, value); }
  181. remove { Events.RemoveHandler (EventDataSourceViewChanged, value); }
  182. }
  183. }
  184. }
  185. #endif