Пример #1
0
        public ICommandBuilder Build(SimpleQuery query)
        {
            var commandBuilder = new CommandBuilder(GetSelectClause(ObjectName.Parse(query.TableName)), _schema.SchemaProvider);

            if (query.Criteria != null)
            {
                commandBuilder.Append(" WHERE " + new ExpressionFormatter(commandBuilder, _schema).Format(query.Criteria));
            }

            if (query.Order != null)
            {
                var orderNames = query.Order.Select(q => q.Reference.GetName() + (q.Direction == OrderByDirection.Descending ? " DESC" : string.Empty));
                commandBuilder.Append(" ORDER BY " + string.Join(", ", orderNames));
            }

            if (query.SkipCount != null || query.TakeCount != null)
            {
                var queryPager = _adoAdapter.ProviderHelper.GetCustomProvider <IQueryPager>(_adoAdapter.ConnectionProvider);
                if (queryPager == null)
                {
                    throw new NotSupportedException("Paging is not supported by the current ADO provider.");
                }

                var skipTemplate = commandBuilder.AddParameter("skip", DbType.Int32, query.SkipCount ?? 0);
                var takeTemplate = commandBuilder.AddParameter("take", DbType.Int32, query.TakeCount ?? int.MaxValue);
                commandBuilder.SetText(queryPager.ApplyPaging(commandBuilder.Text, skipTemplate.Name, takeTemplate.Name));
            }

            return(commandBuilder);
        }
Пример #2
0
        public ICommandBuilder Build(SimpleQuery query)
        {
            var commandBuilder = new CommandBuilder(GetSelectClause(ObjectName.Parse(query.TableName)), _schema.SchemaProvider);

            if (query.Criteria != null)
            {
                commandBuilder.Append(" WHERE " + new ExpressionFormatter(commandBuilder, _schema).Format(query.Criteria));
            }

            if (query.Order != null)
            {
                var orderNames = query.Order.Select(q => q.Reference.GetName() + (q.Direction == OrderByDirection.Descending ? " DESC" : string.Empty));
                commandBuilder.Append(" ORDER BY " + string.Join(", ", orderNames));
            }

            if (query.SkipCount != null || query.TakeCount != null)
            {
                var queryPager = _adoAdapter.ProviderHelper.GetCustomProvider<IQueryPager>(_adoAdapter.ConnectionProvider);
                if (queryPager == null)
                {
                    throw new NotSupportedException("Paging is not supported by the current ADO provider.");
                }

                var skipTemplate = commandBuilder.AddParameter("skip", DbType.Int32, query.SkipCount ?? 0);
                var takeTemplate = commandBuilder.AddParameter("take", DbType.Int32, query.TakeCount ?? int.MaxValue);
                commandBuilder.SetText(queryPager.ApplyPaging(commandBuilder.Text, skipTemplate.Name, takeTemplate.Name));
            }

            return commandBuilder;
        }
Пример #3
0
        private void HandlePaging()
        {
            const int maxInt = 2147483646;

            var skipClause = _query.Clauses.OfType <SkipClause>().FirstOrDefault() ?? new SkipClause(0);
            var takeClause = _query.Clauses.OfType <TakeClause>().FirstOrDefault() ?? new TakeClause(maxInt);

            if (skipClause.Count != 0 || takeClause.Count != maxInt)
            {
                var queryPager = _adoAdapter.ProviderHelper.GetCustomProvider <IQueryPager>(_adoAdapter.ConnectionProvider);
                if (queryPager == null)
                {
                    _unhandledClauses.AddRange(_query.OfType <SkipClause>());
                    _unhandledClauses.AddRange(_query.OfType <TakeClause>());
                }

                var skipTemplate = _commandBuilder.AddParameter("skip", DbType.Int32, skipClause.Count);
                var takeTemplate = _commandBuilder.AddParameter("take", DbType.Int32, takeClause.Count);
                _commandBuilder.SetText(queryPager.ApplyPaging(_commandBuilder.Text, skipTemplate.Name, takeTemplate.Name));
            }
        }