SqlDataSourceView.cs 16 KB

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