regular expressions for postgres syntax

J

Jan Biel

Hi there!

I'm looking for a regular expression or a set of expressions able to cut
Postgres (or any SQL) code into its syntactical parts.

For example if the code was

CREATE TABLE table_a (
attrib_a varchar(8) NOT NULL,
attrib_b varchar(10)
);

I need to extract the components like this:

CREATE TABLE
table_a
---
attib_a
varchar(8)
NOT NULL
---
attrib_b
varchar(10)
---

This is only a simplified example, since SQL code can be much more complex.
I was hoping there are given regular expressions so I wouldn't have to do
all the work by myself (which I'm not sure I'm able to).

Postgres would be awesome, but any SQL version would do since I could
translate the expressions rather easily, I guess.

Thanks in advance on any help towards the matter
Janbiel
 
J

Jack Challen

Jan said:
I'm looking for a regular expression or a set of expressions able to cut
Postgres (or any SQL) code into its syntactical parts.

Whoops. Previous post missed out obvious module to try...

You really *don't* want to try a regex approach. SQL's an entire language,
I doubt a regular expression would be possible, and it certainly wouldn't
be readable.

You want a parser.
In fact, you want SQL::parser, or SQL::Translator::parser::postgreSQL

http://search.cpan.org/search?query=sql::parser&mode=module
http://search.cpan.org/~kclark/SQL-Translator-0.05/lib/SQL/Translator/Parser/PostgreSQL.pm
-or-
http://tinyurl.com/yruzd

jack
 
G

Gregory Toomey

Jan said:
Hi there!

I'm looking for a regular expression or a set of expressions able to cut
Postgres (or any SQL) code into its syntactical parts.

You want a parser, not a regular expression. If you dont know the difference
then you are out of your depth.

gtoomey
 
J

Jan Biel

Jack said:

Thanks a bunch, exactly what I need (I guess). Didn't know there were
parsers available just like that, like standalone parsers outside of
compilers.

Yes, I still have a lot to learn :)

Thanks anyway,
Janbiel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top