SqlDataSourceView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. //
  2. // System.Web.UI.WebControls.SqlDataSourceView
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) 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.Data;
  36. using System.ComponentModel;
  37. using System.Data.SqlClient;
  38. namespace System.Web.UI.WebControls {
  39. public class SqlDataSourceView : DataSourceView, IStateManager {
  40. SqlCommand command;
  41. SqlConnection connection;
  42. public SqlDataSourceView (SqlDataSource owner, string name)
  43. {
  44. this.owner = owner;
  45. this.name = name;
  46. connection = new SqlConnection (owner.ConnectionString);
  47. }
  48. public int Delete (IDictionary keys, IDictionary oldValues)
  49. {
  50. return ExecuteDelete (keys, oldValues);
  51. }
  52. [MonoTODO ("Handle keys and oldValues and parameters")]
  53. protected override int ExecuteDelete(IDictionary keys, IDictionary oldValues)
  54. {
  55. command = new SqlCommand (this.DeleteCommand, connection);
  56. connection.Open ();
  57. int result = command.ExecuteNonQuery ();
  58. connection.Close ();
  59. return result;
  60. }
  61. public int Insert (IDictionary values)
  62. {
  63. return Insert (values);
  64. }
  65. [MonoTODO ("Handle values and parameters")]
  66. protected override int ExecuteInsert (IDictionary values)
  67. {
  68. command = new SqlCommand (this.InsertCommand, connection);
  69. connection.Open ();
  70. int result = command.ExecuteNonQuery ();
  71. connection.Close ();
  72. return result;
  73. }
  74. public IEnumerable Select (DataSourceSelectArguments arguments)
  75. {
  76. return ExecuteSelect (arguments);
  77. }
  78. [MonoTODO("Extra method to keep things compiling, need to remove later")]
  79. public override IEnumerable Select()
  80. {
  81. throw new NotImplementedException ("Not required");
  82. }
  83. [MonoTODO ("Handle arguments")]
  84. protected internal override IEnumerable ExecuteSelect (
  85. DataSourceSelectArguments arguments)
  86. {
  87. command = new SqlCommand (this.SelectCommand, connection);
  88. connection.Open ();
  89. SqlDataReader reader = command.ExecuteReader ();
  90. //return reader.GetEnumerator ();
  91. throw new NotImplementedException ("SqlDataReader doesnt implements GetEnumerator method yet");
  92. }
  93. public int Update(IDictionary keys, IDictionary values,
  94. IDictionary oldValues)
  95. {
  96. return ExecuteUpdate (keys, values, oldValues);
  97. }
  98. [MonoTODO ("Handle keys, values and oldValues")]
  99. protected override int ExecuteUpdate (IDictionary keys,
  100. IDictionary values, IDictionary oldValues)
  101. {
  102. command = new SqlCommand (this.UpdateCommand, connection);
  103. connection.Open ();
  104. int result = command.ExecuteNonQuery();
  105. connection.Close();
  106. return result;
  107. }
  108. void IStateManager.LoadViewState (object savedState)
  109. {
  110. LoadViewState (savedState);
  111. }
  112. object IStateManager.SaveViewState ()
  113. {
  114. return SaveViewState ();
  115. }
  116. void IStateManager.TrackViewState ()
  117. {
  118. TrackViewState ();
  119. }
  120. protected virtual void LoadViewState (object savedState)
  121. {
  122. object [] vs = savedState as object [];
  123. if (vs == null)
  124. return;
  125. if (vs [0] != null) ((IStateManager) deleteParameters).LoadViewState (vs [0]);
  126. if (vs [1] != null) ((IStateManager) filterParameters).LoadViewState (vs [1]);
  127. if (vs [2] != null) ((IStateManager) insertParameters).LoadViewState (vs [2]);
  128. if (vs [3] != null) ((IStateManager) selectParameters).LoadViewState (vs [3]);
  129. if (vs [4] != null) ((IStateManager) updateParameters).LoadViewState (vs [4]);
  130. if (vs [5] != null) ((IStateManager) viewState).LoadViewState (vs [5]);
  131. }
  132. protected virtual object SaveViewState ()
  133. {
  134. object [] vs = new object [6];
  135. if (deleteParameters != null) vs [0] = ((IStateManager) deleteParameters).SaveViewState ();
  136. if (filterParameters != null) vs [1] = ((IStateManager) filterParameters).SaveViewState ();
  137. if (insertParameters != null) vs [2] = ((IStateManager) insertParameters).SaveViewState ();
  138. if (selectParameters != null) vs [3] = ((IStateManager) selectParameters).SaveViewState ();
  139. if (updateParameters != null) vs [4] = ((IStateManager) updateParameters).SaveViewState ();
  140. if (viewState != null) vs [5] = ((IStateManager) viewState).SaveViewState ();
  141. foreach (object o in vs)
  142. if (o != null) return vs;
  143. return null;
  144. }
  145. protected virtual void TrackViewState ()
  146. {
  147. tracking = true;
  148. if (deleteParameters != null) ((IStateManager) deleteParameters).TrackViewState ();
  149. if (filterParameters != null) ((IStateManager) filterParameters).TrackViewState ();
  150. if (insertParameters != null) ((IStateManager) insertParameters).TrackViewState ();
  151. if (selectParameters != null) ((IStateManager) selectParameters).TrackViewState ();
  152. if (updateParameters != null) ((IStateManager) updateParameters).TrackViewState ();
  153. if (viewState != null) ((IStateManager) viewState).TrackViewState ();
  154. }
  155. protected bool IsTrackingViewState {
  156. get { return tracking; }
  157. }
  158. bool IStateManager.IsTrackingViewState {
  159. get { return IsTrackingViewState; }
  160. }
  161. public string DeleteCommand {
  162. get {
  163. string val = ViewState ["DeleteCommand"] as string;
  164. return val == null ? "" : val;
  165. }
  166. set { ViewState ["DeleteCommand"] = value; }
  167. }
  168. public string FilterExpression {
  169. get {
  170. string val = ViewState ["FilterExpression"] as string;
  171. return val == null ? "" : val;
  172. }
  173. set { ViewState ["FilterExpression"] = value; }
  174. }
  175. public string InsertCommand {
  176. get {
  177. string val = ViewState ["InsertCommand"] as string;
  178. return val == null ? "" : val;
  179. }
  180. set { ViewState ["InsertCommand"] = value; }
  181. }
  182. public string SelectCommand {
  183. get {
  184. string val = ViewState ["SelectCommand"] as string;
  185. return val == null ? "" : val;
  186. }
  187. set { ViewState ["SelectCommand"] = value; }
  188. }
  189. public string UpdateCommand {
  190. get {
  191. string val = ViewState ["UpdateCommand"] as string;
  192. return val == null ? "" : val;
  193. }
  194. set { ViewState ["UpdateCommand"] = value; }
  195. }
  196. public string SortExpression {
  197. get {
  198. string val = ViewState ["SortExpression"] as string;
  199. return val == null ? "" : val;
  200. }
  201. set { ViewState ["SortExpression"] = value; }
  202. }
  203. public override bool CanDelete {
  204. get { return DeleteCommand != ""; }
  205. }
  206. public override bool CanInsert {
  207. get { return UpdateCommand != ""; }
  208. }
  209. public override bool CanSort {
  210. get { return owner.DataSourceMode == SqlDataSourceMode.DataSet; }
  211. }
  212. public override bool CanUpdate {
  213. get { return UpdateCommand != ""; }
  214. }
  215. EventHandlerList events;
  216. protected EventHandlerList Events {
  217. get {
  218. if (events == null)
  219. events = new EventHandlerList ();
  220. return events;
  221. }
  222. }
  223. void ParametersChanged (object source, EventArgs args)
  224. {
  225. OnDataSourceViewChanged (EventArgs.Empty);
  226. }
  227. ParameterCollection GetParameterCollection (ref ParameterCollection output)
  228. {
  229. if (output != null)
  230. return output;
  231. output = new ParameterCollection ();
  232. output.ParametersChanged += new EventHandler (ParametersChanged);
  233. if (IsTrackingViewState)
  234. ((IStateManager) output).TrackViewState ();
  235. return output;
  236. }
  237. public ParameterCollection DeleteParameters {
  238. get { return GetParameterCollection (ref deleteParameters); }
  239. }
  240. public ParameterCollection FilterParameters {
  241. get { return GetParameterCollection (ref filterParameters); }
  242. }
  243. public ParameterCollection InsertParameters {
  244. get { return GetParameterCollection (ref insertParameters); }
  245. }
  246. public ParameterCollection SelectParameters {
  247. get { return GetParameterCollection (ref selectParameters); }
  248. }
  249. public ParameterCollection UpdateParameters {
  250. get { return GetParameterCollection (ref updateParameters); }
  251. }
  252. public override string Name {
  253. get { return name; }
  254. }
  255. protected virtual string ParameterPrefix {
  256. get { return "@"; }
  257. }
  258. StateBag viewState;
  259. protected StateBag ViewState {
  260. get {
  261. if (viewState != null)
  262. return viewState;
  263. viewState = new StateBag ();
  264. if (IsTrackingViewState)
  265. viewState.TrackViewState ();
  266. return viewState;
  267. }
  268. }
  269. ParameterCollection deleteParameters;
  270. ParameterCollection filterParameters;
  271. ParameterCollection insertParameters;
  272. ParameterCollection selectParameters;
  273. ParameterCollection updateParameters;
  274. bool tracking;
  275. string name;
  276. SqlDataSource owner;
  277. static readonly object EventDataSourceViewChanged = new object ();
  278. protected virtual void OnDataSourceViewChanged (EventArgs e)
  279. {
  280. if (events == null) return;
  281. EventHandler h = events [EventDataSourceViewChanged] as EventHandler;
  282. if (h != null)
  283. h (this, e);
  284. }
  285. public event EventHandler DataSourceViewChanged {
  286. add { Events.AddHandler (EventDataSourceViewChanged, value); }
  287. remove { Events.RemoveHandler (EventDataSourceViewChanged, value); }
  288. }
  289. #region OnDelete
  290. static readonly object EventDeleted = new object ();
  291. protected virtual void OnDeleted (SqlDataSourceStatusEventArgs e)
  292. {
  293. if (events == null) return;
  294. SqlDataSourceStatusEventHandler h = events [EventDeleted] as SqlDataSourceStatusEventHandler;
  295. if (h != null)
  296. h (this, e);
  297. }
  298. public event SqlDataSourceStatusEventHandler Deleted {
  299. add { Events.AddHandler (EventDeleted, value); }
  300. remove { Events.RemoveHandler (EventDeleted, value); }
  301. }
  302. static readonly object EventDeleting = new object ();
  303. protected virtual void OnDeleting (SqlDataSourceCommandEventArgs e)
  304. {
  305. if (events == null) return;
  306. SqlDataSourceCommandEventHandler h = events [EventDeleting] as SqlDataSourceCommandEventHandler;
  307. if (h != null)
  308. h (this, e);
  309. }
  310. public event SqlDataSourceCommandEventHandler Deleting {
  311. add { Events.AddHandler (EventDeleting, value); }
  312. remove { Events.RemoveHandler (EventDeleting, value); }
  313. }
  314. #endregion
  315. #region OnInsert
  316. static readonly object EventInserted = new object ();
  317. protected virtual void OnInserted (SqlDataSourceStatusEventArgs e)
  318. {
  319. if (events == null) return;
  320. SqlDataSourceStatusEventHandler h = events [EventInserted] as SqlDataSourceStatusEventHandler;
  321. if (h != null)
  322. h (this, e);
  323. }
  324. public event SqlDataSourceStatusEventHandler Inserted {
  325. add { Events.AddHandler (EventInserted, value); }
  326. remove { Events.RemoveHandler (EventInserted, value); }
  327. }
  328. static readonly object EventInserting = new object ();
  329. protected virtual void OnInserting (SqlDataSourceCommandEventArgs e)
  330. {
  331. if (events == null) return;
  332. SqlDataSourceCommandEventHandler h = events [EventInserting] as SqlDataSourceCommandEventHandler;
  333. if (h != null)
  334. h (this, e);
  335. }
  336. public event SqlDataSourceCommandEventHandler Inserting {
  337. add { Events.AddHandler (EventInserting, value); }
  338. remove { Events.RemoveHandler (EventInserting, value); }
  339. }
  340. #endregion
  341. #region OnSelect
  342. static readonly object EventSelected = new object ();
  343. protected virtual void OnSelected (SqlDataSourceStatusEventArgs e)
  344. {
  345. if (events == null) return;
  346. SqlDataSourceStatusEventHandler h = events [EventSelected] as SqlDataSourceStatusEventHandler;
  347. if (h != null)
  348. h (this, e);
  349. }
  350. public event SqlDataSourceStatusEventHandler Selected {
  351. add { Events.AddHandler (EventSelected, value); }
  352. remove { Events.RemoveHandler (EventSelected, value); }
  353. }
  354. static readonly object EventSelecting = new object ();
  355. protected virtual void OnSelecting (SqlDataSourceCommandEventArgs e)
  356. {
  357. if (events == null) return;
  358. SqlDataSourceCommandEventHandler h = events [EventSelecting] as SqlDataSourceCommandEventHandler;
  359. if (h != null)
  360. h (this, e);
  361. }
  362. public event SqlDataSourceCommandEventHandler Selecting {
  363. add { Events.AddHandler (EventSelecting, value); }
  364. remove { Events.RemoveHandler (EventSelecting, value); }
  365. }
  366. #endregion
  367. #region OnUpdate
  368. static readonly object EventUpdated = new object ();
  369. protected virtual void OnUpdated (SqlDataSourceStatusEventArgs e)
  370. {
  371. if (events == null) return;
  372. SqlDataSourceStatusEventHandler h = events [EventUpdated] as SqlDataSourceStatusEventHandler;
  373. if (h != null)
  374. h (this, e);
  375. }
  376. public event SqlDataSourceStatusEventHandler Updated {
  377. add { Events.AddHandler (EventUpdated, value); }
  378. remove { Events.RemoveHandler (EventUpdated, value); }
  379. }
  380. static readonly object EventUpdating = new object ();
  381. protected virtual void OnUpdating (SqlDataSourceCommandEventArgs e)
  382. {
  383. if (events == null) return;
  384. SqlDataSourceCommandEventHandler h = events [EventUpdating] as SqlDataSourceCommandEventHandler;
  385. if (h != null)
  386. h (this, e);
  387. }
  388. public event SqlDataSourceCommandEventHandler Updating {
  389. add { Events.AddHandler (EventUpdating, value); }
  390. remove { Events.RemoveHandler (EventUpdating, value); }
  391. }
  392. #endregion
  393. }
  394. }
  395. #endif