What is a good method for parsing an sql string into it separate components. I\'v tried with a regex, however I can\'t get it to work just right.
Say for instance:
The example that you have shown,is a code that will read your SQL statement and tokenize with spaced in between and store each token in the array. If you are looking for the such pice of code, you can make that with split functions in php: http://ca.php.net/split
Regards,
Pooria.
The full syntax of an SQL SELECT statement is very complex. I think you may want to limit yourself to a subset of it.
A regular expression can parse only a regular language. That is, a language that can be expressed with a finite state machine.
Most programming languages, including SQL, cannot be expressed as a regular language, and thus cannot be parsed with regular expressions. Even doing limited tasks like splitting SQL expressions is going to be maddeningly difficult with regular expressions.
You'll need to use a more robust parser generator that can handle more complex grammars. You tag your question with PHP, so you might want to look into Lime.
You'll also need to find a grammar file for PHP compatible with the parser generator you choose. I'm not sure where to get one for PHP and Lime, but there's a pure Perl project called DBIx::MyParsePP that is based on MySQL's grammar.
Parsing SQL correctly and completely is hard. For free you could use a parser generator toolkit like ANTLR along with a SQL 'grammar'. For cost, I know that Visual Studio Team System 2008 Database Edition includes a SQL parser.
I'd go for a sql parser that could be integrated to your language: don't reinvent the wheel.
I haven't used one in particular to recommend, but I'm sure that you can find one that do what you need.
See related: PHP MySQL SQL parser (INSERT and UPDATE)