From f157c56e86d66817e34c0d0acc44b6d69affe29c Mon Sep 17 00:00:00 2001 From: rchavz Date: Fri, 8 Apr 2022 23:39:54 -0500 Subject: [PATCH 1/2] Raul Chavez this change is for mave virtual scroll or infinite scroll --- SqlKata.Execution/QueryFactory.cs | 80 +++++++++++++++++++++++++++-- SqlKata.Execution/ScrollToResult.cs | 13 +++++ 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 SqlKata.Execution/ScrollToResult.cs diff --git a/SqlKata.Execution/QueryFactory.cs b/SqlKata.Execution/QueryFactory.cs index 5ff399aa..fe2d435c 100644 --- a/SqlKata.Execution/QueryFactory.cs +++ b/SqlKata.Execution/QueryFactory.cs @@ -285,7 +285,7 @@ public SqlMapper.GridReader GetMultiple( public async Task GetMultipleAsync( Query[] queries, IDbTransaction transaction = null, - int? timeout = null, + int? timeout = null, CancellationToken cancellationToken = default) { var compiled = this.Compiler.Compile(queries); @@ -324,7 +324,7 @@ public IEnumerable> Get( public async Task>> GetAsync( Query[] queries, IDbTransaction transaction = null, - int? timeout = null, + int? timeout = null, CancellationToken cancellationToken = default ) { @@ -388,7 +388,7 @@ public async Task AggregateAsync( string aggregateOperation, string[] columns = null, IDbTransaction transaction = null, - int? timeout = null, + int? timeout = null, CancellationToken cancellationToken = default ) { @@ -524,6 +524,76 @@ public async Task> PaginateAsync(Query query, int page, i }; } + public ScrollToResult ScrollTo(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(query.Clone(), null, transaction, timeout); + + IEnumerable list; + + if (count > 0) + { + list = Get(query.Clone().Skip(skip).Take(take), transaction, timeout); + } + else + { + list = Enumerable.Empty(); + } + + return new ScrollToResult + { + Query = query, + Skip = skip, + Take = take, + Count = count, + List = list + }; + } + + public async Task> ScrollToAsync(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(query.Clone(), null, transaction, timeout, cancellationToken); + + IEnumerable list; + + if (count > 0) + { + list = await GetAsync(query.Clone().Skip(skip).Take(take), transaction, timeout, cancellationToken); + } + else + { + list = Enumerable.Empty(); + } + + return new ScrollToResult + { + Query = query, + Skip = skip, + Take = take, + Count = count, + List = list + }; + } + public void Chunk( Query query, int chunkSize, @@ -553,7 +623,7 @@ public async Task ChunkAsync( int chunkSize, Func, int, bool> func, IDbTransaction transaction = null, - int? timeout = null, + int? timeout = null, CancellationToken cancellationToken = default ) { @@ -592,7 +662,7 @@ public async Task ChunkAsync( int chunkSize, Action, int> action, IDbTransaction transaction = null, - int? timeout = null, + int? timeout = null, CancellationToken cancellationToken = default ) { diff --git a/SqlKata.Execution/ScrollToResult.cs b/SqlKata.Execution/ScrollToResult.cs new file mode 100644 index 00000000..13ec8987 --- /dev/null +++ b/SqlKata.Execution/ScrollToResult.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace SqlKata.Execution +{ + public class ScrollToResult + { + public Query Query { get; set; } + public long Count { get; set; } + public IEnumerable List { get; set; } + public int Skip { get; set; } + public int Take { get; set; } + } +} From 00c98fbe2b4d8bbe35e8d1efc0b277f9f654aa84 Mon Sep 17 00:00:00 2001 From: rchavz Date: Sat, 9 Apr 2022 01:28:55 -0500 Subject: [PATCH 2/2] Raul Chavez complementation for the scrollto implentation on queryexensions --- SqlKata.Execution/Query.Extensions.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/SqlKata.Execution/Query.Extensions.cs b/SqlKata.Execution/Query.Extensions.cs index af0af252..6f865723 100644 --- a/SqlKata.Execution/Query.Extensions.cs +++ b/SqlKata.Execution/Query.Extensions.cs @@ -112,6 +112,30 @@ public static async Task> PaginateAsync(this Query que return await PaginateAsync(query, page, perPage, transaction, timeout, cancellationToken); } + public static ScrollToResult ScrollTo(this Query query, int page, int perPage = 25, IDbTransaction transaction = null, int? timeout = null) + { + var db = CreateQueryFactory(query); + + return db.ScrollTo(query, page, perPage, transaction, timeout); + } + + public static async Task> ScrollToAsync(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(query, page, perPage, transaction, timeout, cancellationToken); + } + + public static ScrollToResult ScrollTo(this Query query, int page, int perPage = 25, IDbTransaction transaction = null, int? timeout = null) + { + return query.ScrollTo(page, perPage, transaction, timeout); + } + + public static async Task> ScrollToAsync(this Query query, int page, int perPage = 25, IDbTransaction transaction = null, int? timeout = null, CancellationToken cancellationToken = default) + { + return await ScrollToAsync(query, page, perPage, transaction, timeout, cancellationToken); + } + public static void Chunk(this Query query, int chunkSize, Func, int, bool> func, IDbTransaction transaction = null, int? timeout = null) { var db = CreateQueryFactory(query);