Class TDbCommandBuilder

Description

TDbCommandBuilder provides basic methods to create query commands for tables giving by setTableInfo the property.

  • author: Wei Zhuo <weizho[at]gmail[dot]com>
  • version: $Id: TDbCommandBuilder.php 2996 2011-06-20 15:24:57Z ctrlaltca@gmail.com $
  • since: 3.1

Located in /Data/Common/TDbCommandBuilder.php (line 22)

TComponent
   |
   --TDbCommandBuilder
Direct descendents
Class Description
TMssqlCommandBuilder TMssqlCommandBuilder provides specifics methods to create limit/offset query commands for MSSQL servers.
TMysqlCommandBuilder TMysqlCommandBuilder implements default TDbCommandBuilder
TOracleCommandBuilder TOracleCommandBuilder provides specifics methods to create limit/offset query commands for Oracle database.
TPgsqlCommandBuilder TPgsqlCommandBuilder provides specifics methods to create limit/offset query commands for Pgsql database.
TSqliteCommandBuilder TSqliteCommandBuilder provides specifics methods to create limit/offset query commands for Sqlite database.
Method Summary
static integer getPdoType (mixed $value)
TDbCommandBuilder __construct ([TDbConnection $connection = null], [TDbTableInfo $tableInfo = null])
void applyCriterias ( $sql, [ $parameters = array()], [ $ordering = array()], [ $limit = -1], [ $offset = -1])
string applyLimitOffset (string $sql, [integer $limit = -1], [integer $offset = -1])
string applyOrdering (string $sql, array $ordering)
void bindArrayValues (TDbCommand $command, array $values)
void bindColumnValues (TDbCommand $command, array $values)
TDbCommand createCommand (string $sql)
TDbCommand createCountCommand ([string $where = '1=1'], [array $parameters = array()], [ $ordering = array()], [ $limit = -1], [ $offset = -1])
TDbCommand createDeleteCommand (string $where, [array $parameters = array()])
TDbCommand createFindCommand ([string $where = '1=1'], [array $parameters = array()], [ $ordering = array()], [ $limit = -1], [ $offset = -1], [ $select = '*'])
TDbCommand createUpdateCommand (array $data, string $where, [array $parameters = array()])
string getColumnBindings (array $values, [boolean $position = false])
array getInsertFieldBindings (object array $values)
mixed getLastInsertID ()
string getSearchCondition (string $column, array $words)
string getSearchExpression (array $fields, string $keywords)
array getSelectFieldList ([ $data = '*'], mixed $value)
boolean hasIntegerKey (array $array)
void setTableInfo (TDbTableInfo $value)
Methods
static method getPdoType (line 483)
  • return: PDO parameter types.
  • access: public
static integer getPdoType (mixed $value)
  • mixed $value: PHP value
Constructor __construct (line 31)
  • access: public
TDbCommandBuilder __construct ([TDbConnection $connection = null], [TDbTableInfo $tableInfo = null])
applyCriterias (line 317)
  • access: public
void applyCriterias ( $sql, [ $parameters = array()], [ $ordering = array()], [ $limit = -1], [ $offset = -1])
  • $sql
  • $parameters
  • $ordering
  • $limit
  • $offset
applyLimitOffset (line 91)

Alters the sql to apply $limit and $offset. Default implementation is applicable for PostgreSQL, MySQL and SQLite.

  • return: SQL with limit and offset.
  • access: public
string applyLimitOffset (string $sql, [integer $limit = -1], [integer $offset = -1])
  • string $sql: SQL query string.
  • integer $limit: maximum number of rows, -1 to ignore limit.
  • integer $offset: row offset, -1 to ignore offset.

Redefined in descendants as:
applyOrdering (line 105)
  • return: modified SQL applied with ORDER BY.
  • access: public
string applyOrdering (string $sql, array $ordering)
  • string $sql: SQL string without existing ordering.
  • array $ordering: pairs of column names as key and direction as value.
bindArrayValues (line 461)
  • access: public
void bindArrayValues (TDbCommand $command, array $values)
  • TDbCommand $command: database command
  • array $values: values for binding.
bindColumnValues (line 445)

Bind the name-value pairs of $values where the array keys correspond to column names.

  • access: public
void bindColumnValues (TDbCommand $command, array $values)
  • TDbCommand $command: database command.
  • array $values: name-value pairs.
createCommand (line 434)
  • return: corresponding database command.
  • access: public
TDbCommand createCommand (string $sql)
  • string $sql: SQL query string.
createCountCommand (line 334)

Creates a count(*) command for the table described in setTableInfo.

  • return: count command.
  • access: public
TDbCommand createCountCommand ([string $where = '1=1'], [array $parameters = array()], [ $ordering = array()], [ $limit = -1], [ $offset = -1])
  • string $where: count condition.
  • array $parameters: binding parameters.
  • $ordering
  • $limit
  • $offset
createDeleteCommand (line 347)

Creates a delete command for the table described in setTableInfo.

The conditions for delete is given by the $where argument and the parameters for the condition is given by $parameters.

  • return: delete command.
  • access: public
TDbCommand createDeleteCommand (string $where, [array $parameters = array()])
  • string $where: delete condition.
  • array $parameters: delete parameters.
createFindCommand (line 307)

Appends the $where condition to the string "SELECT * FROM tableName WHERE ".

The tableName is obtained from the TableInfo property.

  • return: query command.
  • access: public
TDbCommand createFindCommand ([string $where = '1=1'], [array $parameters = array()], [ $ordering = array()], [ $limit = -1], [ $offset = -1], [ $select = '*'])
  • string $where: query condition
  • array $parameters: condition parameters.
  • $ordering
  • $limit
  • $offset
  • $select
createInsertCommand (line 365)

Creates an insert command for the table described in setTableInfo for the given data.

Each array key in the $data array must correspond to the column name of the table (if a column allows to be null, it may be omitted) to be inserted with the corresponding array value.

  • return: insert command
  • access: public
TDbCommand createInsertCommand (array $data)
  • array $data: name-value pairs of new data to be inserted.
createUpdateCommand (line 382)

Creates an update command for the table described in setTableInfo for the given data.

Each array key in the $data array must correspond to the column name to be updated with the corresponding array value.

  • return: update command.
  • access: public
TDbCommand createUpdateCommand (array $data, string $where, [array $parameters = array()])
  • array $data: name-value pairs of data to be updated.
  • string $where: update condition.
  • array $parameters: update parameters.
getColumnBindings (line 419)

Create a name-value or position-value if $position=true binding strings.

  • return: update column names with corresponding binding substrings.
  • access: protected
string getColumnBindings (array $values, [boolean $position = false])
  • array $values: data for binding.
  • boolean $position: true to bind as position values.
getDbConnection (line 40)
  • return: database connection.
  • access: public
TDbConnection getDbConnection ()
getInsertFieldBindings (line 402)

Returns a list of insert field name and a list of binding names.

  • return: tuple ($fields, $bindings)
  • access: protected
array getInsertFieldBindings (object array $values)
  • object array $values: or object to be inserted.
getLastInsertID (line 74)

Iterate through all the columns and returns the last insert id of the first column that has a sequence or serial.

  • return: last insert id, null if none is found.
  • access: public
mixed getLastInsertID ()

Redefined in descendants as:
getSearchCondition (line 151)
  • return: search condition for all words in one column.
  • access: protected
string getSearchCondition (string $column, array $words)
  • string $column: column name.
  • array $words: keywords

Redefined in descendants as:
getSearchExpression (line 133)

Computes the SQL condition for search a set of column using regular expression (or LIKE, depending on database implementation) to match a string of keywords (default matches all keywords).

  • return: SQL search condition matching on a set of columns.
  • access: public
string getSearchExpression (array $fields, string $keywords)
  • array $fields: list of column id for potential search condition.
  • string $keywords: string of keywords

Redefined in descendants as:
getSelectFieldList (line 198)

Different behavior depends on type of passed data string usage without modification

null will be expanded to full list of quoted table column names (quoting depends on database)

array

  • Column names will be quoted if used as key or value of array
    1.      array('col1''col2''col2')
    2.      // SELECT `col1`, `col2`, `col3` FROM...
  • Column aliasing
    1.  array('mycol1' => 'col1''mycol2' => 'COUNT(*)')
    2.  // SELECT `col1` AS mycol1, COUNT(*) AS mycol2 FROM...
  • NULL and scalar values (strings will be quoted depending on database)
    1.  array('col1' => 'my custom string''col2' => 1.0'col3' => 'NULL')
    2.  // SELECT "my custom string" AS `col1`, 1.0 AS `col2`, NULL AS `col3` FROM...
  • If the *-wildcard char is used as key or value, add the full list of quoted table column names
    1.  array('col1' => 'NULL''*')
    2.  // SELECT `col1`, `col2`, `col3`, NULL AS `col1` FROM...

  • return: of generated fields - use implode(', ', $selectfieldlist) to collapse field list for usage
  • todo: add support for table aliasing
  • todo: add support for quoting of column aliasing
  • since: 3.1.7
  • access: public
array getSelectFieldList ([ $data = '*'], mixed $value)
  • mixed $value
  • $data
getTableInfo (line 64)
  • access: public
void getTableInfo (TDbTableInfo 0)
hasIntegerKey (line 498)
  • return: true if any array key is an integer.
  • access: protected
boolean hasIntegerKey (array $array)
  • array $array
setDbConnection (line 48)
  • access: public
void setDbConnection (TDbConnection $value)
setTableInfo (line 56)
  • access: public
void setTableInfo (TDbTableInfo $value)

Inherited Methods

Inherited From TComponent

TComponent::addParsedObject()
TComponent::attachEventHandler()
TComponent::canGetProperty()
TComponent::canSetProperty()
TComponent::createdOnTemplate()
TComponent::detachEventHandler()
TComponent::evaluateExpression()
TComponent::evaluateStatements()
TComponent::getEventHandlers()
TComponent::getSubProperty()
TComponent::hasEvent()
TComponent::hasEventHandler()
TComponent::hasProperty()
TComponent::raiseEvent()
TComponent::setSubProperty()
TComponent::__call()
TComponent::__get()
TComponent::__set()
TComponent::__sleep()
TComponent::__wakeup()

Documentation generated on Mon, 25 Jun 2012 14:37:59 +0200 by phpDocumentor 1.4.3