EntityDataSourceQueryBuilder.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataWebControls / System / Data / WebControls / EntityDataSourceQueryBuilder.cs / 1 / 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;
 
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 Build(ObjectContext context, bool computeCount)
        { 
            ObjectQuery queryT = QueryBuilderUtils.ConstructQuery(context, _entitySetQueryExpression, _commandText, _commandParameters); 
            queryT = ApplyWhere(queryT);
            queryT = ApplySelect(queryT); //Select and/or GroupBy application 

            if (computeCount)
            {
                _count = queryT.Count(); 
            }
            _resultType = queryT.GetResultType(); 
 
            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); 

        private ObjectQuery ApplyOrderByAndPaging(ObjectQuery queryT) 
        { 
            string orderByClause;
            ObjectParameter[] orderByParameters; 
            _orderByBuilder.Generate(_resultType, out orderByClause, out orderByParameters);

            if ((_arguments.MaximumRows > 0) && (_arguments.StartRowIndex >= 0))
            { 
                if (String.IsNullOrEmpty(orderByClause))
                { 
                    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 (!string.IsNullOrEmpty(orderByClause))
            { 
                queryT = queryT.OrderBy(orderByClause, orderByParameters);
            } 
            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 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.
//---------------------------------------------------------------------- 
// 
//      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;
 
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 Build(ObjectContext context, bool computeCount)
        { 
            ObjectQuery queryT = QueryBuilderUtils.ConstructQuery(context, _entitySetQueryExpression, _commandText, _commandParameters); 
            queryT = ApplyWhere(queryT);
            queryT = ApplySelect(queryT); //Select and/or GroupBy application 

            if (computeCount)
            {
                _count = queryT.Count(); 
            }
            _resultType = queryT.GetResultType(); 
 
            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); 

        private ObjectQuery ApplyOrderByAndPaging(ObjectQuery queryT) 
        { 
            string orderByClause;
            ObjectParameter[] orderByParameters; 
            _orderByBuilder.Generate(_resultType, out orderByClause, out orderByParameters);

            if ((_arguments.MaximumRows > 0) && (_arguments.StartRowIndex >= 0))
            { 
                if (String.IsNullOrEmpty(orderByClause))
                { 
                    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 (!string.IsNullOrEmpty(orderByClause))
            { 
                queryT = queryT.OrderBy(orderByClause, orderByParameters);
            } 
            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 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

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK