LinqDataSourceView.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //
  2. // LinqDataSourceView.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc http://novell.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. // FIXME: in general we should create something like
  30. // System.Web.Query,Dynamic.DynamicClass to execute DataContext operations.
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using System.ComponentModel;
  35. using System.Data.Linq;
  36. using System.Reflection;
  37. using System.Security.Permissions;
  38. using System.Web.DynamicData;
  39. using System.Web.UI;
  40. namespace System.Web.UI.WebControls
  41. {
  42. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  44. public class LinqDataSourceView : DataSourceView, IStateManager
  45. {
  46. public LinqDataSourceView (LinqDataSource owner, string name, HttpContext context)
  47. : base (owner, name)
  48. {
  49. source = owner;
  50. }
  51. LinqDataSource source;
  52. bool tracking;
  53. ParameterCollection delete_parameters,
  54. insert_parameters,
  55. select_new_parameters,
  56. update_parameters;
  57. ParameterCollection group_by_parameters,
  58. order_by_parameters,
  59. order_group_by_parameters,
  60. where_parameters;
  61. IEnumerable<ParameterCollection> AllParameters {
  62. get {
  63. yield return delete_parameters;
  64. yield return insert_parameters;
  65. yield return select_new_parameters;
  66. yield return update_parameters;
  67. yield return group_by_parameters;
  68. yield return order_by_parameters;
  69. yield return order_group_by_parameters;
  70. yield return where_parameters;
  71. }
  72. }
  73. [MonoTODO]
  74. public bool AutoGenerateOrderByClause { get; set; }
  75. [MonoTODO]
  76. public bool AutoGenerateWhereClause { get; set; }
  77. [MonoTODO]
  78. public bool AutoPage { get; set; }
  79. [MonoTODO]
  80. public bool AutoSort { get; set; }
  81. public override bool CanDelete {
  82. get { return EnableDelete; }
  83. }
  84. public override bool CanInsert {
  85. get { return EnableInsert; }
  86. }
  87. public override bool CanPage {
  88. get { return true; }
  89. }
  90. public override bool CanRetrieveTotalRowCount {
  91. get { return true; }
  92. }
  93. public override bool CanSort {
  94. get { return true; }
  95. }
  96. public override bool CanUpdate {
  97. get { return EnableUpdate; }
  98. }
  99. protected virtual Type ContextType {
  100. get { return ((IDynamicDataSource) source).ContextType; }
  101. }
  102. public virtual string ContextTypeName {
  103. get { return source.ContextTypeName; }
  104. set { source.ContextTypeName = value; }
  105. }
  106. [MonoTODO]
  107. public bool EnableDelete { get; set; }
  108. [MonoTODO]
  109. public bool EnableInsert { get; set; }
  110. [MonoTODO]
  111. public bool EnableObjectTracking { get; set; }
  112. [MonoTODO]
  113. public bool EnableUpdate { get; set; }
  114. [MonoTODO]
  115. public string TableName { get; set; }
  116. [MonoTODO]
  117. public string GroupBy { get; set; }
  118. [MonoTODO]
  119. public string OrderBy { get; set; }
  120. [MonoTODO]
  121. public string OrderGroupsBy { get; set; }
  122. [MonoTODO]
  123. public string SelectNew { get; set; }
  124. [MonoTODO]
  125. public string Where { get; set; }
  126. public ParameterCollection DeleteParameters {
  127. get { return GetParameterCollection (ref delete_parameters, false, false); }
  128. }
  129. public ParameterCollection InsertParameters {
  130. get { return GetParameterCollection (ref insert_parameters, false, false); }
  131. }
  132. public ParameterCollection UpdateParameters {
  133. get { return GetParameterCollection (ref update_parameters, true, true); }
  134. }
  135. public ParameterCollection SelectNewParameters {
  136. get { return GetParameterCollection (ref select_new_parameters, false, false); }
  137. }
  138. public ParameterCollection WhereParameters {
  139. get { return GetParameterCollection (ref where_parameters, true, true); }
  140. }
  141. public ParameterCollection GroupByParameters {
  142. get { return GetParameterCollection (ref group_by_parameters, true, true); }
  143. }
  144. public ParameterCollection OrderByParameters {
  145. get { return GetParameterCollection (ref order_by_parameters, true, true); }
  146. }
  147. public ParameterCollection OrderGroupsByParameters {
  148. get { return GetParameterCollection (ref order_group_by_parameters, true, true); }
  149. }
  150. ParameterCollection GetParameterCollection (ref ParameterCollection output, bool propagateTrackViewState, bool subscribeChanged)
  151. {
  152. if (output != null)
  153. return output;
  154. output = new ParameterCollection ();
  155. if (subscribeChanged)
  156. output.ParametersChanged += new EventHandler (ParametersChanged);
  157. if (IsTrackingViewState && propagateTrackViewState)
  158. ((IStateManager) output).TrackViewState ();
  159. return output;
  160. }
  161. void ParametersChanged (object source, EventArgs args)
  162. {
  163. OnDataSourceViewChanged (EventArgs.Empty);
  164. }
  165. object data_context;
  166. object GetDataContext ()
  167. {
  168. if (data_context == null)
  169. data_context = CreateContext (ContextType);
  170. return data_context;
  171. }
  172. public int Delete (IDictionary keys, IDictionary oldValues)
  173. {
  174. return ExecuteDelete (keys, oldValues);
  175. }
  176. public int Insert (IDictionary values)
  177. {
  178. return ExecuteInsert (values);
  179. }
  180. public IEnumerable Select (DataSourceSelectArguments arguments)
  181. {
  182. return ExecuteSelect (arguments);
  183. }
  184. public int Update (IDictionary keys, IDictionary values, IDictionary oldValues)
  185. {
  186. return ExecuteUpdate (keys, values, oldValues);
  187. }
  188. [MonoTODO]
  189. protected override int ExecuteDelete (IDictionary keys, IDictionary oldValues)
  190. {
  191. throw new NotImplementedException ();
  192. }
  193. protected override int ExecuteInsert (IDictionary values)
  194. {
  195. var dc = (DataContext) GetDataContext ();
  196. foreach (var mt in dc.Mapping.GetTables ()) {
  197. if (mt.TableName != TableName)
  198. continue;
  199. var t = mt.RowType.Type;
  200. ITable table = dc.GetTable (t);
  201. object entity = Activator.CreateInstance (t);
  202. // FIXME: merge InsertParameters
  203. foreach (DictionaryEntry p in values)
  204. t.GetProperty ((string) p.Key).SetValue (entity, p.Value, null);
  205. InsertDataObject (dc, table, entity);
  206. return 1;
  207. }
  208. throw new InvalidOperationException (String.Format ("Table '{0}' was not found on the data context '{1}'", TableName, ContextType));
  209. }
  210. [MonoTODO]
  211. protected override int ExecuteUpdate (IDictionary keys, IDictionary values, IDictionary oldValues)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. [MonoTODO]
  216. protected internal override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  217. {
  218. int max = arguments.MaximumRows;
  219. max = max == 0 ? int.MaxValue : max;
  220. int total = arguments.TotalRowCount;
  221. total = total < 0 ? int.MaxValue : total;
  222. int end = total == int.MaxValue ? total : arguments.StartRowIndex + total;
  223. DataContext dc = Activator.CreateInstance (ContextType, true) as DataContext;
  224. MemberInfo mi = GetTableMemberInfo (dc.GetType ());
  225. // am totally not sure it is fine.
  226. IEnumerable results;
  227. if (source.Select != null) {
  228. // FIXME: merge SelectParameters.
  229. results = dc.ExecuteQuery (mi is FieldInfo ? ((FieldInfo) mi).FieldType : ((PropertyInfo) mi).PropertyType, source.Select, new object [0]);
  230. } else {
  231. results = (IEnumerable) (mi is FieldInfo ? ((FieldInfo) mi).GetValue (dc) : ((PropertyInfo) mi).GetValue (dc, null));
  232. }
  233. int i = 0;
  234. foreach (var e in results) {
  235. if (i++ < arguments.StartRowIndex)
  236. continue; // skip rows before StartRowIndex.
  237. if (i < end)
  238. yield return e;
  239. }
  240. }
  241. protected virtual void DeleteDataObject (object dataContext, object table, object oldDataObject)
  242. {
  243. ITable itable = ((DataContext) dataContext).GetTable (table.GetType ());
  244. itable.DeleteOnSubmit (oldDataObject);
  245. }
  246. protected virtual void InsertDataObject (object dataContext, object table, object newDataObject)
  247. {
  248. ITable itable = ((DataContext) dataContext).GetTable (table.GetType ());
  249. itable.InsertOnSubmit (newDataObject);
  250. }
  251. [MonoTODO]
  252. protected virtual void ResetDataObject (object table, object dataObject)
  253. {
  254. var dc = GetDataContext ();
  255. ITable itable = ((DataContext) dc).GetTable (table.GetType ());
  256. UpdateDataObject (dc, table, dataObject, itable.GetOriginalEntityState (dataObject));
  257. }
  258. protected virtual void UpdateDataObject (object dataContext, object table, object oldDataObject, object newDataObject)
  259. {
  260. DeleteDataObject (dataContext, table, oldDataObject);
  261. InsertDataObject (dataContext, table, newDataObject);
  262. }
  263. protected virtual object CreateContext (Type contextType)
  264. {
  265. OnContextCreating (new LinqDataSourceContextEventArgs ());
  266. var o = Activator.CreateInstance (contextType);
  267. OnContextCreated (new LinqDataSourceStatusEventArgs (o));
  268. return o;
  269. }
  270. [MonoTODO]
  271. protected virtual Type GetDataObjectType (Type tableType)
  272. {
  273. throw new NotImplementedException ();
  274. }
  275. MemberInfo table_member;
  276. protected virtual MemberInfo GetTableMemberInfo (Type contextType)
  277. {
  278. if (contextType == null)
  279. throw new ArgumentNullException ("contextType");
  280. if (String.IsNullOrEmpty (TableName))
  281. throw new InvalidOperationException (String.Format ("The TableName property of LinqDataSource '{0}' must specify a table property or field on the data context type.", source.ID));
  282. if (table_member != null && table_member.DeclaringType == contextType)
  283. return table_member;
  284. var marr = contextType.GetMember (TableName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty);
  285. if (marr == null || marr.Length == 0)
  286. throw new InvalidOperationException (String.Format ("Could not find a property or field called '{0}' on the data context type '{1}' of LinqDataSource '{2}'", TableName, contextType, source.ID));
  287. table_member = marr [0];
  288. return table_member;
  289. }
  290. #region Validation
  291. [MonoTODO]
  292. protected virtual void ValidateContextType (Type contextType, bool selecting)
  293. {
  294. throw new NotImplementedException ();
  295. }
  296. [MonoTODO]
  297. protected virtual void ValidateDeleteSupported (IDictionary keys, IDictionary oldValues)
  298. {
  299. throw new NotImplementedException ();
  300. }
  301. [MonoTODO]
  302. protected virtual void ValidateInsertSupported (IDictionary values)
  303. {
  304. throw new NotImplementedException ();
  305. }
  306. [MonoTODO]
  307. protected virtual void ValidateOrderByParameter (string name, string value)
  308. {
  309. throw new NotImplementedException ();
  310. }
  311. [MonoTODO]
  312. protected virtual void ValidateParameterName (string name)
  313. {
  314. throw new NotImplementedException ();
  315. }
  316. [MonoTODO]
  317. protected virtual void ValidateTableType (Type tableType, bool selecting)
  318. {
  319. throw new NotImplementedException ();
  320. }
  321. [MonoTODO]
  322. protected virtual void ValidateUpdateSupported (IDictionary keys, IDictionary values, IDictionary oldValues)
  323. {
  324. throw new NotImplementedException ();
  325. }
  326. #endregion
  327. #region ViewState
  328. bool IStateManager.IsTrackingViewState {
  329. get { return IsTrackingViewState; }
  330. }
  331. protected bool IsTrackingViewState {
  332. get { return tracking; }
  333. }
  334. [MonoTODO]
  335. public bool StoreOriginalValuesInViewState {
  336. get { throw new NotImplementedException (); }
  337. set { throw new NotImplementedException (); }
  338. }
  339. void IStateManager.LoadViewState (object savedState)
  340. {
  341. LoadViewState (savedState);
  342. }
  343. object IStateManager.SaveViewState ()
  344. {
  345. return SaveViewState ();
  346. }
  347. void IStateManager.TrackViewState ()
  348. {
  349. TrackViewState ();
  350. }
  351. protected virtual void LoadViewState (object savedState)
  352. {
  353. object [] vs = savedState as object [];
  354. if (vs == null)
  355. return;
  356. int i = 0;
  357. foreach (var p in AllParameters) {
  358. if (vs [i] != null)
  359. ((IStateManager) p).LoadViewState (vs [i]);
  360. i++;
  361. }
  362. }
  363. protected virtual object SaveViewState ()
  364. {
  365. object [] vs = new object [8];
  366. int i = 0;
  367. foreach (var p in AllParameters) {
  368. if (p != null)
  369. vs [i] = ((IStateManager) p).SaveViewState ();
  370. i++;
  371. }
  372. foreach (object o in vs)
  373. if (o != null)
  374. return vs;
  375. return null;
  376. }
  377. protected virtual void TrackViewState ()
  378. {
  379. tracking = true;
  380. foreach (var p in AllParameters)
  381. if (p != null)
  382. ((IStateManager) p).TrackViewState ();
  383. }
  384. #endregion
  385. #region Events and Overridable Event Handler Invocations
  386. [MonoTODO]
  387. public event EventHandler<LinqDataSourceStatusEventArgs> ContextCreated;
  388. [MonoTODO]
  389. public event EventHandler<LinqDataSourceContextEventArgs> ContextCreating;
  390. [MonoTODO]
  391. public event EventHandler<LinqDataSourceDisposeEventArgs> ContextDisposing;
  392. [MonoTODO]
  393. public event EventHandler<LinqDataSourceStatusEventArgs> Deleted;
  394. [MonoTODO]
  395. public event EventHandler<LinqDataSourceDeleteEventArgs> Deleting;
  396. [MonoTODO]
  397. internal event EventHandler<DynamicValidatorEventArgs> Exception;
  398. [MonoTODO]
  399. public event EventHandler<LinqDataSourceStatusEventArgs> Inserted;
  400. [MonoTODO]
  401. public event EventHandler<LinqDataSourceInsertEventArgs> Inserting;
  402. [MonoTODO]
  403. public event EventHandler<LinqDataSourceStatusEventArgs> Selected;
  404. [MonoTODO]
  405. public event EventHandler<LinqDataSourceSelectEventArgs> Selecting;
  406. [MonoTODO]
  407. public event EventHandler<LinqDataSourceStatusEventArgs> Updated;
  408. [MonoTODO]
  409. public event EventHandler<LinqDataSourceUpdateEventArgs> Updating;
  410. protected virtual void OnContextCreated (LinqDataSourceStatusEventArgs e)
  411. {
  412. if (ContextCreated != null)
  413. ContextCreated (this, e);
  414. }
  415. protected virtual void OnContextCreating (LinqDataSourceContextEventArgs e)
  416. {
  417. if (ContextCreating != null)
  418. ContextCreating (this, e);
  419. }
  420. protected virtual void OnContextDisposing (LinqDataSourceDisposeEventArgs e)
  421. {
  422. if (ContextDisposing != null)
  423. ContextDisposing (this, e);
  424. }
  425. protected virtual void OnDeleted (LinqDataSourceStatusEventArgs e)
  426. {
  427. if (Deleted != null)
  428. Deleted (this, e);
  429. }
  430. protected virtual void OnDeleting (LinqDataSourceDeleteEventArgs e)
  431. {
  432. if (Deleting != null)
  433. Deleting (this, e);
  434. }
  435. protected virtual void OnException (DynamicValidatorEventArgs e)
  436. {
  437. if (Exception != null)
  438. Exception (this, e);
  439. }
  440. protected virtual void OnInserted (LinqDataSourceStatusEventArgs e)
  441. {
  442. if (Inserted != null)
  443. Inserted (this, e);
  444. }
  445. protected virtual void OnInserting (LinqDataSourceInsertEventArgs e)
  446. {
  447. if (Inserting != null)
  448. Inserting (this, e);
  449. }
  450. protected virtual void OnSelected (LinqDataSourceStatusEventArgs e)
  451. {
  452. if (Selected != null)
  453. Selected (this, e);
  454. }
  455. protected virtual void OnSelecting (LinqDataSourceSelectEventArgs e)
  456. {
  457. if (Selecting != null)
  458. Selecting (this, e);
  459. }
  460. protected virtual void OnUpdated (LinqDataSourceStatusEventArgs e)
  461. {
  462. if (Updated != null)
  463. Updated (this, e);
  464. }
  465. protected virtual void OnUpdating (LinqDataSourceUpdateEventArgs e)
  466. {
  467. if (Updating != null)
  468. Updating (this, e);
  469. }
  470. #endregion
  471. }
  472. }