2

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

Community
  • 1
  • 1
Orpheo
  • 386
  • 1
  • 6
  • 15
  • 1
    Possible duplicate of [get table name from query](http://stackoverflow.com/questions/11401542/get-table-name-from-query) – Arnaud Jan 18 '16 at 15:27
  • Actually I saw that one but it's not exactly the same pattern – Orpheo Jan 18 '16 at 15:30
  • Could you edit your question to include some examples of SQL queries that you're not currently able to parse? – Kenster Jan 18 '16 at 17:48

5 Answers5

4

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

JonM
  • 1,314
  • 10
  • 14
3

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.

craftsmannadeem
  • 2,665
  • 26
  • 22
1

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%';
Marco
  • 11
  • 2
0

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

Nikitas
  • 1,013
  • 13
  • 27
0

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+[|'|"]([^|'|"]+)[`|'|"]
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Salah
  • 64
  • 7