Authored by qinchao

sql查询增加分页功能

@@ -159,18 +159,16 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -159,18 +159,16 @@ public class SqlOperateServiceImpl implements SqlOperateService {
159 159
160 con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd()); 160 con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
161 161
162 - if(sql.toLowerCase().startsWith("select ")){ 162 + if(sql.toLowerCase().startsWith("select")){
  163 + if(!sql.toLowerCase().startsWith("select ")){//select之后必须跟着一个空格
  164 + log.error("SqlOperateServiceImpl query4Table sql语句不合法"+sql);
  165 + res=new BaseResponse("sql语句不合法,select之后请添加空格");
  166 + return res;
  167 + }
163 selectFlag=true; 168 selectFlag=true;
164 rtnList.put("showPi",true); 169 rtnList.put("showPi",true);
165 rtnList.put("pageSizePi",MAX_QUERY_RESULT_COUNT); 170 rtnList.put("pageSizePi",MAX_QUERY_RESULT_COUNT);
166 rtnList.put("currentPagePi",sqlOperateModel.getCurrentPage()); 171 rtnList.put("currentPagePi",sqlOperateModel.getCurrentPage());
167 - st_pi = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);  
168 - st_pi.setFetchSize(1);  
169 - st_pi.setMaxRows(1);  
170 - rs_pi = st_pi.executeQuery(" select count(1) from ( "+sql+" ) _tmp_sqloeaer_49_cnt ");  
171 - while (rs_pi.next()) {  
172 - rtnList.put("totalPi",rs_pi.getInt(1));  
173 - }  
174 }else{ 172 }else{
175 rtnList.put("showPi",false); 173 rtnList.put("showPi",false);
176 } 174 }
@@ -277,6 +275,32 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -277,6 +275,32 @@ public class SqlOperateServiceImpl implements SqlOperateService {
277 275
278 276
279 } 277 }
  278 +
  279 + //计算总页数
  280 + if(selectFlag){
  281 + st_pi = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  282 + st_pi.setFetchSize(1);
  283 + st_pi.setMaxRows(1);
  284 +
  285 + boolean countSqlExeFlag=false;
  286 + String countSql=this.ayaSqlToCount(sql.substring(6));
  287 + if(StringUtils.isNotBlank(countSql)){
  288 + try{
  289 + rs_pi = st_pi.executeQuery(countSql);
  290 + countSqlExeFlag=true;
  291 + }catch (Exception e){
  292 + log.error("query4Table exe select count error "+countSql);
  293 + countSqlExeFlag=false;
  294 + }
  295 + }
  296 +
  297 + if(!countSqlExeFlag){
  298 + rs_pi = st_pi.executeQuery(" select count(1) from ( "+sql+" ) _tmp_sqloeaer_49_cnt ");
  299 + }
  300 + while (rs_pi.next()) {
  301 + rtnList.put("totalPi",rs_pi.getInt(1));
  302 + }
  303 + }
280 rtnList.put("jsonArrayColumn",ja_column); 304 rtnList.put("jsonArrayColumn",ja_column);
281 rtnList.put("jsonArrayData",ja_data); 305 rtnList.put("jsonArrayData",ja_data);
282 sqlOperateLogs.setQueryResult("1"); 306 sqlOperateLogs.setQueryResult("1");
@@ -341,6 +365,115 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -341,6 +365,115 @@ public class SqlOperateServiceImpl implements SqlOperateService {
341 return res; 365 return res;
342 } 366 }
343 367
  368 + /**
  369 + * 解析select * from tt 到 select count(1) from tt
  370 + * 包括复杂的sql解析,例如:select col1,col2,'aaaa',"bbbb",'"""""""',"''''''''''''''",'''',"""" ' \",\'\' FROM from ' FROM user
  371 + * @param selectSqlWithoutFirstSelect
  372 + * @return
  373 + */
  374 + public String ayaSqlToCount(String selectSqlWithoutFirstSelect){
  375 + String FROM_CONSTANT=" from ";
  376 + int FROM_CONSTANT_LENGTH=FROM_CONSTANT.length();
  377 + selectSqlWithoutFirstSelect=selectSqlWithoutFirstSelect.trim();
  378 + System.out.println(selectSqlWithoutFirstSelect);
  379 + if(selectSqlWithoutFirstSelect!=null&&selectSqlWithoutFirstSelect.length()>0){
  380 + int indexFrom=selectSqlWithoutFirstSelect.toLowerCase().indexOf(FROM_CONSTANT);
  381 + if(indexFrom>0){
  382 + String cutColumns=selectSqlWithoutFirstSelect.substring(0,indexFrom);
  383 + String leftSql=selectSqlWithoutFirstSelect.substring(indexFrom+FROM_CONSTANT_LENGTH);
  384 + if(leftSql.toLowerCase().indexOf(FROM_CONSTANT)<0){
  385 + return " select count(1) "+selectSqlWithoutFirstSelect.substring(indexFrom);
  386 + }else{
  387 + cutColumns=cutColumns.trim();
  388 +
  389 + int cutLength=cutColumns.length();
  390 + boolean quotationMarks=false;
  391 + String lastQuotationChar="";
  392 + for(int i=0;i<cutLength;i++){
  393 + String curChar=String.valueOf(cutColumns.charAt(i));
  394 + if(quotationMarks){
  395 + if(curChar.equals(",")){
  396 + StringBuilder sb = new StringBuilder(cutColumns);
  397 + sb.replace(i, i+1, "$" );
  398 + cutColumns=sb.toString();
  399 + }else{
  400 + if(cutColumns.substring(i).length()>=2){
  401 + String transferredMeaning=cutColumns.substring(i,i+2);
  402 + if(transferredMeaning.equals("\\'")||transferredMeaning.equals("''")
  403 + ||transferredMeaning.equals("\\\"")||transferredMeaning.equals("\"\"")){
  404 + StringBuilder sb = new StringBuilder(cutColumns);
  405 + sb.replace(i, i+2, "##" );
  406 + cutColumns=sb.toString();
  407 + }else{
  408 + if(curChar.equals(lastQuotationChar)){
  409 + lastQuotationChar="";
  410 + quotationMarks=false;
  411 + }
  412 + }
  413 + }else{
  414 + if(curChar.equals(lastQuotationChar)){
  415 + lastQuotationChar="";
  416 + quotationMarks=false;
  417 + }
  418 + }
  419 +
  420 +
  421 + }
  422 + }else{
  423 + if(curChar.equals("'")||curChar.equals("\"")){
  424 + if(lastQuotationChar.equals("")){
  425 + quotationMarks=true;
  426 + lastQuotationChar=curChar;
  427 + }
  428 + }
  429 + }
  430 +
  431 + }
  432 + String[] array_column=cutColumns.split(",");
  433 + if(array_column!=null&&array_column.length>0){
  434 + String lastColumn=array_column[array_column.length-1];
  435 + lastColumn=lastColumn.trim();
  436 +
  437 + if((lastColumn.startsWith("'")&&lastColumn.endsWith("'"))
  438 + ||(lastColumn.startsWith("\"")&&lastColumn.endsWith("\""))){
  439 + return " select count(1) "+selectSqlWithoutFirstSelect.substring(indexFrom);
  440 + }else{
  441 + //如果出现""||''||""或者"aaaa"'bbbb'交替配对出现的情况,也是符合条件的
  442 + boolean next=true;
  443 + if(lastColumn.startsWith("'")||lastColumn.startsWith("\"")){
  444 + StringBuilder sb=new StringBuilder();
  445 + for(int i=0;i<lastColumn.length();i++){
  446 + String curChar=String.valueOf(lastColumn.charAt(i));
  447 + if(curChar.equals("'")||curChar.equals("\"")){
  448 + sb.append(curChar);
  449 + }
  450 + }//end for
  451 + String tmp=sb.toString();
  452 + tmp=tmp.replace("\"\"", "");
  453 + tmp=tmp.replace("''", "");
  454 + if(tmp.length()==0){
  455 + next=false;
  456 + }
  457 + }
  458 + if(next){
  459 + return ayaSqlToCount(cutColumns + "******" +leftSql);
  460 + }else{
  461 + return " select count(1) "+selectSqlWithoutFirstSelect.substring(indexFrom);
  462 + }
  463 +
  464 + }
  465 + }
  466 + return "";
  467 + }
  468 + }else{
  469 + return "";
  470 + }
  471 +
  472 + }
  473 + return "";
  474 +
  475 + }
  476 +
344 477
345 478
346 private void resetDbUerAndPwd(SqlOperateModel sqlOperateModel){ 479 private void resetDbUerAndPwd(SqlOperateModel sqlOperateModel){