@@ -298,28 +298,33 @@ public String getTablePath() {
298298 String sqlTable = getSQLTable ();
299299 String sch = getSQLSchema (sqlTable );
300300
301- return (StringUtil .isEmpty (sch , true ) ? "" : q + sch + q + "." ) + q + sqlTable + q + ( isKeyPrefix () ? " AS " + getAlias () : "" );
301+ return (StringUtil .isEmpty (sch , true ) ? "" : q + sch + q + "." ) + q + sqlTable + q + ( isKeyPrefix () ? " AS " + getAliasWithQuote () : "" );
302302 }
303303 @ Override
304304 public AbstractSQLConfig setTable (String table ) { //Table已经在Parser中校验,所以这里不用防SQL注入
305305 this .table = table ;
306306 return this ;
307307 }
308+
308309 @ Override
309310 public String getAlias () {
310- if (StringUtil .isEmpty (alias , true )) {
311- alias = getTable ();
312- }
313- String q = getQuote ();
314- //getTable 不能小写,因为Verifier用大小写敏感的名称判断权限
315- //如果要强制小写,则可在子类重写这个方法再 toLowerCase return q + (DATABASE_POSTGRESQL.equals(getDatabase()) ? alias.toLowerCase() : alias) + q;
316- return q + alias + q ;
311+ return alias ;
317312 }
318313 @ Override
319314 public AbstractSQLConfig setAlias (String alias ) {
320315 this .alias = alias ;
321316 return this ;
322317 }
318+ public String getAliasWithQuote () {
319+ String a = getAlias ();
320+ if (StringUtil .isEmpty (a , true )) {
321+ a = getTable ();
322+ }
323+ String q = getQuote ();
324+ //getTable 不能小写,因为Verifier用大小写敏感的名称判断权限
325+ //如果要强制小写,则可在子类重写这个方法再 toLowerCase return q + (DATABASE_POSTGRESQL.equals(getDatabase()) ? a.toLowerCase() : a) + q;
326+ return q + a + q ;
327+ }
323328
324329 @ Override
325330 public String getGroup () {
@@ -347,7 +352,9 @@ public String getGroupString(boolean hasPrefix) {
347352 }
348353
349354 cfg = j .isLeftOrRightJoin () ? j .getOutterConfig () : j .getJoinConfig ();
350- cfg .setAlias (cfg .getTable ());
355+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
356+ cfg .setAlias (cfg .getTable ());
357+ }
351358
352359 c = ((AbstractSQLConfig ) cfg ).getGroupString (false );
353360 if (StringUtil .isEmpty (c , true ) == false ) {
@@ -407,7 +414,9 @@ public String getHavingString(boolean hasPrefix) {
407414 }
408415
409416 cfg = j .isLeftOrRightJoin () ? j .getOutterConfig () : j .getJoinConfig ();
410- cfg .setAlias (cfg .getTable ());
417+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
418+ cfg .setAlias (cfg .getTable ());
419+ }
411420
412421 c = ((AbstractSQLConfig ) cfg ).getHavingString (false );
413422 if (StringUtil .isEmpty (c , true ) == false ) {
@@ -515,7 +524,9 @@ public String getOrderString(boolean hasPrefix) {
515524 }
516525
517526 cfg = j .isLeftOrRightJoin () ? j .getOutterConfig () : j .getJoinConfig ();
518- cfg .setAlias (cfg .getTable ());
527+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
528+ cfg .setAlias (cfg .getTable ());
529+ }
519530
520531 c = ((AbstractSQLConfig ) cfg ).getOrderString (false );
521532 if (StringUtil .isEmpty (c , true ) == false ) {
@@ -648,8 +659,10 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
648659 else {
649660 cfg = j .getJoinConfig ();
650661 }
651-
652- cfg .setAlias (cfg .getTable ());
662+
663+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
664+ cfg .setAlias (cfg .getTable ());
665+ }
653666
654667 c = ((AbstractSQLConfig ) cfg ).getColumnString (true );
655668 if (StringUtil .isEmpty (c , true ) == false ) {
@@ -661,7 +674,7 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
661674 }
662675 }
663676
664- String tableAlias = getAlias ();
677+ String tableAlias = getAliasWithQuote ();
665678
666679 // String c = StringUtil.getString(column); //id,name;json_length(contactIdList):contactCount;...
667680
@@ -1399,7 +1412,7 @@ public String getCompareString(String key, Object value, String type) throws Exc
13991412
14001413 public String getKey (String key ) {
14011414 String q = getQuote ();
1402- return (isKeyPrefix () ? getAlias () + "." : "" ) + q + key + q ;
1415+ return (isKeyPrefix () ? getAliasWithQuote () + "." : "" ) + q + key + q ;
14031416 }
14041417
14051418 /**
@@ -2001,7 +2014,7 @@ private static String getConditionString(String column, String table, AbstractSQ
20012014
20022015 Subquery from = config .getFrom ();
20032016 if (from != null ) {
2004- table = config .getSubqueryString (from ) + " AS " + config .getAlias () + " " ; //TODO Comment:c 转为 AS `Comment:c`
2017+ table = config .getSubqueryString (from ) + " AS " + config .getAliasWithQuote () + " " ;
20052018 }
20062019
20072020 String condition = table + config .getJoinString () + where + (
@@ -2081,7 +2094,7 @@ public String getJoinString() throws Exception {
20812094 jc = j .getJoinConfig ();
20822095 jc .setPrepared (isPrepared ());
20832096
2084- jt = jc . getTable (); //FIXME getAlias 不能加 `` StringUtil.isEmpty(jc.getAlias(), true) ? jc.getTable() : jc.getAlias();
2097+ jt = StringUtil .isEmpty (jc .getAlias (), true ) ? jc .getTable () : jc .getAlias ();
20852098 tn = j .getTargetName ();
20862099
20872100 //如果要强制小写,则可在子类重写这个方法再 toLowerCase
@@ -2144,14 +2157,16 @@ public String getJoinString() throws Exception {
21442157 * @return
21452158 * @throws Exception
21462159 */
2147- public static AbstractSQLConfig newSQLConfig (RequestMethod method , String table , JSONObject request , List <Join > joinList , boolean isProcedure , Callback callback ) throws Exception {
2160+ public static AbstractSQLConfig newSQLConfig (RequestMethod method , String table , String alias , JSONObject request , List <Join > joinList , boolean isProcedure , Callback callback ) throws Exception {
21482161 if (request == null ) { // User:{} 这种空内容在查询时也有效
21492162 throw new NullPointerException (TAG + ": newSQLConfig request == null!" );
21502163 }
2151- AbstractSQLConfig config = callback .getSQLConfig (method , table );
2152-
21532164 String database = request .getString (KEY_DATABASE );
21542165 String schema = request .getString (KEY_SCHEMA );
2166+
2167+ AbstractSQLConfig config = callback .getSQLConfig (method , database , schema , table );
2168+ config .setAlias (alias );
2169+
21552170 config .setDatabase (database ); //不删,后面表对象还要用的,必须放在 parseJoin 前
21562171 config .setSchema (schema ); //不删,后面表对象还要用的
21572172
@@ -2168,9 +2183,9 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
21682183 return config ;
21692184 }
21702185
2171- String idKey = callback .getIdKey (schema , table );
2186+ String idKey = callback .getIdKey (database , schema , table );
21722187 String idInKey = idKey + "{}" ;
2173- String userIdKey = callback .getUserIdKey (schema , table );
2188+ String userIdKey = callback .getUserIdKey (database , schema , table );
21742189 String userIdInKey = userIdKey + "{}" ;
21752190
21762191 Object idIn = request .get (idInKey ); //可能是 id{}:">0"
@@ -2181,7 +2196,7 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
21812196 throw new IllegalArgumentException ("POST请求,生成多条记录请用 id{}:[] ! [] 类型为JSONArray且不能为空!" );
21822197 }
21832198 } else if (request .get (idKey ) == null ) {
2184- request .put (idKey , callback .newId (method , table ));
2199+ request .put (idKey , callback .newId (method , database , schema , table ));
21852200 }
21862201 }
21872202
@@ -2478,11 +2493,13 @@ public static AbstractSQLConfig parseJoin(RequestMethod method, AbstractSQLConfi
24782493
24792494
24802495 String name ;
2496+ String alias ;
24812497 for (Join j : joinList ) {
24822498 name = j .getName ();
2499+ alias = j .getAlias ();
24832500 //JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
2484- SQLConfig joinConfig = newSQLConfig (method , name , j .getTable (), null , false , callback );
2485- SQLConfig cacheConfig = newSQLConfig (method , name , j .getTable (), null , false , callback ).setCount (1 );
2501+ SQLConfig joinConfig = newSQLConfig (method , name , alias , j .getTable (), null , false , callback );
2502+ SQLConfig cacheConfig = newSQLConfig (method , name , alias , j .getTable (), null , false , callback ).setCount (1 );
24862503
24872504 if (j .isAppJoin () == false ) { //除了 @ APP JOIN,其它都是 SQL JOIN,则副表要这样配置
24882505 if (joinConfig .getDatabase () == null ) {
@@ -2504,7 +2521,7 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
25042521 joinConfig .setMain (false ).setKeyPrefix (true );
25052522
25062523 if (j .isLeftOrRightJoin ()) {
2507- SQLConfig outterConfig = newSQLConfig (method , name , j .getOutter (), null , false , callback );
2524+ SQLConfig outterConfig = newSQLConfig (method , name , alias , j .getOutter (), null , false , callback );
25082525 outterConfig .setMain (false ).setKeyPrefix (true ).setDatabase (joinConfig .getDatabase ()).setSchema (joinConfig .getSchema ()); //解决主表 JOIN 副表,引号不一致
25092526 j .setOutterConfig (outterConfig );
25102527 }
@@ -2645,49 +2662,55 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
26452662 public static interface Callback {
26462663 /**获取 SQLConfig 的实例
26472664 * @param method
2665+ * @param database
2666+ * @param schema
26482667 * @param table
26492668 * @return
26502669 */
2651- AbstractSQLConfig getSQLConfig (RequestMethod method , String table );
2670+ AbstractSQLConfig getSQLConfig (RequestMethod method , String database , String schema , String table );
26522671
26532672
26542673 /**为 post 请求新建 id, 只能是 Long 或 String
26552674 * @param method
2675+ * @param database
2676+ * @param schema
26562677 * @param table
26572678 * @return
26582679 */
2659- Object newId (RequestMethod method , String table );
2680+ Object newId (RequestMethod method , String database , String schema , String table );
26602681
26612682 /**获取主键名
2683+ * @param database
26622684 * @param schema
26632685 * @param table
26642686 * @return
26652687 */
2666- String getIdKey (String schema , String table );
2688+ String getIdKey (String database , String schema , String table );
26672689
26682690 /**获取 User 的主键名
2691+ * @param database
26692692 * @param schema
26702693 * @param table
26712694 * @return
26722695 */
2673- String getUserIdKey (String schema , String table );
2696+ String getUserIdKey (String database , String schema , String table );
26742697 }
26752698
26762699 public static abstract class SimpleCallback implements Callback {
26772700
26782701
26792702 @ Override
2680- public Object newId (RequestMethod method , String table ) {
2703+ public Object newId (RequestMethod method , String database , String schema , String table ) {
26812704 return System .currentTimeMillis ();
26822705 }
26832706
26842707 @ Override
2685- public String getIdKey (String schema , String table ) {
2708+ public String getIdKey (String database , String schema , String table ) {
26862709 return KEY_ID ;
26872710 }
26882711
26892712 @ Override
2690- public String getUserIdKey (String schema , String table ) {
2713+ public String getUserIdKey (String database , String schema , String table ) {
26912714 return KEY_USER_ID ;
26922715 }
26932716
0 commit comments