@@ -18,10 +18,29 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
18
18
_tableMapper = tableMapper ;
19
19
db = _db ;
20
20
}
21
-
21
+ /// <summary>
22
+ /// 判断表名是否正确
23
+ /// </summary>
24
+ /// <param name="table"></param>
25
+ /// <returns></returns>
26
+ public bool IsTable ( string table )
27
+ {
28
+ return db . Db . DbMaintenance . GetTableInfoList ( ) . Any ( it => it . Name . Equals ( table , StringComparison . CurrentCultureIgnoreCase ) ) ;
29
+ }
30
+ /// <summary>
31
+ /// 判断表的列名是否正确
32
+ /// </summary>
33
+ /// <param name="table"></param>
34
+ /// <param name="col"></param>
35
+ /// <returns></returns>
36
+ public bool IsCol ( string table , string col )
37
+ {
38
+ return db . Db . DbMaintenance . GetColumnInfosByTableName ( table ) . Any ( it => it . DbColumnName . Equals ( table , StringComparison . CurrentCultureIgnoreCase ) ) ;
39
+ }
40
+
22
41
public ( dynamic , int ) GetTableData ( string subtable , int page , int count , string json , JObject dd )
23
42
{
24
- if ( ! subtable . IsTable ( ) )
43
+ if ( ! IsTable ( subtable ) )
25
44
{
26
45
throw new Exception ( $ "表名{ subtable } 不正确!") ;
27
46
}
@@ -32,6 +51,7 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
32
51
}
33
52
string selectrole = role . Item2 ;
34
53
subtable = _tableMapper . GetTableName ( subtable ) ;
54
+
35
55
JObject values = JObject . Parse ( json ) ;
36
56
page = values [ "page" ] == null ? page : int . Parse ( values [ "page" ] . ToString ( ) ) ;
37
57
count = values [ "count" ] == null ? count : int . Parse ( values [ "count" ] . ToString ( ) ) ;
@@ -50,7 +70,7 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
50
70
}
51
71
public dynamic GetFirstData ( string subtable , string json , JObject dd )
52
72
{
53
- if ( ! subtable . IsTable ( ) )
73
+ if ( IsTable ( subtable ) )
54
74
{
55
75
throw new Exception ( $ "表名{ subtable } 不正确!") ;
56
76
}
@@ -70,7 +90,6 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
70
90
}
71
91
private ISugarQueryable < System . Dynamic . ExpandoObject > sugarQueryable ( string subtable , string selectrole , JObject values , JObject dd )
72
92
{
73
-
74
93
var tb = db . Db . Queryable ( subtable , "tb" ) ;
75
94
if ( values [ "@column" ] . IsValue ( ) )
76
95
{
@@ -80,15 +99,15 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
80
99
string [ ] ziduan = item . Split ( ":" ) ;
81
100
if ( ziduan . Length > 1 )
82
101
{
83
- if ( _identitySvc . ColIsRole ( ziduan [ 0 ] , selectrole . Split ( "," ) ) )
102
+ if ( IsCol ( subtable , ziduan [ 0 ] ) && _identitySvc . ColIsRole ( ziduan [ 0 ] , selectrole . Split ( "," ) ) )
84
103
{
85
104
86
105
str . Append ( ziduan [ 0 ] + " as " + ziduan [ 1 ] + "," ) ;
87
106
}
88
107
}
89
108
else
90
109
{
91
- if ( _identitySvc . ColIsRole ( item , selectrole . Split ( "," ) ) )
110
+ if ( IsCol ( subtable , item ) && _identitySvc . ColIsRole ( item , selectrole . Split ( "," ) ) )
92
111
{
93
112
str . Append ( item + "," ) ;
94
113
}
@@ -111,7 +130,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
111
130
string vakey = va . Key . Trim ( ) ;
112
131
if ( vakey . EndsWith ( "$" ) ) //模糊查询
113
132
{
114
- if ( vakey . TrimEnd ( '$' ) . IsTable ( ) )
133
+ if ( IsCol ( subtable , vakey . TrimEnd ( '$' ) ) )
115
134
{
116
135
conModels . Add ( new ConditionalModel ( ) { FieldName = vakey . TrimEnd ( '$' ) , ConditionalType = ConditionalType . Like , FieldValue = va . Value . ToString ( ) } ) ;
117
136
}
@@ -172,7 +191,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
172
191
conModels . Add ( new ConditionalModel ( ) { FieldName = vakey . TrimEnd ( '@' ) , ConditionalType = ConditionalType . Equal , FieldValue = value } ) ;
173
192
174
193
}
175
- else if ( vakey . IsTable ( ) ) //其他where条件
194
+ else if ( IsCol ( subtable , vakey ) ) //其他where条件
176
195
{
177
196
conModels . Add ( new ConditionalModel ( ) { FieldName = vakey , ConditionalType = ConditionalType . Equal , FieldValue = va . Value . ToString ( ) } ) ;
178
197
}
@@ -184,7 +203,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
184
203
{
185
204
foreach ( var item in values [ "@order" ] . ToString ( ) . Split ( "," ) )
186
205
{
187
- if ( item . Replace ( "-" , "" ) . IsTable ( ) )
206
+ if ( IsCol ( subtable , item . Replace ( "-" , "" ) ) )
188
207
{
189
208
if ( item . EndsWith ( "-" ) )
190
209
{
@@ -203,7 +222,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
203
222
var str = new System . Text . StringBuilder ( 100 ) ;
204
223
foreach ( var and in values [ "@group" ] . ToString ( ) . Split ( ',' ) )
205
224
{
206
- if ( and . IsField ( ) )
225
+ if ( IsCol ( subtable , and ) )
207
226
{
208
227
str . Append ( and + "," ) ;
209
228
}
@@ -212,7 +231,54 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
212
231
}
213
232
if ( values [ "@having" ] . IsValue ( ) )
214
233
{
215
- tb . Having ( $ "{ values [ "@having" ] . ToString ( ) } ") ;
234
+ List < IConditionalModel > hw = new List < IConditionalModel > ( ) ;
235
+ JArray jArray = JArray . Parse ( values [ "@having" ] . ToString ( ) ) ;
236
+ foreach ( var item in jArray )
237
+ {
238
+ string and = item . ToString ( ) ;
239
+ var model = new ConditionalModel ( ) ;
240
+ if ( and . Contains ( ">=" ) )
241
+ {
242
+ model . FieldName = and . Split ( new string [ ] { ">=" } , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
243
+ model . ConditionalType = ConditionalType . GreaterThanOrEqual ;
244
+ model . FieldValue = and . Split ( new string [ ] { ">=" } , StringSplitOptions . RemoveEmptyEntries ) [ 1 ] ;
245
+ }
246
+ else if ( and . Contains ( "<=" ) )
247
+ {
248
+
249
+ model . FieldName = and . Split ( new string [ ] { "<=" } , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
250
+ model . ConditionalType = ConditionalType . LessThanOrEqual ;
251
+ model . FieldValue = and . Split ( new string [ ] { "<=" } , StringSplitOptions . RemoveEmptyEntries ) [ 1 ] ;
252
+ }
253
+ else if ( and . Contains ( ">" ) )
254
+ {
255
+ model . FieldName = and . Split ( new string [ ] { ">" } , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
256
+ model . ConditionalType = ConditionalType . GreaterThan ;
257
+ model . FieldValue = and . Split ( new string [ ] { ">" } , StringSplitOptions . RemoveEmptyEntries ) [ 1 ] ;
258
+ }
259
+ else if ( and . Contains ( "<" ) )
260
+ {
261
+ model . FieldName = and . Split ( new string [ ] { "<" } , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
262
+ model . ConditionalType = ConditionalType . LessThan ;
263
+ model . FieldValue = and . Split ( new string [ ] { "<" } , StringSplitOptions . RemoveEmptyEntries ) [ 1 ] ;
264
+ }
265
+ else if ( and . Contains ( "!=" ) )
266
+ {
267
+ model . FieldName = and . Split ( new string [ ] { "!=" } , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
268
+ model . ConditionalType = ConditionalType . NoEqual ;
269
+ model . FieldValue = and . Split ( new string [ ] { "!=" } , StringSplitOptions . RemoveEmptyEntries ) [ 1 ] ;
270
+ }
271
+ else if ( and . Contains ( "=" ) )
272
+ {
273
+ model . FieldName = and . Split ( new string [ ] { "=" } , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
274
+ model . ConditionalType = ConditionalType . Equal ;
275
+ model . FieldValue = and . Split ( new string [ ] { "=" } , StringSplitOptions . RemoveEmptyEntries ) [ 1 ] ;
276
+ }
277
+ hw . Add ( model ) ;
278
+ }
279
+
280
+ var d = db . Db . Context . Utilities . ConditionalModelToSql ( hw ) ;
281
+ tb . Having ( d . Key , d . Value ) ;
216
282
}
217
283
return tb ;
218
284
}
0 commit comments