private SqlProvider VisitPagingNative(UnaryProvider provider, Func <ParameterContext, int> take, Func <ParameterContext, int> skip) { var compiledSource = Compile(provider.Source); var query = ExtractSqlSelect(provider, compiledSource); var bindings = new List <QueryParameterBinding>(); if (take != null) {
private SqlProvider VisitPagingNative(UnaryProvider provider, Func <int> take, Func <int> skip) { var compiledSource = Compile(provider.Source); var query = ExtractSqlSelect(provider, compiledSource); var bindings = new List <QueryParameterBinding>(); if (take != null) { // Some servers (e.g. SQL Server 2012) don't like Take(0) // We work around it with special hacks: // Limit argument is replaced with 1 // and false condition is added to "where" part. var applyZeroLimitHack = providerInfo.Supports(ProviderFeatures.ZeroLimitIsError); var takeBinding = CreateLimitOffsetParameterBinding(take, applyZeroLimitHack); bindings.Add(takeBinding); if (applyZeroLimitHack) { query.Where &= SqlDml.Variant(takeBinding, SqlDml.Literal(1), SqlDml.Literal(0)) != SqlDml.Literal(0); } query.Limit = takeBinding.ParameterReference; } if (skip != null) { var skipBinding = CreateLimitOffsetParameterBinding(skip); bindings.Add(skipBinding); query.Offset = skipBinding.ParameterReference; } query.OrderBy.Clear(); var columnExpressions = ExtractColumnExpressions(query); foreach (KeyValuePair <int, Direction> pair in provider.Source.Header.Order) { query.OrderBy.Add(columnExpressions[pair.Key], pair.Value == Direction.Positive); } return(CreateProvider(query, bindings, provider, compiledSource)); }