...
|
...
|
@@ -11,6 +11,7 @@ import com.monitor.model.response.BaseResponse; |
|
|
import com.monitor.model.response.PageResponse;
|
|
|
import com.monitor.mysql.mapper.SqlOperateDbSourceMapper;
|
|
|
import com.monitor.mysql.mapper.SqlOperateLogsMapper;
|
|
|
import com.monitor.other.sqlOperate.constant.SqlQueryConstant;
|
|
|
import com.monitor.other.sqlOperate.service.SqlOperateService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
...
|
...
|
@@ -38,12 +39,6 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
private SqlOperateDbSourceMapper sqlOperateDbSourceMapper;
|
|
|
|
|
|
Logger log = LoggerFactory.getLogger(SqlOperateServiceImpl.class);
|
|
|
private final String DRIVE_CLASS_MYSQL = "com.mysql.jdbc.Driver";
|
|
|
private final String DATA_SOURCE_USER="yh_ts_db";
|
|
|
private final String DATA_SOURCE_PWD="Fij8631jkdx^ua870klp";
|
|
|
private final int MAX_QUERY_RESULT_COUNT=30;
|
|
|
private final int FETCH_COUNT_FROM_MYSQL=MAX_QUERY_RESULT_COUNT+1;
|
|
|
private final String DB_PARAM="?useOldAliasMetadataBehavior=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&defaultFetchSize=" + FETCH_COUNT_FROM_MYSQL;
|
|
|
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -75,7 +70,7 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
resetDbUerAndPwd(sqlOperateModel);
|
|
|
BaseResponse res=null;
|
|
|
try {
|
|
|
Class.forName(DRIVE_CLASS_MYSQL);
|
|
|
Class.forName(SqlQueryConstant.DRIVE_CLASS_MYSQL);
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
log.error("SqlOperateServiceImpl queryDbList 找不到驱动:com.mysql.jdbc.Driver",e);
|
|
|
res=new BaseResponse("找不到驱动:com.mysql.jdbc.Driver");
|
...
|
...
|
@@ -86,9 +81,9 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
Statement st = null;
|
|
|
ResultSet rs = null;
|
|
|
try {
|
|
|
con = DriverManager.getConnection("jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() + "/"+DB_PARAM, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
|
|
|
con = DriverManager.getConnection("jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() + "/"+SqlQueryConstant.DB_PARAM, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
|
|
|
st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
|
|
st.setFetchSize(FETCH_COUNT_FROM_MYSQL);
|
|
|
st.setFetchSize(SqlQueryConstant.FETCH_COUNT_FROM_MYSQL);
|
|
|
//st.setMaxRows(FETCH_COUNT_FROM_MYSQL);取全部数据
|
|
|
rs = st.executeQuery("show databases");
|
|
|
List<String> dbNameList=new ArrayList<String>();
|
...
|
...
|
@@ -163,7 +158,7 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
log.info("query4Table begin "+sql);
|
|
|
|
|
|
try {
|
|
|
Class.forName(DRIVE_CLASS_MYSQL);
|
|
|
Class.forName(SqlQueryConstant.DRIVE_CLASS_MYSQL);
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
log.error("SqlOperateServiceImpl query4Table 找不到驱动:com.mysql.jdbc.Driver",e);
|
|
|
res=new BaseResponse("找不到驱动:com.mysql.jdbc.Driver");
|
...
|
...
|
@@ -180,9 +175,9 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
try {
|
|
|
String dbUrl="jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() ;
|
|
|
if(StringUtils.isNotBlank(sqlOperateModel.getDbInfo())){
|
|
|
dbUrl+= "/"+sqlOperateModel.getDbInfo()+DB_PARAM;
|
|
|
dbUrl+= "/"+sqlOperateModel.getDbInfo()+SqlQueryConstant.DB_PARAM;
|
|
|
}else{
|
|
|
dbUrl+= "/"+DB_PARAM;
|
|
|
dbUrl+= "/"+SqlQueryConstant.DB_PARAM;
|
|
|
}
|
|
|
|
|
|
con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
|
...
|
...
|
@@ -195,23 +190,23 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
}
|
|
|
selectFlag=true;
|
|
|
rtnList.put("showPi",true);
|
|
|
rtnList.put("pageSizePi",MAX_QUERY_RESULT_COUNT);
|
|
|
rtnList.put("pageSizePi",SqlQueryConstant.MAX_QUERY_RESULT_COUNT);
|
|
|
rtnList.put("currentPagePi",sqlOperateModel.getCurrentPage());
|
|
|
}else{
|
|
|
rtnList.put("showPi",false);
|
|
|
}
|
|
|
|
|
|
st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
|
|
st.setFetchSize(FETCH_COUNT_FROM_MYSQL);
|
|
|
st.setFetchSize(SqlQueryConstant.FETCH_COUNT_FROM_MYSQL);
|
|
|
//不是desc的查询需要限定数量了
|
|
|
//前台有了分页,不再需要设定desc的限定数量
|
|
|
if(selectFlag){
|
|
|
st.setMaxRows(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage() +FETCH_COUNT_FROM_MYSQL);
|
|
|
st.setMaxRows(SqlQueryConstant.MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage() +SqlQueryConstant.FETCH_COUNT_FROM_MYSQL);
|
|
|
}
|
|
|
|
|
|
rs = st.executeQuery(sql);
|
|
|
rs.beforeFirst();
|
|
|
rs.relative(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage());
|
|
|
rs.relative(SqlQueryConstant.MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage());
|
|
|
JSONArray ja_column=new JSONArray();
|
|
|
JSONArray ja_data=new JSONArray();
|
|
|
List<String> colNameList=new ArrayList<String>();
|
...
|
...
|
@@ -262,9 +257,9 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
}
|
|
|
while (rs.next()) {
|
|
|
times++;
|
|
|
if(selectFlag&×>MAX_QUERY_RESULT_COUNT){
|
|
|
if(selectFlag&×>SqlQueryConstant.MAX_QUERY_RESULT_COUNT){
|
|
|
//if(times>MAX_QUERY_RESULT_COUNT){
|
|
|
queryDescr="数据量超过限制,本页只显示"+MAX_QUERY_RESULT_COUNT+"条";
|
|
|
queryDescr="数据量超过限制,本页只显示"+SqlQueryConstant.MAX_QUERY_RESULT_COUNT+"条";
|
|
|
hasNext="yes";
|
|
|
break;
|
|
|
}
|
...
|
...
|
@@ -304,7 +299,7 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
|
|
|
//计算总页数
|
|
|
if(selectFlag){
|
|
|
if(count<MAX_QUERY_RESULT_COUNT&&sqlOperateModel.getCurrentPage()==0){
|
|
|
if(count<SqlQueryConstant.MAX_QUERY_RESULT_COUNT&&sqlOperateModel.getCurrentPage()==0){
|
|
|
//当查询第一页时,如果返回的数量小于游标循环的数量,则总数量=本次游标循环的数量,不需要再查询一次了
|
|
|
rtnList.put("totalPi",count);
|
|
|
}else{
|
...
|
...
|
@@ -508,10 +503,10 @@ public class SqlOperateServiceImpl implements SqlOperateService { |
|
|
|
|
|
private void resetDbUerAndPwd(SqlOperateModel sqlOperateModel){
|
|
|
if(StringUtils.isBlank(sqlOperateModel.getDataSourceUser())){
|
|
|
sqlOperateModel.setDataSourceUser(DATA_SOURCE_USER);
|
|
|
sqlOperateModel.setDataSourceUser(SqlQueryConstant.DATA_SOURCE_USER);
|
|
|
}
|
|
|
if(StringUtils.isBlank(sqlOperateModel.getDataSourcePwd())){
|
|
|
sqlOperateModel.setDataSourcePwd(DATA_SOURCE_PWD);
|
|
|
sqlOperateModel.setDataSourcePwd(SqlQueryConstant.DATA_SOURCE_PWD);
|
|
|
}
|
|
|
if(sqlOperateModel.getCurrentPage()<0){
|
|
|
sqlOperateModel.setCurrentPage(0);
|
...
|
...
|
|