Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raul Chavez this change is for mave virtual scroll or infinite scroll #566

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions SqlKata.Execution/Query.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ public static async Task<PaginationResult<dynamic>> PaginateAsync(this Query que
return await PaginateAsync<dynamic>(query, page, perPage, transaction, timeout, cancellationToken);
}

public static ScrollToResult<T> ScrollTo<T>(this Query query, int page, int perPage = 25, IDbTransaction transaction = null, int? timeout = null)
{
var db = CreateQueryFactory(query);

return db.ScrollTo<T>(query, page, perPage, transaction, timeout);
}

public static async Task<ScrollToResult<T>> ScrollToAsync<T>(this Query query, int page, int perPage = 25, IDbTransaction transaction = null, int? timeout = null, CancellationToken cancellationToken = default)
{
var db = CreateQueryFactory(query);

return await db.ScrollToAsync<T>(query, page, perPage, transaction, timeout, cancellationToken);
}

public static ScrollToResult<dynamic> ScrollTo(this Query query, int page, int perPage = 25, IDbTransaction transaction = null, int? timeout = null)
{
return query.ScrollTo<dynamic>(page, perPage, transaction, timeout);
}

public static async Task<ScrollToResult<dynamic>> ScrollToAsync(this Query query, int page, int perPage = 25, IDbTransaction transaction = null, int? timeout = null, CancellationToken cancellationToken = default)
{
return await ScrollToAsync<dynamic>(query, page, perPage, transaction, timeout, cancellationToken);
}

public static void Chunk<T>(this Query query, int chunkSize, Func<IEnumerable<T>, int, bool> func, IDbTransaction transaction = null, int? timeout = null)
{
var db = CreateQueryFactory(query);
Expand Down
80 changes: 75 additions & 5 deletions SqlKata.Execution/QueryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public SqlMapper.GridReader GetMultiple<T>(
public async Task<SqlMapper.GridReader> GetMultipleAsync<T>(
Query[] queries,
IDbTransaction transaction = null,
int? timeout = null,
int? timeout = null,
CancellationToken cancellationToken = default)
{
var compiled = this.Compiler.Compile(queries);
Expand Down Expand Up @@ -324,7 +324,7 @@ public IEnumerable<IEnumerable<T>> Get<T>(
public async Task<IEnumerable<IEnumerable<T>>> GetAsync<T>(
Query[] queries,
IDbTransaction transaction = null,
int? timeout = null,
int? timeout = null,
CancellationToken cancellationToken = default
)
{
Expand Down Expand Up @@ -388,7 +388,7 @@ public async Task<T> AggregateAsync<T>(
string aggregateOperation,
string[] columns = null,
IDbTransaction transaction = null,
int? timeout = null,
int? timeout = null,
CancellationToken cancellationToken = default
)
{
Expand Down Expand Up @@ -524,6 +524,76 @@ public async Task<PaginationResult<T>> PaginateAsync<T>(Query query, int page, i
};
}

public ScrollToResult<T> ScrollTo<T>(Query query, int skip, int take = 25, IDbTransaction transaction = null, int? timeout = null)
{
if (skip < 0)
{
throw new ArgumentException("Skip param should be greater than or equal to 0", nameof(skip));
}

if (take < 1)
{
throw new ArgumentException("PerPage param should be greater than or equal to 1", nameof(take));
}

var count = Count<long>(query.Clone(), null, transaction, timeout);

IEnumerable<T> list;

if (count > 0)
{
list = Get<T>(query.Clone().Skip(skip).Take(take), transaction, timeout);
}
else
{
list = Enumerable.Empty<T>();
}

return new ScrollToResult<T>
{
Query = query,
Skip = skip,
Take = take,
Count = count,
List = list
};
}

public async Task<ScrollToResult<T>> ScrollToAsync<T>(Query query, int skip, int take = 25, IDbTransaction transaction = null, int? timeout = null, CancellationToken cancellationToken = default)
{
if (skip < 1)
{
throw new ArgumentException("Page param should be greater than or equal to 1", nameof(skip));
}

if (take < 1)
{
throw new ArgumentException("PerPage param should be greater than or equal to 1", nameof(take));
}

var count = await CountAsync<long>(query.Clone(), null, transaction, timeout, cancellationToken);

IEnumerable<T> list;

if (count > 0)
{
list = await GetAsync<T>(query.Clone().Skip(skip).Take(take), transaction, timeout, cancellationToken);
}
else
{
list = Enumerable.Empty<T>();
}

return new ScrollToResult<T>
{
Query = query,
Skip = skip,
Take = take,
Count = count,
List = list
};
}

public void Chunk<T>(
Query query,
int chunkSize,
Expand Down Expand Up @@ -553,7 +623,7 @@ public async Task ChunkAsync<T>(
int chunkSize,
Func<IEnumerable<T>, int, bool> func,
IDbTransaction transaction = null,
int? timeout = null,
int? timeout = null,
CancellationToken cancellationToken = default
)
{
Expand Down Expand Up @@ -592,7 +662,7 @@ public async Task ChunkAsync<T>(
int chunkSize,
Action<IEnumerable<T>, int> action,
IDbTransaction transaction = null,
int? timeout = null,
int? timeout = null,
CancellationToken cancellationToken = default
)
{
Expand Down
13 changes: 13 additions & 0 deletions SqlKata.Execution/ScrollToResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace SqlKata.Execution
{
public class ScrollToResult<T>
{
public Query Query { get; set; }
public long Count { get; set; }
public IEnumerable<T> List { get; set; }
public int Skip { get; set; }
public int Take { get; set; }
}
}