@@ -18,10 +18,29 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
1818 _tableMapper = tableMapper ;
1919 db = _db ;
2020 }
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+
2241 public ( dynamic , int ) GetTableData ( string subtable , int page , int count , string json , JObject dd )
2342 {
24- if ( ! subtable . IsTable ( ) )
43+ if ( ! IsTable ( subtable ) )
2544 {
2645 throw new Exception ( $ "表名{ subtable } 不正确!") ;
2746 }
@@ -32,6 +51,7 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
3251 }
3352 string selectrole = role . Item2 ;
3453 subtable = _tableMapper . GetTableName ( subtable ) ;
54+
3555 JObject values = JObject . Parse ( json ) ;
3656 page = values [ "page" ] == null ? page : int . Parse ( values [ "page" ] . ToString ( ) ) ;
3757 count = values [ "count" ] == null ? count : int . Parse ( values [ "count" ] . ToString ( ) ) ;
@@ -50,7 +70,7 @@ public SelectTable(IIdentityService identityService, ITableMapper tableMapper, D
5070 }
5171 public dynamic GetFirstData ( string subtable , string json , JObject dd )
5272 {
53- if ( ! subtable . IsTable ( ) )
73+ if ( IsTable ( subtable ) )
5474 {
5575 throw new Exception ( $ "表名{ subtable } 不正确!") ;
5676 }
@@ -70,7 +90,6 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
7090 }
7191 private ISugarQueryable < System . Dynamic . ExpandoObject > sugarQueryable ( string subtable , string selectrole , JObject values , JObject dd )
7292 {
73-
7493 var tb = db . Db . Queryable ( subtable , "tb" ) ;
7594 if ( values [ "@column" ] . IsValue ( ) )
7695 {
@@ -80,15 +99,15 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
8099 string [ ] ziduan = item . Split ( ":" ) ;
81100 if ( ziduan . Length > 1 )
82101 {
83- if ( _identitySvc . ColIsRole ( ziduan [ 0 ] , selectrole . Split ( "," ) ) )
102+ if ( IsCol ( subtable , ziduan [ 0 ] ) && _identitySvc . ColIsRole ( ziduan [ 0 ] , selectrole . Split ( "," ) ) )
84103 {
85104
86105 str . Append ( ziduan [ 0 ] + " as " + ziduan [ 1 ] + "," ) ;
87106 }
88107 }
89108 else
90109 {
91- if ( _identitySvc . ColIsRole ( item , selectrole . Split ( "," ) ) )
110+ if ( IsCol ( subtable , item ) && _identitySvc . ColIsRole ( item , selectrole . Split ( "," ) ) )
92111 {
93112 str . Append ( item + "," ) ;
94113 }
@@ -111,7 +130,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
111130 string vakey = va . Key . Trim ( ) ;
112131 if ( vakey . EndsWith ( "$" ) ) //模糊查询
113132 {
114- if ( vakey . TrimEnd ( '$' ) . IsTable ( ) )
133+ if ( IsCol ( subtable , vakey . TrimEnd ( '$' ) ) )
115134 {
116135 conModels . Add ( new ConditionalModel ( ) { FieldName = vakey . TrimEnd ( '$' ) , ConditionalType = ConditionalType . Like , FieldValue = va . Value . ToString ( ) } ) ;
117136 }
@@ -172,7 +191,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
172191 conModels . Add ( new ConditionalModel ( ) { FieldName = vakey . TrimEnd ( '@' ) , ConditionalType = ConditionalType . Equal , FieldValue = value } ) ;
173192
174193 }
175- else if ( vakey . IsTable ( ) ) //其他where条件
194+ else if ( IsCol ( subtable , vakey ) ) //其他where条件
176195 {
177196 conModels . Add ( new ConditionalModel ( ) { FieldName = vakey , ConditionalType = ConditionalType . Equal , FieldValue = va . Value . ToString ( ) } ) ;
178197 }
@@ -184,7 +203,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
184203 {
185204 foreach ( var item in values [ "@order" ] . ToString ( ) . Split ( "," ) )
186205 {
187- if ( item . Replace ( "-" , "" ) . IsTable ( ) )
206+ if ( IsCol ( subtable , item . Replace ( "-" , "" ) ) )
188207 {
189208 if ( item . EndsWith ( "-" ) )
190209 {
@@ -203,7 +222,7 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
203222 var str = new System . Text . StringBuilder ( 100 ) ;
204223 foreach ( var and in values [ "@group" ] . ToString ( ) . Split ( ',' ) )
205224 {
206- if ( and . IsField ( ) )
225+ if ( IsCol ( subtable , and ) )
207226 {
208227 str . Append ( and + "," ) ;
209228 }
@@ -212,7 +231,54 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
212231 }
213232 if ( values [ "@having" ] . IsValue ( ) )
214233 {
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 ) ;
216282 }
217283 return tb ;
218284 }
0 commit comments