DBIx::MyParsePP - Pure-perl SQL parser based on MySQL grammar and lexer
use DBIx::MyParsePP; use Data::Dumper; my $parser = DBIx::MyParsePP->new(); my $query = $parser->parse("SELECT 1"); print Dumper $query; print $query->toString();
DBIx::MyParsePP is a pure-perl SQL parser that implements the MySQL grammar and lexer. The grammar was automatically 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.
stmt_prepare_mode can be passed as arguments to the constructor. Please
use DBIx::MyParsePP::Lexer to bring in the required constants and see DBIx::MyParsePP::Lexer for information.
parse() which takes the string to be parsed. The result is a DBIx::MyParsePP::Query object which contains the result from the parsing.
Queries can be reconstructed back into SQL by calling the
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. The DBIx::MyParsePP::Rule module contains the
shrink() methods which are useful for dealing with the inherent complexity of the MySQL grammar.
The package by default parses strings using the grammar from MySQL version 5.0.45. If you wish to use the grammar from a different version, you can use the
bin/myconvpp.pl script to prepare the grammar:
$ perl bin/myconvpp.pl --
For Yacc grammars, please see the Bison manual at:
For generating Yacc parsers in Perl, please see:
For a full-speed C++-based parser that generates nicer parse trees, please see DBIx::MyParse
Philip Stoev, <email@example.com>
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.
The Parse::Yapp module and its related modules and shell scripts are copyright (c) 1998-2001 Francois Desarmenien, France. All rights reserved.