Authored by qinchao

sql查询增加分页功能

... ... @@ -159,18 +159,16 @@ public class SqlOperateServiceImpl implements SqlOperateService {
con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
if(sql.toLowerCase().startsWith("select ")){
if(sql.toLowerCase().startsWith("select")){
if(!sql.toLowerCase().startsWith("select ")){//select之后必须跟着一个空格
log.error("SqlOperateServiceImpl query4Table sql语句不合法"+sql);
res=new BaseResponse("sql语句不合法,select之后请添加空格");
return res;
}
selectFlag=true;
rtnList.put("showPi",true);
rtnList.put("pageSizePi",MAX_QUERY_RESULT_COUNT);
rtnList.put("currentPagePi",sqlOperateModel.getCurrentPage());
st_pi = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st_pi.setFetchSize(1);
st_pi.setMaxRows(1);
rs_pi = st_pi.executeQuery(" select count(1) from ( "+sql+" ) _tmp_sqloeaer_49_cnt ");
while (rs_pi.next()) {
rtnList.put("totalPi",rs_pi.getInt(1));
}
}else{
rtnList.put("showPi",false);
}
... ... @@ -277,6 +275,32 @@ public class SqlOperateServiceImpl implements SqlOperateService {
}
//计算总页数
if(selectFlag){
st_pi = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st_pi.setFetchSize(1);
st_pi.setMaxRows(1);
boolean countSqlExeFlag=false;
String countSql=this.ayaSqlToCount(sql.substring(6));
if(StringUtils.isNotBlank(countSql)){
try{
rs_pi = st_pi.executeQuery(countSql);
countSqlExeFlag=true;
}catch (Exception e){
log.error("query4Table exe select count error "+countSql);
countSqlExeFlag=false;
}
}
if(!countSqlExeFlag){
rs_pi = st_pi.executeQuery(" select count(1) from ( "+sql+" ) _tmp_sqloeaer_49_cnt ");
}
while (rs_pi.next()) {
rtnList.put("totalPi",rs_pi.getInt(1));
}
}
rtnList.put("jsonArrayColumn",ja_column);
rtnList.put("jsonArrayData",ja_data);
sqlOperateLogs.setQueryResult("1");
... ... @@ -341,6 +365,115 @@ public class SqlOperateServiceImpl implements SqlOperateService {
return res;
}
/**
* 解析select * from tt 到 select count(1) from tt
* 包括复杂的sql解析,例如:select col1,col2,'aaaa',"bbbb",'"""""""',"''''''''''''''",'''',"""" ' \",\'\' FROM from ' FROM user
* @param selectSqlWithoutFirstSelect
* @return
*/
public String ayaSqlToCount(String selectSqlWithoutFirstSelect){
String FROM_CONSTANT=" from ";
int FROM_CONSTANT_LENGTH=FROM_CONSTANT.length();
selectSqlWithoutFirstSelect=selectSqlWithoutFirstSelect.trim();
System.out.println(selectSqlWithoutFirstSelect);
if(selectSqlWithoutFirstSelect!=null&&selectSqlWithoutFirstSelect.length()>0){
int indexFrom=selectSqlWithoutFirstSelect.toLowerCase().indexOf(FROM_CONSTANT);
if(indexFrom>0){
String cutColumns=selectSqlWithoutFirstSelect.substring(0,indexFrom);
String leftSql=selectSqlWithoutFirstSelect.substring(indexFrom+FROM_CONSTANT_LENGTH);
if(leftSql.toLowerCase().indexOf(FROM_CONSTANT)<0){
return " select count(1) "+selectSqlWithoutFirstSelect.substring(indexFrom);
}else{
cutColumns=cutColumns.trim();
int cutLength=cutColumns.length();
boolean quotationMarks=false;
String lastQuotationChar="";
for(int i=0;i<cutLength;i++){
String curChar=String.valueOf(cutColumns.charAt(i));
if(quotationMarks){
if(curChar.equals(",")){
StringBuilder sb = new StringBuilder(cutColumns);
sb.replace(i, i+1, "$" );
cutColumns=sb.toString();
}else{
if(cutColumns.substring(i).length()>=2){
String transferredMeaning=cutColumns.substring(i,i+2);
if(transferredMeaning.equals("\\'")||transferredMeaning.equals("''")
||transferredMeaning.equals("\\\"")||transferredMeaning.equals("\"\"")){
StringBuilder sb = new StringBuilder(cutColumns);
sb.replace(i, i+2, "##" );
cutColumns=sb.toString();
}else{
if(curChar.equals(lastQuotationChar)){
lastQuotationChar="";
quotationMarks=false;
}
}
}else{
if(curChar.equals(lastQuotationChar)){
lastQuotationChar="";
quotationMarks=false;
}
}
}
}else{
if(curChar.equals("'")||curChar.equals("\"")){
if(lastQuotationChar.equals("")){
quotationMarks=true;
lastQuotationChar=curChar;
}
}
}
}
String[] array_column=cutColumns.split(",");
if(array_column!=null&&array_column.length>0){
String lastColumn=array_column[array_column.length-1];
lastColumn=lastColumn.trim();
if((lastColumn.startsWith("'")&&lastColumn.endsWith("'"))
||(lastColumn.startsWith("\"")&&lastColumn.endsWith("\""))){
return " select count(1) "+selectSqlWithoutFirstSelect.substring(indexFrom);
}else{
//如果出现""||''||""或者"aaaa"'bbbb'交替配对出现的情况,也是符合条件的
boolean next=true;
if(lastColumn.startsWith("'")||lastColumn.startsWith("\"")){
StringBuilder sb=new StringBuilder();
for(int i=0;i<lastColumn.length();i++){
String curChar=String.valueOf(lastColumn.charAt(i));
if(curChar.equals("'")||curChar.equals("\"")){
sb.append(curChar);
}
}//end for
String tmp=sb.toString();
tmp=tmp.replace("\"\"", "");
tmp=tmp.replace("''", "");
if(tmp.length()==0){
next=false;
}
}
if(next){
return ayaSqlToCount(cutColumns + "******" +leftSql);
}else{
return " select count(1) "+selectSqlWithoutFirstSelect.substring(indexFrom);
}
}
}
return "";
}
}else{
return "";
}
}
return "";
}
private void resetDbUerAndPwd(SqlOperateModel sqlOperateModel){
... ...