网站首页 > 知识剖析 正文
以前做人口数据库时经常写些比较长的SQL语句,最长至上百行,看懂也得半天,恰巧在项目中遇见需要提取其中的条件子句,那时对正则也研究了一下,所以就顺便用正则写了个SQL语句分析、校验工具。正则表达式一般用在验证模式中,但个人认为验证只是正则的功能之一,在文本数据处理中才能发挥它的真正作用。利用正则表达式校验分析SQL语句,对于日常开发中,可能有简单问题复杂化之嫌,但这属于一个学习SQL和正则的绝佳机会。
【问题】先提出一个很简单的问题:如何提取Select语句中的使用的表和字段,以及过滤条件?
select count(distinct pis_p_renk_1.sysid) as result,
pis_p_renk.juzhd as address,
pis_p_guanlshx.guanlshx as guanlshx,
pis_p_renk.hukxzh as hukxzh
from pis_p_renk,
pis_p_guanlshx,
pis_p_zhuxyy,
pis_p_zinshx,
pis_p_guanx,
pis_p_renk as pis_p_renk_1
where pis_p_renk.sysid = pis_p_guanlshx.renkid
and pis_p_renk.sysid = pis_p_zhuxyy.renkid
and pis_p_renk_1.sysid = pis_p_guanx.renkid
and pis_p_renk.sysid = pis_p_guanx.guanxrid
and pis_p_guanx.sysid = pis_p_zinshx.guanxid
and pis_p_guanx.guanxlx = '2'
and pis_p_renk_1.xingb = '2'
and substr(pis_p_renk.chushrq, 1, 6) between substr('@StartTime', 1, 6) and
substr('@EndTime', 1, 6)
and pis_p_guanlshx.dangq = '1'
and (pis_p_guanlshx.guanlshxbdshj = '' or
pis_p_guanlshx.guanlshxbdshj is null or
pis_p_guanlshx.guanlshxbdshj is null or
substr(pis_p_guanlshx.guanlshxbdshj, 1, 6) <=
substr(pis_p_renk.chushrq, 1, 6))
and pis_p_renk.juzhd like '@Address%'
and pis_p_zinshx.zinshx not in ('3', '4', '8')
and pis_p_renk.zhengcnw = '5'
and pis_p_renk.xingb = '2'
(一)以下是概括分析思路:
将Select语句中各组成部分进行粗粒度、细粒度划分,然后将细粒度部分匹配正则表达式,再将细粒度合成粗粒度部分,最后组成完整正则表达式,可以校验语句,在正则捕获中可以获取各组成部分。
SQL正则构建工具:用于粗粒度、细粒度正则匹配。
SQL语句分析工具:分析SQL语句各组成部分。
(二)语句构造粗粒度分析(待续)
(三)细粒度正则匹配
(四)完整正则表达式搭建
(五)SQL正则分析过程
猜你喜欢
- 2024-12-19 为Navicat正名,Navicat完全使用手册(上)
- 2024-12-19 SQL学习 | 查询之ORDER BY sql order by sum
- 2024-12-19 SQL学习:有了这个语句删除数据中重复数据,超简单
- 2024-12-19 VBA, PQ, Power BI, SQL对财务工作有用吗
- 2024-12-19 初学者程序员要学好.Net,只要学习这几个框架就够了
- 2024-12-19 Python 学习 第17篇:sqlalchemy读写SQL
- 2024-12-19 零基础入门学习SQL Server数据库表
- 2024-12-19 SQL学习:SQL CASE 行转列新用法,太实用了
- 2024-12-19 零基础入门开始学习SQL Server存储过程
- 2024-12-19 「C# 数据操作系列」SqlSugar 精华大浓缩
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)