Path

ez components / documentation / api reference / 2006.2 / database


eZ Components 2006.2

Database: ezcQuery

[ Tutorial ] [ Oracle_setup ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]

Class: ezcQuery

The ezcQuery class provides the common API for all Query objects. [source]
ezcQuery has three main purposes:
  • it provides a common API for building queries through the getQuery() method.
  • it provides a common API for binding parameters to queries through bindValue() and bindParam()
  • it provides internal aliasing functionality that allows you to use aliases for table and column names. The substitution is done inside of the query classes before the query itself is built.
Through the bind methods you can bind parameters and values to your query. Finally you can use prepare to get a PDOStatement object from your query object.
Subclasses should provide functionality to build an actual query.

Descendents

Child Class Description
ezcQueryDelete Class to create select database independent DELETE queries.
ezcQueryUpdate Class to create select database independent UPDATE queries.
ezcQuerySelect Class to create select database independent SELECT queries.
ezcQueryInsert Class to create select database independent INSERT queries.

Member Variables

protected PDO $db
A pointer to the database handler to use for this query.
public ezcQueryExpression $expr = null
The expression object for this class.

Method Summary

public ezcQuery __construct( $db, [$aliases = array()] )
Constructs a new ezcQuery that works on the database $db and with the aliases $aliases.
public static array arrayFlatten( $array )
Returns all the elements in $array as one large single dimensional array.
public string bindParam( &$param, [$placeHolder = null], $param )
Binds the parameter $param to the specified variable name $placeHolder..
public string bindValue( $value, [$placeHolder = null] )
Binds the value $value to the specified variable name $placeHolder.
public void doBind( $stmt )
Performs binding of variables bound with bindValue and bindParam on the statement $stmt.
protected string getIdentifier( $alias )
Returns the correct identifier for the alias $alias.
protected array(string) getIdentifiers( $aliasList, $alias )
Returns the correct identifiers for the aliases found in $aliases.
public abstract string getQuery( )
Returns the query string for this query object.
public bool hasAliases( )
Returns true if this object has aliases.
public PDOStatement prepare( )
Returns a prepared statement from this query which can be used for execution.
protected void resetBinds( )
Resets the bound values and parameters to empty.
public void setAliases( $aliases )
Sets the aliases $aliases for this object.

Methods

__construct

ezcQuery __construct( $db, [ $aliases = array()] )
Constructs a new ezcQuery that works on the database $db and with the aliases $aliases.
The aliases can be used to substitute the column and table names with more friendly names. E.g PersistentObject uses it to allow using property and class names instead of column and table names.

Parameters

Name Type Description
$db PDO  
$aliases array(string=>string)  

Redefined in descendants as

Method Description
ezcQueryDelete::__construct() Constructs a new ezcQueryDelete that works on the database $db and with the aliases $aliases.
ezcQueryUpdate::__construct() Constructs a new ezcQueryUpdate that works on the database $db and with the aliases $aliases.
ezcQuerySelect::__construct() Constructs a new ezcQuery object.
ezcQuerySubSelect::__construct() Constructs a new ezcQuery object.
ezcQuerySelectOracle::__construct() Constructs a new ezcQueryOracle object.
ezcQuerySelectSqlite::__construct() Constructs a new ezcQuerySelectSqlite object.
ezcQueryInsert::__construct() Constructs a new ezcQueryInsert that works on the database $db and with the aliases $aliases.

arrayFlatten

array arrayFlatten( $array )
Returns all the elements in $array as one large single dimensional array.

Parameters

Name Type Description
$array array  

bindParam

string bindParam( &$param, [string $placeHolder = null], &mixed $param )
Binds the parameter $param to the specified variable name $placeHolder..
This method provides a shortcut for PDOStatement::bindParam when using prepared statements.
The parameter $param specifies the variable that you want to bind. If $placeholder is not provided bind() will automatically create a placeholder for you. An automatic placeholder will be of the name 'ezcValue1', 'ezcValue2' etc.

Parameters

Name Type Description
$param &mixed  
$placeHolder string the name to bind with. The string must start with a colon ':'.
&$param  

See also:

doBind() Example: <code> $value = 2; $q->eq( 'id', $q->bindParam( $value ) ); $stmt = $q->prepare(); // the parameter $value is bound to the query. $value = 4; $stmt->execute(); // executed with 'id = 4' </code>, http://no.php.net/manual/en/function.pdostatement-bindparam.php.

Redefined in descendants as

Method Description
ezcQuerySubSelect::bindParam() Binds the parameter $param to the specified variable name $placeHolder..

bindValue

string bindValue( mixed $value, [string $placeHolder = null] )
Binds the value $value to the specified variable name $placeHolder.
This method provides a shortcut for PDOStatement::bindValue when using prepared statements.
The parameter $value specifies the value that you want to bind. If $placeholder is not provided bindValue() will automatically create a placeholder for you. An automatic placeholder will be of the name 'ezcValue1', 'ezcValue2' etc.

Parameters

Name Type Description
$value mixed  
$placeHolder string the name to bind with. The string must start with a colon ':'.

See also:

doBind() Example: <code> $value = 2; $q->eq( 'id', $q->bindValue( $value ) ); $stmt = $q->prepare(); // the value 2 is bound to the query. $value = 4; $stmt->execute(); // executed with 'id = 2' </code>, http://no.php.net/manual/en/function.pdostatement-bindparam.php.

Redefined in descendants as

Method Description
ezcQuerySubSelect::bindValue() Binds the value $value to the specified variable name $placeHolder.

doBind

void doBind( $stmt )
Performs binding of variables bound with bindValue and bindParam on the statement $stmt.
This method must be called if you have used the bind methods in your query and you build the method yourself using build.

getIdentifier

string getIdentifier( $alias )
Returns the correct identifier for the alias $alias.
If the alias does not exists in the list of aliases it is returned unchanged.

Parameters

Name Type Description
$alias  

getIdentifiers

array(string) getIdentifiers( $aliasList, array(string) $alias )
Returns the correct identifiers for the aliases found in $aliases.
This method is similar to getIdentifier except that it works on an array.

Parameters

Name Type Description
$alias array(string)  
$aliasList array  

getQuery

string getQuery( )
Returns the query string for this query object.

Throws

ClassDescription
ezcQueryInvalidException if it was not possible to build a valid query.

Redefined in descendants as

Method Description
ezcQueryDelete::getQuery() Returns the query string for this query object.
ezcQueryUpdate::getQuery() Returns the query string for this query object.
ezcQuerySelect::getQuery() Returns the complete select query string.
ezcQuerySubSelect::getQuery() Return string with SQL query for subselect.
ezcQuerySelectOracle::getQuery() Transforms the query from the parent to provide LIMIT functionality.
ezcQueryInsert::getQuery() Returns the query string for this query object.

hasAliases

bool hasAliases( )
Returns true if this object has aliases.

prepare

PDOStatement prepare( )
Returns a prepared statement from this query which can be used for execution.
prepare() automatically calls doBind() on the statement.

Redefined in descendants as

Method Description
ezcQuerySelectOracle::prepare() Handles preparing query.

resetBinds

void resetBinds( )
Resets the bound values and parameters to empty.
This is useful if your query can be reset and used multiple times.

setAliases

void setAliases( $aliases )
Sets the aliases $aliases for this object.
The aliases should be in the form array( "aliasName" => "databaseName" ). Table and column aliases can be mixed in the array.
The aliases can be used to substitute the column and table names with more friendly names. The substitution is done when the query is built, not using AS statements in the database itself.
Example of a select query with aliases:
1.  $q->setAliasesarray'Identifier' => 'id''Company' => 'company' ) );
2.  $this->q->select'Company' )->from'table' )->where$q->expr->eq'Identifier') );
3.  echo $q->getQuery();
This example will output SQL similar to:
1.  SELECT company FROM table WHERE id 5

Parameters

Name Type Description
$aliases array(string=>string)  

Last updated: Thu, 01 Nov 2007