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.
DBIx::MyParsePP
sql_yacc.yy
sql_lex.cc
The grammar is converted into Perl form using Parse::Yapp.
charset, version, sql_mode, client_capabilities and 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.
charset
version
sql_mode
client_capabilities
stmt_prepare_mode
use DBIx::MyParsePP::Lexer
DBIx::MyParsePP provides parse() which takes the string to be parsed. The result is a DBIx::MyParsePP::Query object which contains the result from the parsing.
parse()
Queries can be reconstructed back into SQL by calling the toString() method.
toString()
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.
lib/DBIx/MyParsePP/Parser.pm
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 extract() and shrink() methods which are useful for dealing with the inherent complexity of the MySQL grammar.
SELECT 1
extract()
shrink()
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:
bin/myconvpp.pl
$ perl bin/myconvpp.pl --
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
Philip Stoev, <philip@stoev.org>
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.
To install DBIx::MyParsePP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::MyParsePP
CPAN shell
perl -MCPAN -e shell install DBIx::MyParsePP
For more information on module installation, please visit the detailed CPAN module installation guide.