1- using Dapper ;
2- using SqlKata ;
31using System ;
42using System . Collections . Generic ;
5- using System . Linq ;
63using System . Threading . Tasks ;
74
85namespace SqlKata . Execution
@@ -11,31 +8,17 @@ public static class QueryExtensionsAsync
118 {
129 public static async Task < IEnumerable < T > > GetAsync < T > ( this Query query )
1310 {
14- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( GetAsync ) ) ;
15-
16- var compiled = xQuery . Compiler . Compile ( query ) ;
17-
18- xQuery . Logger ( compiled ) ;
19-
20- return await xQuery . Connection . QueryAsync < T > ( compiled . Sql , compiled . NamedBindings ) ;
11+ return await QueryHelper . CreateQueryFactory ( query ) . GetAsync < T > ( query ) ;
2112 }
2213
2314 public static async Task < IEnumerable < dynamic > > GetAsync ( this Query query )
2415 {
25- return await query . GetAsync < dynamic > ( ) ;
16+ return await GetAsync < dynamic > ( query ) ;
2617 }
2718
2819 public static async Task < T > FirstOrDefaultAsync < T > ( this Query query )
2920 {
30-
31- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( FirstOrDefaultAsync ) ) ;
32-
33- var compiled = xQuery . Compiler . Compile ( query . Limit ( 1 ) ) ;
34-
35- xQuery . Logger ( compiled ) ;
36-
37- return await xQuery . Connection . QueryFirstOrDefaultAsync < T > ( compiled . Sql , compiled . NamedBindings ) ;
38-
21+ return await QueryHelper . CreateQueryFactory ( query ) . FirstOrDefaultAsync < T > ( query ) ;
3922 }
4023
4124 public static async Task < dynamic > FirstOrDefaultAsync ( this Query query )
@@ -45,15 +28,7 @@ public static async Task<dynamic> FirstOrDefaultAsync(this Query query)
4528
4629 public static async Task < T > FirstAsync < T > ( this Query query )
4730 {
48-
49- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( FirstAsync ) ) ;
50-
51- var compiled = xQuery . Compiler . Compile ( query . Limit ( 1 ) ) ;
52-
53- xQuery . Logger ( compiled ) ;
54-
55- return await xQuery . Connection . QueryFirstAsync < T > ( compiled . Sql , compiled . NamedBindings ) ;
56-
31+ return await QueryHelper . CreateQueryFactory ( query ) . FirstAsync < T > ( query ) ;
5732 }
5833
5934 public static async Task < dynamic > FirstAsync ( this Query query )
@@ -63,172 +38,86 @@ public static async Task<dynamic> FirstAsync(this Query query)
6338
6439 public static async Task < PaginationResult < T > > PaginateAsync < T > ( this Query query , int page , int perPage = 25 )
6540 {
41+ var db = QueryHelper . CreateQueryFactory ( query ) ;
6642
67- if ( page < 1 )
68- {
69- throw new ArgumentException ( "Page param should be greater than or equal to 1" , nameof ( page ) ) ;
70- }
71-
72- if ( perPage < 1 )
73- {
74- throw new ArgumentException ( "PerPage param should be greater than or equal to 1" , nameof ( perPage ) ) ;
75- }
76-
77- var count = await query . Clone ( ) . CountAsync < long > ( ) ;
78-
79- IEnumerable < T > list ;
80- if ( count > 0 )
81- {
82- list = await query . Clone ( ) . ForPage ( page , perPage ) . GetAsync < T > ( ) ;
83- }
84- else
85- {
86- list = Enumerable . Empty < T > ( ) ;
87- }
88-
89- return new PaginationResult < T >
90- {
91- Query = query . Clone ( ) ,
92- Page = page ,
93- PerPage = perPage ,
94- Count = count ,
95- List = list
96- } ;
97-
43+ return await db . PaginateAsync < T > ( query , page , perPage ) ;
9844 }
9945
10046 public static async Task < PaginationResult < dynamic > > PaginateAsync ( this Query query , int page , int perPage = 25 )
10147 {
102- return await query . PaginateAsync < dynamic > ( page , perPage ) ;
48+ return await PaginateAsync < dynamic > ( query , page , perPage ) ;
10349 }
10450
10551 public static async Task ChunkAsync < T > ( this Query query , int chunkSize , Func < IEnumerable < T > , int , bool > func )
10652 {
107- var result = await query . PaginateAsync < T > ( 1 , chunkSize ) ;
108-
109- if ( ! func ( result . List , 1 ) )
110- {
111- return ;
112- }
113-
114- while ( result . HasNext )
115- {
116- result = result . Next ( ) ;
117- if ( ! func ( result . List , result . Page ) )
118- {
119- return ;
120- }
121- }
122-
53+ await QueryHelper . CreateQueryFactory ( query ) . ChunkAsync < T > ( query , chunkSize , func ) ;
12354 }
12455
125- public static async Task ChunkAsync ( this Query query , int chunkSize , Func < IEnumerable < dynamic > , int , bool > func )
56+ public static async Task ChunkAsync < T > ( this Query query , int chunkSize , Action < IEnumerable < T > , int > action )
12657 {
127- await query . ChunkAsync < dynamic > ( chunkSize , func ) ;
58+ await QueryHelper . CreateQueryFactory ( query ) . ChunkAsync < T > ( query , chunkSize , action ) ;
12859 }
12960
130- public static async Task ChunkAsync < T > ( this Query query , int chunkSize , Action < IEnumerable < T > , int > action )
61+ public static async Task ChunkAsync ( this Query query , int chunkSize , Func < IEnumerable < dynamic > , int , bool > func )
13162 {
132- var result = await query . PaginateAsync < T > ( 1 , chunkSize ) ;
133-
134- action ( result . List , 1 ) ;
135-
136- while ( result . HasNext )
137- {
138- result = result . Next ( ) ;
139- action ( result . List , result . Page ) ;
140- }
141-
63+ await ChunkAsync < dynamic > ( query , chunkSize , func ) ;
14264 }
14365
66+
14467 public static async Task ChunkAsync ( this Query query , int chunkSize , Action < IEnumerable < dynamic > , int > action )
14568 {
146- await query . ChunkAsync < dynamic > ( chunkSize , action ) ;
69+ await ChunkAsync < dynamic > ( query , chunkSize , action ) ;
14770 }
14871
149- public static async Task < int > InsertAsync ( this Query query , IReadOnlyDictionary < string , object > values )
72+ public static async Task < int > InsertAsync (
73+ this Query query ,
74+ IReadOnlyDictionary < string , object > values
75+ )
15076 {
151- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( InsertAsync ) ) ;
152-
153- var compiled = xQuery . Compiler . Compile ( query . AsInsert ( values ) ) ;
154-
155- xQuery . Logger ( compiled ) ;
156-
157- return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . NamedBindings ) ;
77+ return await QueryHelper . CreateQueryFactory ( query )
78+ . ExecuteAsync ( query . AsInsert ( values ) ) ;
15879 }
15980
16081 public static async Task < int > InsertAsync ( this Query query , object data )
16182 {
162- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( InsertAsync ) ) ;
163-
164- var compiled = xQuery . Compiler . Compile ( query . AsInsert ( data ) ) ;
165-
166- xQuery . Logger ( compiled ) ;
167-
168- return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . NamedBindings ) ;
83+ return await QueryHelper . CreateQueryFactory ( query )
84+ . ExecuteAsync ( query . AsInsert ( data ) ) ;
16985 }
17086
17187 public static async Task < T > InsertGetIdAsync < T > ( this Query query , object data )
17288 {
173-
174- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( InsertGetIdAsync ) ) ;
175-
176- var compiled = xQuery . Compiler . Compile ( query . AsInsert ( data , true ) ) ;
177-
178- xQuery . Logger ( compiled ) ;
179-
180- var row = await xQuery . Connection . QueryFirstAsync < InsertGetIdRow < T > > (
181- compiled . Sql , compiled . NamedBindings
182- ) ;
89+ var row = await QueryHelper . CreateQueryFactory ( query )
90+ . FirstAsync < InsertGetIdRow < T > > ( query . AsInsert ( data , true ) ) ;
18391
18492 return row . Id ;
185-
18693 }
18794
188- public static async Task < int > InsertAsync ( this Query query , IEnumerable < string > columns , Query fromQuery )
95+ public static async Task < int > InsertAsync (
96+ this Query query ,
97+ IEnumerable < string > columns ,
98+ Query fromQuery
99+ )
189100 {
190-
191- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( InsertAsync ) ) ;
192-
193- var compiled = xQuery . Compiler . Compile ( query . AsInsert ( columns , fromQuery ) ) ;
194-
195- xQuery . Logger ( compiled ) ;
196-
197- return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . NamedBindings ) ;
198-
101+ return await QueryHelper . CreateQueryFactory ( query )
102+ . ExecuteAsync ( query . AsInsert ( columns , fromQuery ) ) ;
199103 }
200104
201105 public static async Task < int > UpdateAsync ( this Query query , IReadOnlyDictionary < string , object > values )
202106 {
203- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( UpdateAsync ) ) ;
204-
205- var compiled = xQuery . Compiler . Compile ( query . AsUpdate ( values ) ) ;
206-
207- xQuery . Logger ( compiled ) ;
208-
209- return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . NamedBindings ) ;
107+ return await QueryHelper . CreateQueryFactory ( query )
108+ . ExecuteAsync ( query . AsUpdate ( values ) ) ;
210109 }
211110
212111 public static async Task < int > UpdateAsync ( this Query query , object data )
213112 {
214- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( UpdateAsync ) ) ;
215-
216- var compiled = xQuery . Compiler . Compile ( query . AsUpdate ( data ) ) ;
217-
218- xQuery . Logger ( compiled ) ;
219-
220- return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . NamedBindings ) ;
113+ return await QueryHelper . CreateQueryFactory ( query )
114+ . ExecuteAsync ( query . AsUpdate ( data ) ) ;
221115 }
222116
223117 public static async Task < int > DeleteAsync ( this Query query )
224118 {
225- var xQuery = QueryHelper . CastToXQuery ( query , nameof ( DeleteAsync ) ) ;
226-
227- var compiled = xQuery . Compiler . Compile ( query . AsDelete ( ) ) ;
228-
229- xQuery . Logger ( compiled ) ;
230-
231- return await xQuery . Connection . ExecuteAsync ( compiled . Sql , compiled . NamedBindings ) ;
119+ return await QueryHelper . CreateQueryFactory ( query )
120+ . ExecuteAsync ( query . AsDelete ( ) ) ;
232121 }
233122
234123 }
0 commit comments