1+ using Dapper ;
2+ using SqlKata . Execution ;
3+ using System . Collections . Generic ;
4+ using System ;
5+ using System . Threading . Tasks ;
6+
7+ namespace SqlKata . Execution
8+ {
9+ public static class QueryExtensionsAsync
10+ {
11+ public static async Task < IEnumerable < T > > GetAsync < T > ( this Query query )
12+ {
13+ var xQuery = QueryHelper . CastToXQuery ( query , nameof ( GetAsync ) ) ;
14+
15+ var compiled = xQuery . Compiler . Compile ( query ) ;
16+
17+ return await xQuery . Connection . QueryAsync < T > ( compiled . Sql , compiled . Bindings ) ;
18+ }
19+
20+ public static async Task < IEnumerable < dynamic > > GetAsync ( this Query query )
21+ {
22+ return await query . GetAsync < dynamic > ( ) ;
23+ }
24+
25+ public static async Task < T > FirstOrDefaultAsync < T > ( this Query query )
26+ {
27+
28+ var xQuery = QueryHelper . CastToXQuery ( query , nameof ( FirstOrDefaultAsync ) ) ;
29+
30+ var compiled = xQuery . Compiler . Compile ( query . Limit ( 1 ) ) ;
31+
32+ return await xQuery . Connection . QueryFirstOrDefaultAsync < T > ( compiled . Sql , compiled . Bindings ) ;
33+
34+ }
35+
36+ public static async Task < dynamic > FirstOrDefaultAsync ( this Query query )
37+ {
38+ return await FirstOrDefaultAsync < dynamic > ( query ) ;
39+ }
40+
41+ public static async Task < T > FirstAsync < T > ( this Query query )
42+ {
43+
44+ var xQuery = QueryHelper . CastToXQuery ( query , nameof ( FirstAsync ) ) ;
45+
46+ var compiled = xQuery . Compiler . Compile ( query . Limit ( 1 ) ) ;
47+
48+ return await xQuery . Connection . QueryFirstAsync < T > ( compiled . Sql , compiled . Bindings ) ;
49+
50+ }
51+
52+ public static async Task < dynamic > FirstAsync ( this Query query )
53+ {
54+ return await FirstAsync < dynamic > ( query ) ;
55+ }
56+
57+ public static async Task < PaginationResult < T > > PaginateAsync < T > ( this Query query , int page , int perPage = 25 )
58+ {
59+
60+ if ( page < 1 )
61+ {
62+ throw new ArgumentException ( "Page param should be greater than or equal to 1" , nameof ( page ) ) ;
63+ }
64+
65+ if ( perPage < 1 )
66+ {
67+ throw new ArgumentException ( "PerPage param should be greater than or equal to 1" , nameof ( perPage ) ) ;
68+ }
69+
70+ var xQuery = QueryHelper . CastToXQuery ( query , nameof ( PaginateAsync ) ) ;
71+
72+ var count = await query . Clone ( ) . CountAsync < long > ( ) ;
73+
74+ var list = await query . ForPage ( page , perPage ) . GetAsync < T > ( ) ;
75+
76+ return new PaginationResult < T >
77+ {
78+ Query = query . Clone ( ) ,
79+ Page = page ,
80+ PerPage = perPage ,
81+ Count = count ,
82+ List = list
83+ } ;
84+
85+ }
86+
87+ public static async Task < PaginationResult < dynamic > > PaginateASync ( this Query query , int page , int perPage = 25 )
88+ {
89+ return await query . PaginateAsync < dynamic > ( page , perPage ) ;
90+ }
91+
92+ public static async Task ChunkAsync < T > ( this Query query , int chunkSize , Func < IEnumerable < T > , int , bool > func )
93+ {
94+ var result = await query . PaginateAsync < T > ( 1 , chunkSize ) ;
95+
96+ if ( ! func ( result . List , 1 ) )
97+ {
98+ return ;
99+ }
100+
101+ while ( result . HasNext )
102+ {
103+ result = result . Next ( ) ;
104+ if ( ! func ( result . List , result . Page ) )
105+ {
106+ return ;
107+ }
108+ }
109+
110+ }
111+
112+ public static async Task ChunkAsync ( this Query query , int chunkSize , Func < IEnumerable < dynamic > , int , bool > func )
113+ {
114+ await query . ChunkAsync < dynamic > ( chunkSize , func ) ;
115+ }
116+
117+ public static async Task ChunkAsync < T > ( this Query query , int chunkSize , Action < IEnumerable < T > , int > action )
118+ {
119+ var result = await query . PaginateAsync < T > ( 1 , chunkSize ) ;
120+
121+ action ( result . List , 1 ) ;
122+
123+ while ( result . HasNext )
124+ {
125+ result = result . Next ( ) ;
126+ action ( result . List , result . Page ) ;
127+ }
128+
129+ }
130+
131+ public static async Task ChunkAsync ( this Query query , int chunkSize , Action < IEnumerable < dynamic > , int > action )
132+ {
133+ await query . ChunkAsync < dynamic > ( chunkSize , action ) ;
134+ }
135+
136+ public static async Task < int > InsertAsync ( this Query query , IReadOnlyDictionary < string , object > values )
137+ {
138+ var xQuery = QueryHelper . CastToXQuery ( query , nameof ( InsertAsync ) ) ;
139+
140+ var compiled = xQuery . Compiler . Compile ( query . AsInsert ( values ) ) ;
141+
142+ return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . Bindings ) ;
143+ }
144+
145+ public static async Task < int > UpdateAsync ( this Query query , IReadOnlyDictionary < string , object > values )
146+ {
147+ var xQuery = QueryHelper . CastToXQuery ( query , nameof ( UpdateAsync ) ) ;
148+
149+ var compiled = xQuery . Compiler . Compile ( query . AsUpdate ( values ) ) ;
150+
151+ return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . Bindings ) ;
152+ }
153+
154+ public static async Task < int > DeleteAsync ( this Query query )
155+ {
156+ var xQuery = QueryHelper . CastToXQuery ( query , nameof ( DeleteAsync ) ) ;
157+
158+ var compiled = xQuery . Compiler . Compile ( query . AsDelete ( ) ) ;
159+
160+ return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . Bindings ) ;
161+ }
162+
163+ }
164+ }
0 commit comments