Hi,
On Tue, Feb 10, 2009 at 10:32 AM, Tim Oliver <[email protected]> wrote:

>
> Jochen Daum wrote:
> > Hi,
> >
> > does anybody here know or has used a regular expression to tokenise a
> > Mysql query? What I'm looking at is getting the parts of the query in an
> > array of matches, i.e. the 'select ...' part, the 'from ...' part etc.
> >
> > I got this one for now, but it has some shortfalls:
> >
> > $found = preg_match
> >
> ('/^(\s?(SELECT|select)(.*?)\s(FROM|from)\s([^\s]+)(\s+((AS|as)\s+)?([^\s]+))?(\s+?(JOIN|join|LEFT|left|NATURAL|natural|RIGHT|right|INNER|inner).*?)?)(\s+(WHERE|where)\s(.+?))?(\s+(GROUP
> > BY|group by)\s.+?)?(\s+(ORDER BY|order
> > by)\s(.+?))?(\s+(LIMIT|limit)\s.*)?$/sm', $sql, $matches);
>
> > Any other issues anyone can see? Any regex or libraries that you've used?
>
> Writing a regex that'll handle all queries is going to be pretty much
> impossible - it's pretty much a case of deciding what subset you want to
> handle.
>
> select * from (select a,b,(select c from d) from e inner join f)
> union
> select * from g
> union
> select * from h
>
> Also, sElEcT is valid, and I've seen people use Select in production code.
>

Thanks Tim and Mike,

Yeah, using the 'i' modifier is going to make it a bit simpler. I also
probably be fine in not supporting subqueries in from and join clauses nor
supporting union.

I have a look at the phpmyadmin code.

Cheers,

Jochen


>
> --
> Tim Oliver
> Software Engineer
>
> 160 Cashel Street (Level 2)
> PO Box 22 128  Christchurch  New Zealand
> P +64 3 377 0007  F +64 3 377 6582
> E [email protected]
>
> www.e2media.co.nz
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]
-~----------~----~----~----~------~----~------~--~---

Reply via email to