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