Orpheo
Orpheo

Reputation: 406

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;

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 (?,?)

[from|into|update]\s+(?:\w+.)(\w+)(\s$|\s+(WHERE))

Thanks

NB : it's not the same pattern than in get table name from query

Upvotes: 2

Views: 6370

Answers (5)

Marco
Marco

Reputation: 11

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.

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%';

Upvotes: 1

Salah
Salah

Reputation: 64

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+[|'|"]([^|'|"]+)[`|'|"]

Upvotes: 0

Nikitas
Nikitas

Reputation: 1003

Definitely not fool proof but may be good start: (?i)(SELECT|JOIN)(?=((\s+)(\`?)(?P<entity_name>[A-Za-z0-9_$""]*)(\`?)(\s)))

Upvotes: 0

craftsmannadeem
craftsmannadeem

Reputation: 2953

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....

Then what?? Full proof sql parsers like JSQL parser?

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.

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. Refer to unit test cases for more detail.

Upvotes: 3

JonM
JonM

Reputation: 1374

This will never be a very fool proof way of extracting table names from 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?

(?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.

Demo

Upvotes: 4

Related Questions