mirror of https://github.com/elunez/eladmin
新增 执行SQL脚本 功能
parent
cf211a6766
commit
6d16bcda3b
|
@ -3,15 +3,23 @@ package me.zhengjie.modules.mnt.rest;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import me.zhengjie.aop.log.Log;
|
import me.zhengjie.aop.log.Log;
|
||||||
|
import me.zhengjie.exception.BadRequestException;
|
||||||
import me.zhengjie.modules.mnt.domain.Database;
|
import me.zhengjie.modules.mnt.domain.Database;
|
||||||
import me.zhengjie.modules.mnt.service.DatabaseService;
|
import me.zhengjie.modules.mnt.service.DatabaseService;
|
||||||
|
import me.zhengjie.modules.mnt.service.dto.DatabaseDto;
|
||||||
import me.zhengjie.modules.mnt.service.dto.DatabaseQueryCriteria;
|
import me.zhengjie.modules.mnt.service.dto.DatabaseQueryCriteria;
|
||||||
|
import me.zhengjie.modules.mnt.util.SqlUtils;
|
||||||
|
import me.zhengjie.utils.FileUtil;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhanghouying
|
* @author zhanghouying
|
||||||
|
@ -22,6 +30,8 @@ import org.springframework.web.bind.annotation.*;
|
||||||
@RequestMapping("/api/database")
|
@RequestMapping("/api/database")
|
||||||
public class DatabaseController {
|
public class DatabaseController {
|
||||||
|
|
||||||
|
private String fileSavePath = System.getProperty("java.io.tmpdir");
|
||||||
|
|
||||||
private final DatabaseService databaseService;
|
private final DatabaseService databaseService;
|
||||||
|
|
||||||
public DatabaseController(DatabaseService databaseService) {
|
public DatabaseController(DatabaseService databaseService) {
|
||||||
|
@ -70,4 +80,23 @@ public class DatabaseController {
|
||||||
return new ResponseEntity<>(databaseService.testConnection(resources),HttpStatus.CREATED);
|
return new ResponseEntity<>(databaseService.testConnection(resources),HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Log("执行SQL脚本")
|
||||||
|
@ApiOperation(value = "执行SQL脚本")
|
||||||
|
@PostMapping(value = "/upload")
|
||||||
|
@PreAuthorize("@el.check('database:add')")
|
||||||
|
public ResponseEntity upload(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{
|
||||||
|
String id = request.getParameter("id");
|
||||||
|
DatabaseDto database = databaseService.findById(id);
|
||||||
|
String fileName = "";
|
||||||
|
if(database != null){
|
||||||
|
fileName = file.getOriginalFilename();
|
||||||
|
File executeFile = new File(fileSavePath+fileName);
|
||||||
|
FileUtil.del(executeFile);
|
||||||
|
file.transferTo(executeFile);
|
||||||
|
String result = SqlUtils.executeFile(database.getJdbcUrl(), database.getUserName(), database.getPwd(), executeFile);
|
||||||
|
return new ResponseEntity<>(result,HttpStatus.OK);
|
||||||
|
}else{
|
||||||
|
throw new BadRequestException("Database not exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,15 +3,14 @@ package me.zhengjie.modules.mnt.util;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import com.alibaba.druid.pool.DruidDataSource;
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
import com.alibaba.druid.util.StringUtils;
|
import com.alibaba.druid.util.StringUtils;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
import java.io.*;
|
||||||
import java.sql.DriverManager;
|
import java.sql.*;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -55,7 +54,7 @@ public class SqlUtils {
|
||||||
}
|
}
|
||||||
if (StringUtils.isEmpty(className)) {
|
if (StringUtils.isEmpty(className)) {
|
||||||
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
||||||
if (null == dataTypeEnum ) {
|
if (null == dataTypeEnum) {
|
||||||
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
||||||
}
|
}
|
||||||
druidDataSource.setDriverClassName(dataTypeEnum.getDriver());
|
druidDataSource.setDriverClassName(dataTypeEnum.getDriver());
|
||||||
|
@ -141,23 +140,84 @@ public class SqlUtils {
|
||||||
public static boolean testConnection(String jdbcUrl, String userName, String password) {
|
public static boolean testConnection(String jdbcUrl, String userName, String password) {
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
try {
|
try {
|
||||||
connection = getConnection(jdbcUrl, userName, password);
|
connection = getConnection(jdbcUrl, userName, password);
|
||||||
if (null != connection) {
|
if (null != connection) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
log.info("Get connection failed:",e.getMessage());
|
log.info("Get connection failed:", e.getMessage());
|
||||||
}finally {
|
} finally {
|
||||||
releaseConnection(connection);
|
releaseConnection(connection);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JdbcTemplate jdbcTemplate(String jdbcUrl, String userName, String password) {
|
public static String executeFile(String jdbcUrl, String userName, String password, File sqlFile) {
|
||||||
DataSource dataSource = getDataSource(jdbcUrl, userName, password);
|
Connection connection = getConnection(jdbcUrl, userName, password);
|
||||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
try {
|
||||||
jdbcTemplate.setFetchSize(1000);
|
batchExecute(connection, readSqlList( sqlFile));
|
||||||
return jdbcTemplate;
|
} catch (Exception e) {
|
||||||
|
log.error("sql脚本执行发生异常:{}",e.getMessage());
|
||||||
|
return e.getMessage();
|
||||||
|
}finally {
|
||||||
|
releaseConnection(connection);
|
||||||
|
}
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量执行sql
|
||||||
|
* @param connection
|
||||||
|
* @param sqlList
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static void batchExecute(Connection connection, List<String> sqlList) throws SQLException {
|
||||||
|
Statement st = connection.createStatement();
|
||||||
|
for (String sql : sqlList) {
|
||||||
|
if (sql.endsWith(";")) {
|
||||||
|
sql = sql.substring(0, sql.length() - 1);
|
||||||
|
}
|
||||||
|
st.addBatch(sql);
|
||||||
|
}
|
||||||
|
st.executeBatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将文件中的sql语句以;为单位读取到列表中
|
||||||
|
* @param sqlFile
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static List<String> readSqlList(File sqlFile) throws Exception {
|
||||||
|
List<String> sqlList = Lists.newArrayList();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
BufferedReader reader = null;
|
||||||
|
try {
|
||||||
|
reader = new BufferedReader(new InputStreamReader(
|
||||||
|
new FileInputStream(sqlFile), "UTF-8"));
|
||||||
|
String tmp = null;
|
||||||
|
while ((tmp = reader.readLine()) != null) {
|
||||||
|
log.info("line:{}", tmp);
|
||||||
|
if (tmp.endsWith(";")) {
|
||||||
|
sb.append(tmp);
|
||||||
|
sqlList.add(sb.toString());
|
||||||
|
sb.delete(0, sb.length());
|
||||||
|
} else {
|
||||||
|
sb.append(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!"".endsWith(sb.toString().trim())) {
|
||||||
|
sqlList.add(sb.toString());
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
reader.close();
|
||||||
|
} catch (IOException e1) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqlList;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue