From 91f58ac34a01db7bb4e30a57af4454e0c36fd1c9 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期日, 14 六月 2020 13:06:21 +0800
Subject: [PATCH] 处理服务启动失败问题
---
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java | 96 +++++++++++++++++++++++++++++++++---------------
1 files changed, 66 insertions(+), 30 deletions(-)
diff --git a/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java b/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
index 28688d1..a17b616 100644
--- a/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
+++ b/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
@@ -4,19 +4,24 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
-import com.java110.common.cache.ServiceSqlCache;
-import com.java110.common.constant.CommonConstant;
-import com.java110.common.constant.ResponseConstant;
-import com.java110.common.exception.BusinessException;
+import com.java110.utils.cache.ServiceSqlCache;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.ResponseConstant;
+import com.java110.utils.exception.BusinessException;
import com.java110.core.factory.DataTransactionFactory;
-import com.java110.common.log.LoggerEngine;
-import com.java110.common.util.Assert;
-import com.java110.common.util.StringUtil;
-import com.java110.entity.service.DataQuery;
+import com.java110.utils.log.LoggerEngine;
+import com.java110.utils.util.Assert;
+import com.java110.utils.util.StringUtil;
+import com.java110.service.context.DataQuery;
import com.java110.entity.service.ServiceSql;
import com.java110.service.dao.IQueryServiceDAO;
import com.java110.service.smo.IQueryServiceSMO;
import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.ognl.Ognl;
+import org.apache.ibatis.ognl.OgnlException;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -177,20 +182,12 @@
Interpreter interpreter = new Interpreter();
interpreter.eval(javaCode);
- /* String param = "";
- for (String key : params.keySet()) {
- param += (params.getString(key) + ",");
- }
-
- if (param.endsWith(",")) {
- param = param.substring(0, param.length() - 1);
- }*/
-
+ interpreter.set("dataQuery", dataQuery);
dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
- "鎴愬姛", JSONObject.parseObject(interpreter.eval("execute(" + dataQuery + ")").toString())));
+ "鎴愬姛", JSONObject.parseObject(interpreter.eval("execute(dataQuery)").toString())));
} catch (Exception e) {
logger.error("鏁版嵁浜や簰寮傚父锛�", e);
- throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "鏁版嵁浜や簰寮傚父,"+e.getMessage());
+ throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "鏁版嵁浜や簰寮傚父," + e.getMessage());
}
}
@@ -252,7 +249,10 @@
String currentSql = sqlObj.getString(dataQuery.getTemplateKey());
//澶勭悊 if 鍒ゆ柇
- currentSql = dealSqlIf(currentSql);
+ logger.debug("dealSqlIf寮�濮嬪鐞唖ql涓殑<if>鑺傜偣 " + currentSql + " 鍏ュ弬:" + params.toJSONString());
+ currentSql = dealSqlIf(currentSql, params);
+ logger.debug("dealSqlIf澶勭悊瀹屾垚sql涓殑<if>鑺傜偣 " + currentSql + " 鍏ュ弬:" + params.toJSONString());
+
String[] sqls = currentSql.split("#");
String currentSqlNew = "";
@@ -284,7 +284,7 @@
param = params.getInteger(sqls[sqlIndex]);
}
//杩欓噷瀵� page 鍜� rows 鐗规畩澶勭悊 锛岀洰鍓嶆病鏈夋兂鍒板叾浠栫殑鍔炴硶
- if (StringUtils.isNumeric(param.toString()) && "page,rows".contains(sqls[sqlIndex])) {
+ if (StringUtils.isNumeric(param.toString()) && "page,rows,row".contains(sqls[sqlIndex])) {
param = Integer.parseInt(param.toString());
}
currentParams.add(param);
@@ -325,22 +325,58 @@
/**
* 澶勭悊SQL璇彞
- * @param oldSql
- * select * from s_a a
- * where if(name != null && name != ''){
- * a.name = #name#
- * }
+ *
+ * @param oldSql select * from s_a a
+ * where <if test="name != null && name != ''">
+ * a.name = #name#
+ * </if>
* @return
*/
- private String dealSqlIf(String oldSql){
- String newSql = "";
+ public String dealSqlIf(String oldSql, JSONObject requestParams) throws DocumentException, OgnlException {
+ StringBuffer newSql = new StringBuffer();
+ String tmpSql = "";
+ Boolean conditionResult = false;
// 鏈寘鍚� 鏉′欢璇彞
- if(!oldSql.replace(" ","").contains("if(")){
+ if (!oldSql.contains("<if")) {
return oldSql;
}
+ String[] oSqls = oldSql.split("</if>");
+ for (String oSql : oSqls) {
+ logger.debug("澶勭悊if 鑺傜偣锛屽綋鍓嶅鐞嗙殑oSql=" + oSql + "鎬荤殑oSqls = " + oSqls);
+ if(StringUtil.isNullOrNone(oSql) || !oSql.contains("<if")){
+ newSql.append(oSql);
+ continue;
+ }
+ if (!oSql.startsWith("<if")) {
+ newSql.append(oSql.substring(0, oSql.indexOf("<if")));
+ }
- return newSql;
+ tmpSql = oSql.substring(oSql.indexOf("<if")) + "</if>";
+
+ Element root = DocumentHelper.parseText(tmpSql).getRootElement();
+
+ String condition = root.attribute("test").getValue();
+
+ Object condObj = Ognl.parseExpression(condition);
+
+ Object value = Ognl.getValue(condObj, requestParams);
+
+ if (value instanceof Boolean) {
+ conditionResult = (Boolean) value;
+ } else {
+ throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "閰嶇疆閿欒锛宨f璇彞閰嶇疆閿欒 " + condition);
+ }
+
+ if (conditionResult) {
+ newSql.append(root.getText());
+ }
+
+
+ }
+
+
+ return newSql.toString().replace(">", ">").replace("<", "<");
}
--
Gitblit v1.8.0