Re: Re: Writing a parser

From: Date: Tue, 09 Jul 2002 19:35:13 +0000
Subject: Re: Re: Writing a parser
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-7602@lists.php.net to get a copy of this message
> > > > I'd like to see a SQL parser (SQL-99?) in PEAR. This would enable me to > > replace some hacks I used to expand SQL queries automatically. > > > > Ulf > > Can be very hard to create an efficiant parser. Limit it to the ANSI 99 > can simplify the job, and can make useless with pear::db. Yes, it can be hard to create an elegant parser too. I have checked in a class in CVS under DB_DBA that can parse some SQL create statements; DBA_Sql.php. Perhaps this will belong in its own package when it grows up. It is a lot more robust than what I had attached previously, but still a ways from understanding all of create's options. The function DBA_Sql::parseCreate() accepts a string containing a create statement and returns an associative array of a table schema. You can run the file in tests, test_sql.php, to see it in action. It currently parses int, enum, set, varchar and several other types, understands quoted strings, escaped quotes and other things. The bad news is that it's about 150 lines to do this, since I just used strtok() and went to town. Maybe regular expressions would be more efficient (but possibly less clear). Five dollars to the first person to do it in less than 100 lines ;) - Brent

« previous php.pear.dev (#7602) next »