From ceca88724389f14d66f686dbbb414a62429b2337 Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Mon, 10 Oct 2022 03:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20JOIN=20=E5=BD=93=E4=B8=BB?= =?UTF-8?q?=E5=89=AF=E8=A1=A8=E9=83=BD=E6=9C=89=E6=9D=A1=E4=BB=B6=E3=80=81?= =?UTF-8?q?=E5=AD=90=E6=9F=A5=E8=AF=A2=E5=BD=93=E5=86=85=E5=A4=96=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E9=83=BD=E6=9C=89=E6=9D=A1=E4=BB=B6=E6=97=B6=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E9=A2=84=E7=BC=96=E8=AF=91=E5=80=BC=E9=94=99=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/apijson/orm/AbstractSQLConfig.java | 75 +++++++++++++------ 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java index 7f9fb0d1c..9753b4b02 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java @@ -3808,11 +3808,33 @@ public String getSubqueryString(Subquery subquery) throws Exception { cfg.setPreparedValueList(new ArrayList<>()); String sql = (range == null || range.isEmpty() ? "" : range) + "(" + cfg.getSQL(isPrepared()) + ") "; - List origPreparedValueList = preparedValueList; - preparedValueList = cfg.getPreparedValueList(); - preparedValueList.addAll(origPreparedValueList); + //// SELECT .. FROM(SELECT ..) .. WHERE .. 格式需要把子查询中的预编译值提前 + //// 如果外查询 SELECT concat(`name`,?) 这种 SELECT 里也有预编译值,那就不能这样简单反向 + //List subPvl = cfg.getPreparedValueList(); + //if (subPvl != null && subPvl.isEmpty() == false) { + // List pvl = getPreparedValueList(); + // + // if (pvl != null && pvl.isEmpty() == false) { + // subPvl.addAll(pvl); + // } + // setPreparedValueList(subPvl); + //} + + List subPvl = cfg.getPreparedValueList(); + if (subPvl != null && subPvl.isEmpty() == false) { + List pvl = getPreparedValueList(); + + if (pvl == null || pvl.isEmpty()) { + pvl = subPvl; + } + else { + pvl.addAll(subPvl); + } - return sql; + setPreparedValueList(pvl); + } + + return sql; } //key@:{} Subquery >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @@ -3830,7 +3852,7 @@ public static String getCondition(boolean not, String condition) { /**拼接条件 * @param not * @param condition - * @param outerBreaket + * @param addOuterBracket * @return */ public static String getCondition(boolean not, String condition, boolean addOuterBracket) { @@ -3974,7 +3996,7 @@ public static String getSQL(AbstractSQLConfig config) throws Exception { //TODO procedure 改为 List procedureList; behind : true; function: callFunction(); String key; ... // for (...) { Call procedure1();\n SQL \n; Call procedure2(); ... } - // 貌似不需要,因为 ObjecParser 里就已经处理的顺序等,只是这里要解决下 Schema 问题。 + // 貌似不需要,因为 ObjectParser 里就已经处理的顺序等,只是这里要解决下 Schema 问题。 String sch = config.getSQLSchema(); if (StringUtil.isNotEmpty(config.getProcedure(), true)) { @@ -4014,14 +4036,14 @@ public static String getSQL(AbstractSQLConfig config) throws Exception { //When config's database is oracle,Using subquery since Oracle12 below does not support OFFSET FETCH paging syntax. //针对oracle分组后条数的统计 if (StringUtil.isNotEmpty(config.getGroup(),true) && RequestMethod.isHeadMethod(config.getMethod(), true)){ - return explain + "SELECT count(*) FROM (SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + ") " + config.getLimitString(); + return explain + "SELECT count(*) FROM (SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(tablePath, config) + ") " + config.getLimitString(); } - String sql = "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config); + String sql = "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(tablePath, config); return explain + config.getOraclePageSql(sql); } - return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + config.getLimitString(); + return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(tablePath, config) + config.getLimitString(); } } @@ -4044,13 +4066,15 @@ protected String getOraclePageSql(String sql) { * @return * @throws Exception */ - private static String getConditionString(String column, String table, AbstractSQLConfig config) throws Exception { - String where = config.getWhereString(true); + private static String getConditionString(String table, AbstractSQLConfig config) throws Exception { + Subquery from = config.getFrom(); + if (from != null) { + table = config.getSubqueryString(from) + " AS " + config.getAliasWithQuote() + " "; + } - Subquery from = config.getFrom(); - if (from != null) { - table = config.getSubqueryString(from) + " AS " + config.getAliasWithQuote() + " "; - } + String join = config.getJoinString(); + + String where = config.getWhereString(true); //根据方法不同,聚合语句不同。GROUP BY 和 HAVING 可以加在 HEAD 上, HAVING 可以加在 PUT, DELETE 上,GET 全加,POST 全都不加 String aggregation; @@ -4067,7 +4091,7 @@ else if (config.getMethod() == PUT || config.getMethod() == DELETE) { aggregation = ""; } - String condition = table + config.getJoinString() + where + aggregation; + String condition = table + join + where + aggregation; ; //+ config.getLimitString(); //no need to optimize @@ -4123,8 +4147,8 @@ public String getJoinString() throws Exception { if (joinList != null) { String quote = getQuote(); - List pvl = new ArrayList<>(); - boolean changed = false; + List pvl = getPreparedValueList(); // new ArrayList<>(); + //boolean changed = false; // 主表不用别名 String ta; for (Join j : joinList) { @@ -4167,7 +4191,7 @@ public String getJoinString() throws Exception { jc.setMain(false).setKeyPrefix(true); pvl.addAll(jc.getPreparedValueList()); - changed = true; + //changed = true; break; case "&": // INNER JOIN: A & B @@ -4197,17 +4221,20 @@ public String getJoinString() throws Exception { ow = oc.getWhereString(false); pvl.addAll(oc.getPreparedValueList()); - changed = true; + //changed = true; } joinOns += " \n " + sql + (StringUtil.isEmpty(ow, true) ? "" : " AND ( " + ow + " ) "); } - if (changed) { - pvl.addAll(preparedValueList); - preparedValueList = pvl; - } + //if (changed) { + // List opvl = getPreparedValueList(); + // if (opvl != null && opvl.isEmpty() == false) { + // pvl.addAll(opvl); + // } + setPreparedValueList(pvl); + //} }