37
37
import static zuo .biao .apijson .SQL .OR ;
38
38
39
39
import java .util .ArrayList ;
40
+ import java .util .Arrays ;
40
41
import java .util .Collection ;
41
42
import java .util .HashMap ;
42
43
import java .util .LinkedHashMap ;
@@ -94,8 +95,8 @@ public abstract class AbstractSQLConfig implements SQLConfig {
94
95
private String group ; //分组方式的字符串数组,','分隔
95
96
private String having ; //聚合函数的字符串数组,','分隔
96
97
private String order ; //排序方式的字符串数组,','分隔
97
- private String column ; //表内字段名(或函数名,仅查询操作可用)的字符串数组,','分隔
98
- private String values ; //对应表内字段的值的字符串数组,','分隔
98
+ private List < String > column ; //表内字段名(或函数名,仅查询操作可用)的字符串数组,','分隔
99
+ private List < List < Object >> values ; //对应表内字段的值的字符串数组,','分隔
99
100
private Map <String , Object > content ; //Request内容,key:value形式,column = content.keySet(),values = content.values()
100
101
private Map <String , Object > where ; //筛选条件,key:value形式
101
102
private Map <String , List <String >> combine ; //条件组合,{ "&":[key], "|":[key], "!":[key] }
@@ -453,14 +454,11 @@ public String getOrderString() {
453
454
454
455
455
456
@ Override
456
- public String getColumn () {
457
+ public List < String > getColumn () {
457
458
return column ;
458
459
}
459
- public AbstractSQLConfig setColumn (String ... keys ) {
460
- return setColumn (StringUtil .getString (keys ));
461
- }
462
460
@ Override
463
- public AbstractSQLConfig setColumn (String column ) {
461
+ public AbstractSQLConfig setColumn (List < String > column ) {
464
462
this .column = column ;
465
463
return this ;
466
464
}
@@ -469,29 +467,28 @@ public String getColumnString() throws Exception {
469
467
switch (getMethod ()) {
470
468
case HEAD :
471
469
case HEADS : //StringUtil.isEmpty(column, true) || column.contains(",") 时SQL.count(column)会return "*"
472
- if (isPrepared () && StringUtil .isEmpty (column , true ) == false
473
- && column .contains ("," ) == false && StringUtil .isName (column ) == false ) {
474
- throw new IllegalArgumentException ("HEAD请求: @column:value 中 value里面用 , 分割的每一项都必须是1个单词!" );
470
+ if (isPrepared () && column != null ) {
471
+ for (String c : column ) {
472
+ if (StringUtil .isName (c ) == false ) {
473
+ throw new IllegalArgumentException ("HEAD请求: @column:value 中 value里面用 , 分割的每一项都必须是1个单词!" );
474
+ }
475
+ }
475
476
}
476
- return SQL .count (column );
477
+ return SQL .count (column != null && column . size () == 1 ? column . get ( 0 ) : "*" );
477
478
case POST :
478
- if (StringUtil .isEmpty (column , true )) {
479
- throw new NotExistException (TAG + "getColumnString getMethod() = POST"
480
- + " >> StringUtil.isEmpty(column, true)" );
479
+ if (column == null || column .isEmpty ()) {
480
+ throw new IllegalArgumentException ("POST 请求必须在Table内设置要保存的 key:value !" );
481
481
}
482
482
483
483
if (isPrepared ()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
484
- String [] keys = StringUtil .split (column );
485
- if (keys != null && keys .length > 0 ) {
486
- for (int i = 0 ; i < keys .length ; i ++) {
487
- if (StringUtil .isName (keys [i ]) == false ) {
488
- throw new IllegalArgumentException ("POST请求: 每一个 key:value 中的key都必须是1个单词!" );
489
- }
484
+ for (String c : column ) {
485
+ if (StringUtil .isName (c ) == false ) {
486
+ throw new IllegalArgumentException ("POST请求: 每一个 key:value 中的key都必须是1个单词!" );
490
487
}
491
488
}
492
489
}
493
490
494
- return "(" + column + ")" ;
491
+ return "(" + StringUtil . getString ( column . toArray ()) + ")" ;
495
492
case GET :
496
493
case GETS : //TODO 支持SQL函数 json_length(contactIdList):contactCount
497
494
boolean isQuery = RequestMethod .isQueryMethod (method );
@@ -510,9 +507,9 @@ public String getColumnString() throws Exception {
510
507
511
508
String tableAlias = getAlias ();
512
509
513
- String c = StringUtil .getString (column ); //id,name;json_length(contactIdList):contactCount;...
510
+ // String c = StringUtil.getString(column); //id,name;json_length(contactIdList):contactCount;...
514
511
515
- String [] keys = StringUtil .split (c , ";" );
512
+ String [] keys = column == null ? null : column . toArray ( new String []{}); // StringUtil.split(c, ";");
516
513
if (keys == null || keys .length <= 0 ) {
517
514
return isKeyPrefix () == false ? "*" : (tableAlias + ".*" + (StringUtil .isEmpty (joinColumn , true ) ? "" : ", " + joinColumn ));
518
515
}
@@ -627,7 +624,7 @@ public String getColumnString() throws Exception {
627
624
628
625
}
629
626
630
- c = StringUtil .getString (keys );
627
+ String c = StringUtil .getString (keys );
631
628
632
629
return (c .contains (":" ) == false ? c : c .replaceAll (":" , " AS " )) + (StringUtil .isEmpty (joinColumn , true ) ? "" : ", " + joinColumn );//不能在这里改,后续还要用到:
633
630
@@ -641,37 +638,34 @@ public String getColumnString() throws Exception {
641
638
642
639
643
640
@ Override
644
- public String getValues () {
641
+ public List < List < Object >> getValues () {
645
642
return values ;
646
643
}
647
644
@ JSONField (serialize = false )
648
645
public String getValuesString () {
649
- return values ;
650
- }
651
- public AbstractSQLConfig setValues (Object [][] valuess ) {
652
646
String s = "" ;
653
- if (valuess != null && valuess . length > 0 ) {
654
- Object [] items = new Object [valuess . length ];
655
- Object [] vs ;
656
- for (int i = 0 ; i < valuess . length ; i ++) {
657
- vs = valuess [ i ] ;
647
+ if (values != null && values . size () > 0 ) {
648
+ Object [] items = new Object [values . size () ];
649
+ List < Object > vs ;
650
+ for (int i = 0 ; i < values . size () ; i ++) {
651
+ vs = values . get ( i ) ;
658
652
if (vs == null ) {
659
653
continue ;
660
654
}
661
655
662
656
items [i ] = "(" ;
663
- for (int j = 0 ; j < vs .length ; j ++) {
664
- items [i ] += ((j <= 0 ? "" : "," ) + getValue (vs [ j ] ));
657
+ for (int j = 0 ; j < vs .size () ; j ++) {
658
+ items [i ] += ((j <= 0 ? "" : "," ) + getValue (vs . get ( j ) ));
665
659
}
666
660
items [i ] += ")" ;
667
661
}
668
662
s = StringUtil .getString (items );
669
663
}
670
- return setValues ( s ) ;
664
+ return s ;
671
665
}
672
666
@ Override
673
- public AbstractSQLConfig setValues (String values ) {
674
- this .values = values ;
667
+ public AbstractSQLConfig setValues (List < List < Object >> valuess ) {
668
+ this .values = valuess ;
675
669
return this ;
676
670
}
677
671
@@ -1120,7 +1114,7 @@ public String getEqualString(String key, Object value) {
1120
1114
if (value instanceof Collection <?>) {
1121
1115
throw new IllegalArgumentException (key + ":value 中value不合法!非PUT请求只支持 [Boolean, Number, String] 内的类型 !" );
1122
1116
}
1123
-
1117
+
1124
1118
boolean not = key .endsWith ("!" ); // & | 没有任何意义,写法多了不好控制
1125
1119
if (not ) {
1126
1120
key = key .substring (0 , key .length () - 1 );
@@ -1520,7 +1514,7 @@ public String getSetString() throws Exception {
1520
1514
public String getSetString (RequestMethod method , Map <String , Object > content , boolean verifyName ) throws Exception {
1521
1515
Set <String > set = content == null ? null : content .keySet ();
1522
1516
String setString = "" ;
1523
-
1517
+
1524
1518
if (set != null && set .size () > 0 ) {
1525
1519
String quote = getQuote ();
1526
1520
@@ -1548,7 +1542,7 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
1548
1542
isFirst = false ;
1549
1543
}
1550
1544
}
1551
-
1545
+
1552
1546
if (setString .isEmpty ()) {
1553
1547
throw new IllegalArgumentException ("PUT 请求必须在Table内设置要修改的 key:value !" );
1554
1548
}
@@ -1846,15 +1840,15 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
1846
1840
column = KEY_ID + "," + StringUtil .getString (columns ); //set已经判断过不为空
1847
1841
final int size = columns .length + 1 ; //以key数量为准
1848
1842
1849
- Object [][] valuess = new Object [ idList .size ()][] ; // [idList.size()][]
1850
- Object [] items ; //(item0, item1, ...)
1843
+ List < List < Object >> valuess = new ArrayList <>( idList .size ()) ; // [idList.size()][]
1844
+ List < Object > items ; //(item0, item1, ...)
1851
1845
for (int i = 0 ; i < idList .size (); i ++) {
1852
- items = new Object [ size ] ;
1853
- items [ 0 ] = idList .get (i ); //第0个就是id
1846
+ items = new ArrayList <>( size ) ;
1847
+ items . add ( idList .get (i ) ); //第0个就是id
1854
1848
for (int j = 1 ; j < size ; j ++) {
1855
- items [ j ] = values [j -1 ]; //从第1个开始,允许"null"
1849
+ items . add ( values [j -1 ]) ; //从第1个开始,允许"null"
1856
1850
}
1857
- valuess [ i ] = items ;
1851
+ valuess . add ( items ) ;
1858
1852
}
1859
1853
config .setValues (valuess );
1860
1854
}
@@ -1968,7 +1962,24 @@ else if (whereList != null && whereList.contains(key)) {
1968
1962
config .setContent (tableContent );
1969
1963
}
1970
1964
1965
+ List <String > cs = new ArrayList <>();
1966
+ String [] fks = StringUtil .split (column , ";" ); // key0,key1;fun0(key0,...);fun1(key0,...);key3;fun2(key0,...)
1967
+ if (fks != null ) {
1968
+ String [] ks ;
1969
+ for (String fk : fks ) {
1970
+ if (fk .contains ("(" )) { //fun0(key0,...)
1971
+ cs .add (fk );
1972
+ }
1973
+ else { //key0,key1...
1974
+ ks = StringUtil .split (fk );
1975
+ if (ks != null && ks .length > 0 ) {
1976
+ cs .addAll (Arrays .asList (ks ));
1977
+ }
1978
+ }
1979
+ }
1980
+ }
1971
1981
1982
+ config .setColumn (cs );
1972
1983
config .setWhere (tableWhere );
1973
1984
1974
1985
config .setId (id == null ? 0 : id );
@@ -1977,7 +1988,6 @@ else if (whereList != null && whereList.contains(key)) {
1977
1988
config .setRole (role );
1978
1989
config .setDatabase (database );
1979
1990
config .setSchema (schema );
1980
- config .setColumn (column );
1981
1991
config .setGroup (group );
1982
1992
config .setHaving (having );
1983
1993
config .setOrder (order );
@@ -2022,10 +2032,10 @@ public static List<Join> parseJoin(RequestMethod method, List<Join> joinList, Ca
2022
2032
LEFT JOIN ( SELECT count(*) AS count FROM sys.Comment ) AS Comment ON Comment.momentId = Moment.id LIMIT 1 OFFSET 0 */
2023
2033
if (RequestMethod .isHeadMethod (method , true )) {
2024
2034
joinConfig .setMethod (GET ); //子查询不能为 SELECT count(*) ,而应该是 SELECT momentId
2025
- joinConfig .setColumn (j .getKey ()); //优化性能,不取非必要的字段
2035
+ joinConfig .setColumn (Arrays . asList ( j .getKey () )); //优化性能,不取非必要的字段
2026
2036
2027
2037
cacheConfig .setMethod (GET ); //子查询不能为 SELECT count(*) ,而应该是 SELECT momentId
2028
- cacheConfig .setColumn (j .getKey ()); //优化性能,不取非必要的字段
2038
+ cacheConfig .setColumn (Arrays . asList ( j .getKey () )); //优化性能,不取非必要的字段
2029
2039
}
2030
2040
2031
2041
j .setJoinConfig (joinConfig );
0 commit comments