简体   繁体   English

使用正则表达式从查询中获取表名

[英]get table name from query using a regex

I'm looking into finding a Pattern in order to get the table name for this type of SQL query;我正在寻找一个模式,以便为这种类型的 SQL 查询获取表名;

INSERT INTO table(uuid,type) VALUES (?,?)

Here I want to get "table"在这里我想得到“桌子”

I have a pattern but it's working only if there is no brackets like INSERT INTO table VALUES (?,?)我有一个模式,但只有在没有像 INSERT INTO table VALUES (?,?) 这样的括号时它才有效

[from|into|update]\s+(?:\w+.) (\w+)(\s $|\s+(WHERE)) [从|进入|更新]\s+(?:\w+.) (\w+)(\s $|\s+(WHERE))

Thanks谢谢

NB: it's not the same pattern than in get table name from query注意:它与从查询中获取表名的模式不同

This will never be a very fool proof way of extracting table names from SQL.这绝不是一种非常简单的从 SQL 中提取表名的方法。 But here is a solution to the problem using regex like you need.但这里有一个使用正则表达式解决问题的方法,就像你需要的那样。 You've specified the WHERE part in the provided expression but is it really necessary if you just need the name?您已经在提供的表达式中指定了 WHERE 部分,但如果您只需要名称,是否真的有必要?

(?is)\b(?:from|into|update)\s+(\w+)

Note that there are many ways a SQL statement can be formatted and it's very unlikely that anyone can come up with an expression that can parse SQL in the way you need.请注意,可以通过多种方式格式化 SQL 语句,任何人都不太可能想出一个可以按照您需要的方式解析 SQL 的表达式。

Demo演示

As people have already mentioned, Regular Expressions are not full proof solution for extracting table names from SQL queries... As tons of things has to be considered, which would be trickier to express in RegX, and would break out in one or other cases....正如人们已经提到的,正则表达式并不是从 SQL 查询中提取表名的完整证明解决方案......因为必须考虑很多事情,这在 RegX 中表达起来会更棘手,并且会在一种或其他情况下爆发....

Then what??然后呢?? Full proof sql parsers like JSQL parser?像 JSQL 解析器这样的完全证明的 sql 解析器?

Well if you just need to extract table names from SQLs, full blown sql parsers would be over kill, further most of the parser does not support all the dialects, You may end up modifying the grammer files for you need, just to extract table names.好吧,如果您只需要从 SQL 中提取表名,那么成熟的 sql 解析器就太过分了,而且大多数解析器不支持所有方言,您最终可能会根据需要修改语法文件,只是为了提取表名.

For this purpose I have written a small library for parsing table names from SQLs, with hardly 80 physical lines of code, it work with almost any SQL.为此,我编写了一个小型库,用于从 SQL 解析表名,物理代码几乎没有 80 行,它几乎适用于任何 SQL。 Refer to unit test cases for more detail.有关详细信息,请参阅单元测试用例

Came up with this merging all the others answer and with my use case.想出了这个合并所有其他答案和我的用例。

(?ims)\b(?:FROM|JOIN|UPDATE|INTO)\s+(\w+(?:\.\w+)*)

it also fetch things like INFORMATION_SCHEMA.TABLES if you use postgres or something that regroups tables.如果您使用 postgres 或重组表的东西,它还会获取 INFORMATION_SCHEMA.TABLES 之类的东西。

the following test string I used to test下面是我用来测试的测试字符串

SELECT columnA, columnB, columnC
FROM (
    SELECT t1.columnA as columnA, t1.columnB as columnB
    FROM table1 t1
    UNION
    SELECT t2.columnA as columnA, t2.columnB as columnB
    from table2 t2
) as tu
left JOIN table3 t3 ON (tu.columnA = t3.columnA)

WITH temporaryTable (averageValue) as
    (SELECT avg(Attr1)
    FROM Table),
    SELECT Attr1
    FROM Table
    WHERE Table.Attr1 > temporaryTable.averageValue;

Select ' Select productid,price'||
         ' from '|| table_name ||
         ' Union' quer
From tabs
Where table_name like 'table%';

Select ' Select productid,price'+
         ' from '+ table_name +
         ' Union' quer
From  INFORMATION_SCHEMA.TABLES 
where table_name  'table%';

Select ' Select productid,price'+
         ' from '+ table_name +
         ' Union' quer
From  INFORMATION_SCHEMA.TABLES.test 
where table_name  'table%';

Definitely not fool proof but may be good start: (?i)(SELECT|JOIN)(?=((\s+)(\`?)(?P<entity_name>[A-Za-z0-9_$""]*)(\`?)(\s)))绝对不是万无一失,但可能是一个好的开始: (?i)(SELECT|JOIN)(?=((\s+)(\`?)(?P<entity_name>[A-Za-z0-9_$""]*)(\`?)(\s)))

For me, the table name between quotes (?is)\b(?:from|into|update)\s+(\w+) didn't work, I changed to work for all queries where between quotes like `table_name` or 'table_name' or "table_name"对我来说,引号之间的表名(?is)\b(?:from|into|update)\s+(\w+)不起作用,我更改为适用于引号之间的所有查询,如 `table_name` 或 '表名'或“表名”

(?is)\b(?:from|into|update)\s+[|'|"]([^|'|"]+)[`|'|"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM