protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments) { if (this.dataSourceControl.Enabled == true) { IOrderedDictionary parameters = this.dataSourceControl.DataParameters.GetValues(HttpContext.Current, this.dataSourceControl); DataEventArgs args = new DataEventArgs(this.dataSourceControl.PageSize, arguments.StartRowIndex, arguments.SortExpression, parameters); this.dataSourceControl.GetData(args); arguments.TotalRowCount = args.TotalRowCount; arguments.MaximumRows = this.dataSourceControl.PageSize; arguments.AddSupportedCapabilities(DataSourceCapabilities.Page | DataSourceCapabilities.Sort | DataSourceCapabilities.RetrieveTotalRowCount); arguments.RetrieveTotalRowCount = true; if (!(args.Data is ICollection)) { return (args.Data.OfType<Object>().ToList()); } else { return (args.Data); } } else { arguments.TotalRowCount = 0; return (new ArrayList()); } }
protected Array ExecuteCustomSelect(DataSourceSelectArguments arguments) { if (CanPage) arguments.AddSupportedCapabilities(DataSourceCapabilities.Page); if (CanSort) arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort); if (CanRetrieveTotalRowCount) arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount); ActiveRecordDataSourceFindEventArgs args = new ActiveRecordDataSourceFindEventArgs(); CreateOrderFromFindParameters(args, arguments.SortExpression); if (args.Order.Count <= 0) CreateOrderFromFindParameters(args, Source.DefaultSort); args.ModelType = BuildManager.GetType(Source.TypeName, false, true); Source.OnBeforeFind(args); if (args.Cancel) return null; bool useSlicedFind = false; int firstResult = 0; int maxResults = 0; if (CanPage) { useSlicedFind = true; firstResult = arguments.StartRowIndex; maxResults = arguments.MaximumRows; } else if (Source.DefaultMaximumRows > 0) { useSlicedFind = true; firstResult = 0; maxResults = Source.DefaultMaximumRows; } Dictionary<string, object> methodParameters = CreateMethodParametersFromFindParameters(); if (!String.IsNullOrEmpty(Source.FindMethodFirstResultParam) && useSlicedFind) methodParameters[Source.FindMethodFirstResultParam] = firstResult; if (!String.IsNullOrEmpty(Source.FindMethodMaxResultsParam) && useSlicedFind) methodParameters[Source.FindMethodMaxResultsParam] = maxResults; if (!String.IsNullOrEmpty(Source.FindMethodOrderParam) && CanSort) methodParameters[Source.FindMethodOrderParam] = args.Order.ToArray(); Array result = null; try { if (arguments.RetrieveTotalRowCount) arguments.TotalRowCount = ExecuteCount(args); MethodInfo findMethod; Dictionary<string, int> methodParameterNameToIndex; ResolveCustomFindMethod(args.ModelType, methodParameters, out findMethod, out methodParameterNameToIndex); if (findMethod == null) throw new ApplicationException(String.Format("Invalid custom find method '{0}'.", Source.FindMethod)); object[] findMethodArgs = new object[methodParameters.Keys.Count]; foreach (string key in methodParameters.Keys) findMethodArgs[methodParameterNameToIndex[key]] = methodParameters[key]; result = findMethod.Invoke(null, findMethodArgs) as Array; } catch (Exception e) { args.Exception = e; args.WasError = true; Source.OnFindError(args); if (Source.ThrowOnError && !args.DoNotThrow) throw; return null; } args.Result = result; Source.OnFind(args); return result; }
protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments) { if (!String.IsNullOrEmpty(Source.FindMethod)) return ExecuteCustomSelect(arguments); if (CanPage) arguments.AddSupportedCapabilities(DataSourceCapabilities.Page); if (CanSort) arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort); if (CanRetrieveTotalRowCount) arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount); ActiveRecordDataSourceFindEventArgs args = new ActiveRecordDataSourceFindEventArgs(); CreateCriteriaFromFindParameters(args); CreateOrderFromFindParameters(args, arguments.SortExpression); //set default sort if (args.Order.Count <= 0) CreateOrderFromFindParameters(args, Source.DefaultSort); args.ModelType = BuildManager.GetType(Source.TypeName, false, true); Source.OnBeforeFind(args); if (args.Cancel) return null; Array result = null; try { if (arguments.RetrieveTotalRowCount) arguments.TotalRowCount = ExecuteCount(args); bool useSlicedFind = false; int firstResult = 0; int maxResults = 0; if (CanPage) { useSlicedFind = true; firstResult = arguments.StartRowIndex; maxResults = arguments.MaximumRows; } else if (Source.DefaultMaximumRows > 0) { useSlicedFind = true; firstResult = 0; maxResults = Source.DefaultMaximumRows; } if (useSlicedFind) result = ActiveRecordMediator.SlicedFindAll(args.ModelType, firstResult, maxResults, args.Order.ToArray(), args.Criteria.ToArray()); else result = ActiveRecordMediator.FindAll(args.ModelType, args.Order.ToArray(), args.Criteria.ToArray()); } catch (Exception e) { args.Exception = e; args.WasError = true; Source.OnFindError(args); if (Source.ThrowOnError && !args.DoNotThrow) throw; return null; } args.Result = result; Source.OnFind(args); return result; }
/// <devdoc> /// Returns all the rows of the datasource. /// Parameters are taken from the SqlDataSource.Parameters property collection. /// If DataSourceMode is set to DataSet then a DataView is returned. /// If DataSourceMode is set to DataReader then a DataReader is returned, and it must be closed when done. /// </devdoc> protected internal override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments) { if (SelectCommand.Length == 0) { return null; } DbConnection connection = _owner.CreateConnection(_owner.ConnectionString); if (connection == null) { throw new InvalidOperationException(SR.GetString(SR.SqlDataSourceView_CouldNotCreateConnection, _owner.ID)); } DataSourceCache cache = _owner.Cache; bool cacheEnabled = (cache != null) && (cache.Enabled); //int startRowIndex = arguments.StartRowIndex; //int maximumRows = arguments.MaximumRows; string sortExpression = arguments.SortExpression; if (CanPage) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Page); } if (CanSort) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort); } if (CanRetrieveTotalRowCount) { arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount); } // If caching is enabled, load DataSet from cache if (cacheEnabled) { if (_owner.DataSourceMode != SqlDataSourceMode.DataSet) { throw new NotSupportedException(SR.GetString(SR.SqlDataSourceView_CacheNotSupported, _owner.ID)); } arguments.RaiseUnsupportedCapabilitiesError(this); DataSet dataSet = _owner.LoadDataFromCache(0, -1) as DataSet; if (dataSet != null) { /*if (arguments.RetrieveTotalRowCount) { int cachedTotalRowCount = _owner.LoadTotalRowCountFromCache(); if (cachedTotalRowCount >= 0) { arguments.TotalRowCount = cachedTotalRowCount; } else { // query for row count and then save it in cache cachedTotalRowCount = QueryTotalRowCount(connection, arguments); arguments.TotalRowCount = cachedTotalRowCount; _owner.SaveTotalRowCountToCache(cachedTotalRowCount); } }*/ IOrderedDictionary parameterValues = FilterParameters.GetValues(_context, _owner); if (FilterExpression.Length > 0) { SqlDataSourceFilteringEventArgs filterArgs = new SqlDataSourceFilteringEventArgs(parameterValues); OnFiltering(filterArgs); if (filterArgs.Cancel) { return null; } } return FilteredDataSetHelper.CreateFilteredDataView(dataSet.Tables[0], sortExpression, FilterExpression, parameterValues); } } // Create command and add parameters DbCommand command = _owner.CreateCommand(SelectCommand, connection); InitializeParameters(command, SelectParameters, null); command.CommandType = GetCommandType(SelectCommandType); // Raise event to allow customization and cancellation SqlDataSourceSelectingEventArgs selectingEventArgs = new SqlDataSourceSelectingEventArgs(command, arguments); OnSelecting(selectingEventArgs); // If the operation was cancelled, exit immediately if (selectingEventArgs.Cancel) { return null; } // Add the sort parameter to allow for custom stored procedure sorting, if necessary string sortParameterName = SortParameterName; if (sortParameterName.Length > 0) { if (command.CommandType != CommandType.StoredProcedure) { throw new NotSupportedException(SR.GetString(SR.SqlDataSourceView_SortParameterRequiresStoredProcedure, _owner.ID)); } command.Parameters.Add(_owner.CreateParameter(ParameterPrefix + sortParameterName, sortExpression)); // We reset the sort expression here so that we pretend as // though we're not really sorting (since the developer is // worrying about it instead of us). arguments.SortExpression = String.Empty; } arguments.RaiseUnsupportedCapabilitiesError(this); // reset these values, since they might have changed in the OnSelecting event sortExpression = arguments.SortExpression; //startRowIndex = arguments.StartRowIndex; //maximumRows = arguments.MaximumRows; // Perform null check if user wants to cancel on any null parameter value if (CancelSelectOnNullParameter) { int paramCount = command.Parameters.Count; for (int i = 0; i < paramCount; i++) { DbParameter parameter = command.Parameters[i]; if ((parameter != null) && (parameter.Value == null) && ((parameter.Direction == ParameterDirection.Input) || (parameter.Direction == ParameterDirection.InputOutput))) { return null; } } } // Replace null values in parameters with DBNull.Value ReplaceNullValues(command); /*if (arguments.RetrieveTotalRowCount && SelectCountCommand.Length > 0) { int cachedTotalRowCount = -1; if (cacheEnabled) { cachedTotalRowCount = _owner.LoadTotalRowCountFromCache(); if (cachedTotalRowCount >= 0) { arguments.TotalRowCount = cachedTotalRowCount; } } if (cachedTotalRowCount < 0) { cachedTotalRowCount = QueryTotalRowCount(connection, arguments); arguments.TotalRowCount = cachedTotalRowCount; if (cacheEnabled) { _owner.SaveTotalRowCountToCache(cachedTotalRowCount); } } }*/ IEnumerable selectResult = null; switch (_owner.DataSourceMode) { case SqlDataSourceMode.DataSet: { SqlCacheDependency cacheDependency = null; if (cacheEnabled && cache is SqlDataSourceCache) { SqlDataSourceCache sqlCache = (SqlDataSourceCache)cache; if (String.Equals(sqlCache.SqlCacheDependency, SqlDataSourceCache.Sql9CacheDependencyDirective, StringComparison.OrdinalIgnoreCase)) { if (!(command is System.Data.SqlClient.SqlCommand)) { throw new InvalidOperationException(SR.GetString(SR.SqlDataSourceView_CommandNotificationNotSupported, _owner.ID)); } cacheDependency = new SqlCacheDependency((System.Data.SqlClient.SqlCommand)command); } } DbDataAdapter adapter = _owner.CreateDataAdapter(command); DataSet dataSet = new DataSet(); int rowsAffected = 0; bool eventRaised = false; try { rowsAffected = adapter.Fill(dataSet, Name); // Raise the Selected event eventRaised = true; SqlDataSourceStatusEventArgs selectedEventArgs = new SqlDataSourceStatusEventArgs(command, rowsAffected, null); OnSelected(selectedEventArgs); } catch (Exception ex) { if (!eventRaised) { // Raise the Selected event SqlDataSourceStatusEventArgs selectedEventArgs = new SqlDataSourceStatusEventArgs(command, rowsAffected, ex); OnSelected(selectedEventArgs); if (!selectedEventArgs.ExceptionHandled) { throw; } } else { bool isCustomException; ex = BuildCustomException(ex, DataSourceOperation.Select, command, out isCustomException); if (isCustomException) { throw ex; } else { throw; } } } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } // If caching is enabled, save DataSet to cache DataTable dataTable = (dataSet.Tables.Count > 0 ? dataSet.Tables[0] : null); if (cacheEnabled && dataTable != null) { _owner.SaveDataToCache(0, -1, dataSet, cacheDependency); } if (dataTable != null) { IOrderedDictionary parameterValues = FilterParameters.GetValues(_context, _owner); if (FilterExpression.Length > 0) { SqlDataSourceFilteringEventArgs filterArgs = new SqlDataSourceFilteringEventArgs(parameterValues); OnFiltering(filterArgs); if (filterArgs.Cancel) { return null; } } selectResult = FilteredDataSetHelper.CreateFilteredDataView(dataTable, sortExpression, FilterExpression, parameterValues); } break; } case SqlDataSourceMode.DataReader: { if (FilterExpression.Length > 0) { throw new NotSupportedException(SR.GetString(SR.SqlDataSourceView_FilterNotSupported, _owner.ID)); } if (sortExpression.Length > 0) { throw new NotSupportedException(SR.GetString(SR.SqlDataSourceView_SortNotSupported, _owner.ID)); } bool eventRaised = false; try { if (connection.State != ConnectionState.Open) { connection.Open(); } selectResult = command.ExecuteReader(CommandBehavior.CloseConnection); // Raise the Selected event eventRaised = true; SqlDataSourceStatusEventArgs selectedEventArgs = new SqlDataSourceStatusEventArgs(command, 0, null); OnSelected(selectedEventArgs); } catch (Exception ex) { if (!eventRaised) { // Raise the Selected event SqlDataSourceStatusEventArgs selectedEventArgs = new SqlDataSourceStatusEventArgs(command, 0, ex); OnSelected(selectedEventArgs); if (!selectedEventArgs.ExceptionHandled) { throw; } } else { bool isCustomException; ex = BuildCustomException(ex, DataSourceOperation.Select, command, out isCustomException); if (isCustomException) { throw ex; } else { throw; } } } break; } } return selectResult; }
public void RaiseUnsupportedCapabilitiesError () { PockerDataSourceView view = new PockerDataSourceView (); DataSourceSelectArguments arg = new DataSourceSelectArguments (); arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsFalse (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); view = new PockerDataSourceView (); arg = new DataSourceSelectArguments (); arg.StartRowIndex = 10; arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsTrue (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); Assert.AreEqual (DataSourceCapabilities.Page, view.DataSourceCapabilities, "RaiseUnsupportedCapabilitiesError"); view = new PockerDataSourceView (); arg = new DataSourceSelectArguments (); arg.MaximumRows = 5; arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsTrue (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); Assert.AreEqual (DataSourceCapabilities.Page, view.DataSourceCapabilities, "RaiseUnsupportedCapabilitiesError"); view = new PockerDataSourceView (); arg = new DataSourceSelectArguments (); arg.SortExpression = "Sort"; arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsTrue (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); Assert.AreEqual (DataSourceCapabilities.Sort, view.DataSourceCapabilities, "RaiseUnsupportedCapabilitiesError"); view = new PockerDataSourceView (); arg = new DataSourceSelectArguments (); arg.RetrieveTotalRowCount = true; arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsTrue (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); Assert.AreEqual (DataSourceCapabilities.RetrieveTotalRowCount, view.DataSourceCapabilities, "RaiseUnsupportedCapabilitiesError"); view = new PockerDataSourceView (); arg = new DataSourceSelectArguments (); arg.AddSupportedCapabilities (DataSourceCapabilities.Page | DataSourceCapabilities.Sort | DataSourceCapabilities.RetrieveTotalRowCount); arg.SortExpression = "Sort"; arg.StartRowIndex = 10; arg.MaximumRows = 5; arg.RetrieveTotalRowCount = true; arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsFalse (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); view = new PockerDataSourceView (); arg = new DataSourceSelectArguments (); arg.SortExpression = "Sort"; arg.StartRowIndex = 10; arg.MaximumRows = 5; arg.RetrieveTotalRowCount = true; arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsTrue (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); Assert.AreEqual (DataSourceCapabilities.RetrieveTotalRowCount, view.DataSourceCapabilities, "RaiseUnsupportedCapabilitiesError"); view = new PockerDataSourceView (); arg = new DataSourceSelectArguments (); arg.SortExpression = "Sort"; arg.StartRowIndex = 10; arg.MaximumRows = 5; arg.RaiseUnsupportedCapabilitiesError (view); Assert.IsTrue (view.RaiseUnsupportedCapabilityErrorCalled, "RaiseUnsupportedCapabilitiesError"); Assert.AreEqual (DataSourceCapabilities.Page, view.DataSourceCapabilities, "RaiseUnsupportedCapabilitiesError"); }
/// <devdoc> /// </devdoc> protected internal override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments) { if (SelectMethod.Length == 0) { throw new InvalidOperationException(SR.GetString(SR.ObjectDataSourceView_SelectNotSupported, _owner.ID)); } if (CanSort) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort); } if (CanPage) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Page); } if (CanRetrieveTotalRowCount) { arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount); } arguments.RaiseUnsupportedCapabilitiesError(this); // Copy the parameters into a case insensitive dictionary IOrderedDictionary mergedParameters = new OrderedDictionary(StringComparer.OrdinalIgnoreCase); IDictionary selectParameters = SelectParameters.GetValues(_context, _owner); foreach (DictionaryEntry de in selectParameters) { mergedParameters[de.Key] = de.Value; } // If caching is enabled, load DataView, DataSet, DataTable, or IEnumerable from cache bool cacheEnabled = _owner.Cache.Enabled; if (cacheEnabled) { object cachedData = _owner.LoadDataFromCache(arguments.StartRowIndex, arguments.MaximumRows); if (cachedData != null) { DataView dataView = cachedData as DataView; if (dataView != null) { if (arguments.RetrieveTotalRowCount && SelectCountMethod.Length == 0) { arguments.TotalRowCount = dataView.Count; } if (FilterExpression.Length > 0) { // Since this type is not valid for filtering, throw if there is a filter throw new NotSupportedException(SR.GetString(SR.ObjectDataSourceView_FilterNotSupported, _owner.ID)); } if (String.IsNullOrEmpty(arguments.SortExpression)) { // If there is no sort expression, we can return the cached DataView // If sorting is requested, we don't use the cached one and we go create a new one (and we'll // most likely get exception, since caching+sorting+DataView=exception). return dataView; } // Fall through as though the data wasn't in cache } else { DataTable dataTable = FilteredDataSetHelper.GetDataTable(_owner, cachedData); if (dataTable != null) { // If we got back a DataTable from cache, return that ProcessPagingData(arguments, mergedParameters); return CreateFilteredDataView(dataTable, arguments.SortExpression, FilterExpression); } else { // If we got back an IEnumerable from cache, return that IEnumerable enumerableReturnValue = CreateEnumerableData(cachedData, arguments); ProcessPagingData(arguments, mergedParameters); return enumerableReturnValue; } } } } // We have to raise the Selecting event early on so that we respect // any changes the user has made to the DataSourceSelectArguments. ObjectDataSourceSelectingEventArgs eventArgs = new ObjectDataSourceSelectingEventArgs(mergedParameters, arguments, false); OnSelecting(eventArgs); if (eventArgs.Cancel) { return null; } // Create a copy of mergedParameters for queryRowCount that doesn't get all the Select, Sort, paging parameters OrderedDictionary queryRowCountParameters = new OrderedDictionary(mergedParameters.Count); foreach (DictionaryEntry entry in mergedParameters) { queryRowCountParameters.Add(entry.Key, entry.Value); } // NOTE: These special parameters are only added here so that they don't show up // for the SelectCount operation. // Add the sort expression as a parameter if necessary string sortParameterName = SortParameterName; if (sortParameterName.Length > 0) { mergedParameters[sortParameterName] = arguments.SortExpression; // We reset the sort expression here so that we pretend as // though we're not really sorting (since the developer is // worrying about it instead of us). arguments.SortExpression = String.Empty; } // Add the paging arguments as parameters if necessary if (EnablePaging) { string maximumRowsParameterName = MaximumRowsParameterName; string startRowIndexParameterName = StartRowIndexParameterName; if (String.IsNullOrEmpty(maximumRowsParameterName) || String.IsNullOrEmpty(startRowIndexParameterName)) { throw new InvalidOperationException(SR.GetString(SR.ObjectDataSourceView_MissingPagingSettings, _owner.ID)); } // Create a new dictionary with the paging information and merge it in (so we get type conversions) IDictionary pagingParameters = new OrderedDictionary(StringComparer.OrdinalIgnoreCase); pagingParameters[maximumRowsParameterName] = arguments.MaximumRows; pagingParameters[startRowIndexParameterName] = arguments.StartRowIndex; MergeDictionaries(SelectParameters, pagingParameters, mergedParameters); } Type type = GetType(TypeName); Debug.Assert(type != null, "Should not have a null type at this point"); object instance = null; ObjectDataSourceResult result = null; try { // Resolve and invoke the method ObjectDataSourceMethod method = GetResolvedMethodData(type, SelectMethod, mergedParameters, DataSourceOperation.Select); result = InvokeMethod(method, false, ref instance); // If the return value is null, there is no more processing to be done if (result.ReturnValue == null) { return null; } // Get the total row count if it is requested if (arguments.RetrieveTotalRowCount && SelectCountMethod.Length > 0) { int cachedTotalRowCount = -1; if (cacheEnabled) { cachedTotalRowCount = _owner.LoadTotalRowCountFromCache(); if (cachedTotalRowCount >= 0) { arguments.TotalRowCount = cachedTotalRowCount; } } if (cachedTotalRowCount < 0) { cachedTotalRowCount = QueryTotalRowCount(queryRowCountParameters, arguments, true, ref instance); arguments.TotalRowCount = cachedTotalRowCount; if (cacheEnabled) { _owner.SaveTotalRowCountToCache(cachedTotalRowCount); } } } } finally { if (instance != null) { ReleaseInstance(instance); } } // Process the return value // Order of precedence: DataView, DataTable, DataSet, IEnumerable, <everything else> { // Check if the return value was a DataView DataView dataView = result.ReturnValue as DataView; if (dataView != null) { if (arguments.RetrieveTotalRowCount && SelectCountMethod.Length == 0) { arguments.TotalRowCount = dataView.Count; } if (FilterExpression.Length > 0) { // Since this type is not valid for filtering, throw if there is a filter throw new NotSupportedException(SR.GetString(SR.ObjectDataSourceView_FilterNotSupported, _owner.ID)); } if (!String.IsNullOrEmpty(arguments.SortExpression)) { if (cacheEnabled) { // Since this type is not valid for caching, throw if caching is enabled throw new NotSupportedException(SR.GetString(SR.ObjectDataSourceView_CacheNotSupportedOnSortedDataView, _owner.ID)); } dataView.Sort = arguments.SortExpression; } // If caching is enabled, save data to cache if (cacheEnabled) { SaveDataAndRowCountToCache(arguments, result.ReturnValue); } return dataView; } else { // Check if the return value was a DataSet or DataTable DataTable dataTable = FilteredDataSetHelper.GetDataTable(_owner, result.ReturnValue); if (dataTable != null) { if (arguments.RetrieveTotalRowCount && SelectCountMethod.Length == 0) { arguments.TotalRowCount = dataTable.Rows.Count; } // If caching is enabled, save data to cache if (cacheEnabled) { SaveDataAndRowCountToCache(arguments, result.ReturnValue); } // If we got a DataTable from the result, create a view with filtering and sorting return CreateFilteredDataView(dataTable, arguments.SortExpression, FilterExpression); } else { // CreateEnumerableData will get an appropriate IEnumerable from the data, and also // validate that filtering and sorting are not used. IEnumerable enumerableReturnValue = CreateEnumerableData(result.ReturnValue, arguments); // If caching is enabled, save data to cache if (cacheEnabled) { if (enumerableReturnValue is IDataReader) { // IDataReader is specifically not supported with caching since the contract // is that they are forward-only (i.e. not resettable). throw new NotSupportedException(SR.GetString(SR.ObjectDataSourceView_CacheNotSupportedOnIDataReader, _owner.ID)); } SaveDataAndRowCountToCache(arguments, enumerableReturnValue); } return enumerableReturnValue; } } } }
protected internal override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments) { SqlCacheDependency dependency; if (this.SelectCommand.Length == 0) { return null; } DbConnection connection = this._owner.CreateConnection(this._owner.ConnectionString); if (connection == null) { throw new InvalidOperationException(System.Web.SR.GetString("SqlDataSourceView_CouldNotCreateConnection", new object[] { this._owner.ID })); } DataSourceCache cache = this._owner.Cache; bool flag = (cache != null) && cache.Enabled; string sortExpression = arguments.SortExpression; if (this.CanPage) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Page); } if (this.CanSort) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort); } if (this.CanRetrieveTotalRowCount) { arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount); } if (flag) { if (this._owner.DataSourceMode != SqlDataSourceMode.DataSet) { throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_CacheNotSupported", new object[] { this._owner.ID })); } arguments.RaiseUnsupportedCapabilitiesError(this); DataSet set = this._owner.LoadDataFromCache(0, -1) as DataSet; if (set != null) { IOrderedDictionary parameterValues = this.FilterParameters.GetValues(this._context, this._owner); if (this.FilterExpression.Length > 0) { SqlDataSourceFilteringEventArgs args = new SqlDataSourceFilteringEventArgs(parameterValues); this.OnFiltering(args); if (args.Cancel) { return null; } } return FilteredDataSetHelper.CreateFilteredDataView(set.Tables[0], sortExpression, this.FilterExpression, parameterValues); } } DbCommand command = this._owner.CreateCommand(this.SelectCommand, connection); this.InitializeParameters(command, this.SelectParameters, null); command.CommandType = GetCommandType(this.SelectCommandType); SqlDataSourceSelectingEventArgs e = new SqlDataSourceSelectingEventArgs(command, arguments); this.OnSelecting(e); if (e.Cancel) { return null; } string sortParameterName = this.SortParameterName; if (sortParameterName.Length > 0) { if (command.CommandType != CommandType.StoredProcedure) { throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_SortParameterRequiresStoredProcedure", new object[] { this._owner.ID })); } command.Parameters.Add(this._owner.CreateParameter(this.ParameterPrefix + sortParameterName, sortExpression)); arguments.SortExpression = string.Empty; } arguments.RaiseUnsupportedCapabilitiesError(this); sortExpression = arguments.SortExpression; if (this.CancelSelectOnNullParameter) { int count = command.Parameters.Count; for (int i = 0; i < count; i++) { DbParameter parameter = command.Parameters[i]; if (((parameter != null) && (parameter.Value == null)) && ((parameter.Direction == ParameterDirection.Input) || (parameter.Direction == ParameterDirection.InputOutput))) { return null; } } } this.ReplaceNullValues(command); IEnumerable enumerable = null; switch (this._owner.DataSourceMode) { case SqlDataSourceMode.DataReader: { if (this.FilterExpression.Length > 0) { throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_FilterNotSupported", new object[] { this._owner.ID })); } if (sortExpression.Length > 0) { throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_SortNotSupported", new object[] { this._owner.ID })); } bool flag4 = false; try { if (connection.State != ConnectionState.Open) { connection.Open(); } enumerable = command.ExecuteReader(CommandBehavior.CloseConnection); flag4 = true; SqlDataSourceStatusEventArgs args6 = new SqlDataSourceStatusEventArgs(command, 0, null); this.OnSelected(args6); } catch (Exception exception2) { bool flag5; if (!flag4) { SqlDataSourceStatusEventArgs args7 = new SqlDataSourceStatusEventArgs(command, 0, exception2); this.OnSelected(args7); if (!args7.ExceptionHandled) { throw; } return enumerable; } exception2 = this.BuildCustomException(exception2, DataSourceOperation.Select, command, out flag5); if (flag5) { throw exception2; } throw; } return enumerable; } case SqlDataSourceMode.DataSet: dependency = null; if (flag && (cache is SqlDataSourceCache)) { SqlDataSourceCache cache2 = (SqlDataSourceCache) cache; if (string.Equals(cache2.SqlCacheDependency, "CommandNotification", StringComparison.OrdinalIgnoreCase)) { if (!(command is SqlCommand)) { throw new InvalidOperationException(System.Web.SR.GetString("SqlDataSourceView_CommandNotificationNotSupported", new object[] { this._owner.ID })); } dependency = new SqlCacheDependency((SqlCommand) command); break; } } break; default: return enumerable; } DbDataAdapter adapter = this._owner.CreateDataAdapter(command); DataSet dataSet = new DataSet(); int affectedRows = 0; bool flag2 = false; try { affectedRows = adapter.Fill(dataSet, base.Name); flag2 = true; SqlDataSourceStatusEventArgs args3 = new SqlDataSourceStatusEventArgs(command, affectedRows, null); this.OnSelected(args3); } catch (Exception exception) { if (flag2) { bool flag3; exception = this.BuildCustomException(exception, DataSourceOperation.Select, command, out flag3); if (flag3) { throw exception; } throw; } SqlDataSourceStatusEventArgs args4 = new SqlDataSourceStatusEventArgs(command, affectedRows, exception); this.OnSelected(args4); if (!args4.ExceptionHandled) { throw; } } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } DataTable table = (dataSet.Tables.Count > 0) ? dataSet.Tables[0] : null; if (flag && (table != null)) { this._owner.SaveDataToCache(0, -1, dataSet, dependency); } if (table == null) { return enumerable; } IOrderedDictionary values = this.FilterParameters.GetValues(this._context, this._owner); if (this.FilterExpression.Length > 0) { SqlDataSourceFilteringEventArgs args5 = new SqlDataSourceFilteringEventArgs(values); this.OnFiltering(args5); if (args5.Cancel) { return null; } } return FilteredDataSetHelper.CreateFilteredDataView(table, sortExpression, this.FilterExpression, values); }
protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs) { if (CanSort) { selectArgs.AddSupportedCapabilities(DataSourceCapabilities.Sort); } DataView view = View; if (view != null && CanSort && !string.IsNullOrEmpty(selectArgs.SortExpression)) { view.Sort = selectArgs.SortExpression; } return view; }
protected internal override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments) { if (this.SelectMethod.Length == 0) { throw new InvalidOperationException(System.Web.SR.GetString("ObjectDataSourceView_SelectNotSupported", new object[] { this._owner.ID })); } if (this.CanSort) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort); } if (this.CanPage) { arguments.AddSupportedCapabilities(DataSourceCapabilities.Page); } if (this.CanRetrieveTotalRowCount) { arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount); } arguments.RaiseUnsupportedCapabilitiesError(this); IOrderedDictionary parameters = new OrderedDictionary(StringComparer.OrdinalIgnoreCase); foreach (DictionaryEntry entry in this.SelectParameters.GetValues(this._context, this._owner)) { parameters[entry.Key] = entry.Value; } bool enabled = this._owner.Cache.Enabled; if (enabled) { object dataObject = this._owner.LoadDataFromCache(arguments.StartRowIndex, arguments.MaximumRows); if (dataObject != null) { DataView view = dataObject as DataView; if (view != null) { if (arguments.RetrieveTotalRowCount && (this.SelectCountMethod.Length == 0)) { arguments.TotalRowCount = view.Count; } if (this.FilterExpression.Length > 0) { throw new NotSupportedException(System.Web.SR.GetString("ObjectDataSourceView_FilterNotSupported", new object[] { this._owner.ID })); } if (string.IsNullOrEmpty(arguments.SortExpression)) { return view; } } else { DataTable table = FilteredDataSetHelper.GetDataTable(this._owner, dataObject); if (table != null) { this.ProcessPagingData(arguments, parameters); return this.CreateFilteredDataView(table, arguments.SortExpression, this.FilterExpression); } IEnumerable enumerable = this.CreateEnumerableData(dataObject, arguments); this.ProcessPagingData(arguments, parameters); return enumerable; } } } ObjectDataSourceSelectingEventArgs e = new ObjectDataSourceSelectingEventArgs(parameters, arguments, false); this.OnSelecting(e); if (e.Cancel) { return null; } OrderedDictionary mergedParameters = new OrderedDictionary(parameters.Count); foreach (DictionaryEntry entry2 in parameters) { mergedParameters.Add(entry2.Key, entry2.Value); } string sortParameterName = this.SortParameterName; if (sortParameterName.Length > 0) { parameters[sortParameterName] = arguments.SortExpression; arguments.SortExpression = string.Empty; } if (this.EnablePaging) { string maximumRowsParameterName = this.MaximumRowsParameterName; string startRowIndexParameterName = this.StartRowIndexParameterName; if (string.IsNullOrEmpty(maximumRowsParameterName) || string.IsNullOrEmpty(startRowIndexParameterName)) { throw new InvalidOperationException(System.Web.SR.GetString("ObjectDataSourceView_MissingPagingSettings", new object[] { this._owner.ID })); } IDictionary source = new OrderedDictionary(StringComparer.OrdinalIgnoreCase); source[maximumRowsParameterName] = arguments.MaximumRows; source[startRowIndexParameterName] = arguments.StartRowIndex; MergeDictionaries(this.SelectParameters, source, parameters); } Type type = this.GetType(this.TypeName); object instance = null; ObjectDataSourceResult result = null; try { ObjectDataSourceMethod method = this.GetResolvedMethodData(type, this.SelectMethod, parameters, DataSourceOperation.Select); result = this.InvokeMethod(method, false, ref instance); if (result.ReturnValue == null) { return null; } if (arguments.RetrieveTotalRowCount && (this.SelectCountMethod.Length > 0)) { int totalRowCount = -1; if (enabled) { totalRowCount = this._owner.LoadTotalRowCountFromCache(); if (totalRowCount >= 0) { arguments.TotalRowCount = totalRowCount; } } if (totalRowCount < 0) { totalRowCount = this.QueryTotalRowCount(mergedParameters, arguments, true, ref instance); arguments.TotalRowCount = totalRowCount; if (enabled) { this._owner.SaveTotalRowCountToCache(totalRowCount); } } } } finally { if (instance != null) { this.ReleaseInstance(instance); } } DataView returnValue = result.ReturnValue as DataView; if (returnValue != null) { if (arguments.RetrieveTotalRowCount && (this.SelectCountMethod.Length == 0)) { arguments.TotalRowCount = returnValue.Count; } if (this.FilterExpression.Length > 0) { throw new NotSupportedException(System.Web.SR.GetString("ObjectDataSourceView_FilterNotSupported", new object[] { this._owner.ID })); } if (!string.IsNullOrEmpty(arguments.SortExpression)) { if (enabled) { throw new NotSupportedException(System.Web.SR.GetString("ObjectDataSourceView_CacheNotSupportedOnSortedDataView", new object[] { this._owner.ID })); } returnValue.Sort = arguments.SortExpression; } if (enabled) { this.SaveDataAndRowCountToCache(arguments, result.ReturnValue); } return returnValue; } DataTable dataTable = FilteredDataSetHelper.GetDataTable(this._owner, result.ReturnValue); if (dataTable != null) { if (arguments.RetrieveTotalRowCount && (this.SelectCountMethod.Length == 0)) { arguments.TotalRowCount = dataTable.Rows.Count; } if (enabled) { this.SaveDataAndRowCountToCache(arguments, result.ReturnValue); } return this.CreateFilteredDataView(dataTable, arguments.SortExpression, this.FilterExpression); } IEnumerable data = this.CreateEnumerableData(result.ReturnValue, arguments); if (enabled) { if (data is IDataReader) { throw new NotSupportedException(System.Web.SR.GetString("ObjectDataSourceView_CacheNotSupportedOnIDataReader", new object[] { this._owner.ID })); } this.SaveDataAndRowCountToCache(arguments, data); } return data; }