Re: Parsing a simple sql string in php
| From: | Stuart Dallas | Date: | Fri, 06 May 2011 09:14:08 +0000 |
| Subject: | Re: Parsing a simple sql string in php | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-312702@lists.php.net to get a copy of this message | ||
On Friday, 6 May 2011 at 10:05, Ashley Sheridan wrote:
Hiya, has anyone had any experience with parsing a string of sql to break it down into its component
parts? At the moment I'm using several regex's to parse a string, which works, but
I'm sure there's a more elegant solution.
>
> The general idea is to produce a nice looking page giving details of updated fields and values.
>
> I'm only concerned with update statements really, and the queries are very simple,
> operating on one table at a time, with no sub queries or selects.
>
> Thanks in advance if anyone is able to suggest anything!
I don't have any experience doing it with SQL, but I have written several parsers in my time,
and I'd strongly recommend against using regexes to do it.
The usual way to approach this problem is to tokenise the input, then take each token at a time and
branch where there are several options for what comes next. As you go along, build up a data
structure that represents the data you need.
Alternatively you could ask Google...
http://www.phpclasses.org/package/4916-PHP-Build-a-tree-to-represent-an-SQL-query.html
http://code.google.com/p/php-sqlparser/
and more: http://jmp.li/fmy
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/