diff --git a/build.gradle b/build.gradle index f298e4a..6b14ffe 100644 --- a/build.gradle +++ b/build.gradle @@ -55,7 +55,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '2.1.6' + version '2.1.8' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/repository/CustomJdbcTemplate.java b/src/main/java/com/taover/repository/CustomJdbcTemplate.java index 5fa31a2..e8d4040 100644 --- a/src/main/java/com/taover/repository/CustomJdbcTemplate.java +++ b/src/main/java/com/taover/repository/CustomJdbcTemplate.java @@ -601,16 +601,12 @@ public class CustomJdbcTemplate { } public Map getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ - int fromIndex = coreSql.toUpperCase().indexOf("FROM"); - String selectSql = ""; - String fromAndWhereSql = ""; - if(fromIndex > -1) { - selectSql = coreSql.substring(0, fromIndex); - fromAndWhereSql = coreSql.substring(fromIndex, coreSql.length()); - }else { + try { + String[] splitedSql = UtilsSql.splitCoreSql(coreSql); + return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize); + }catch (Exception e) { return UtilsSql.createPage(page, pageSize, 0, new ArrayList()); } - return this.getPageData(selectSql, fromAndWhereSql, orderByPartSql, page, pageSize); } public Map getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize){ @@ -629,16 +625,12 @@ public class CustomJdbcTemplate { } public Map getBeanPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize, Class beanClass){ - int fromIndex = coreSql.toUpperCase().indexOf("FROM"); - String selectSql = ""; - String fromAndWhereSql = ""; - if(fromIndex > -1) { - selectSql = coreSql.substring(0, fromIndex); - fromAndWhereSql = coreSql.substring(fromIndex, coreSql.length()); - }else { + try { + String[] splitedSql = UtilsSql.splitCoreSql(coreSql); + return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize, beanClass); + }catch (Exception e) { return UtilsSql.createPage(page, pageSize, 0, new ArrayList()); } - return this.getPageData(selectSql, fromAndWhereSql, orderByPartSql, page, pageSize, beanClass); } public Map getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize, Class beanClass){ diff --git a/src/main/java/com/taover/repository/UtilsSql.java b/src/main/java/com/taover/repository/UtilsSql.java index 3e618a5..16d660b 100644 --- a/src/main/java/com/taover/repository/UtilsSql.java +++ b/src/main/java/com/taover/repository/UtilsSql.java @@ -7,6 +7,64 @@ import org.springframework.util.StringUtils; public class UtilsSql { + public static String[] splitCoreSql(String coreSql) throws Exception { + //去除''内的信息 + String coreSqlRemoveQuate = replaceSelectAndFromBetweenTopCornerMark(coreSql.toUpperCase()); + int fromIndex = calcFromIndex(coreSqlRemoveQuate); + if(fromIndex > -1) { + return new String[] {coreSql.substring(0, fromIndex), coreSql.substring(fromIndex, coreSql.length())}; + }else { + throw new Exception("未找到FROM子句"); + } + } + + private static int calcFromIndex(String coreSqlUpper) { + //计算位置 + int selectSubSqlNum = 0; + int currWindowIndex = 0; + for(currWindowIndex=0; (currWindowIndex+6) -1) { + result.append(sql.substring(iMark, currIndex)); + } + return result.toString(); + } + /** * 获取排序字符串 * @param sort @@ -84,4 +142,14 @@ public class UtilsSql { pageData.put("total", total); return pageData; } + + public static void main(String[] args) { + try { + String[] data = splitCoreSql("select t,(select * as 'fromCActio', dd as 'seelctsele\'ctd' from t) from www where 223"); + System.out.println(data[0]); + System.out.println(data[1]); + } catch (Exception e) { + e.printStackTrace(); + } + } } -- libgit2 0.21.2