Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataWebControls / System / Data / WebControls / EntityDataSourceQueryBuilder.cs / 1305376 / EntityDataSourceQueryBuilder.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System; using System.Data; using System.Data.Common; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.ComponentModel; using System.Data.Entity; using System.Data.Metadata.Edm; using System.Data.Objects; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Text; using System.Web.DynamicData; using System.Web.UI; using System.Linq.Expressions; namespace System.Web.UI.WebControls { internal abstract class EntityDataSourceQueryBuilder{ private readonly DataSourceSelectArguments _arguments; private readonly string _commandText; private readonly ObjectParameter[] _commandParameters; private readonly string _whereExpression; private readonly ObjectParameter[] _whereParameters; private readonly string _entitySetQueryExpression; private readonly OrderByBuilder _orderByBuilder; private string _includePaths; private TypeUsage _resultType; private Nullable _count; protected EntityDataSourceQueryBuilder(DataSourceSelectArguments arguments, string commandText, ObjectParameter[] commandParameters, string whereExpression, ObjectParameter[] whereParameters, string entitySetQueryExpression, string selectExpression, string groupByExpression, ObjectParameter[] selectParameters, OrderByBuilder orderByBuilder, string includePaths) { _arguments = arguments; _commandText = commandText; _commandParameters = commandParameters; _whereExpression = whereExpression; _whereParameters = whereParameters; _entitySetQueryExpression = entitySetQueryExpression; _orderByBuilder = orderByBuilder; _includePaths = includePaths; } internal delegate EntityDataSourceQueryBuilder Creator(DataSourceSelectArguments arguments, string commandText, ObjectParameter[] commandParameters, string whereExpression, ObjectParameter[] whereParameters, string entitySetQueryExpression, string selectExpression, string groupByExpression, ObjectParameter[] selectParameters, OrderByBuilder orderByBuilder, string includePaths); internal TypeUsage ResultType { get { Debug.Assert(_resultType != null, "ResultType is only valid after Build()"); return _resultType; } } internal int TotalCount { get { Debug.Assert(_count.HasValue, "Count is not valid until after Build. And only then if computeCount is true"); return _count.Value; } } internal IEnumerable Execute(ObjectQuery queryT) { return (IEnumerable)(((IListSource)(queryT)).GetList()); } internal ObjectQuery BuildBasicQuery(ObjectContext context, bool computeCount) { ObjectQuery queryT = QueryBuilderUtils.ConstructQuery (context, _entitySetQueryExpression, _commandText, _commandParameters); queryT = ApplyWhere(queryT); queryT = ApplySelect(queryT); // Select and/or GroupBy application _resultType = queryT.GetResultType(); return queryT; } internal ObjectQuery CompleteBuild(ObjectQuery queryT, ObjectContext context, bool computeCount, bool wasExtended) { if (computeCount) { _count = queryT.Count(); } queryT = wasExtended ? ApplyQueryableOrderByAndPaging(queryT) : ApplyOrderByAndPaging(queryT); queryT = ApplyIncludePaths(queryT); return queryT; } private ObjectQuery ApplyWhere(ObjectQuery queryT) { if (!String.IsNullOrEmpty(_whereExpression)) { queryT = queryT.Where(_whereExpression, _whereParameters); } return queryT; } protected abstract ObjectQuery ApplySelect(ObjectQuery queryT); internal ObjectQuery ApplyOrderBy(ObjectQuery queryT) { string orderByClause; ObjectParameter[] orderByParameters; // Apply all possible ordering except the sort expression, because it might only be valid after the query has been extended _orderByBuilder.Generate(_resultType, out orderByClause, out orderByParameters, false /*applySortExpression*/); return String.IsNullOrEmpty(orderByClause) ? queryT : queryT.OrderBy(orderByClause, orderByParameters); } private ObjectQuery ApplyOrderByAndPaging(ObjectQuery queryT) { // This re-applys the order-by as part of the skip string orderByClause; ObjectParameter[] orderByParameters; _orderByBuilder.Generate(_resultType, out orderByClause, out orderByParameters, true /*applySortExpression*/); bool paging = _arguments.MaximumRows > 0 && _arguments.StartRowIndex >= 0; var hasOrderByClause = !String.IsNullOrEmpty(orderByClause); if (paging) { if (!hasOrderByClause) { throw new InvalidOperationException(Strings.EntityDataSourceQueryBuilder_PagingRequiresOrderBy); } queryT = queryT.Skip(orderByClause, _arguments.StartRowIndex.ToString(CultureInfo.InvariantCulture), orderByParameters).Top(_arguments.MaximumRows.ToString(CultureInfo.InvariantCulture), QueryBuilderUtils.EmptyObjectParameters); } else { if (hasOrderByClause) { queryT = queryT.OrderBy(orderByClause, orderByParameters); } } return queryT; } private ObjectQuery ApplyQueryableOrderByAndPaging(ObjectQuery queryT) { queryT = _orderByBuilder.BuildQueryableOrderBy(queryT) as ObjectQuery ; bool paging = _arguments.MaximumRows > 0 && _arguments.StartRowIndex >= 0; if (paging) { queryT = queryT.Skip(_arguments.StartRowIndex).Take(_arguments.MaximumRows) as ObjectQuery ; } return queryT; } private ObjectQuery ApplyIncludePaths(ObjectQuery objectQuery) { if (!string.IsNullOrEmpty(_includePaths)) { foreach (string include in _includePaths.Split(',')) { string trimmedInclude = include.Trim(); if (!string.IsNullOrEmpty(trimmedInclude)) { objectQuery = objectQuery.Include(trimmedInclude); } } } return objectQuery; } } internal class EntityDataSourceObjectQueryBuilder : EntityDataSourceQueryBuilder { private EntityDataSourceObjectQueryBuilder(DataSourceSelectArguments arguments, string commandText, ObjectParameter[] commandParameters, string whereExpression, ObjectParameter[] whereParameters, string entitySetQueryExpression, string selectExpression, string groupByExpression, ObjectParameter[] selectParameters, OrderByBuilder orderByBuilder, string includePaths) : base(arguments, commandText, commandParameters, whereExpression, whereParameters, entitySetQueryExpression, selectExpression, groupByExpression, selectParameters, orderByBuilder, includePaths) { } static internal EntityDataSourceQueryBuilder .Creator GetCreator() { return Create; } static internal EntityDataSourceQueryBuilder Create(DataSourceSelectArguments arguments, string commandText, ObjectParameter[] commandParameters, string whereExpression, ObjectParameter[] whereParameters, string entitySetQueryExpression, string selectExpression, string groupByExpression, ObjectParameter[] selectParameters, OrderByBuilder orderByBuilder, string includePaths) { return new EntityDataSourceObjectQueryBuilder (arguments, commandText, commandParameters, whereExpression, whereParameters, entitySetQueryExpression, selectExpression, groupByExpression, selectParameters, orderByBuilder, includePaths); } protected override ObjectQuery ApplySelect(ObjectQuery queryT) { return queryT; } } internal class EntityDataSourceRecordQueryBuilder : EntityDataSourceQueryBuilder { private readonly string _selectExpression; private readonly string _groupByExpression; private readonly ObjectParameter[] _selectParameters; private EntityDataSourceRecordQueryBuilder(DataSourceSelectArguments arguments, string commandText, ObjectParameter[] commandParameters, string whereExpression, ObjectParameter[] whereParameters, string entitySetQueryExpression, string selectExpression, string groupByExpression, ObjectParameter[] selectParameters, OrderByBuilder orderByBuilder, string includePaths) : base(arguments, commandText, commandParameters, whereExpression, whereParameters, entitySetQueryExpression, selectExpression, groupByExpression, selectParameters, orderByBuilder, includePaths) { _selectExpression = selectExpression; _groupByExpression = groupByExpression; _selectParameters = selectParameters; } static internal EntityDataSourceQueryBuilder Create(DataSourceSelectArguments arguments, string commandText, ObjectParameter[] commandParameters, string whereExpression, ObjectParameter[] whereParameters, string entitySetQueryExpression, string selectExpression, string groupByExpression, ObjectParameter[] selectParameters, OrderByBuilder orderByBuilder, string includePaths) { return new EntityDataSourceRecordQueryBuilder(arguments, commandText, commandParameters, whereExpression, whereParameters, entitySetQueryExpression, selectExpression, groupByExpression, selectParameters, orderByBuilder, includePaths); } protected override ObjectQuery ApplySelect(ObjectQuery queryT) { Debug.Assert(!String.IsNullOrEmpty(_selectExpression), "Select expression should not be of zero length."); if (!string.IsNullOrEmpty(_groupByExpression)) { queryT = queryT.GroupBy(_groupByExpression, _selectExpression, _selectParameters); } else { queryT = queryT.Select(_selectExpression, _selectParameters); } return queryT; } } internal static class QueryBuilderUtils { internal static readonly ObjectParameter[] EmptyObjectParameters = new ObjectParameter[] { }; internal static ObjectQuery ConstructQuery (ObjectContext context, string entitySetQueryExpression, string commandText, ObjectParameter[] commandParameters) { string queryExpression; ObjectParameter[] queryParameters; if (!string.IsNullOrEmpty(commandText)) { queryExpression = commandText; queryParameters = commandParameters; } else { queryExpression = entitySetQueryExpression; queryParameters = QueryBuilderUtils.EmptyObjectParameters; } return context.CreateQuery (queryExpression, queryParameters); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- HtmlMobileTextWriter.cs
- RuleSettings.cs
- HttpException.cs
- ActivityInfo.cs
- RegisteredHiddenField.cs
- TreeNodeBindingCollection.cs
- listitem.cs
- SapiRecoContext.cs
- Monitor.cs
- CounterCreationData.cs
- SimpleTableProvider.cs
- Accessible.cs
- X509Extension.cs
- AttachedPropertiesService.cs
- SafeSecurityHandles.cs
- StatusBarItem.cs
- XPathDocumentIterator.cs
- InfoCardXmlSerializer.cs
- FileDialogCustomPlaces.cs
- ViewGenerator.cs
- SHA1.cs
- UpdateRecord.cs
- BaseResourcesBuildProvider.cs
- DataViewSetting.cs
- TabControlDesigner.cs
- TimeEnumHelper.cs
- AddInStore.cs
- CellTreeNodeVisitors.cs
- AssociationSetEnd.cs
- ComponentManagerBroker.cs
- HttpModuleActionCollection.cs
- TextDpi.cs
- TextServicesLoader.cs
- QuotedPairReader.cs
- XmlILModule.cs
- log.cs
- NestPullup.cs
- RequestValidator.cs
- serverconfig.cs
- InputQueueChannel.cs
- ClientConvert.cs
- StreamAsIStream.cs
- WebRequestModulesSection.cs
- MenuStrip.cs
- PropertyToken.cs
- SkinBuilder.cs
- EventWaitHandle.cs
- SqlUnionizer.cs
- XmlDomTextWriter.cs
- ReadOnlyHierarchicalDataSourceView.cs
- Timer.cs
- ColorMatrix.cs
- Token.cs
- ResXResourceReader.cs
- MobileErrorInfo.cs
- PasswordTextNavigator.cs
- GridViewPageEventArgs.cs
- QuotedPrintableStream.cs
- ListViewItemCollectionEditor.cs
- connectionpool.cs
- JpegBitmapEncoder.cs
- Span.cs
- SettingsProperty.cs
- EntitySqlQueryBuilder.cs
- EntityTypeEmitter.cs
- ResetableIterator.cs
- CompositeScriptReference.cs
- XmlWellformedWriter.cs
- RegexReplacement.cs
- PlainXmlDeserializer.cs
- AssociationSetMetadata.cs
- ExpressionNormalizer.cs
- ImagingCache.cs
- AccessKeyManager.cs
- MethodBuilderInstantiation.cs
- PartialCachingControl.cs
- WebZone.cs
- SafeThemeHandle.cs
- FontWeightConverter.cs
- DbMetaDataColumnNames.cs
- SetStoryboardSpeedRatio.cs
- ClonableStack.cs
- EventLogPermissionAttribute.cs
- RegexCompilationInfo.cs
- OrderByExpression.cs
- DefaultBinder.cs
- DataStorage.cs
- MailSettingsSection.cs
- WebPartConnectionsEventArgs.cs
- SessionSwitchEventArgs.cs
- GroupBoxDesigner.cs
- DataTransferEventArgs.cs
- StyleModeStack.cs
- InstanceOwner.cs
- Error.cs
- TileBrush.cs
- HttpRequestCacheValidator.cs
- Baml6ConstructorInfo.cs
- SctClaimSerializer.cs
- FixedTextContainer.cs