private static String getCountSql(String sql) {
return "select count(*) from "+cutOrderByOrGroupBy(getFromHql(sql));
} private static String cutOrderByOrGroupBy(String hql) {
int lastFromIndex = hql.lastIndexOf(" from ");
int lastOG = hql.lastIndexOf(" order by ");
if (lastOG < 0) {
lastOG = hql.lastIndexOf(" group by ");
}
if (lastFromIndex < lastOG) {
return hql.substring(0, lastOG);
}
return hql;
} private static String getFromHql(String sql) {
String tmpSql = sql;
int fIndex = tmpSql.indexOf(" from ");
tmpSql = tmpSql.substring(fIndex+5);
//确保from没被( 和 )包裹起来,即为该语句主from
if (!equalOpenAndCloseParenthesis(tmpSql)) {
tmpSql = getFromHql(tmpSql);
}
return tmpSql;
} private static Boolean equalOpenAndCloseParenthesis(String subSql) {
final Pattern opPattern = Pattern.compile("\\(");
final Matcher opMatch = opPattern.matcher(subSql);
final List<String> opMatches = new ArrayList<String>();
while (opMatch.find()) {
opMatches.add(opMatch.group(0));
}
final Pattern cpPattern = Pattern.compile("\\)");
final Matcher cpMatch = cpPattern.matcher(subSql);
final List<String> cpMatches = new ArrayList<String>();
while (cpMatch.find()) {
cpMatches.add(cpMatch.group(0));
}
return opMatches.size() == cpMatches.size();
}

根据from左右两侧括号情况判断某个from是否是主sql的from关键字。

SQL解析的更多相关文章

  1. 步步深入:MySQL架构总览->查询执行流程->SQL解析顺序

    前言: 一直是想知道一条SQL语句是怎么被执行的,它执行的顺序是怎样的,然后查看总结各方资料,就有了下面这一篇博文了. 本文将从MySQL总体架构--->查询执行流程--->语句执行顺序来 ...

  2. Spark之SQL解析(源码阅读十)

    如何能更好的运用与监控sparkSQL?或许我们改更深层次的了解它深层次的原理是什么.之前总结的已经写了传统数据库与Spark的sql解析之间的差别.那么我们下来直切主题~ 如今的Spark已经支持多 ...

  3. 高大上技术之sql解析

    Question: 为何sql解析和高大上有关系?Answer:因为数据库永远都是系统的核心,CRUD如此深入码农的内心...如果能把CRUD改造成高大上技术,如此不是造福嘛... CRUD就是Cre ...

  4. sql 解析字符串添加到临时表中 sql存储过程in 参数输入

    sql 解析字符串添加到临时表中  sql存储过程in 参数输入 解决方法 把字符串解析 添加到 临时表中 SELECT * into #临时表   FROM dbo.Func_SplitOneCol ...

  5. oracle 内存结构 share pool sql解析的过程

    1.sql解析的过程 oracle首先将SQL文本转化为ASCII字符,然后根据hash函数计算其对应的hash值(hash_value).根据计算出的hash值到library cache中找到对应 ...

  6. 基于简单sql语句的sql解析原理及在大数据中的应用

    基于简单sql语句的sql解析原理及在大数据中的应用 李万鸿 老百姓呼吁打土豪分田地.共同富裕,总有一天会实现. 全面了解你所不知道的外星人和宇宙真想:http://pan.baidu.com/s/1 ...

  7. 自己实现一个SQL解析引擎

    自己实现一个SQL解析引擎 功能:将用户输入的SQL语句序列转换为一个可运行的操作序列,并返回查询的结果集. SQL的解析引擎包含查询编译与查询优化和查询的执行,主要包含3个步骤: 查询分析: 制定逻 ...

  8. [转]ORACLE SQL解析之硬解析和软解析

    http://blog.chinaunix.net/uid-25909722-id-3363789.html 当客户端进程,将SQL语句通过监听器发送到Oracle时, 会触发一个Server pro ...

  9. 关系型数据库工作原理-SQL解析(翻译自Coding-Geek文章)

    本文翻译自Coding-Geek文章:< How does a relational database work>.原文链接:http://coding-geek.com/how-data ...

  10. SQL解析在美团的应用

    https://tech.meituan.com/SQL_parser_used_in_mtdp.html 数据库作为核心的基础组件,是需要重点保护的对象.任何一个线上的不慎操作,都有可能给数据库带来 ...

随机推荐

  1. plus调用android原生页面

    var main = plus.android.runtimeMainActivity(); var Intent = plus.android.importClass("android.c ...

  2. Java代理模式之Cglib代理

    Cglib代理,也叫做子类代理.在内存中构建一个子类对象从而实现对目标对象功能的扩展. CGLIB包的底层是通过使用一个小而快的字节码处理框架ASM,来转换字节码并生成新的类.不鼓励直接使用ASM,因 ...

  3. SpringBoot配置MySql数据库和Druid连接池

    1.pom文件增加相关依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connec ...

  4. Manual write code to record error log in .net by Global.asax

    完整的Glabal.asax代码: <%@ Application Language="C#" %> <script RunAt="server&quo ...

  5. MongoDB ODBC Driver for Data Integration with Power BI

    This guide will walk you through connecting Microsoft Power BI to a MongoDB DataSet using our MongoD ...

  6. Azure Function & AWS Function With C#

    Using C# with Azure Functions Two important prerequisites need to be met to build Azure Functions ap ...

  7. Nginx CONTENT阶段 autoindex、index模块

    L 66 autoindex 指令 syntax : on | off; default : off; context : http,server,location; autoindex_exact_ ...

  8. select2 3.5.3 二级下拉及搜索

    select2 [3.5.3]版本 select2 插件地址 http://select2.github.io/select2/ 支持搜索: JS代码,如果Group不需要勾选,goup不加id就可以 ...

  9. 【集训队作业2018】【XSY3372】取石子 DP

    题目大意 有 \(n\) 堆石子,初始时第 \(i\) 堆石子有 \(a_i\) 个. 你每次取石子会取 \(k\) 个.在你取完一堆石子之后才能在下一堆中取石子. 游戏会进行 \(t\) 轮,每轮会 ...

  10. Python_003_Python循环控制

    ♥3.1  顺序结构 程序中语句执行的基本顺序按各语句出现位置的先后次序执行 ♥3.2  选择结构 主要理解一下if语句的使用即可,单分支 .双分支.多分支以及if语句的嵌套. ♥3.3  循环结构 ...