Skip to content

Commit 5b7d54a

Browse files
committed
优化代码
1 parent b9da3fb commit 5b7d54a

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

APIJSONORM/src/main/java/apijson/RequestMethod.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public enum RequestMethod {
5252
* @return
5353
*/
5454
public static boolean isGetMethod(RequestMethod method, boolean containPrivate) {
55-
boolean is = method == null || method == GET;
56-
return containPrivate == false ? is : is || method == GETS;
55+
return method == null || method == GET || (containPrivate && method == GETS);
5756
}
5857

5958
/**是否为HEAD请求方法
@@ -62,8 +61,7 @@ public static boolean isGetMethod(RequestMethod method, boolean containPrivate)
6261
* @return
6362
*/
6463
public static boolean isHeadMethod(RequestMethod method, boolean containPrivate) {
65-
boolean is = method == HEAD;
66-
return containPrivate == false ? is : is || method == HEADS;
64+
return method == HEAD || (containPrivate && method == HEADS);
6765
}
6866

6967
/**是否为查询的请求方法

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static apijson.RequestMethod.DELETE;
2828
import static apijson.RequestMethod.GET;
2929
import static apijson.RequestMethod.GETS;
30-
import static apijson.RequestMethod.HEAD;
3130
import static apijson.RequestMethod.HEADS;
3231
import static apijson.RequestMethod.POST;
3332
import static apijson.RequestMethod.PUT;
@@ -3942,28 +3941,30 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
39423941
if (config.isOracle()) {
39433942
//When config's database is oracle,Using subquery since Oracle12 below does not support OFFSET FETCH paging syntax.
39443943
//针对oracle分组后条数的统计
3945-
if ((config.getMethod() == HEAD || config.getMethod() == HEADS)
3946-
&& StringUtil.isNotEmpty(config.getGroup(),true)){
3944+
if (StringUtil.isNotEmpty(config.getGroup(),true) && RequestMethod.isHeadMethod(config.getMethod(), true)){
39473945
return explain + "SELECT count(*) FROM (SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + ") " + config.getLimitString();
39483946
}
3947+
39493948
String sql = "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config);
3950-
return explain + config.getOraclePageSql(config, sql);
3949+
return explain + config.getOraclePageSql(sql);
39513950
}
3951+
39523952
return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + config.getLimitString();
39533953
}
3954-
}
3955-
3954+
}
3955+
39563956
/**Oracle的分页获取
39573957
* @param config
39583958
* @param sql
39593959
* @return
39603960
*/
3961-
private String getOraclePageSql(AbstractSQLConfig config, String sql) {
3962-
int offset = getOffset(config.getPage(), config.getCount());
3963-
String pageSql;
3964-
pageSql = "SELECT * FROM (SELECT t.*,ROWNUM RN FROM (" + sql + ") t WHERE ROWNUM <= " + (offset + count) + ") WHERE RN > " + offset;
3965-
return pageSql;
3966-
}
3961+
protected String getOraclePageSql(String sql) {
3962+
int count = getCount();
3963+
int offset = getOffset(getPage(), count);
3964+
String alias = getAliasWithQuote();
3965+
3966+
return "SELECT * FROM (SELECT " + alias + ".*, ROWNUM RN FROM (" + sql + ") " + alias + " WHERE ROWNUM <= " + (offset + count) + ") WHERE RN > " + offset;
3967+
}
39673968

39683969
/**获取条件SQL字符串
39693970
* @param column
@@ -3981,16 +3982,19 @@ private static String getConditionString(String column, String table, AbstractSQ
39813982
}
39823983

39833984
//根据方法不同,聚合语句不同。GROUP BY 和 HAVING 可以加在 HEAD 上, HAVING 可以加在 PUT, DELETE 上,GET 全加,POST 全都不加
3984-
String aggregation = "";
3985+
String aggregation;
39853986
if (RequestMethod.isGetMethod(config.getMethod(), true)) {
39863987
aggregation = config.getGroupString(true) + config.getHavingString(true) + config.getOrderString(true);
39873988
}
3988-
if (RequestMethod.isHeadMethod(config.getMethod(), true)) { // TODO 加参数 isPagenation 判断是 GET 内分页 query:2 查总数,不用加这些条件
3989+
else if (RequestMethod.isHeadMethod(config.getMethod(), true)) { // TODO 加参数 isPagenation 判断是 GET 内分页 query:2 查总数,不用加这些条件
39893990
aggregation = config.getGroupString(true) + config.getHavingString(true) ;
39903991
}
3991-
if (config.getMethod() == PUT || config.getMethod() == DELETE) {
3992+
else if (config.getMethod() == PUT || config.getMethod() == DELETE) {
39923993
aggregation = config.getHavingString(true) ;
39933994
}
3995+
else {
3996+
aggregation = "";
3997+
}
39943998

39953999
String condition = table + config.getJoinString() + where + aggregation;
39964000
; //+ config.getLimitString();

0 commit comments

Comments
 (0)