Commit 1adcb6de0be14a2aa0dea25d1e6a8a80dac8fc08
1 parent
4631c60c
Exists in
master
and in
2 other branches
1.fix a bug about splict sql
Showing
3 changed files
with
77 additions
and
17 deletions
Show diff stats
build.gradle
| @@ -55,7 +55,7 @@ uploadArchives { | @@ -55,7 +55,7 @@ uploadArchives { | ||
| 55 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 55 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
| 56 | } | 56 | } |
| 57 | pom.project { | 57 | pom.project { |
| 58 | - version '2.1.6' | 58 | + version '2.1.8' |
| 59 | artifactId ARTIFACT_Id | 59 | artifactId ARTIFACT_Id |
| 60 | groupId GROUP_ID | 60 | groupId GROUP_ID |
| 61 | packaging TYPE | 61 | packaging TYPE |
src/main/java/com/taover/repository/CustomJdbcTemplate.java
| @@ -601,16 +601,12 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | @@ -601,16 +601,12 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | ||
| 601 | } | 601 | } |
| 602 | 602 | ||
| 603 | public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ | 603 | public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ |
| 604 | - int fromIndex = coreSql.toUpperCase().indexOf("FROM"); | ||
| 605 | - String selectSql = ""; | ||
| 606 | - String fromAndWhereSql = ""; | ||
| 607 | - if(fromIndex > -1) { | ||
| 608 | - selectSql = coreSql.substring(0, fromIndex); | ||
| 609 | - fromAndWhereSql = coreSql.substring(fromIndex, coreSql.length()); | ||
| 610 | - }else { | 604 | + try { |
| 605 | + String[] splitedSql = UtilsSql.splitCoreSql(coreSql); | ||
| 606 | + return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize); | ||
| 607 | + }catch (Exception e) { | ||
| 611 | return UtilsSql.createPage(page, pageSize, 0, new ArrayList<Object>()); | 608 | return UtilsSql.createPage(page, pageSize, 0, new ArrayList<Object>()); |
| 612 | } | 609 | } |
| 613 | - return this.getPageData(selectSql, fromAndWhereSql, orderByPartSql, page, pageSize); | ||
| 614 | } | 610 | } |
| 615 | 611 | ||
| 616 | public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize){ | 612 | public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize){ |
| @@ -629,16 +625,12 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | @@ -629,16 +625,12 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | ||
| 629 | } | 625 | } |
| 630 | 626 | ||
| 631 | public <E> Map<String, Object> getBeanPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize, Class<E> beanClass){ | 627 | public <E> Map<String, Object> getBeanPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize, Class<E> beanClass){ |
| 632 | - int fromIndex = coreSql.toUpperCase().indexOf("FROM"); | ||
| 633 | - String selectSql = ""; | ||
| 634 | - String fromAndWhereSql = ""; | ||
| 635 | - if(fromIndex > -1) { | ||
| 636 | - selectSql = coreSql.substring(0, fromIndex); | ||
| 637 | - fromAndWhereSql = coreSql.substring(fromIndex, coreSql.length()); | ||
| 638 | - }else { | 628 | + try { |
| 629 | + String[] splitedSql = UtilsSql.splitCoreSql(coreSql); | ||
| 630 | + return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize, beanClass); | ||
| 631 | + }catch (Exception e) { | ||
| 639 | return UtilsSql.createPage(page, pageSize, 0, new ArrayList<Object>()); | 632 | return UtilsSql.createPage(page, pageSize, 0, new ArrayList<Object>()); |
| 640 | } | 633 | } |
| 641 | - return this.getPageData(selectSql, fromAndWhereSql, orderByPartSql, page, pageSize, beanClass); | ||
| 642 | } | 634 | } |
| 643 | 635 | ||
| 644 | public <E> Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize, Class<E> beanClass){ | 636 | public <E> Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize, Class<E> beanClass){ |
src/main/java/com/taover/repository/UtilsSql.java
| @@ -7,6 +7,64 @@ import org.springframework.util.StringUtils; | @@ -7,6 +7,64 @@ import org.springframework.util.StringUtils; | ||
| 7 | 7 | ||
| 8 | public class UtilsSql { | 8 | public class UtilsSql { |
| 9 | 9 | ||
| 10 | + public static String[] splitCoreSql(String coreSql) throws Exception { | ||
| 11 | + //去除''内的信息 | ||
| 12 | + String coreSqlRemoveQuate = replaceSelectAndFromBetweenTopCornerMark(coreSql.toUpperCase()); | ||
| 13 | + int fromIndex = calcFromIndex(coreSqlRemoveQuate); | ||
| 14 | + if(fromIndex > -1) { | ||
| 15 | + return new String[] {coreSql.substring(0, fromIndex), coreSql.substring(fromIndex, coreSql.length())}; | ||
| 16 | + }else { | ||
| 17 | + throw new Exception("未找到FROM子句"); | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + private static int calcFromIndex(String coreSqlUpper) { | ||
| 22 | + //计算位置 | ||
| 23 | + int selectSubSqlNum = 0; | ||
| 24 | + int currWindowIndex = 0; | ||
| 25 | + for(currWindowIndex=0; (currWindowIndex+6)<coreSqlUpper.length(); ) { | ||
| 26 | + String currSubStr = coreSqlUpper.substring(currWindowIndex, currWindowIndex+6); | ||
| 27 | + if("SELECT".equals(currSubStr)) { | ||
| 28 | + ++selectSubSqlNum; | ||
| 29 | + currWindowIndex += 6; | ||
| 30 | + }else if(currSubStr.startsWith("FROM")) { | ||
| 31 | + if(selectSubSqlNum == 1) { | ||
| 32 | + return currWindowIndex; | ||
| 33 | + } | ||
| 34 | + --selectSubSqlNum; | ||
| 35 | + currWindowIndex += 4; | ||
| 36 | + } | ||
| 37 | + ++currWindowIndex; | ||
| 38 | + } | ||
| 39 | + return -1; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + private static String replaceSelectAndFromBetweenTopCornerMark(String sql) { | ||
| 43 | + StringBuffer result = new StringBuffer(); | ||
| 44 | + boolean isBetweenMark = false; | ||
| 45 | + int iMark = 0; | ||
| 46 | + int currIndex = 0; | ||
| 47 | + for(currIndex=0; currIndex<sql.length(); ++currIndex) { | ||
| 48 | + char currChar = sql.charAt(currIndex); | ||
| 49 | + if(currChar == '\'') { | ||
| 50 | + if(!isBetweenMark) { | ||
| 51 | + result.append(sql.substring(iMark, currIndex)); | ||
| 52 | + isBetweenMark = true; | ||
| 53 | + }else { | ||
| 54 | + result.append(sql.substring(iMark, currIndex).replaceAll("SELECT", "******").replaceAll("FROM", "****")); | ||
| 55 | + isBetweenMark = false; | ||
| 56 | + } | ||
| 57 | + iMark = currIndex; | ||
| 58 | + }else if(currChar == '\\') { | ||
| 59 | + ++currIndex; | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + if(iMark > -1) { | ||
| 63 | + result.append(sql.substring(iMark, currIndex)); | ||
| 64 | + } | ||
| 65 | + return result.toString(); | ||
| 66 | + } | ||
| 67 | + | ||
| 10 | /** | 68 | /** |
| 11 | * 获取排序字符串 | 69 | * 获取排序字符串 |
| 12 | * @param sort | 70 | * @param sort |
| @@ -84,4 +142,14 @@ public class UtilsSql { | @@ -84,4 +142,14 @@ public class UtilsSql { | ||
| 84 | pageData.put("total", total); | 142 | pageData.put("total", total); |
| 85 | return pageData; | 143 | return pageData; |
| 86 | } | 144 | } |
| 145 | + | ||
| 146 | + public static void main(String[] args) { | ||
| 147 | + try { | ||
| 148 | + String[] data = splitCoreSql("select t,(select * as 'fromCActio', dd as 'seelctsele\'ctd' from t) from www where 223"); | ||
| 149 | + System.out.println(data[0]); | ||
| 150 | + System.out.println(data[1]); | ||
| 151 | + } catch (Exception e) { | ||
| 152 | + e.printStackTrace(); | ||
| 153 | + } | ||
| 154 | + } | ||
| 87 | } | 155 | } |