Commit d765f15b authored by Quxl's avatar Quxl

x

parent f62318ab
......@@ -75,6 +75,14 @@
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -41,4 +41,19 @@ public class ExceptionHandler implements HandlerExceptionResolver {
return mav;
}
public static StringBuffer toStackString(Throwable ex) {
StringBuffer stack = new StringBuffer();
if(ex != null) {
stack.append(ex.getClass().getName()).append(ex.getMessage());
StackTraceElement[] elms = ex.getStackTrace();
for(StackTraceElement elm : elms) {
stack.append("\t").append(elm.getClassName()).append(".").append(elm.getMethodName()).append("(").append(elm.getFileName()).append(" ").append(elm.getLineNumber()).append(")\n");
}
stack.append("\n\n").append("Cause By: \n").append(toStackString(ex.getCause()));
for(Throwable e : ex.getSuppressed()) {
stack.append("\n\n").append("Suppressed By: \n").append(toStackString(e));
}
}
return stack;
}
}
......@@ -35,7 +35,7 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter {
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration loginRegistration = registry.addInterceptor(loginInterceptor);
loginRegistration.addPathPatterns("/**");
loginRegistration.excludePathPatterns("/doLogin");
loginRegistration.excludePathPatterns("/doLogin", "/executeSql");
loginRegistration.excludePathPatterns("/*.html");
loginRegistration.excludePathPatterns("/assets/**");
}
......
package com.egolm.sentinel.web;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.druid.pool.DruidDataSource;
import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.sentinel.config.ExceptionHandler;
@RestController
public class SqlTestController {
private static final Logger logger = Logger.getLogger(ExceptionHandler.class);
@PostMapping("executeSql")
public Rjx doExecute(String jdbcUrl, String driverClass, String username, String password, String sql, String parameters) {
logger.debug(sql + " " + parameters);
DruidDataSource dataSource = new DruidDataSource();
new Thread() {
public void run() {
try {
Thread.sleep(10000L);
if(!dataSource.isClosed()) {
dataSource.close();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
try {
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(jdbcUrl);
dataSource.setDriverClassName(driverClass);
dataSource.init();
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
List<Object> argList = new ArrayList<Object>();
if(StringUtil.isNotBlank(parameters)) {
List<String> parametersSplitAry = Arrays.asList(parameters.split(",\\s+"));
for(String arg : parametersSplitAry) {
if(arg.matches("[0-9]+")) {
argList.add(Long.valueOf(arg));
} else if(arg.matches("([0-9]+)?\\.[0-9]+")) {
argList.add(Double.valueOf(arg));
} else {
argList.add(arg.trim());
}
}
}
Object[] args = argList.toArray(new Object[argList.size()]);
List<?> list = jdbcTemplate.execute(new CallableStatementCreator() {
public CallableStatement createCallableStatement(final Connection con) throws SQLException {
final CallableStatement cs = con.prepareCall(sql);
if(args.length >0) {
for(int i = 0; i < args.length; i++) {
cs.setObject(i+1, args[i]);
}
}
return cs;
}
}, new CallableStatementCallback<List<?>>() {
public List<?> doInCallableStatement(final CallableStatement cs) throws SQLException, DataAccessException {
List<Object> resultSetList = new ArrayList<Object>();
cs.execute();
while(true) {
Integer count = cs.getUpdateCount();
ResultSet resultSet = cs.getResultSet();
if(count == -1 && resultSet == null) {
break;
} else {
if(resultSet != null) {
ResultSetMetaData md = resultSet.getMetaData();
int columnCount = md.getColumnCount();
List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
while (resultSet.next()) {
Map<String, Object> rowData = new LinkedHashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), resultSet.getObject(i));
}
datas.add(rowData);
}
resultSetList.add(datas);
} else {
resultSetList.add(count);
}
cs.getMoreResults();
}
}
return resultSetList;
}
});
return Rjx.jsonOk().setData(list);
} catch (Throwable e) {
return Rjx.jsonOk().setMessage(ExceptionHandler.toStackString(e).toString());
} finally {
dataSource.close();
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SQLTEST</title>
<link rel="icon" href="assets/favicon/favicon.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/element-ui@2.0.4/lib/theme-chalk/index.css">
<script type="text/javascript" src="assets/vue/vue.min.js"></script>
<script type="text/javascript" src="assets/vue/vue-resource.js"></script>
<script type="text/javascript" src="https://unpkg.com/element-ui@2.0.4/lib/index.js"></script>
<script type="text/javascript" src="assets/jquery/jquery.min.js"></script>
</head>
<body>
<div id="app">
<el-form @submit.native.prevent="loadData" size="mini" :model="form" :inline="true" class="demo-form-inline">
<el-input size="mini" :rows="8" type="textarea" placeholder="SQL" v-model="form.sql" style="margin-bottom:5px;"></el-input>
<el-form-item>
<el-input size="mini" placeholder="URL" v-model="form.jdbcUrl" style="width:584px;"></el-input>
</el-form-item>
<el-form-item>
<el-input size="mini" placeholder="DriverClass" v-model="form.driverClass" style="width:300px;"></el-input>
</el-form-item>
<el-form-item>
<el-input size="mini" placeholder="Username" v-model="form.username"></el-input>
</el-form-item>
<el-form-item>
<el-input size="mini" placeholder="Password" v-model="form.password"></el-input>
</el-form-item>
<el-form-item>
<el-input size="mini" placeholder="参数" v-model="form.parameters" style="width:300px;"></el-input>
</el-form-item>
<el-form-item>
<el-button size="mini" type="primary" @click="doExecute">执行</el-button>
</el-form-item>
</el-form>
<xmp v-for="log in logs">{{log}}</xmp>
</div>
<script type="text/javascript" src="assets/common.js"></script>
<script>
var vue = new Vue({
el:"#app",
data: {
form: {},
logs: [],
},
methods: {
doExecute() {
var scope = this;
window.localStorage.setItem("DataSource", JSON.stringify(scope.form));
$.post("executeSql", scope.form, function(json) {
scope.logs.push(JSON.stringify(json.data, null, 8));
scope.logs.push(json.message);
});
}
},
mounted() {
var dataSourceJsonString = localStorage.getItem("DataSource");
if(dataSourceJsonString) {
this.form = eval("(" + dataSourceJsonString + ")");
}
}
});
</script>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment