SQL解析
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解析的更多相关文章
- 步步深入:MySQL架构总览->查询执行流程->SQL解析顺序
前言: 一直是想知道一条SQL语句是怎么被执行的,它执行的顺序是怎样的,然后查看总结各方资料,就有了下面这一篇博文了. 本文将从MySQL总体架构--->查询执行流程--->语句执行顺序来 ...
- Spark之SQL解析(源码阅读十)
如何能更好的运用与监控sparkSQL?或许我们改更深层次的了解它深层次的原理是什么.之前总结的已经写了传统数据库与Spark的sql解析之间的差别.那么我们下来直切主题~ 如今的Spark已经支持多 ...
- 高大上技术之sql解析
Question: 为何sql解析和高大上有关系?Answer:因为数据库永远都是系统的核心,CRUD如此深入码农的内心...如果能把CRUD改造成高大上技术,如此不是造福嘛... CRUD就是Cre ...
- sql 解析字符串添加到临时表中 sql存储过程in 参数输入
sql 解析字符串添加到临时表中 sql存储过程in 参数输入 解决方法 把字符串解析 添加到 临时表中 SELECT * into #临时表 FROM dbo.Func_SplitOneCol ...
- oracle 内存结构 share pool sql解析的过程
1.sql解析的过程 oracle首先将SQL文本转化为ASCII字符,然后根据hash函数计算其对应的hash值(hash_value).根据计算出的hash值到library cache中找到对应 ...
- 基于简单sql语句的sql解析原理及在大数据中的应用
基于简单sql语句的sql解析原理及在大数据中的应用 李万鸿 老百姓呼吁打土豪分田地.共同富裕,总有一天会实现. 全面了解你所不知道的外星人和宇宙真想:http://pan.baidu.com/s/1 ...
- 自己实现一个SQL解析引擎
自己实现一个SQL解析引擎 功能:将用户输入的SQL语句序列转换为一个可运行的操作序列,并返回查询的结果集. SQL的解析引擎包含查询编译与查询优化和查询的执行,主要包含3个步骤: 查询分析: 制定逻 ...
- [转]ORACLE SQL解析之硬解析和软解析
http://blog.chinaunix.net/uid-25909722-id-3363789.html 当客户端进程,将SQL语句通过监听器发送到Oracle时, 会触发一个Server pro ...
- 关系型数据库工作原理-SQL解析(翻译自Coding-Geek文章)
本文翻译自Coding-Geek文章:< How does a relational database work>.原文链接:http://coding-geek.com/how-data ...
- SQL解析在美团的应用
https://tech.meituan.com/SQL_parser_used_in_mtdp.html 数据库作为核心的基础组件,是需要重点保护的对象.任何一个线上的不慎操作,都有可能给数据库带来 ...
随机推荐
- Python scrapy爬虫数据保存到MySQL数据库
除将爬取到的信息写入文件中之外,程序也可通过修改 Pipeline 文件将数据保存到数据库中.为了使用数据库来保存爬取到的信息,在 MySQL 的 python 数据库中执行如下 SQL 语句来创建 ...
- 网络视频会议openmeetings Windows安装
官网 http://openmeetings.apache.org/index.html 下载文件解压运行install-service脚本之后运行red5脚本启动 官方安装文档 http://ope ...
- npm install报错
npm ERR! C:\Users\xxxxxx\AppData\Roaming\npm-cache\_logs\2019-02-27T07_48_47_283Z-debug.log " ...
- squid3.5缓存代理实践记录
准备: 两台服务器,一台web,一台squid缓存代理 squid机域名:www.dannylinux.top web机IP:12.1.1.1 1.版本: [root@danny squid]# sq ...
- MarkDown&思维导图
从markdown文件创建思维导图 pzhaonet/mindr: an R package which converts markdown files (.md, .Rmd) into mindma ...
- 函数中的this与argument对象,以及argument中的callee与caller属性
相关阅读:https://segmentfault.com/a/1190000015438195 相关阅读: https://zhuanlan.zhihu.com/p/23804247 相关阅读: h ...
- consul剔除节点
查询节点注册 curl -s http://192.168.1.1:8500/v1/catalog/service/aaa | python -m json.tool 删除注册信息 curl http ...
- Java中interrupt的使用
通常我们会有这样的需求,即停止一个线程.在java的api中有stop.suspend等方法可以达到目的,但由于这些方法在使用上存在不安全性,会带来不好的副作用,不建议被使用.具体原因可以参考Why ...
- hadoop记录-hive常见设置
分区表 set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict;create tabl ...
- Axure PR的使用
1759139 王越 Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或W ...