好的,这似乎太难在这里发布,所以我请你原谅。已经为此工作了将近一周。
我需要提取给定 Oracle SQL 字符串中的所有选定列。它应该通过以下测试用例:
// single column test
select col1 from dual
// ^ should match "col1"
// multiple column test
select col1,col2 from dual
// ^ should match "col1", "col2"
// multiple space test
select col1 , col2 from dual
// ^ should match "col1", "col2"
// "distinct" tests
select distinct col1 from dual
// ^ should match "col1"
select distinct col1, col2 from dual
// ^ should match "col1", "col2"
// "distinct" with whitespaces tests
select distinct col1 from dual
// ^ should match "col1"
select distinct col1 , col2 from dual
// ^ should match "col1", "col2"
// "as" tests
select col1 from dual
// ^ should match "col1"
select colA as col1 from dual
// ^ should match "col1"
select colA as col1, col2, col3 from dual
// ^ should match "col1", "col2", "col3"
select col1, colB as col2, col3 from dual
// ^ should match "col1", "col2", "col3"
select col1, col2, colC as col3 from dual
// ^ should match "col1", "col2", "col3"
// "as" tests with whitespaces tests
select colA as col1, colB as col2, colC as col3 from dual
// ^ should match "col1", "col2", "col3"
// "distinct" with "as" tests
select distinct colA as col1 from dual
// ^ should match "col1"
select distinct colA as col1, colB as col2, col3 from dual
// ^ should match "col1", "col2", "col3"
select distinct colA as col1, col2, colC as col3 from dual
// ^ should match "col1", "col2", "col3"
// function test
select funct('1','2') as col1 from dual
// ^ should match "col1"
select col1, funct('1','2') as col2 from dual
// ^ should match "col1", "col2"
select col1, colB as col2, funct('1','2') as col3 from dual
// ^ should match "col1", "col2", "col3"
我在 Java 中尝试了以下 RegEx
((?<=select\ )(?!distinct\ ).*?(?=,|from))
((?<=select\ distinct\ ).*?(?=,|from))
((?<=as\ ).*?(?=,|from))
((?<=,\ ).*?(?=,|from))(?!.*\ as\ ) // <- Right, I'm guessing here
或将它们组合在一起,但我不能简单地通过上面的所有测试用例。(我正在使用这个工具来验证我的正则表达式)。
我尝试搜索 SQL 求值器,但找不到任何可以提取所有列而不对真实数据库执行它并且假定所有引用的表和函数都存在的方法。
一个 Java 正则表达式,一个可以通过测试的免费 SQL 评估器(不需要真正的数据库),或者任何更好的东西,这两个是可以接受的答案。假设 SQL 始终采用 Oracle 11g 格式。