2
2
// Licensed under the BSD License. See LICENSE.txt in the project root for license information.
3
3
4
4
using Belgrade . SqlClient ;
5
- using SqlServerRestApi . SQL ;
6
5
using System ;
7
6
using System . Data . SqlClient ;
8
7
using System . IO ;
9
8
using System . Threading . Tasks ;
10
9
11
10
namespace SqlServerRestApi
12
11
{
13
- public class ODataHandler
12
+ public class RequestHandler
14
13
{
15
- private SqlCommand cmd ;
16
- private IQueryPipe pipe ;
17
- private Stream stream ;
14
+ protected SqlCommand cmd ;
15
+ protected IQueryPipe pipe ;
16
+ protected Stream stream ;
18
17
19
- internal ODataHandler ( SqlCommand cmd , IQueryPipe pipe , Stream stream )
18
+ internal RequestHandler ( SqlCommand cmd , IQueryPipe pipe , Stream stream )
20
19
{
21
20
this . cmd = cmd ;
22
21
this . pipe = pipe ;
23
22
this . stream = stream ;
24
23
}
25
- public ODataHandler OnError ( Action < Exception > onErrorHandler )
24
+
25
+ public virtual RequestHandler OnError ( Action < Exception > onErrorHandler )
26
26
{
27
27
pipe . OnError ( onErrorHandler ) ;
28
28
return this ;
29
29
}
30
30
31
- public async Task Process ( )
31
+ public virtual async Task Process ( )
32
+ {
33
+ await pipe . Stream ( cmd , stream , "[]" ) ;
34
+ }
35
+ }
36
+
37
+ public class JQueryDataTablesHandler : RequestHandler
38
+ {
39
+ private string draw ;
40
+ private int length ;
41
+ private int start ;
42
+
43
+ internal JQueryDataTablesHandler ( SqlCommand cmd , string draw , int start , int length , IQueryPipe pipe , Stream stream ) : base ( cmd , pipe , stream )
44
+ {
45
+ this . draw = draw ;
46
+ this . start = start ;
47
+ this . length = length ;
48
+ }
49
+
50
+ public override async Task Process ( )
32
51
{
52
+ var header = System . Text . Encoding . UTF8 . GetBytes (
53
+ $@ "{{
54
+ ""draw"":""{ draw } "",
55
+ ""recordsTotal"":""{ start + length + 1 } "",
56
+ ""recordsFiltered"":""{ start + length + 1 } "",
57
+ ""data"":" ) ;
58
+ await stream . WriteAsync ( header , 0 , header . Length ) ;
33
59
await pipe . Stream ( cmd , stream , "[]" ) ;
60
+ await stream . WriteAsync ( System . Text . Encoding . UTF8 . GetBytes ( "}" ) , 0 , 1 ) ;
34
61
}
62
+ }
35
63
64
+ public class ODataHandler : RequestHandler
65
+ {
66
+ internal ODataHandler ( SqlCommand cmd , IQueryPipe pipe , Stream stream ) : base ( cmd , pipe , stream )
67
+ {
68
+ }
36
69
}
37
70
38
71
public static class RestApiControllerExtensions
39
72
{
40
-
41
- public static ODataHandler ODataHandler (
73
+ public static RequestHandler ODataHandler (
42
74
this Microsoft . AspNetCore . Mvc . Controller ctrl ,
43
75
TableSpec tableSpec ,
44
76
IQueryPipe sqlQuery )
45
77
{
46
78
var querySpec = SqlServerRestApi . OData . UriParser . Parse ( tableSpec , ctrl . Request ) ;
47
- var sql = SqlServerRestApi . SQL . QueryBuilder . Build ( querySpec , tableSpec ) ;
79
+ var sql = SqlServerRestApi . QueryBuilder . Build ( querySpec , tableSpec ) ;
48
80
if ( ! querySpec . count )
49
- sql = sql . AsJson ( ) ;
81
+ sql = sql . AsJson ( "value" ) ;
50
82
return new ODataHandler ( sql , sqlQuery , ctrl . Response . Body ) ;
51
83
}
52
84
53
- public static async Task ProcessODataRequest (
85
+ public static RequestHandler JQueryDataTablesHandler (
54
86
this Microsoft . AspNetCore . Mvc . Controller ctrl ,
55
87
TableSpec tableSpec ,
56
88
IQueryPipe sqlQuery )
57
89
{
58
- var querySpec = OData . UriParser . Parse ( tableSpec , ctrl . Request ) ;
59
- var sql = QueryBuilder . Build ( querySpec , tableSpec ) ;
90
+ var querySpec = SqlServerRestApi . JQueryDataTable . UriParser . Parse ( tableSpec , ctrl . Request ) ;
91
+ var sql = SqlServerRestApi . QueryBuilder . Build ( querySpec , tableSpec ) ;
60
92
if ( ! querySpec . count )
61
93
sql = sql . AsJson ( ) ;
62
- await sqlQuery . Stream ( sql , ctrl . Response . Body , "[]" ) ;
94
+ return new JQueryDataTablesHandler ( sql , ctrl . Request . Query [ "draw" ] . ToString ( ) , Convert . ToInt32 ( ctrl . Request . Query [ "start" ] ) , Convert . ToInt32 ( ctrl . Request . Query [ "length" ] ) , sqlQuery , ctrl . Response . Body ) ;
63
95
}
64
-
96
+
65
97
public static async Task ProcessJQueryDataTablesRequest (
66
98
this Microsoft . AspNetCore . Mvc . Controller ctrl ,
67
99
TableSpec tableSpec ,
@@ -78,11 +110,9 @@ public static async Task ProcessJQueryDataTablesRequest(
78
110
""recordsFiltered"":""{ start + length + 1 } "",
79
111
""data"":" ) ;
80
112
await ctrl . Response . Body . WriteAsync ( header , 0 , header . Length ) ;
81
-
82
113
var querySpec = JQueryDataTable . UriParser . Parse ( tableSpec , Request ) ;
83
114
var sql = QueryBuilder . Build ( querySpec , tableSpec ) . AsJson ( ) ;
84
115
await sqlQuery . Stream ( sql , ctrl . Response . Body , "[]" ) ;
85
-
86
116
await ctrl . Response . Body . WriteAsync ( System . Text . Encoding . UTF8 . GetBytes ( "}" ) , 0 , 1 ) ;
87
117
}
88
118
}
0 commit comments