Taxi::Mysql::Query::Base - extendable full-text index using mysql: queries: base class


NAME

Taxi::Mysql::Query::Base - extendable full-text index using mysql: queries: base class

(Back to Top)


SYNOPSIS

 ##========================================================================
 ## PRELIMINARIES
 use Taxi::Mysql::Query::Base;
 ##========================================================================
 ## Constructors etc.
 $q  = $CLASS_OR_OBJ->new(%args);
 $q  = $q->initialize();
 $q2   = $q1->clone();
 $qdst = $qdst->assign($qsrc);
 $qdst = $qdst->merge($qsrc);
 $qdst = $qdst->unify($qsrc);
 \%tabName2vars = $query->tab2vars();
 $q->clear();
 $q = $q->clearCache();
 ##========================================================================
 ## Properties
 $bool = $q->ok();
 ##========================================================================
 ## Query Construction: Utilities
 $var = $q->newVariable(%args);
 $var = $q->newTokenVariable();
 $varName = $q->setToken($varName);
 $canonical_ref = $q->canonicalVariable($var);
 \%alias_table = $q->canonicalHash();
 undef = $q->aliasVariables($varFrom=>$varTo, ...);
 $conds = flatConditions($cond_string_or_ref);
 $conds = nestedConditions($cond_string_or_ref_or_refref);
 $q = $q->pushConditions(\@conditions);
 ##========================================================================
 ## SQL: Generation
 \%sql_alias = $q->_sql_alias();
 $q          = $q->sql_prepare();
 $options_string    = $q->sql_select_options();
 $column_string     = $q->sql_select_columns();
 $table_string      = $q->sql_from_tables();
 $join_string       = $q->sql_join_conditions();
 $where_strings     = $q->sql_where_conditions();
 $conditions_string = $q->sql_conditions();
 $sql_fragment      = $q->sql_limit(%args);
 $sql_query_string  = $q->sql();
 ##========================================================================
 ## SQL: execution
 $q       = $q->open();
 $q       = $q->close();
 $hit     = $q->fetchHit();
 $hitList = $q->fetchHitList();
 $q       = $q->openPage($pagenum);
 undef    = $q->closePage();
 $hitList = $q->fetchPage($pagenum);
 ##========================================================================
 ## DEBUG
 $hash = $q->_debug();

(Back to Top)


DESCRIPTION

Globals etc.

Variable: @ISA

Taxi::Mysql::Query::Base inherits from Taxi::Mysql::Connection.

Variable: %EXPORT_TAGS

Taxi::Mysql::Query::Base exports the utility functions flatConditions() and nestedConditions under the tag ':child'. Useful for derived subclasses.

Constructors etc.

new
 $q = $CLASS_OR_OBJ->new(%args);

Low-level query constructor. Variable references in conditions, joins, etc. are strings of the form '${'.$VARNAME.'}'. See the Taxi::Mysql::MacroUtils manpage for details. Object structure / keyword %args:

   {
    ##-- Defaults
    index => $ix,                      ##-- underlying Taxi::MySQL object
    default_table => $table,           ##-- default table-suffix for token variables: default='token'
    default_hit   => $default_hit_col, ##-- column name for default value of $q->{hit}: default='doc'
    ##-- Tables and Variables
    var2tab => { "$var1"=>$table1, ... },    ##-- maps *all* variable labels to Table objects
    vareq   => { "$var1"=>$alias1, ... },    ##-- variable alias table (canonical variable name)
    toks    => { "$var1"=>undef, ..., },     ##-- pseudo-set: holds all independent (token) vars
    ##-- Tokens and Hits: [BAD IDEAS?]
    hit        => $current_hit_column,       ##-- identifies current hit column [may be undef]
    match_table=> $match_table,              ##-- matched-token table (default=$q->{default_table})
    ##-- Query conditions
    joins => [ $join_1, ..., $join_NJ ],        ##-- join conditions (AND'ed together)
    conds => [
              $and_1=[$or_1_1, ..., $or_1_N1],  ##-- query conditions in conjunctive normal form
              ...,                              ##   interp:
              $and_M=[$or_M_1, ..., $or_M_NM],  ##     (($or_1_1,...,$or_1_N1) AND...AND ($or_M_1,...,$or_M_NM))
             ],
    ##-- Query execution
    sql => $cached_sql_string,  ##-- cached on $query->open(), cleared on $query->close()
    pagesize => $nHitsPerPage,  ##-- max number of rows to return per 'page', undef --> all rows (default)
    pagenum => $pageNumber      ##-- page number requested (counting from 0; default=0)
    ##-- inherited from Taxi::Mysql::Base
    verbose=>$level,          ##-- verbosity level
    ##-- inherited from Taxi::Mysql::Connection
    handleArgs=>\%handleArgs, ##-- arguments for Taxi::Mysql::Handle->new()
                              ##   + default: inherited from $r->{index}
    handle=>$handle,          ##-- a Taxi::Mysql::Handle object
   }
initialize
 $q = $q->initialize();

Initializes a query object. Default implementation currently does nothing.

clone
 $q2 = $q1->clone();

Creates and returns a deep clone of a query object.

assign
 $qdst = $qdst->assign($qsrc);

Assigns $qdst structure to $qsrc. Structure in this sense includes variable names, join and query conditions, as well as selected hit.

mergeVariables
 $qdst = $qdst->mergeVars($qsrc)

Adopts token list, variables, and var-equivalances, from $qsrc into $qdst. Does NOT check for variable-name conflicts.

merge
 $qdst = $qdst->merge($qsrc);

``Heavy'' merge operation.

unify
 $qdst = $qdst->unify($qsrc);
tab2vars
 \%tabName2vars = $query->tab2vars();

Low-level method. Returns a hash keyed by table name with values sorted array-refs of (independent) variable names.

clear
 $q->clear();

Clears all query structure and cached values.

clearCache
 $q = $q->clearCache();

Clears query cached data.

DESTROY

Destructor.

Properties

ok
 $bool = $q->ok();

Checks whether query is valid. Validity tests are currently not very sophisticated, but should return quickly.

TODO: be more paranoid here.

Query Construction: Utilities

newVariable
 $var = $q->newVariable(%args);

Allocate a new query-local variable string symbol.

Known %args:

 table => $varTable, ##-- default: $q->{default_table}: may be name or ref
 label => $varLabel, ##-- default: independent (new variable)
                     ##   + may contain 1 or more references to independent vars
 tok   => $bool,     ##-- if true $varLabel is added to $q->{toks}

The allocated variable will have the label $varLabel (default: generated) and be bound to the index table $varTable. If the 'tok' keyword argument is given as a true value, then the new variable will be added to the pseudo-set of 'independent' variables $q->{toks}. Otherwise, if the 'tok' argument was not specified or undefined, the new variable will only be considered 'independent' if no 'label' argument was specified. In all other cases, the new variable is considered 'dependent', and therefore not added to $q->{toks}. Returns the identifier for the newly allocated variable.

newTokenVariable
 $var = $q->newTokenVariable();
 $var = $q->newTokenVariable($table);

Wrapper for $q->newVariable() which allocates a new independent variable for $table.

setToken
 $varName = $q->setToken($varName);

Low-level method. Sets the name of the currently 'active' independent variable $q->{tok} to $varName, and adds $varName to $q->{toks}.

canonicalVariable
 $canonical_ref = $q->canonicalVariable($var);

Returns the canonical name of the variable $var.

canonicalHash
 \%alias_table = $q->canonicalHash();

Returns a HASH-ref mapping all known variable identifiers to their canonical forms. Sets $q->{_canonical}.

aliasVariables
 $undef = $q->aliasVariables($varFrom=>$varTo, ...);

Aliases $varFrom variable(s) to $varTo variable(s), causing $varFrom and $varTo to refer to the same underlying canonical variable. Both $varFrom and $varTo must be independent (read: ``token'') variables for this to work correctly.

flatConditions
 $conds = flatConditions($cond_string_or_ref);

Utility function to ensure that an array is returned.

nestedConditions
 $conds = nestedConditions($cond_string_or_ref_or_refref);

Utility function to ensure that an array-of-arrays is returned.

pushConditions
 $q = $q->pushConditions(\@conditions);

Pushes \@conditions onto query conditions for $q. Used for normal-form type logical connective.

distributeConditions
 $q = $q->distributeConditions(\@conditions)

Distributes \@conditions over query conditions for $q Used for non-normal-form type logical connective.

SQL Generation

_sql_alias
 \%sql_alias = $q->_sql_alias();
 \%sql_alias = $q->_sql_alias($prefix)

Generates and returns an sql-safe alias table, storing it in $q->{_sql_alias}. The generated table %sql_alias maps variable identifiers to SQL-safe table alias names. $prefix is an sql-safe alias prefix; defaults to 'tab_'.

sql_prepare
 $q = $q->sql_prepare();

Prepare query for SQL generation. Current implementation imposes the default hit restrictor, caches canonical variable forms and SQL variable aliases, as well as an SQL alias macro expansion table.

sql_select_options
 $options_string = $q->sql_select_options();

Additional options between SELECT and column list in the generated query.

sql_select_columns
 @column_strings = $q->sql_select_columns(); ##-- list context
 $columns_string = $q->sql_select_columns(); ##-- scalar context

Returns SQL backend query column list.

sql_from_tables
 @table_strings = $q->sql_from_tables(); ##-- list context;
 $tables_string = $q->sql_from_tables(); ##-- scalar context

Returns SQL backend query FROM tables list, with 'AS' aliases.

sql_join_conditions
 @join_strings = $q->sql_join_conditions(); ##-- list context;
 $joins_string = $q->sql_join_conditions(); ##-- scalar context

Returns SQL backend query join conditions.

NOTE join computation using the 'JOIN' keyword in the FROM tables list is slightly faster in MySQL v5.x than joining using explicit conditions in the 'WHERE' clause. MySQL v4.x seems incapable of correctly optimizing queries containing the 'JOIN' keyword however, so these strings live in the 'WHERE' clause for now.

sql_where_conditions
 @where_strings = $q->sql_where_conditions(); ##-- list context;
 $wheres_string = $q->sql_where_conditions(); ##-- scalar context

Returns query-specific restrictions for the backend WHERE clause (except for join conditions).

sql_conditions
 $conditions_string = $q->sql_conditions(); ##-- scalar context only;

Wrapper to return all conditions for the backend 'WHERE' clause.

sql_group_by

Returns backend SQL 'GROUP BY' column list. For the current implementation, the 'GROUP BY' column list must contain exactly one column, and that column must be the primary key of some indexed table. Thus, the selected 'GROUP BY' table is also known as the 'hit container'.

Default implementation returns $q->{hit}.

sql
 $sql_query_string = $q->sql();

Generates and returns basic backend SQL query: everything except for LIMIT clause.

sql_limit
 $sql_fragment = $q->sql_limit(%args);

Generates and returns LIMIT clause for paging options (pagesize, pagenum) in $q, which may overridden by %args.

SQL: execution

open
 $q = $q->open(%args);

Open a backend SQL statement handle $q->{sth} for the query, caching $q->{sth}{NAME} as $q->{sth_NAME}.

Known %args: sql, pagenum, pagesize, ...

close
 $q = $q->close();
fetchHit
 $hit = $q->fetchHit(%hitArgs)
fetchHitList
 $hitList = $q->fetchHitList();
 $hitList = $q->fetchHitList($hitList,%hitArgs)

Fetches and populates all available hits, returns the result as a Taxi::Mysql::HitList object.

openPage
 $q = $q->openPage($pagenum);

Convenience wrapper for setting $q->{pagenum} and calling $q->open().

closePage
 undef = $q->closePage();

Closes the current page by closing $q->{sth}, but does not clear any cached data.

fetchPage
 $hitList = $q->fetchPage($pagenum);
 $hitList = $q->fetchPage($pagenum, $hitlist)
 $hitList = $q->fetchPage($pagenum, $hitlist, %hitArgs)

Convenience wrapper for openPage() and fetchHitList().

DEBUG

_debug
 $hash = $q->_debug();

Returns a more or less human-readble string representation of the query for debugging purposes.

(Back to Top)


ACKNOWLEDGEMENTS

Perl by Larry Wall.

(Back to Top)


AUTHOR

Bryan Jurish <moocow@ling.uni-potsdam.de>

(Back to Top)


COPYRIGHT AND LICENSE

Copyright (C) 2006 by Bryan Jurish

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.

(Back to Top)


SEE ALSO

perl(1), Taxi::Mysql(3perl), Taxi::Mysql::Query(3perl), Taxi::Mysql::Query::Boolean(3perl), Taxi::Mysql::Query::Parser(3perl), Taxi::Mysql::Hit(3perl), Taxi::Mysql::HitList(3perl).

(Back to Top)

 Taxi::Mysql::Query::Base - extendable full-text index using mysql: queries: base class