Browse Source

修改增加去除锁的方法

zk 6 months ago
parent
commit
688a16ec7f

+ 4 - 1
src/main/java/com/citygis/controller/WSForSqlServerController.java

@@ -27,5 +27,8 @@ public class WSForSqlServerController {
     public void getDataBysql(@RequestParam("data")String data, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         wSForSql.getPost(data, request, response);
     }
-
+    @PostMapping("/WSForSql2")
+    public String getDataBysql2(@RequestParam("data")String data) throws ServletException, IOException {
+       return wSForSql.getPost2(data);
+    }
 }

+ 249 - 0
src/main/java/com/citygis/impl/WSForSqlServiceImpl.java

@@ -3,10 +3,12 @@ package com.citygis.impl;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.citygis.pojo.TransVo;
 import com.citygis.service.WSForSqlService;
 import org.apache.commons.codec.binary.Base64;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Service;
+import org.springframework.util.ObjectUtils;
 
 import javax.annotation.Resource;
 import javax.servlet.ServletException;
@@ -94,6 +96,60 @@ public class WSForSqlServiceImpl extends Thread implements WSForSqlService {
         response.getWriter().print(queryResult);
     }
 
+    @Override
+    public String getPost2(String data) throws ServletException, IOException {
+
+        TransVo transVo =new TransVo();
+
+        String queryResult = QueryDataTrans(data,transVo);
+        queryResult = queryResult.replaceAll("\r|\n", " ");
+//        response.setHeader("Access-Control-Allow-Origin", "*");
+//        response.setCharacterEncoding("UTF-8");
+//        response.setContentType("text/html;charset=UTF-8");
+        //操作失败处理
+        if (!ObjectUtils.isEmpty(transVo.getErrorMessage())){
+            queryResult = "[{\"IsSuc\":\"" + "Fault" + "\",\"Msg\":\"" + transVo.getErrorMessage().replaceAll("'"," ").replaceAll("\\\\","\\\\\\\\") + "\",\"AllCount\":\"" + 0 + "\",\"pageIndex\":\"" + 0 + "\",\"CurrCount\":\"1\",\"pkid\":\"\",\"data\":" + "[{}]" + "}]";
+            String res = "Fault";
+            try {
+                res = queryResult.substring(11, 17);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            String tableName_Log = "";
+            Calendar calendar = Calendar.getInstance();
+            int year = calendar.get(Calendar.YEAR);
+            tableName_Log = getStoreTable(String.valueOf(year)); //2022-10-14修改
+            queryResult = queryResult.replaceAll("'", "\"");
+            if (queryResult.length() >= 4000) {
+                queryResult = queryResult.substring(0, 3998);
+            }
+            transVo.setAnalyseSql(transVo.getAnalyseSql().replaceAll("'", "\""));
+            if (transVo.getAnalyseSql().length() >= 4000) {
+                transVo.setAnalyseSql(transVo.getAnalyseSql().substring(0, 3998));
+            }
+            res = res.replaceAll("'", "\"");
+            if (res.length() >= 300) {
+                res = transVo.getAnalyseSql().substring(0, 299);
+            }
+//            //日志记录
+//            String sqlStr = " insert into " + tableName_Log + " (token,操作,传输数据,操作表,备注,result,sqlstr,ip) "
+//                    + "values ('" + token + "','" + operation + "','" + variable + "','" + tablename + "','" + queryResult + "','" + res + "','" + AnalyseSql + "','" + ip + "')";
+//            String Result_RZ = dbUtilSqlServer.UpadteDataBase(sqlStr);
+//            //日志插入失败 再次插入
+//            try {
+//                Result_RZ = Result_RZ.substring(11, 17);
+//                if ("Fault\"".equals(Result_RZ)) {
+//                    dbUtilSqlServer.UpadteDataBase(sqlStr);
+//                }
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+        }
+//        response.setLocale(new java.util.Locale("zh", "CN"));
+//        response.getWriter().print(queryResult);
+        return  queryResult;
+    }
+
 
     private String QueryData(String variable) {
         ErrorMessage = "";
@@ -293,6 +349,199 @@ public class WSForSqlServiceImpl extends Thread implements WSForSqlService {
         return queryResult;
     }
 
+    private String QueryDataTrans(String variable,TransVo transVo) {
+
+        try {
+            try {
+            } catch (Exception e) {
+                e.printStackTrace();
+                transVo.setErrorMessage("data参数错误或数量为空!");
+                return "";
+            }
+            if ("".equals(variable)) {
+                transVo.setErrorMessage("data参数数量为空!");
+                return "";
+            }
+            variable = variable.replaceAll(" ", "+");
+            Base64 base64 = new Base64();
+            variable = new String(base64.decode(variable), "utf-8").replaceAll("'","''");  //接口传参base64解析
+            JSONObject jsonObjData = JSON.parseObject(variable);
+            transVo.setToken(jsonObjData.getString("token"));
+            transVo.setOperation(jsonObjData.getString("operation").toLowerCase());
+            transVo.setTablename(jsonObjData.getString("tablename"));
+            if (transVo.getTablename().toLowerCase().contains("select") || transVo.getTablename().toLowerCase().contains("insert") || transVo.getTablename().toLowerCase().contains("update")
+                    || transVo.getTablename().toLowerCase().contains("delete") || transVo.getTablename().toLowerCase().contains("create") || transVo.getTablename().toLowerCase().contains("drop")
+                    || transVo.getTablename().toLowerCase().contains("alter")) {
+                transVo.setErrorMessage("SQL注入非法请求!!!");
+                return "";
+            }
+            if (!"EEE5FE397FBDE6BE40B825E4B803207A".equals(transVo.getToken())) {
+                transVo.setErrorMessage("token错误!");
+                return "";
+            }
+
+            JSONArray jsArrayData = jsonObjData.getJSONArray("data");//获取sql语句段json
+            for (int i = 0; i < jsArrayData.size(); i++) {
+                JSONObject jsbtemp = jsArrayData.getJSONObject(i);
+                String field = jsbtemp.getString("field");
+                String fieldtype = jsbtemp.getString("fieldtype");
+                String values = jsbtemp.getString("values");
+                String primary = jsbtemp.getString("primary");
+                String where = jsbtemp.getString("where");
+                String[] fieldtypeArray = fieldtype.split(",");
+                String insertStr = "";
+                String updateStr = "";
+                String deleteStr = "";
+                String selectStr = "";
+                String insertValuesStr = "";
+                String whereStr = "";
+                if (primary.split(",").length == where.split(",").length) { //构建where条件
+                    for (int n = 0; n < primary.split(",").length; n++) {
+                        String field2 = primary.split(",")[n].toLowerCase();
+                        if (field2.toLowerCase().contains("select") || field2.toLowerCase().contains("insert") || field2.toLowerCase().contains("update")
+                                || field2.toLowerCase().contains("delete") || field2.toLowerCase().contains("create") || field2.toLowerCase().contains("drop")
+                                || field2.toLowerCase().contains("alter")) {
+                            transVo.setErrorMessage("SQL注入非法请求!!!");
+                            return "";
+                        }
+                        String value2 = where.split(",")[n].toLowerCase();
+                        if (value2.toLowerCase().contains("select") || value2.toLowerCase().contains("insert") || value2.toLowerCase().contains("update")
+                                || value2.toLowerCase().contains("delete") || value2.toLowerCase().contains("create") || value2.toLowerCase().contains("drop")
+                                || value2.toLowerCase().contains("alter")) {
+                            transVo.setErrorMessage("SQL注入非法请求!!!");
+                            return "";
+                        }
+
+                        if (n == 0) {
+                            whereStr = " where " + primary.split(",")[n] + " = '" + where.split(",")[n] + "'";
+                        } else {
+                            whereStr += " and " + primary.split(",")[n] + " = '" + where.split(",")[n] + "'";
+                        }
+                    }
+                } else {
+                    transVo.setErrorMessage("primary,where长度不一致");
+                    return "";
+                }
+                String FieldsList = "";
+                //构建SQL语句
+                if (field.split(",").length == fieldtype.split(",").length && field.split(",").length == values.split("&;@", -1).length) {//判断field,fieldtype,values长度是否一致
+                    updateStr = "update " + transVo.getTablename() + " set ";
+                    for (int j = 0; j < fieldtypeArray.length; j++) {
+                        if ("date".equals(fieldtypeArray[j].toString())) {
+                            updateStr += field.split(",")[j] + " = convert(varchar(19),'" + values.split("&;@", -1)[j] + "',121),";
+                            insertValuesStr += "convert(varchar(19),'" + values.split("&;@", -1)[j] + "',121),";
+                            FieldsList += field.split(",")[j] + ",";
+                        } else if ("number".equals(fieldtypeArray[j].toString())) { //数字为空则不插入不更新数据字段
+                            if (!"".equals(values.split("&;@", -1)[j])) {
+                                updateStr += field.split(",")[j] + " = '" + values.split("&;@", -1)[j] + "',";
+                                insertValuesStr += "'" + values.split("&;@", -1)[j] + "',";
+                                FieldsList += field.split(",")[j] + ",";
+                            }
+                        } else {
+                            updateStr += field.split(",")[j] + " = '" + values.split("&;@", -1)[j] + "',";
+                            insertValuesStr += "'" + values.split("&;@", -1)[j] + "',";
+                            FieldsList += field.split(",")[j] + ",";
+                        }
+                    }
+                    if (fieldtypeArray.length > 0) {
+                        updateStr = updateStr.substring(0, updateStr.length() - 1);
+                        insertValuesStr = insertValuesStr.substring(0, insertValuesStr.length() - 1);
+                        FieldsList = FieldsList.substring(0, FieldsList.length() - 1);
+                    }
+                    deleteStr = "delete from " + transVo.getTablename() ;
+                    insertStr = " insert into " + transVo.getTablename()  + "(" + FieldsList + ")" + "values(" + insertValuesStr + ")";
+
+                    switch (transVo.getOperation()) {
+                        case "insert":
+                            transVo.setAnalyseSql(insertStr + ";");
+                            break;
+                        case "update":
+//                            updateStr += " ,createtime=getdate() "; //2022-10-14修改
+//                            AnalyseSql = updateStr + whereStr + ";";
+                            transVo.setAnalyseSql(updateStr + ";");
+                            break;
+                        case "delete":
+                            //deleteStr = " update "+ tablename + " set createtime = getdate(),option_type=2 "; //2022-10-14修改
+                            transVo.setAnalyseSql( deleteStr + whereStr + ";");
+                            break;
+                        case "select":
+                            transVo.setAnalyseSql(" select count(1) total from " + transVo.getTablename() + " " + whereStr);
+                            break;
+                    }
+                } else {
+                    transVo.setErrorMessage("field或fieldtype或values长度不一致");
+                    return "";
+                }
+
+            }
+            // DBUtilSqlServer dbUtilSqlServer;
+            switch (transVo.getOperation()) {
+                case "select":
+                    String queryResult = dbUtilSqlServer.QueryDataBase(transVo.getAnalyseSql());
+                    transVo.setQueryResult(queryResult);
+                    break;
+                default:
+                    queryResult = dbUtilSqlServer.UpadteDataBase(transVo.getAnalyseSql());
+                    transVo.setQueryResult(queryResult);
+                    break;
+            }
+            String res = "Fault";
+            try {
+                res = transVo.getQueryResult().substring(11, 17);
+            } catch (Exception e) {
+                e.printStackTrace();
+                // TODO: handle exception
+            }
+            String tableName_Log = "";
+            Calendar calendar = Calendar.getInstance();
+            int year = calendar.get(Calendar.YEAR);
+            tableName_Log = getStoreTable(String.valueOf(year)); //2022-10-14修改
+//			tableName_Log = "数据同步日志2";
+          String  queryResult = transVo.getQueryResult().replaceAll("'", "\"");
+            if (queryResult.length() >= 4000) {
+                transVo.setQueryResult(queryResult.substring(0, 3998));
+            }
+            String  AnalyseSql = transVo.getAnalyseSql().replaceAll("'", "\"");
+            if (AnalyseSql.length() >= 4000) {
+                transVo.setAnalyseSql(AnalyseSql.substring(0, 3998));
+            }
+            res = res.replaceAll("'", "\"");
+            if (res.length() >= 300) {
+                res = AnalyseSql.substring(0, 299);
+            }
+            //日志记录
+//            String sqlStr = " insert into " + tableName_Log + " (token,操作,传输数据,操作表,备注,result,sqlstr,ip) values ('" + token + "','" + operation + "','" + variable + "','" + tablename + "','" + queryResult + "','" + res + "','" + AnalyseSql + "','" + ip + "')";
+//
+//            String Result_RZ = dbUtilSqlServer.UpadteDataBase(sqlStr);
+            //日志插入失败 再次插入
+//            try {
+//                Result_RZ = Result_RZ.substring(11, 17);
+//                if ("Fault\"".equals(Result_RZ)) {
+//                    dbUtilSqlServer.UpadteDataBase(sqlStr);
+//                }
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            transVo.setErrorMessage(ex.toString().replaceAll("'"," ").replaceAll("\\\\","\\\\\\\\") + ex.getMessage().replaceAll("'"," ").replaceAll("\\\\","\\\\\\\\"));
+            return "";
+//			queryResult = "[{\"IsSuc\":\""+"Fault"+"\",\"Msg\":\""+exception+"\",\"AllCount\":\""+0+"\",\"pageIndex\":\""+0+"\",\"CurrCount\":\"1\",\"pkid\":\"\",\"data\":"+"[{}]"+"}]" ;
+//			String res = "Fault";
+//			try {
+//				res = queryResult.substring(11,17);
+//			}catch (Exception e) {
+//				// TODO: handle exception
+//			}
+//			BLL_SQL_Class hService = new BLL_SQL_Class();
+//			String tableName_Log = "数据同步日志_x";
+//			//日志记录
+//			String sqlStr = " insert into "+tableName_Log+" (token,操作,传输数据,操作表,备注,result,sqlstr) values ('"+token+"','"+operation+"','"+variable+"','"+tablename+"','"+queryResult+"','"+res+"','"+AnalyseSql+"')";
+//
+//			hService.UpadteDataBase(sqlStr);
+        }
+        return transVo.getQueryResult();
+    }
 
     public boolean afterDate(String date) {
         DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

+ 15 - 0
src/main/java/com/citygis/pojo/TransVo.java

@@ -0,0 +1,15 @@
+package com.citygis.pojo;
+
+import lombok.Data;
+
+@Data
+public class TransVo {
+    private String ErrorMessage ;
+    private   String variable;
+    private  String token ;
+    private  String operation;
+    private String tablename;
+    private   String queryResult;
+    private   String AnalyseSql;
+    private   String ip;
+}

+ 2 - 0
src/main/java/com/citygis/service/WSForSqlService.java

@@ -14,4 +14,6 @@ public interface WSForSqlService {
 
     void getPost(String data, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
 
+
+    String getPost2(String data) throws ServletException, IOException;
 }