NAME

DBIx::MyParsePP::Rule - Access individual elements from the DBIx::MyParsePP parse tree

SYNOPSIS

        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'

DESCRIPTION

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.

METHODS

new($rule_name, @chilren) constructs a new rule

name() and getName() returns the name of the rule

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.

toString() converts the parse tree back into SQL by walking the tree and gathering all tokens in sequence.

EXTRACTING PARTS

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.

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.

getTables() or tables() can be used in the same manner to obtain all tables referenced in the parse tree.

SHRINKING THE TREE

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 |:

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_LEAFS will remove any Rules that have no children.

MYPARSEPP_SHRINK_SINGLES will remove any Rules that have just a single child, linking the child directly to the upper-level Rule.

MYPARSEPP_SHRINK_CHILDREN will apply schrink() recursively for all children.

If no flags are specified, all listed transformations are applied recursively.