LinqDataSourceView.cs 15 KB

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