DBIx::MyParsePP::Rule - Access individual elements from the DBIx::MyParsePP parse tree
use DBIx::MyParsePP; use DBIx::MyParsePP::Rule; my $parser = DBIx::MyParsePP->new(); my $query = $parser->parse("SELECT 1"); # $query is a DBIx::MyParsePP::Rule object my $root = $query->root(); print $root->name(); # prints 'query', the top-level grammar rule my @children = $root->chilren(); # print $children[0]->name(); # prints 'verb_clause', the second-level rule print ref($chilren[1]); # prints 'DBIx::MyParsePP::Token' print $chilren[1]->type(); # prints END_OF_INPUT print [[[$root->chilren()]->[0]->chilren()]->[0]->chilren()]->[0]->name(); # Prints 'select'
DBIx::MyParsePP uses the sql_yacc.yy grammar from the MySQL source to parse SQL strings. A parse tree is produced which contains one branch for every rule encountered during parsing. This means that very deep trees can be produced where only certain branches are important.
sql_yacc.yy
new($rule_name, @chilren) constructs a new rule
new($rule_name, @chilren)
name() and getName() returns the name of the rule
name()
getName()
chilren() and getChildren() return (as array) the right-side items that were matched for that rule, e.g. its "child branches" in the parse tree.
chilren()
getChildren()
toString() converts the parse tree back into SQL by walking the tree and gathering all tokens in sequence.
toString()
extract(@names) can be used to walk the tree and extract relevant parts. The method returns undef if no part of the tree matched, a DBIx::MyParse::Rule or a DBIx::MyParse::Token object if a sigle match was made, or a reference to an array of such objects if several parts matched. Names to the front of the @names list are matched first.
extract(@names)
@names
getFields() or fields() can be used to obtain all fields referenced in a parse tree or a part of it. Those functions will return <undef> if no fields were referenced or a reference to an array containing Rule objects for each field. The Rule object can contain several 'ident' subrules if a database and/or a table name was specified for the given field.
getFields()
fields()
Rule
getTables() or tables() can be used in the same manner to obtain all tables referenced in the parse tree.
getTables()
tables()
The raw tree produced by DBIx::MyParsePP contains too many branches, many of them not containing any useful information. The shrink($flags) method is used to convert the tree into a more manageable form. $flags can contain the following constants joined by |:
shrink($flags)
$flags
|
MYPARSEPP_SHRINK_IDENTICAL if a parent and a child node are of the same type, they will be merged together. This way expressions such as 1 + 1 + 1 or col1, col2, col3 will be converted from a nested tree with one item per branch into a single Rule containing a list of all the items and the tokens between them (e.g. + or ,). Expressions such as 1 + 2 * 3 will remain as a tree because multiplication and addition have different precedence.
MYPARSEPP_SHRINK_IDENTICAL
1 + 1 + 1
col1, col2, col3
+
,
1 + 2 * 3
MYPARSEPP_SHRINK_LEAFS will remove any Rules that have no children.
MYPARSEPP_SHRINK_LEAFS
MYPARSEPP_SHRINK_SINGLES will remove any Rules that have just a single child, linking the child directly to the upper-level Rule.
MYPARSEPP_SHRINK_SINGLES
MYPARSEPP_SHRINK_CHILDREN will apply schrink() recursively for all children.
MYPARSEPP_SHRINK_CHILDREN
schrink()
If no flags are specified, all listed transformations are applied recursively.
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.