I’ve written a simple results cache for an Oracle driven application.
When I run a query I need to know which tables it uses so I can find out
if they have been modified and thus the cached data must be discarded
(as I said, it’s a very simple cache and the DB is basically static).
I didn’t like the idea of passing a table list as argument for each
query because they are dynamically built and, anyway, I thought it’d
become unmaintainable sooner or later. So I wrote an even simpler SQL
parser that looks like this:
$tables = 'PRODUCTS|CATEG ORIES|CUSTOMERS |ORDERS';
$regex = '/\W(FROM|JOIN)\s +(' . $tables . ')(?:\W|$)/i';
preg_match_all( $regex, $sql, $matches);
So far, it works fine enough for my purposes, but I think it’s not a
long term solution. Is it possible to build a generic reliable SQL
parser to fetch the table names of a query without diving in the regex
hell? Do you know about an existing library?* Can you think of any other
solution?
Thank you in advance,
[*] I’ve browsed some code out there that does what I need (typically
via regex soup), but it always fails with the queries I test.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
When I run a query I need to know which tables it uses so I can find out
if they have been modified and thus the cached data must be discarded
(as I said, it’s a very simple cache and the DB is basically static).
I didn’t like the idea of passing a table list as argument for each
query because they are dynamically built and, anyway, I thought it’d
become unmaintainable sooner or later. So I wrote an even simpler SQL
parser that looks like this:
$tables = 'PRODUCTS|CATEG ORIES|CUSTOMERS |ORDERS';
$regex = '/\W(FROM|JOIN)\s +(' . $tables . ')(?:\W|$)/i';
preg_match_all( $regex, $sql, $matches);
So far, it works fine enough for my purposes, but I think it’s not a
long term solution. Is it possible to build a generic reliable SQL
parser to fetch the table names of a query without diving in the regex
hell? Do you know about an existing library?* Can you think of any other
solution?
Thank you in advance,
[*] I’ve browsed some code out there that does what I need (typically
via regex soup), but it always fails with the queries I test.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Comment