SqlDataSourceView.cs 16 KB

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