NAME

DBIx::MyParsePP - Pure-perl SQL parser based on MySQL grammar and lexer

SYNOPSIS

use DBIx::MyParsePP;
use Data::Dumper;

my $parser = DBIx::MyParsePP->new();

my $result = $parser->parse("SELECT 1");

print Dumper $result;
print $result->asString();

DESCRIPTION

DBIx::MyParsePP is a pure-perl SQL parser that implements the MySQL grammar and lexer. The grammar was converted from the original sql_yacc.yy file by removing all the C code. The lexer comes from sql_lex.cc, completely translated in Perl almost verbatim.

The grammar is converted into Perl form using Parse::Yapp.

METHODS

DBIx::MyParsePP provides a single method, parse() which takes the string to be parsed. The result is either undef if the string can not be parsed, or a DBIx::MyParsePP::Rule object that contains all nested grammar elements inside it. Within that tree, terminal symbols are returned as DBIx::MyParsePP::Token objects.

Queries can be reconstructed back into SQL by calling the asString() method of the top-level DBIx:MyParsePP::Rule object.

SPECIAL CONSIDERATIONS

The file containing the grammar lib/DBIx/MyParsePP/Parser.pm is about 5 megabytes in size and takes a while to load. Compex statements take a while to parse, e.g. the first Twins query from the MySQL manual can only be parsed at a speed of few queries per second per 1GHz of CPU. If you require a full-speed parsing solution, please take a look at DBIx::MyParse, which requires a GCC compiler and produces more concise parse trees.

The parse trees produced by DBIx::MyParsePP contain one leaf for every grammar rule that has been matched, even rules that serve no useful purpose. Therefore, parsing event simple statements such as SELECT 1 produce trees dozens of levels deep. Please exercise caution when walking those trees recursively.

SEE ALSO

For Yacc grammars, please see the Bison manual at:

http://www.gnu.org/software/bison

For generating Yacc parsers in Perl, please see:

http://search.cpan.org/~fdesar

For a full-speed C++-based parser that generates nicer parse trees, please see DBIx::MyParse

AUTHOR

Philip Stoev, <philip@stoev.org>

COPYRIGHT AND LICENSE

Copyright (C) 2007 by Philip Stoev

This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public Licence as specified in the README and LICENCE files.

Please note that this module contains code copyright by MySQL AB released under the GNU General Public Licence, and not the GNU Lesser General Public Licence. Using this code for commercial purposes may require purchasing a licence from MySQL AB.