DBIx::MyParse::Item - Accessing the items from a DBIx::MyParse::Query parse tree
DBIx::MyParse::Query
use DBIx::MyParse; use DBIx::MyParse::Query; use DBIx::MyParse::Item; my $parser = DBIx::MyParse->new(); my $query = $parser->parse("SELECT field_name FROM table_name"); my $item_list = $query->getSelectItems(); my $first_item = $item_list->[0]; print $first_item->getType(); # Prints "FIELD_ITEM" print $first_item->getFieldName() # Prints "field_name"
MySQL uses a few dozen Item objects to store the various nodes possible in a parse tree. For the sake of simplicity, we only use a single object type in Perl to represent the same information.
my $string = $item->getType();
This returns the type of the Item as a string, to facilitate dumping and debugging.
Item
if ($item_type eq 'FIELD_ITEM') { ... } # Correct if ($item_type == FIELD_ITEM) { ... } # Will not work
The possible values are listed in enum Type in sql/item.h in the MySQL source.
enum Type
enum Type {FIELD_ITEM, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM, INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM, COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM, PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM, FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM, SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER, PARAM_ITEM };
From those, the following are explicitly supported:
FIELD_ITEM, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM, INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM REF_ITEM, COND_ITEM, PARAM_ITEM
In addition, DBIx::MyParse defines its own TABLE_ITEM in case a table, rather than a field, is being referenced.
TABLE_ITEM
REF_ITEM is a FIELD_ITEM that is used in a HAVING clause. VARBIN_ITEM is created when a Hex value is passed to MySQL (e.g. 0x5061756c). PARAM_ITEM is a ?-style placeholder.
REF_ITEM
FIELD_ITEM
HAVING
VARBIN_ITEM
PARAM_ITEM
my $string = $item->getFuncType();
if $item->getType() eq "FUNC_ITEM", you can call getFuncType() to determine what type of function it is. For MySQL, all operators are also of type FUNC_ITEM.
$item->getType() eq "FUNC_ITEM"
getFuncType()
FUNC_ITEM
The possible values are again strings (see above) and are listed in sql/item_func.h under enum Functype
enum Functype
enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC, GE_FUNC,GT_FUNC,FT_FUNC, LIKE_FUNC,NOTLIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC, COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC, BETWEEN, IN_FUNC, INTERVAL_FUNC, ISNOTNULLTEST_FUNC, SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC, SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC, SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC, SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING, SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN, NOT_FUNC, NOT_ALL_FUNC, NOW_FUNC, VAR_VALUE_FUNC };
if $item->getType() eq "SUM_FUNC_ITEM", $item->getFuncType() can be any of the aggregate functions listed in enum Sumfunctype in sql/item_sum.h:
$item->getType() eq "SUM_FUNC_ITEM"
$item->getFuncType()
enum Sumfunctype { COUNT_FUNC,COUNT_DISTINCT_FUNC,SUM_FUNC,AVG_FUNC,MIN_FUNC, MAX_FUNC,UNIQUE_USERS_FUNC,STD_FUNC,VARIANCE_FUNC,SUM_BIT_FUNC, UDF_SUM_FUNC,GROUP_CONCAT_FUNC };
For MySQL, all functions not specifically listed above are UNKNOWN_FUNC and you must call getFuncName().
UNKNOWN_FUNC
getFuncName()
my $string = $item->getFuncName();
Returns the name of the function called, such as "concat_ws", "md5", etc. If $item is not a function, but an operator, the symbol of the operator is returned, such as "+", "||", etc. The name of the function will be lowercase regardless of the orginal case in the SQL string.
my $item_array_ref = $item->getArguments();
Returns a reference to an array containing all the arguments to the function/operator. Each item from the array is an DBIx::MyParse::Item object, even if it is a simple string or a field name.
my $string = $item->getDatabaseName();
if $item is FIELD_ITEM, REF_ITEM or a TABLE_ITEM, getDatabaseName() returns the database the field belongs to, if it was explicitly specified. If it was not specified explicitly, such as was given previously with a "USE DATABASE" command, getDatabaseName() will return undef. This may change in the future if we incorporate some more of MySQL's logic
my $string = $item->getTableName();
Returns the name of the table for a FIELD_ITEM or TABLE_ITEM object. For FIELD_ITEM, the table name must be explicitly specified with "table_name.field_name" notation. Otherwise returns undef and does not attempt to guess the name of the table.
Returns the name of the field for a FIELD_ITEM object.
my $string = $item->getValue();
Returns, as string, the value of STRING_ITEM, INT_ITEM, REAL_ITEM and VARBIN_ITEM objects.
my $string = $item->getOrderDir();
my $string = $item->getGroupDir();
For an FIELD_ITEM used in GROUP BY or ORDER BY, those two identical functions return either the string "ASC" or the string "DESC" depending on the group/ordering direction. Default is "ASC" and will be returned even if the query does not specify a direction explicitly.
GROUP BY
ORDER BY
"ASC"
"DESC"
my $string = $item->getAlias();
Returns the name of the Item if provided with an AS clause, such as SELECT field AS alias.
my $item_ref = $item->getJoinCond()
For TABLE_ITEM Items, provides a reference to an DBIx::MyParse::Item object containing the condition under which the table in question will be joined to the preceding table ( the ON clause in SQL). The USING clause is automatically converted to an ON internally by MySQL.
my $string = $item->getJoinType();
Returns, as string, the type of join that will be used. Possible values are:
"JOIN_TYPE_LEFT" "JOIN_TYPE_RIGHT" "JOIN_TYPE_STRAIGHT"
Join type is RIGHT OUTER is currently not returned properly. This will be fixed in due time.
RIGHT OUTER
my $string_array_ref = $item->getUseIndex();
Returns a reference to an array containing one string for each index mentioned in the USE INDEX clause for the table in question.
USE INDEX
This may change in the future to an array of Item objects, rather than simple strings, however at this time MySQL provides strings.
my $string_array_ref = $item->getIgnoreIndex();
Returns a reference to an array containing one string for each index mentioned in an IGNORE INDEX clause for the table in question.
IGNORE INDEX
1 POD Error
The following errors were encountered while parsing the POD:
=over without closing =back
To install DBIx::MyParse, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::MyParse
CPAN shell
perl -MCPAN -e shell install DBIx::MyParse
For more information on module installation, please visit the detailed CPAN module installation guide.