|
@@ -1,11 +1,18 @@
|
|
|
package cn.seecoder.web.service.impl.sql;
|
|
package cn.seecoder.web.service.impl.sql;
|
|
|
|
|
|
|
|
|
|
+import cn.seecoder.common.exceptions.AccessDeniedException;
|
|
|
|
|
+import cn.seecoder.web.dao.pipeline.DeploymentMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineMapper;
|
|
|
|
|
+import cn.seecoder.web.model.po.pipeline.DeploymentPO;
|
|
|
import cn.seecoder.web.service.sql.SqlService;
|
|
import cn.seecoder.web.service.sql.SqlService;
|
|
|
|
|
+import cn.seecoder.web.service.user.UserService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.sql.*;
|
|
import java.sql.*;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
import java.util.Locale;
|
|
|
|
|
|
|
@@ -17,18 +24,51 @@ public class SqlServiceImpl implements SqlService {
|
|
|
|
|
|
|
|
private static final String DB_URL_SUFFIX = "?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false";
|
|
private static final String DB_URL_SUFFIX = "?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false";
|
|
|
|
|
|
|
|
|
|
+ private final DeploymentMapper deploymentMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final PipelineMapper pipelineMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public SqlServiceImpl(DeploymentMapper deploymentMapper, PipelineMapper pipelineMapper) {
|
|
|
|
|
+ this.deploymentMapper = deploymentMapper;
|
|
|
|
|
+ this.pipelineMapper = pipelineMapper;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
- public String execute(String url, String db,String username, String password, String sql) {
|
|
|
|
|
|
|
+ public String execute(Integer deploymentId, String db,String username, String password, String sql) throws AccessDeniedException {
|
|
|
|
|
+
|
|
|
|
|
+ sql = sql.trim();
|
|
|
|
|
+ if (sql.endsWith(";")){
|
|
|
|
|
+ sql = sql.substring(0,sql.length()-1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DeploymentPO deployment = deploymentMapper.selectById(deploymentId);
|
|
|
|
|
+ UserService.projectAuthentication(pipelineMapper.selectById(deployment.getPipelineId()).getProjectId());
|
|
|
|
|
+ String url = deploymentMapper.selectById(deploymentId).getAccessUrl();
|
|
|
|
|
+
|
|
|
String dbUrl = DB_URL_PREFIX + url + "/" + db + DB_URL_SUFFIX;
|
|
String dbUrl = DB_URL_PREFIX + url + "/" + db + DB_URL_SUFFIX;
|
|
|
try(Connection conn = DriverManager.getConnection(dbUrl, username, password);
|
|
try(Connection conn = DriverManager.getConnection(dbUrl, username, password);
|
|
|
Statement stmt = conn.createStatement();
|
|
Statement stmt = conn.createStatement();
|
|
|
) {
|
|
) {
|
|
|
- //只有select语句需要解析 resultset
|
|
|
|
|
- if ("select".equals(sql.trim().substring(0,6).toLowerCase(Locale.ROOT))){
|
|
|
|
|
|
|
+ //select query desc走query 其他走update
|
|
|
|
|
+ String prefix = sql.substring(0, sql.indexOf(" ")).toLowerCase(Locale.ROOT);
|
|
|
|
|
+
|
|
|
|
|
+ if ("select".equals(prefix) || "show".equals(prefix) || "desc".equals(prefix)){
|
|
|
ResultSet rs = stmt.executeQuery(sql);
|
|
ResultSet rs = stmt.executeQuery(sql);
|
|
|
return "Boolean\n"+ convertResultSet2List(rs);
|
|
return "Boolean\n"+ convertResultSet2List(rs);
|
|
|
} else {
|
|
} else {
|
|
|
- return "Boolean\n";
|
|
|
|
|
|
|
+ //batch执行,注意;不要用于
|
|
|
|
|
+ if (sql.contains(";")){
|
|
|
|
|
+ String[] batches = sql.split(";");
|
|
|
|
|
+ for (String tmp : batches){
|
|
|
|
|
+ stmt.addBatch(tmp);
|
|
|
|
|
+ }
|
|
|
|
|
+ int[] res = stmt.executeBatch();
|
|
|
|
|
+ return "Boolean\nAffected :"+ Arrays.toString(res);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ int i = stmt.executeUpdate(sql);
|
|
|
|
|
+ return "Boolean\nAffected :"+i;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
} catch (SQLException e) {
|