summaryrefslogtreecommitdiff
path: root/framework/Data/DataGateway
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data/DataGateway')
-rw-r--r--framework/Data/DataGateway/TDataGatewayCommand.php1082
-rw-r--r--framework/Data/DataGateway/TSqlCriteria.php566
-rw-r--r--framework/Data/DataGateway/TTableGateway.php948
3 files changed, 1298 insertions, 1298 deletions
diff --git a/framework/Data/DataGateway/TDataGatewayCommand.php b/framework/Data/DataGateway/TDataGatewayCommand.php
index fa699979..299d39cd 100644
--- a/framework/Data/DataGateway/TDataGatewayCommand.php
+++ b/framework/Data/DataGateway/TDataGatewayCommand.php
@@ -1,541 +1,541 @@
-<?php
-/**
- * TDataGatewayCommand, TDataGatewayEventParameter and TDataGatewayResultEventParameter class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.DataGateway
- */
-
-/**
- * TDataGatewayCommand is command builder and executor class for
- * TTableGateway and TActiveRecordGateway.
- *
- * TDataGatewayCommand builds the TDbCommand for TTableGateway
- * and TActiveRecordGateway commands such as find(), update(), insert(), etc,
- * using the TDbCommandBuilder classes (database specific TDbCommandBuilder
- * classes are used).
- *
- * Once the command is built and the query parameters are binded, the
- * {@link OnCreateCommand} event is raised. Event handlers for the OnCreateCommand
- * event should not alter the Command property nor the Criteria property of the
- * TDataGatewayEventParameter.
- *
- * TDataGatewayCommand excutes the TDbCommands and returns the result obtained from the
- * database (returned value depends on the method executed). The
- * {@link OnExecuteCommand} event is raised after the command is executed and resulting
- * data is set in the TDataGatewayResultEventParameter object's Result property.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.DataGateway
- * @since 3.1
- */
-class TDataGatewayCommand extends TComponent
-{
- private $_builder;
-
- /**
- * @param TDbCommandBuilder database specific database command builder.
- */
- public function __construct($builder)
- {
- $this->_builder = $builder;
- }
-
- /**
- * @return TDbTableInfo
- */
- public function getTableInfo()
- {
- return $this->_builder->getTableInfo();
- }
-
- /**
- * @return TDbConnection
- */
- public function getDbConnection()
- {
- return $this->_builder->getDbConnection();
- }
-
- /**
- * @return TDbCommandBuilder
- */
- public function getBuilder()
- {
- return $this->_builder;
- }
-
- /**
- * Executes a delete command.
- * @param TSqlCriteria delete conditions and parameters.
- * @return integer number of records affected.
- */
- public function delete($criteria)
- {
- $where = $criteria->getCondition();
- $parameters = $criteria->getParameters()->toArray();
- $command = $this->getBuilder()->createDeleteCommand($where, $parameters);
- $this->onCreateCommand($command,$criteria);
- $command->prepare();
- return $command->execute();
- }
-
- /**
- * Updates the table with new data.
- * @param array date for update.
- * @param TSqlCriteria update conditions and parameters.
- * @return integer number of records affected.
- */
- public function update($data, $criteria)
- {
- $where = $criteria->getCondition();
- $parameters = $criteria->getParameters()->toArray();
- $command = $this->getBuilder()->createUpdateCommand($data,$where, $parameters);
- $this->onCreateCommand($command,$criteria);
- $command->prepare();
- return $this->onExecuteCommand($command, $command->execute());
- }
-
- /**
- * @param array update for update
- * @param array primary key-value name pairs.
- * @return integer number of records affected.
- */
- public function updateByPk($data, $keys)
- {
- list($where, $parameters) = $this->getPrimaryKeyCondition((array)$keys);
- return $this->update($data, new TSqlCriteria($where, $parameters));
- }
-
- /**
- * Find one record matching the critera.
- * @param TSqlCriteria find conditions and parameters.
- * @return array matching record.
- */
- public function find($criteria)
- {
- $command = $this->getFindCommand($criteria);
- return $this->onExecuteCommand($command, $command->queryRow());
- }
-
- /**
- * Find one or more matching records.
- * @param TSqlCriteria $criteria
- * @return TDbDataReader record reader.
- */
- public function findAll($criteria)
- {
- $command = $this->getFindCommand($criteria);
- return $this->onExecuteCommand($command, $command->query());
- }
-
- /**
- * Build the find command from the criteria. Limit, Offset and Ordering are applied if applicable.
- * @param TSqlCriteria $criteria
- * @return TDbCommand.
- */
- protected function getFindCommand($criteria)
- {
- if($criteria===null)
- return $this->getBuilder()->createFindCommand();
- $where = $criteria->getCondition();
- $parameters = $criteria->getParameters()->toArray();
- $ordering = $criteria->getOrdersBy();
- $limit = $criteria->getLimit();
- $offset = $criteria->getOffset();
- $select = $criteria->getSelect();
- $command = $this->getBuilder()->createFindCommand($where,$parameters,$ordering,$limit,$offset,$select);
- $this->onCreateCommand($command, $criteria);
- return $command;
- }
-
- /**
- * @param mixed primary key value, or composite key values as array.
- * @return array matching record.
- */
- public function findByPk($keys)
- {
- list($where, $parameters) = $this->getPrimaryKeyCondition((array)$keys);
- $command = $this->getBuilder()->createFindCommand($where, $parameters);
- $this->onCreateCommand($command, new TSqlCriteria($where,$parameters));
- return $this->onExecuteCommand($command, $command->queryRow());
- }
-
- /**
- * @param array multiple primary key values or composite value arrays
- * @return TDbDataReader record reader.
- */
- public function findAllByPk($keys)
- {
- $where = $this->getCompositeKeyCondition((array)$keys);
- $command = $this->getBuilder()->createFindCommand($where);
- $this->onCreateCommand($command, new TSqlCriteria($where,$keys));
- return $this->onExecuteCommand($command,$command->query());
- }
-
- public function findAllByIndex($criteria,$fields,$values)
- {
- $index = $this->getIndexKeyCondition($this->getTableInfo(),$fields,$values);
- if(strlen($where = $criteria->getCondition())>0)
- $criteria->setCondition("({$index}) AND ({$where})");
- else
- $criteria->setCondition($index);
- $command = $this->getFindCommand($criteria);
- $this->onCreateCommand($command, $criteria);
- return $this->onExecuteCommand($command,$command->query());
- }
-
- /**
- * @param array multiple primary key values or composite value arrays
- * @return integer number of rows affected.
- */
- public function deleteByPk($keys)
- {
- $where = $this->getCompositeKeyCondition((array)$keys);
- $command = $this->getBuilder()->createDeleteCommand($where);
- $this->onCreateCommand($command, new TSqlCriteria($where,$keys));
- $command->prepare();
- return $this->onExecuteCommand($command,$command->execute());
- }
-
- public function getIndexKeyCondition($table,$fields,$values)
- {
- if (!count($values))
- return 'FALSE';
- $columns = array();
- $tableName = $table->getTableFullName();
- foreach($fields as $field)
- $columns[] = $tableName.'.'.$table->getColumn($field)->getColumnName();
- return '('.implode(', ',$columns).') IN '.$this->quoteTuple($values);
- }
-
- /**
- * Construct a "pk IN ('key1', 'key2', ...)" criteria.
- * @param array values for IN predicate
- * @param string SQL string for primary keys IN a list.
- */
- protected function getCompositeKeyCondition($values)
- {
- $primary = $this->getTableInfo()->getPrimaryKeys();
- $count = count($primary);
- if($count===0)
- {
- throw new TDbException('dbtablegateway_no_primary_key_found',
- $this->getTableInfo()->getTableFullName());
- }
- if(!is_array($values) || count($values) === 0)
- {
- throw new TDbException('dbtablegateway_missing_pk_values',
- $this->getTableInfo()->getTableFullName());
- }
- if($count>1 && (!isset($values[0]) || !is_array($values[0])))
- $values = array($values);
- if($count > 1 && count($values[0]) !== $count)
- {
- throw new TDbException('dbtablegateway_pk_value_count_mismatch',
- $this->getTableInfo()->getTableFullName());
- }
- return $this->getIndexKeyCondition($this->getTableInfo(),$primary, $values);
- }
-
- /**
- * @param TDbConnection database connection.
- * @param array values
- * @return string quoted recursive tuple values, e.g. "('val1', 'val2')".
- */
- protected function quoteTuple($array)
- {
- $conn = $this->getDbConnection();
- $data = array();
- foreach($array as $k=>$v)
- $data[] = is_array($v) ? $this->quoteTuple($v) : $conn->quoteString($v);
- return '('.implode(', ', $data).')';
- }
-
- /**
- * Create the condition and parameters for find by primary.
- * @param array primary key values
- * @return array tuple($where, $parameters)
- */
- protected function getPrimaryKeyCondition($values)
- {
- $primary = $this->getTableInfo()->getPrimaryKeys();
- if(count($primary)===0)
- {
- throw new TDbException('dbtablegateway_no_primary_key_found',
- $this->getTableInfo()->getTableFullName());
- }
- $criteria=array();
- $bindings=array();
- $i = 0;
- foreach($primary as $key)
- {
- $column = $this->getTableInfo()->getColumn($key)->getColumnName();
- $criteria[] = $column.' = :'.$key;
- $bindings[$key] = isset($values[$key])?$values[$key]:$values[$i++];
- }
- return array(implode(' AND ', $criteria), $bindings);
- }
-
- /**
- * Find one matching records for arbituary SQL.
- * @param TSqlCriteria $criteria
- * @return TDbDataReader record reader.
- */
- public function findBySql($criteria)
- {
- $command = $this->getSqlCommand($criteria);
- return $this->onExecuteCommand($command, $command->queryRow());
- }
-
- /**
- * Find zero or more matching records for arbituary SQL.
- * @param TSqlCriteria $criteria
- * @return TDbDataReader record reader.
- */
- public function findAllBySql($criteria)
- {
- $command = $this->getSqlCommand($criteria);
- return $this->onExecuteCommand($command, $command->query());
- }
-
- /**
- * Build sql command from the criteria. Limit, Offset and Ordering are applied if applicable.
- * @param TSqlCriteria $criteria
- * @return TDbCommand command corresponding to the criteria.
- */
- protected function getSqlCommand($criteria)
- {
- $sql = $criteria->getCondition();
- $ordering = $criteria->getOrdersBy();
- $limit = $criteria->getLimit();
- $offset = $criteria->getOffset();
- if(count($ordering) > 0)
- $sql = $this->getBuilder()->applyOrdering($sql, $ordering);
- if($limit>=0 || $offset>=0)
- $sql = $this->getBuilder()->applyLimitOffset($sql, $limit, $offset);
- $command = $this->getBuilder()->createCommand($sql);
- $this->getBuilder()->bindArrayValues($command, $criteria->getParameters()->toArray());
- $this->onCreateCommand($command, $criteria);
- return $command;
- }
-
- /**
- * @param TSqlCriteria $criteria
- * @return integer number of records.
- */
- public function count($criteria)
- {
- if($criteria===null)
- return (int)$this->getBuilder()->createCountCommand()->queryScalar();
- $where = $criteria->getCondition();
- $parameters = $criteria->getParameters()->toArray();
- $ordering = $criteria->getOrdersBy();
- $limit = $criteria->getLimit();
- $offset = $criteria->getOffset();
- $command = $this->getBuilder()->createCountCommand($where,$parameters,$ordering,$limit,$offset);
- $this->onCreateCommand($command, $criteria);
- return $this->onExecuteCommand($command, (int)$command->queryScalar());
- }
-
- /**
- * Inserts a new record into the table. Each array key must
- * correspond to a column name in the table unless a null value is permitted.
- * @param array new record data.
- * @return mixed last insert id if one column contains a serial or sequence,
- * otherwise true if command executes successfully and affected 1 or more rows.
- */
- public function insert($data)
- {
- $command=$this->getBuilder()->createInsertCommand($data);
- $this->onCreateCommand($command, new TSqlCriteria(null,$data));
- $command->prepare();
- if($this->onExecuteCommand($command, $command->execute()) > 0)
- {
- $value = $this->getLastInsertId();
- return $value !== null ? $value : true;
- }
- return false;
- }
-
- /**
- * Iterate through all the columns and returns the last insert id of the
- * first column that has a sequence or serial.
- * @return mixed last insert id, null if none is found.
- */
- public function getLastInsertID()
- {
- return $this->getBuilder()->getLastInsertID();
- }
-
- /**
- * @param string __call method name
- * @param string criteria conditions
- * @param array method arguments
- * @return TActiveRecordCriteria criteria created from the method name and its arguments.
- */
- public function createCriteriaFromString($method, $condition, $args)
- {
- $fields = $this->extractMatchingConditions($method, $condition);
- $args=count($args) === 1 && is_array($args[0]) ? $args[0] : $args;
- if(count($fields)>count($args))
- {
- throw new TDbException('dbtablegateway_mismatch_args_exception',
- $method,count($fields),count($args));
- }
- return new TSqlCriteria(implode(' ',$fields), $args);
- }
-
- /**
- * Calculates the AND/OR condition from dynamic method substrings using
- * table meta data, allows for any AND-OR combinations.
- * @param string dynamic method name
- * @param string dynamic method search criteria
- * @return array search condition substrings
- */
- protected function extractMatchingConditions($method, $condition)
- {
- $table = $this->getTableInfo();
- $columns = $table->getLowerCaseColumnNames();
- $regexp = '/('.implode('|', array_keys($columns)).')(and|_and_|or|_or_)?/i';
- $matches = array();
- if(!preg_match_all($regexp, strtolower($condition), $matches,PREG_SET_ORDER))
- {
- throw new TDbException('dbtablegateway_mismatch_column_name',
- $method, implode(', ', $columns), $table->getTableFullName());
- }
-
- $fields = array();
- foreach($matches as $match)
- {
- $key = $columns[$match[1]];
- $column = $table->getColumn($key)->getColumnName();
- $sql = $column . ' = ? ';
- if(count($match) > 2)
- $sql .= strtoupper(str_replace('_', '', $match[2]));
- $fields[] = $sql;
- }
- return $fields;
- }
-
- /**
- * Raised when a command is prepared and parameter binding is completed.
- * The parameter object is TDataGatewayEventParameter of which the
- * {@link TDataGatewayEventParameter::getCommand Command} property can be
- * inspected to obtain the sql query to be executed.
- * @param TDataGatewayCommand originator $sender
- * @param TDataGatewayEventParameter
- */
- public function onCreateCommand($command, $criteria)
- {
- $this->raiseEvent('OnCreateCommand', $this, new TDataGatewayEventParameter($command,$criteria));
- }
-
- /**
- * Raised when a command is executed and the result from the database was returned.
- * The parameter object is TDataGatewayResultEventParameter of which the
- * {@link TDataGatewayEventParameter::getResult Result} property contains
- * the data return from the database. The data returned can be changed
- * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
- * @param TDataGatewayCommand originator $sender
- * @param TDataGatewayResultEventParameter
- */
- public function onExecuteCommand($command, $result)
- {
- $parameter = new TDataGatewayResultEventParameter($command, $result);
- $this->raiseEvent('OnExecuteCommand', $this, $parameter);
- return $parameter->getResult();
- }
-}
-
-/**
- * TDataGatewayEventParameter class contains the TDbCommand to be executed as
- * well as the criteria object.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.DataGateway
- * @since 3.1
- */
-class TDataGatewayEventParameter extends TEventParameter
-{
- private $_command;
- private $_criteria;
-
- public function __construct($command,$criteria)
- {
- $this->_command=$command;
- $this->_criteria=$criteria;
- }
-
- /**
- * The database command to be executed. Do not rebind the parameters or change
- * the sql query string.
- * @return TDbCommand command to be executed.
- */
- public function getCommand()
- {
- return $this->_command;
- }
-
- /**
- * @return TSqlCriteria criteria used to bind the sql query parameters.
- */
- public function getCriteria()
- {
- return $this->_criteria;
- }
-}
-
-/**
- * TDataGatewayResultEventParameter contains the TDbCommand executed and the resulting
- * data returned from the database. The data can be changed by changing the
- * {@link setResult Result} property.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.DataGateway
- * @since 3.1
- */
-class TDataGatewayResultEventParameter extends TEventParameter
-{
- private $_command;
- private $_result;
-
- public function __construct($command,$result)
- {
- $this->_command=$command;
- $this->_result=$result;
- }
-
- /**
- * @return TDbCommand database command executed.
- */
- public function getCommand()
- {
- return $this->_command;
- }
-
- /**
- * @return mixed result returned from executing the command.
- */
- public function getResult()
- {
- return $this->_result;
- }
-
- /**
- * @param mixed change the result returned by the gateway.
- */
- public function setResult($value)
- {
- $this->_result=$value;
- }
-}
-
-?>
+<?php
+/**
+ * TDataGatewayCommand, TDataGatewayEventParameter and TDataGatewayResultEventParameter class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005-2012 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Data.DataGateway
+ */
+
+/**
+ * TDataGatewayCommand is command builder and executor class for
+ * TTableGateway and TActiveRecordGateway.
+ *
+ * TDataGatewayCommand builds the TDbCommand for TTableGateway
+ * and TActiveRecordGateway commands such as find(), update(), insert(), etc,
+ * using the TDbCommandBuilder classes (database specific TDbCommandBuilder
+ * classes are used).
+ *
+ * Once the command is built and the query parameters are binded, the
+ * {@link OnCreateCommand} event is raised. Event handlers for the OnCreateCommand
+ * event should not alter the Command property nor the Criteria property of the
+ * TDataGatewayEventParameter.
+ *
+ * TDataGatewayCommand excutes the TDbCommands and returns the result obtained from the
+ * database (returned value depends on the method executed). The
+ * {@link OnExecuteCommand} event is raised after the command is executed and resulting
+ * data is set in the TDataGatewayResultEventParameter object's Result property.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.DataGateway
+ * @since 3.1
+ */
+class TDataGatewayCommand extends TComponent
+{
+ private $_builder;
+
+ /**
+ * @param TDbCommandBuilder database specific database command builder.
+ */
+ public function __construct($builder)
+ {
+ $this->_builder = $builder;
+ }
+
+ /**
+ * @return TDbTableInfo
+ */
+ public function getTableInfo()
+ {
+ return $this->_builder->getTableInfo();
+ }
+
+ /**
+ * @return TDbConnection
+ */
+ public function getDbConnection()
+ {
+ return $this->_builder->getDbConnection();
+ }
+
+ /**
+ * @return TDbCommandBuilder
+ */
+ public function getBuilder()
+ {
+ return $this->_builder;
+ }
+
+ /**
+ * Executes a delete command.
+ * @param TSqlCriteria delete conditions and parameters.
+ * @return integer number of records affected.
+ */
+ public function delete($criteria)
+ {
+ $where = $criteria->getCondition();
+ $parameters = $criteria->getParameters()->toArray();
+ $command = $this->getBuilder()->createDeleteCommand($where, $parameters);
+ $this->onCreateCommand($command,$criteria);
+ $command->prepare();
+ return $command->execute();
+ }
+
+ /**
+ * Updates the table with new data.
+ * @param array date for update.
+ * @param TSqlCriteria update conditions and parameters.
+ * @return integer number of records affected.
+ */
+ public function update($data, $criteria)
+ {
+ $where = $criteria->getCondition();
+ $parameters = $criteria->getParameters()->toArray();
+ $command = $this->getBuilder()->createUpdateCommand($data,$where, $parameters);
+ $this->onCreateCommand($command,$criteria);
+ $command->prepare();
+ return $this->onExecuteCommand($command, $command->execute());
+ }
+
+ /**
+ * @param array update for update
+ * @param array primary key-value name pairs.
+ * @return integer number of records affected.
+ */
+ public function updateByPk($data, $keys)
+ {
+ list($where, $parameters) = $this->getPrimaryKeyCondition((array)$keys);
+ return $this->update($data, new TSqlCriteria($where, $parameters));
+ }
+
+ /**
+ * Find one record matching the critera.
+ * @param TSqlCriteria find conditions and parameters.
+ * @return array matching record.
+ */
+ public function find($criteria)
+ {
+ $command = $this->getFindCommand($criteria);
+ return $this->onExecuteCommand($command, $command->queryRow());
+ }
+
+ /**
+ * Find one or more matching records.
+ * @param TSqlCriteria $criteria
+ * @return TDbDataReader record reader.
+ */
+ public function findAll($criteria)
+ {
+ $command = $this->getFindCommand($criteria);
+ return $this->onExecuteCommand($command, $command->query());
+ }
+
+ /**
+ * Build the find command from the criteria. Limit, Offset and Ordering are applied if applicable.
+ * @param TSqlCriteria $criteria
+ * @return TDbCommand.
+ */
+ protected function getFindCommand($criteria)
+ {
+ if($criteria===null)
+ return $this->getBuilder()->createFindCommand();
+ $where = $criteria->getCondition();
+ $parameters = $criteria->getParameters()->toArray();
+ $ordering = $criteria->getOrdersBy();
+ $limit = $criteria->getLimit();
+ $offset = $criteria->getOffset();
+ $select = $criteria->getSelect();
+ $command = $this->getBuilder()->createFindCommand($where,$parameters,$ordering,$limit,$offset,$select);
+ $this->onCreateCommand($command, $criteria);
+ return $command;
+ }
+
+ /**
+ * @param mixed primary key value, or composite key values as array.
+ * @return array matching record.
+ */
+ public function findByPk($keys)
+ {
+ list($where, $parameters) = $this->getPrimaryKeyCondition((array)$keys);
+ $command = $this->getBuilder()->createFindCommand($where, $parameters);
+ $this->onCreateCommand($command, new TSqlCriteria($where,$parameters));
+ return $this->onExecuteCommand($command, $command->queryRow());
+ }
+
+ /**
+ * @param array multiple primary key values or composite value arrays
+ * @return TDbDataReader record reader.
+ */
+ public function findAllByPk($keys)
+ {
+ $where = $this->getCompositeKeyCondition((array)$keys);
+ $command = $this->getBuilder()->createFindCommand($where);
+ $this->onCreateCommand($command, new TSqlCriteria($where,$keys));
+ return $this->onExecuteCommand($command,$command->query());
+ }
+
+ public function findAllByIndex($criteria,$fields,$values)
+ {
+ $index = $this->getIndexKeyCondition($this->getTableInfo(),$fields,$values);
+ if(strlen($where = $criteria->getCondition())>0)
+ $criteria->setCondition("({$index}) AND ({$where})");
+ else
+ $criteria->setCondition($index);
+ $command = $this->getFindCommand($criteria);
+ $this->onCreateCommand($command, $criteria);
+ return $this->onExecuteCommand($command,$command->query());
+ }
+
+ /**
+ * @param array multiple primary key values or composite value arrays
+ * @return integer number of rows affected.
+ */
+ public function deleteByPk($keys)
+ {
+ $where = $this->getCompositeKeyCondition((array)$keys);
+ $command = $this->getBuilder()->createDeleteCommand($where);
+ $this->onCreateCommand($command, new TSqlCriteria($where,$keys));
+ $command->prepare();
+ return $this->onExecuteCommand($command,$command->execute());
+ }
+
+ public function getIndexKeyCondition($table,$fields,$values)
+ {
+ if (!count($values))
+ return 'FALSE';
+ $columns = array();
+ $tableName = $table->getTableFullName();
+ foreach($fields as $field)
+ $columns[] = $tableName.'.'.$table->getColumn($field)->getColumnName();
+ return '('.implode(', ',$columns).') IN '.$this->quoteTuple($values);
+ }
+
+ /**
+ * Construct a "pk IN ('key1', 'key2', ...)" criteria.
+ * @param array values for IN predicate
+ * @param string SQL string for primary keys IN a list.
+ */
+ protected function getCompositeKeyCondition($values)
+ {
+ $primary = $this->getTableInfo()->getPrimaryKeys();
+ $count = count($primary);
+ if($count===0)
+ {
+ throw new TDbException('dbtablegateway_no_primary_key_found',
+ $this->getTableInfo()->getTableFullName());
+ }
+ if(!is_array($values) || count($values) === 0)
+ {
+ throw new TDbException('dbtablegateway_missing_pk_values',
+ $this->getTableInfo()->getTableFullName());
+ }
+ if($count>1 && (!isset($values[0]) || !is_array($values[0])))
+ $values = array($values);
+ if($count > 1 && count($values[0]) !== $count)
+ {
+ throw new TDbException('dbtablegateway_pk_value_count_mismatch',
+ $this->getTableInfo()->getTableFullName());
+ }
+ return $this->getIndexKeyCondition($this->getTableInfo(),$primary, $values);
+ }
+
+ /**
+ * @param TDbConnection database connection.
+ * @param array values
+ * @return string quoted recursive tuple values, e.g. "('val1', 'val2')".
+ */
+ protected function quoteTuple($array)
+ {
+ $conn = $this->getDbConnection();
+ $data = array();
+ foreach($array as $k=>$v)
+ $data[] = is_array($v) ? $this->quoteTuple($v) : $conn->quoteString($v);
+ return '('.implode(', ', $data).')';
+ }
+
+ /**
+ * Create the condition and parameters for find by primary.
+ * @param array primary key values
+ * @return array tuple($where, $parameters)
+ */
+ protected function getPrimaryKeyCondition($values)
+ {
+ $primary = $this->getTableInfo()->getPrimaryKeys();
+ if(count($primary)===0)
+ {
+ throw new TDbException('dbtablegateway_no_primary_key_found',
+ $this->getTableInfo()->getTableFullName());
+ }
+ $criteria=array();
+ $bindings=array();
+ $i = 0;
+ foreach($primary as $key)
+ {
+ $column = $this->getTableInfo()->getColumn($key)->getColumnName();
+ $criteria[] = $column.' = :'.$key;
+ $bindings[$key] = isset($values[$key])?$values[$key]:$values[$i++];
+ }
+ return array(implode(' AND ', $criteria), $bindings);
+ }
+
+ /**
+ * Find one matching records for arbituary SQL.
+ * @param TSqlCriteria $criteria
+ * @return TDbDataReader record reader.
+ */
+ public function findBySql($criteria)
+ {
+ $command = $this->getSqlCommand($criteria);
+ return $this->onExecuteCommand($command, $command->queryRow());
+ }
+
+ /**
+ * Find zero or more matching records for arbituary SQL.
+ * @param TSqlCriteria $criteria
+ * @return TDbDataReader record reader.
+ */
+ public function findAllBySql($criteria)
+ {
+ $command = $this->getSqlCommand($criteria);
+ return $this->onExecuteCommand($command, $command->query());
+ }
+
+ /**
+ * Build sql command from the criteria. Limit, Offset and Ordering are applied if applicable.
+ * @param TSqlCriteria $criteria
+ * @return TDbCommand command corresponding to the criteria.
+ */
+ protected function getSqlCommand($criteria)
+ {
+ $sql = $criteria->getCondition();
+ $ordering = $criteria->getOrdersBy();
+ $limit = $criteria->getLimit();
+ $offset = $criteria->getOffset();
+ if(count($ordering) > 0)
+ $sql = $this->getBuilder()->applyOrdering($sql, $ordering);
+ if($limit>=0 || $offset>=0)
+ $sql = $this->getBuilder()->applyLimitOffset($sql, $limit, $offset);
+ $command = $this->getBuilder()->createCommand($sql);
+ $this->getBuilder()->bindArrayValues($command, $criteria->getParameters()->toArray());
+ $this->onCreateCommand($command, $criteria);
+ return $command;
+ }
+
+ /**
+ * @param TSqlCriteria $criteria
+ * @return integer number of records.
+ */
+ public function count($criteria)
+ {
+ if($criteria===null)
+ return (int)$this->getBuilder()->createCountCommand()->queryScalar();
+ $where = $criteria->getCondition();
+ $parameters = $criteria->getParameters()->toArray();
+ $ordering = $criteria->getOrdersBy();
+ $limit = $criteria->getLimit();
+ $offset = $criteria->getOffset();
+ $command = $this->getBuilder()->createCountCommand($where,$parameters,$ordering,$limit,$offset);
+ $this->onCreateCommand($command, $criteria);
+ return $this->onExecuteCommand($command, (int)$command->queryScalar());
+ }
+
+ /**
+ * Inserts a new record into the table. Each array key must
+ * correspond to a column name in the table unless a null value is permitted.
+ * @param array new record data.
+ * @return mixed last insert id if one column contains a serial or sequence,
+ * otherwise true if command executes successfully and affected 1 or more rows.
+ */
+ public function insert($data)
+ {
+ $command=$this->getBuilder()->createInsertCommand($data);
+ $this->onCreateCommand($command, new TSqlCriteria(null,$data));
+ $command->prepare();
+ if($this->onExecuteCommand($command, $command->execute()) > 0)
+ {
+ $value = $this->getLastInsertId();
+ return $value !== null ? $value : true;
+ }
+ return false;
+ }
+
+ /**
+ * Iterate through all the columns and returns the last insert id of the
+ * first column that has a sequence or serial.
+ * @return mixed last insert id, null if none is found.
+ */
+ public function getLastInsertID()
+ {
+ return $this->getBuilder()->getLastInsertID();
+ }
+
+ /**
+ * @param string __call method name
+ * @param string criteria conditions
+ * @param array method arguments
+ * @return TActiveRecordCriteria criteria created from the method name and its arguments.
+ */
+ public function createCriteriaFromString($method, $condition, $args)
+ {
+ $fields = $this->extractMatchingConditions($method, $condition);
+ $args=count($args) === 1 && is_array($args[0]) ? $args[0] : $args;
+ if(count($fields)>count($args))
+ {
+ throw new TDbException('dbtablegateway_mismatch_args_exception',
+ $method,count($fields),count($args));
+ }
+ return new TSqlCriteria(implode(' ',$fields), $args);
+ }
+
+ /**
+ * Calculates the AND/OR condition from dynamic method substrings using
+ * table meta data, allows for any AND-OR combinations.
+ * @param string dynamic method name
+ * @param string dynamic method search criteria
+ * @return array search condition substrings
+ */
+ protected function extractMatchingConditions($method, $condition)
+ {
+ $table = $this->getTableInfo();
+ $columns = $table->getLowerCaseColumnNames();
+ $regexp = '/('.implode('|', array_keys($columns)).')(and|_and_|or|_or_)?/i';
+ $matches = array();
+ if(!preg_match_all($regexp, strtolower($condition), $matches,PREG_SET_ORDER))
+ {
+ throw new TDbException('dbtablegateway_mismatch_column_name',
+ $method, implode(', ', $columns), $table->getTableFullName());
+ }
+
+ $fields = array();
+ foreach($matches as $match)
+ {
+ $key = $columns[$match[1]];
+ $column = $table->getColumn($key)->getColumnName();
+ $sql = $column . ' = ? ';
+ if(count($match) > 2)
+ $sql .= strtoupper(str_replace('_', '', $match[2]));
+ $fields[] = $sql;
+ }
+ return $fields;
+ }
+
+ /**
+ * Raised when a command is prepared and parameter binding is completed.
+ * The parameter object is TDataGatewayEventParameter of which the
+ * {@link TDataGatewayEventParameter::getCommand Command} property can be
+ * inspected to obtain the sql query to be executed.
+ * @param TDataGatewayCommand originator $sender
+ * @param TDataGatewayEventParameter
+ */
+ public function onCreateCommand($command, $criteria)
+ {
+ $this->raiseEvent('OnCreateCommand', $this, new TDataGatewayEventParameter($command,$criteria));
+ }
+
+ /**
+ * Raised when a command is executed and the result from the database was returned.
+ * The parameter object is TDataGatewayResultEventParameter of which the
+ * {@link TDataGatewayEventParameter::getResult Result} property contains
+ * the data return from the database. The data returned can be changed
+ * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
+ * @param TDataGatewayCommand originator $sender
+ * @param TDataGatewayResultEventParameter
+ */
+ public function onExecuteCommand($command, $result)
+ {
+ $parameter = new TDataGatewayResultEventParameter($command, $result);
+ $this->raiseEvent('OnExecuteCommand', $this, $parameter);
+ return $parameter->getResult();
+ }
+}
+
+/**
+ * TDataGatewayEventParameter class contains the TDbCommand to be executed as
+ * well as the criteria object.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.DataGateway
+ * @since 3.1
+ */
+class TDataGatewayEventParameter extends TEventParameter
+{
+ private $_command;
+ private $_criteria;
+
+ public function __construct($command,$criteria)
+ {
+ $this->_command=$command;
+ $this->_criteria=$criteria;
+ }
+
+ /**
+ * The database command to be executed. Do not rebind the parameters or change
+ * the sql query string.
+ * @return TDbCommand command to be executed.
+ */
+ public function getCommand()
+ {
+ return $this->_command;
+ }
+
+ /**
+ * @return TSqlCriteria criteria used to bind the sql query parameters.
+ */
+ public function getCriteria()
+ {
+ return $this->_criteria;
+ }
+}
+
+/**
+ * TDataGatewayResultEventParameter contains the TDbCommand executed and the resulting
+ * data returned from the database. The data can be changed by changing the
+ * {@link setResult Result} property.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.DataGateway
+ * @since 3.1
+ */
+class TDataGatewayResultEventParameter extends TEventParameter
+{
+ private $_command;
+ private $_result;
+
+ public function __construct($command,$result)
+ {
+ $this->_command=$command;
+ $this->_result=$result;
+ }
+
+ /**
+ * @return TDbCommand database command executed.
+ */
+ public function getCommand()
+ {
+ return $this->_command;
+ }
+
+ /**
+ * @return mixed result returned from executing the command.
+ */
+ public function getResult()
+ {
+ return $this->_result;
+ }
+
+ /**
+ * @param mixed change the result returned by the gateway.
+ */
+ public function setResult($value)
+ {
+ $this->_result=$value;
+ }
+}
+
+?>
diff --git a/framework/Data/DataGateway/TSqlCriteria.php b/framework/Data/DataGateway/TSqlCriteria.php
index a7da3adf..3a54f4c4 100644
--- a/framework/Data/DataGateway/TSqlCriteria.php
+++ b/framework/Data/DataGateway/TSqlCriteria.php
@@ -1,284 +1,284 @@
-<?php
-/**
- * TDbSqlCriteria class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id: TDbSqlCriteria.php 1835 2007-04-03 01:38:15Z wei $
- * @package System.Data.DataGateway
- */
-
-/**
- * Search criteria for TDbDataGateway.
- *
- * Criteria object for data gateway finder methods. Usage:
- * <code>
- * $criteria = new TSqlCriteria();
- * $criteria->Parameters[':name'] = 'admin';
- * $criteria->Parameters[':pass'] = 'prado';
- * $criteria->OrdersBy['level'] = 'desc';
- * $criteria->OrdersBy['name'] = 'asc';
- * $criteria->Limit = 10;
- * $criteria->Offset = 20;
- * </code>
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id: TDbSqlCriteria.php 1835 2007-04-03 01:38:15Z wei $
- * @package System.Data.DataGateway
- * @since 3.1
- */
-class TSqlCriteria extends TComponent
-{
- /**
- * @var mixed
- * @since 3.1.7
- */
- private $_select='*';
- private $_condition;
- private $_parameters;
- private $_ordersBy;
- private $_limit;
- private $_offset;
-
- /**
- * Creates a new criteria with given condition;
- * @param string sql string after the WHERE stanza
- * @param mixed named or indexed parameters, accepts as multiple arguments.
- */
- public function __construct($condition=null, $parameters=array())
- {
- if(!is_array($parameters) && func_num_args() > 1)
- $parameters = array_slice(func_get_args(),1);
- $this->_parameters=new TAttributeCollection;
- $this->_parameters->setCaseSensitive(true);
- $this->_parameters->copyFrom((array)$parameters);
- $this->_ordersBy=new TAttributeCollection;
- $this->_ordersBy->setCaseSensitive(true);
-
- $this->setCondition($condition);
- }
-
- /**
- * Gets the field list to be placed after the SELECT in the SQL. Default to '*'
- * @return mixed
- * @since 3.1.7
- */
- public function getSelect()
- {
- return $this->_select;
- }
-
- /**
- * Sets the field list to be placed after the SELECT in the SQL.
- *
- * Different behavior depends on type of assigned value
- * 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
- * <code>
- * array('col1', 'col2', 'col2')
- * // SELECT `col1`, `col2`, `col3` FROM...
- * </code>
- *
- * - Column aliasing
- * <code>
- * array('mycol1' => 'col1', 'mycol2' => 'COUNT(*)')
- * // SELECT `col1` AS mycol1, COUNT(*) AS mycol2 FROM...
- * </code>
- *
- * - NULL and scalar values (strings will be quoted depending on database)
- * <code>
- * array('col1' => 'my custom string', 'col2' => 1.0, 'col3' => 'NULL')
- * // SELECT "my custom string" AS `col1`, 1.0 AS `col2`, NULL AS `col3` FROM...
- * </code>
- *
- * - If the *-wildcard char is used as key or value, add the full list of quoted table column names
- * <code>
- * array('col1' => 'NULL', '*')
- * // SELECT `col1`, `col2`, `col3`, NULL AS `col1` FROM...
- * </code>
- *
- * @param mixed
- * @since 3.1.7
- * @see TDbCommandBuilder::getSelectFieldList()
- */
- public function setSelect($value)
- {
- $this->_select = $value;
- }
-
- /**
- * @return string search conditions.
- */
- public function getCondition()
- {
- return $this->_condition;
- }
-
- /**
- * Sets the search conditions to be placed after the WHERE clause in the SQL.
- * @param string search conditions.
- */
- public function setCondition($value)
- {
- if(empty($value)) {
- return;
- }
-
- // supporting the following SELECT-syntax:
- // [ORDER BY {col_name | expr | position}
- // [ASC | DESC], ...]
- // [LIMIT {[offset,] row_count | row_count OFFSET offset}]
- // See: http://dev.mysql.com/doc/refman/5.0/en/select.html
-
- if(preg_match('/ORDER\s+BY\s+(.*?)(?=LIMIT)|ORDER\s+BY\s+(.*?)$/i', $value, $matches) > 0) {
- // condition contains ORDER BY
- $value = str_replace($matches[0], '', $value);
- if(strlen($matches[1]) > 0) {
- $this->setOrdersBy($matches[1]);
- } else if(strlen($matches[2]) > 0) {
- $this->setOrdersBy($matches[2]);
- }
- }
-
- if(preg_match('/LIMIT\s+([\d\s,]+)/i', $value, $matches) > 0) {
- // condition contains limit
- $value = str_replace($matches[0], '', $value); // remove limit from query
- if(strpos($matches[1], ',')) { // both offset and limit given
- list($offset, $limit) = explode(',', $matches[1]);
- $this->_limit = (int)$limit;
- $this->_offset = (int)$offset;
- } else { // only limit given
- $this->_limit = (int)$matches[1];
- }
- }
-
- if(preg_match('/OFFSET\s+(\d+)/i', $value, $matches) > 0) {
- // condition contains offset
- $value = str_replace($matches[0], '', $value); // remove offset from query
- $this->_offset = (int)$matches[1]; // set offset in criteria
- }
-
- $this->_condition = trim($value);
- }
-
- /**
- * @return TAttributeCollection list of named parameters and values.
- */
- public function getParameters()
- {
- return $this->_parameters;
- }
-
- /**
- * @param ArrayAccess named parameters.
- */
- public function setParameters($value)
- {
- if(!(is_array($value) || $value instanceof ArrayAccess))
- throw new TException('value must be array or ArrayAccess');
- $this->_parameters->copyFrom($value);
- }
-
- /**
- * @return boolean true if the parameter index are string base, false otherwise.
- */
- public function getIsNamedParameters()
- {
- foreach($this->getParameters() as $k=>$v)
- return is_string($k);
- }
-
- /**
- * @return TAttributeCollection ordering clause.
- */
- public function getOrdersBy()
- {
- return $this->_ordersBy;
- }
-
- /**
- * @param mixed ordering clause.
- */
- public function setOrdersBy($value)
- {
- if(is_array($value) || $value instanceof Traversable)
- $this->_ordersBy->copyFrom($value);
- else
- {
- $value=trim(preg_replace('/\s+/',' ',(string)$value));
- $orderBys=array();
- foreach(explode(',',$value) as $orderBy)
- {
- $vs=explode(' ',trim($orderBy));
- $orderBys[$vs[0]]=isset($vs[1])?$vs[1]:'asc';
- }
- $this->_ordersBy->copyFrom($orderBys);
- }
- }
-
- /**
- * @return int maximum number of records to return.
- */
- public function getLimit()
- {
- return $this->_limit;
- }
-
- /**
- * @param int maximum number of records to return.
- */
- public function setLimit($value)
- {
- $this->_limit=$value;
- }
-
- /**
- * @return int record offset.
- */
- public function getOffset()
- {
- return $this->_offset;
- }
-
- /**
- * @param int record offset.
- */
- public function setOffset($value)
- {
- $this->_offset=$value;
- }
-
- /**
- * @return string string representation of the parameters. Useful for debugging.
- */
- public function __toString()
- {
- $str = '';
- if(strlen((string)$this->getCondition()) > 0)
- $str .= '"'.(string)$this->getCondition().'"';
- $params = array();
- foreach($this->getParameters() as $k=>$v)
- $params[] = "{$k} => ${v}";
- if(count($params) > 0)
- $str .= ', "'.implode(', ',$params).'"';
- $orders = array();
- foreach($this->getOrdersBy() as $k=>$v)
- $orders[] = "{$k} => ${v}";
- if(count($orders) > 0)
- $str .= ', "'.implode(', ',$orders).'"';
- if($this->_limit !==null)
- $str .= ', '.$this->_limit;
- if($this->_offset !== null)
- $str .= ', '.$this->_offset;
- return $str;
- }
-}
+<?php
+/**
+ * TDbSqlCriteria class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005-2012 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id: TDbSqlCriteria.php 1835 2007-04-03 01:38:15Z wei $
+ * @package System.Data.DataGateway
+ */
+
+/**
+ * Search criteria for TDbDataGateway.
+ *
+ * Criteria object for data gateway finder methods. Usage:
+ * <code>
+ * $criteria = new TSqlCriteria();
+ * $criteria->Parameters[':name'] = 'admin';
+ * $criteria->Parameters[':pass'] = 'prado';
+ * $criteria->OrdersBy['level'] = 'desc';
+ * $criteria->OrdersBy['name'] = 'asc';
+ * $criteria->Limit = 10;
+ * $criteria->Offset = 20;
+ * </code>
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id: TDbSqlCriteria.php 1835 2007-04-03 01:38:15Z wei $
+ * @package System.Data.DataGateway
+ * @since 3.1
+ */
+class TSqlCriteria extends TComponent
+{
+ /**
+ * @var mixed
+ * @since 3.1.7
+ */
+ private $_select='*';
+ private $_condition;
+ private $_parameters;
+ private $_ordersBy;
+ private $_limit;
+ private $_offset;
+
+ /**
+ * Creates a new criteria with given condition;
+ * @param string sql string after the WHERE stanza
+ * @param mixed named or indexed parameters, accepts as multiple arguments.
+ */
+ public function __construct($condition=null, $parameters=array())
+ {
+ if(!is_array($parameters) && func_num_args() > 1)
+ $parameters = array_slice(func_get_args(),1);
+ $this->_parameters=new TAttributeCollection;
+ $this->_parameters->setCaseSensitive(true);
+ $this->_parameters->copyFrom((array)$parameters);
+ $this->_ordersBy=new TAttributeCollection;
+ $this->_ordersBy->setCaseSensitive(true);
+
+ $this->setCondition($condition);
+ }
+
+ /**
+ * Gets the field list to be placed after the SELECT in the SQL. Default to '*'
+ * @return mixed
+ * @since 3.1.7
+ */
+ public function getSelect()
+ {
+ return $this->_select;
+ }
+
+ /**
+ * Sets the field list to be placed after the SELECT in the SQL.
+ *
+ * Different behavior depends on type of assigned value
+ * 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
+ * <code>
+ * array('col1', 'col2', 'col2')
+ * // SELECT `col1`, `col2`, `col3` FROM...
+ * </code>
+ *
+ * - Column aliasing
+ * <code>
+ * array('mycol1' => 'col1', 'mycol2' => 'COUNT(*)')
+ * // SELECT `col1` AS mycol1, COUNT(*) AS mycol2 FROM...
+ * </code>
+ *
+ * - NULL and scalar values (strings will be quoted depending on database)
+ * <code>
+ * array('col1' => 'my custom string', 'col2' => 1.0, 'col3' => 'NULL')
+ * // SELECT "my custom string" AS `col1`, 1.0 AS `col2`, NULL AS `col3` FROM...
+ * </code>
+ *
+ * - If the *-wildcard char is used as key or value, add the full list of quoted table column names
+ * <code>
+ * array('col1' => 'NULL', '*')
+ * // SELECT `col1`, `col2`, `col3`, NULL AS `col1` FROM...
+ * </code>
+ *
+ * @param mixed
+ * @since 3.1.7
+ * @see TDbCommandBuilder::getSelectFieldList()
+ */
+ public function setSelect($value)
+ {
+ $this->_select = $value;
+ }
+
+ /**
+ * @return string search conditions.
+ */
+ public function getCondition()
+ {
+ return $this->_condition;
+ }
+
+ /**
+ * Sets the search conditions to be placed after the WHERE clause in the SQL.
+ * @param string search conditions.
+ */
+ public function setCondition($value)
+ {
+ if(empty($value)) {
+ return;
+ }
+
+ // supporting the following SELECT-syntax:
+ // [ORDER BY {col_name | expr | position}
+ // [ASC | DESC], ...]
+ // [LIMIT {[offset,] row_count | row_count OFFSET offset}]
+ // See: http://dev.mysql.com/doc/refman/5.0/en/select.html
+
+ if(preg_match('/ORDER\s+BY\s+(.*?)(?=LIMIT)|ORDER\s+BY\s+(.*?)$/i', $value, $matches) > 0) {
+ // condition contains ORDER BY
+ $value = str_replace($matches[0], '', $value);
+ if(strlen($matches[1]) > 0) {
+ $this->setOrdersBy($matches[1]);
+ } else if(strlen($matches[2]) > 0) {
+ $this->setOrdersBy($matches[2]);
+ }
+ }
+
+ if(preg_match('/LIMIT\s+([\d\s,]+)/i', $value, $matches) > 0) {
+ // condition contains limit
+ $value = str_replace($matches[0], '', $value); // remove limit from query
+ if(strpos($matches[1], ',')) { // both offset and limit given
+ list($offset, $limit) = explode(',', $matches[1]);
+ $this->_limit = (int)$limit;
+ $this->_offset = (int)$offset;
+ } else { // only limit given
+ $this->_limit = (int)$matches[1];
+ }
+ }
+
+ if(preg_match('/OFFSET\s+(\d+)/i', $value, $matches) > 0) {
+ // condition contains offset
+ $value = str_replace($matches[0], '', $value); // remove offset from query
+ $this->_offset = (int)$matches[1]; // set offset in criteria
+ }
+
+ $this->_condition = trim($value);
+ }
+
+ /**
+ * @return TAttributeCollection list of named parameters and values.
+ */
+ public function getParameters()
+ {
+ return $this->_parameters;
+ }
+
+ /**
+ * @param ArrayAccess named parameters.
+ */
+ public function setParameters($value)
+ {
+ if(!(is_array($value) || $value instanceof ArrayAccess))
+ throw new TException('value must be array or ArrayAccess');
+ $this->_parameters->copyFrom($value);
+ }
+
+ /**
+ * @return boolean true if the parameter index are string base, false otherwise.
+ */
+ public function getIsNamedParameters()
+ {
+ foreach($this->getParameters() as $k=>$v)
+ return is_string($k);
+ }
+
+ /**
+ * @return TAttributeCollection ordering clause.
+ */
+ public function getOrdersBy()
+ {
+ return $this->_ordersBy;
+ }
+
+ /**
+ * @param mixed ordering clause.
+ */
+ public function setOrdersBy($value)
+ {
+ if(is_array($value) || $value instanceof Traversable)
+ $this->_ordersBy->copyFrom($value);
+ else
+ {
+ $value=trim(preg_replace('/\s+/',' ',(string)$value));
+ $orderBys=array();
+ foreach(explode(',',$value) as $orderBy)
+ {
+ $vs=explode(' ',trim($orderBy));
+ $orderBys[$vs[0]]=isset($vs[1])?$vs[1]:'asc';
+ }
+ $this->_ordersBy->copyFrom($orderBys);
+ }
+ }
+
+ /**
+ * @return int maximum number of records to return.
+ */
+ public function getLimit()
+ {
+ return $this->_limit;
+ }
+
+ /**
+ * @param int maximum number of records to return.
+ */
+ public function setLimit($value)
+ {
+ $this->_limit=$value;
+ }
+
+ /**
+ * @return int record offset.
+ */
+ public function getOffset()
+ {
+ return $this->_offset;
+ }
+
+ /**
+ * @param int record offset.
+ */
+ public function setOffset($value)
+ {
+ $this->_offset=$value;
+ }
+
+ /**
+ * @return string string representation of the parameters. Useful for debugging.
+ */
+ public function __toString()
+ {
+ $str = '';
+ if(strlen((string)$this->getCondition()) > 0)
+ $str .= '"'.(string)$this->getCondition().'"';
+ $params = array();
+ foreach($this->getParameters() as $k=>$v)
+ $params[] = "{$k} => ${v}";
+ if(count($params) > 0)
+ $str .= ', "'.implode(', ',$params).'"';
+ $orders = array();
+ foreach($this->getOrdersBy() as $k=>$v)
+ $orders[] = "{$k} => ${v}";
+ if(count($orders) > 0)
+ $str .= ', "'.implode(', ',$orders).'"';
+ if($this->_limit !==null)
+ $str .= ', '.$this->_limit;
+ if($this->_offset !== null)
+ $str .= ', '.$this->_offset;
+ return $str;
+ }
+}
?> \ No newline at end of file
diff --git a/framework/Data/DataGateway/TTableGateway.php b/framework/Data/DataGateway/TTableGateway.php
index bdcb391e..c436e0b0 100644
--- a/framework/Data/DataGateway/TTableGateway.php
+++ b/framework/Data/DataGateway/TTableGateway.php
@@ -1,476 +1,476 @@
-<?php
-/**
- * TTableGateway class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
+<?php
+/**
+ * TTableGateway class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link http://www.pradosoft.com/
* @copyright Copyright &copy; 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.DataGateway
- */
-
-/**
- * Loads the data gateway command builder and sql criteria.
- */
-Prado::using('System.Data.DataGateway.TSqlCriteria');
-Prado::using('System.Data.DataGateway.TDataGatewayCommand');
-
-/**
- * TTableGateway class provides several find methods to get data from the database
- * and update, insert, and delete methods.
- *
- * Each method maps the input parameters into a SQL call and executes the SQL
- * against a database connection. The TTableGateway is stateless
- * (with respect to the data and data objects), as its role is to push data back and forth.
- *
- * Example usage:
- * <code>
- * //create a connection
- * $dsn = 'pgsql:host=localhost;dbname=test';
- * $conn = new TDbConnection($dsn, 'dbuser','dbpass');
- *
- * //create a table gateway for table/view named 'address'
- * $table = new TTableGateway('address', $conn);
- *
- * //insert a new row, returns last insert id (if applicable)
- * $id = $table->insert(array('name'=>'wei', 'phone'=>'111111'));
- *
- * $record1 = $table->findByPk($id); //find inserted record
- *
- * //finds all records, returns an iterator
- * $records = $table->findAll();
- * print_r($records->readAll());
- *
- * //update the row
- * $table->updateByPk($record1, $id);
- * </code>
- *
- * All methods that may return more than one row of data will return an
- * TDbDataReader iterator.
- *
- * The OnCreateCommand event is raised when a command is prepared and parameter
- * binding is completed. The parameter object is a TDataGatewayEventParameter of which the
- * {@link TDataGatewayEventParameter::getCommand Command} property can be
- * inspected to obtain the sql query to be executed.
- *
- * The OnExecuteCommand event is raised when a command is executed and the result
- * from the database was returned. The parameter object is a
- * TDataGatewayResultEventParameter of which the
- * {@link TDataGatewayEventParameter::getResult Result} property contains
- * the data return from the database. The data returned can be changed
- * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
- *
- * <code>
- * $table->OnCreateCommand[] = 'log_it'; //any valid PHP callback statement
- * $table->OnExecuteCommand[] = array($obj, 'method_name'); // calls 'method_name' on $obj
- *
- * function log_it($sender, $param)
- * {
- * var_dump($param); //TDataGatewayEventParameter object.
- * }
- * </code>
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.DataGateway
- * @since 3.1
- */
-class TTableGateway extends TComponent
-{
- private $_command;
- private $_connection;
-
- /**
- * Creates a new generic table gateway for a given table or view name
- * and a database connection.
- * @param string|TDbTableInfo table or view name or table information.
- * @param TDbConnection database connection.
- */
- public function __construct($table,$connection)
- {
- $this->_connection=$connection;
- if(is_string($table))
- $this->setTableName($table);
- else if($table instanceof TDbTableInfo)
- $this->setTableInfo($table);
- else
- throw new TDbException('dbtablegateway_invalid_table_info');
- }
-
- /**
- * @param TDbTableInfo table or view information.
- */
- protected function setTableInfo($tableInfo)
- {
- $builder = $tableInfo->createCommandBuilder($this->getDbConnection());
- $this->initCommandBuilder($builder);
- }
-
- /**
- * Sets up the command builder for the given table.
- * @param string table or view name.
- */
- protected function setTableName($tableName)
- {
- Prado::using('System.Data.Common.TDbMetaData');
- $meta = TDbMetaData::getInstance($this->getDbConnection());
- $this->initCommandBuilder($meta->createCommandBuilder($tableName));
- }
-
- public function getTableInfo()
- {
- return $this->getCommand()->getTableInfo();
- }
-
- public function getTableName()
- {
- return $this->getTableInfo()->getTableName();
- }
-
- /**
- * @param TDbCommandBuilder database specific command builder.
- */
- protected function initCommandBuilder($builder)
- {
- $this->_command = new TDataGatewayCommand($builder);
- $this->_command->OnCreateCommand[] = array($this, 'onCreateCommand');
- $this->_command->OnExecuteCommand[] = array($this, 'onExecuteCommand');
- }
-
- /**
- * Raised when a command is prepared and parameter binding is completed.
- * The parameter object is TDataGatewayEventParameter of which the
- * {@link TDataGatewayEventParameter::getCommand Command} property can be
- * inspected to obtain the sql query to be executed.
- * @param TDataGatewayCommand originator $sender
- * @param TDataGatewayEventParameter
- */
- public function onCreateCommand($sender, $param)
- {
- $this->raiseEvent('OnCreateCommand', $this, $param);
- }
-
- /**
- * Raised when a command is executed and the result from the database was returned.
- * The parameter object is TDataGatewayResultEventParameter of which the
- * {@link TDataGatewayEventParameter::getResult Result} property contains
- * the data return from the database. The data returned can be changed
- * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
- * @param TDataGatewayCommand originator $sender
- * @param TDataGatewayResultEventParameter
- */
- public function onExecuteCommand($sender, $param)
- {
- $this->raiseEvent('OnExecuteCommand', $this, $param);
- }
-
- /**
- * @return TDataGatewayCommand command builder and executor.
- */
- protected function getCommand()
- {
- return $this->_command;
- }
-
- /**
- * @return TDbConnection database connection.
- */
- public function getDbConnection()
- {
- return $this->_connection;
- }
-
- /**
- * Execute arbituary sql command with binding parameters.
- * @param string SQL query string.
- * @param array binding parameters, positional or named.
- * @return array query results.
- */
- public function findBySql($sql, $parameters=array())
- {
- $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
- $criteria = $this->getCriteria($sql,$parameters, $args);
- return $this->getCommand()->findBySql($criteria);
- }
-
- /**
- * Execute arbituary sql command with binding parameters.
- * @param string SQL query string.
- * @param array binding parameters, positional or named.
- * @return TDbDataReader query results.
- */
- public function findAllBySql($sql, $parameters=array())
- {
- $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
- $criteria = $this->getCriteria($sql,$parameters, $args);
- return $this->getCommand()->findAllBySql($criteria);
- }
-
- /**
- * Find one single record that matches the criteria.
- *
- * Usage:
- * <code>
- * $table->find('username = :name AND password = :pass',
- * array(':name'=>$name, ':pass'=>$pass));
- * $table->find('username = ? AND password = ?', array($name, $pass));
- * $table->find('username = ? AND password = ?', $name, $pass);
- * //$criteria is of TSqlCriteria
- * $table->find($criteria); //the 2nd parameter for find() is ignored.
- * </code>
- *
- * @param string|TSqlCriteria SQL condition or criteria object.
- * @param mixed parameter values.
- * @return array matching record object.
- */
- public function find($criteria, $parameters=array())
- {
- $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
- $criteria = $this->getCriteria($criteria,$parameters, $args);
- return $this->getCommand()->find($criteria);
- }
-
- /**
- * Accepts same parameters as find(), but returns TDbDataReader instead.
- * @param string|TSqlCriteria SQL condition or criteria object.
- * @param mixed parameter values.
- * @return TDbDataReader matching records.
- */
- public function findAll($criteria=null, $parameters=array())
- {
- $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
- if($criteria!==null)
- $criteria = $this->getCriteria($criteria,$parameters, $args);
- return $this->getCommand()->findAll($criteria);
- }
-
- /**
- * Find one record using only the primary key or composite primary keys. Usage:
- *
- * <code>
- * $table->findByPk($primaryKey);
- * $table->findByPk($key1, $key2, ...);
- * $table->findByPk(array($key1,$key2,...));
- * </code>
- *
- * @param mixed primary keys
- * @return array matching record.
- */
- public function findByPk($keys)
- {
- if(func_num_args() > 1)
- $keys = func_get_args();
- return $this->getCommand()->findByPk($keys);
- }
-
- /**
- * Similar to findByPk(), but returns TDbDataReader instead.
- *
- * For scalar primary keys:
- * <code>
- * $table->findAllByPk($key1, $key2, ...);
- * $table->findAllByPk(array($key1, $key2, ...));
- * </code>
- *
- * For composite keys:
- * <code>
- * $table->findAllByPk(array($key1, $key2), array($key3, $key4), ...);
- * $table->findAllByPk(array(array($key1, $key2), array($key3, $key4), ...));
- * </code>
- * @param mixed primary keys
- * @return TDbDataReader data reader.
- */
- public function findAllByPks($keys)
- {
- if(func_num_args() > 1)
- $keys = func_get_args();
- return $this->getCommand()->findAllByPk($keys);
- }
-
- /**
- * Delete records from the table with condition given by $where and
- * binding values specified by $parameter argument.
- * This method uses additional arguments as $parameters. E.g.
- * <code>
- * $table->delete('age > ? AND location = ?', $age, $location);
- * </code>
- * @param string delete condition.
- * @param array condition parameters.
- * @return integer number of records deleted.
- */
- public function deleteAll($criteria, $parameters=array())
- {
- $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
- $criteria = $this->getCriteria($criteria,$parameters, $args);
- return $this->getCommand()->delete($criteria);
- }
-
- /**
- * Delete records by primary key. Usage:
- *
- * <code>
- * $table->deleteByPk($primaryKey); //delete 1 record
- * $table->deleteByPk($key1,$key2,...); //delete multiple records
- * $table->deleteByPk(array($key1,$key2,...)); //delete multiple records
- * </code>
- *
- * For composite primary keys (determined from the table definitions):
- * <code>
- * $table->deleteByPk(array($key1,$key2)); //delete 1 record
- *
- * //delete multiple records
- * $table->deleteByPk(array($key1,$key2), array($key3,$key4),...);
- *
- * //delete multiple records
- * $table->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. ));
- * </code>
- *
- * @param mixed primary key values.
- * @return int number of records deleted.
- */
- public function deleteByPk($keys)
- {
- if(func_num_args() > 1)
- $keys = func_get_args();
- return $this->getCommand()->deleteByPk($keys);
- }
-
- /**
- * Alias for deleteByPk()
- */
- public function deleteAllByPks($keys)
- {
- if(func_num_args() > 1)
- $keys = func_get_args();
- return $this->deleteByPk($keys);
- }
-
- /**
- * Find the number of records.
- * @param string|TSqlCriteria SQL condition or criteria object.
- * @param mixed parameter values.
- * @return int number of records.
- */
- public function count($criteria=null,$parameters=array())
- {
- $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
- if($criteria!==null)
- $criteria = $this->getCriteria($criteria,$parameters, $args);
- return $this->getCommand()->count($criteria);
- }
-
- /**
- * Updates the table with new name-value pair $data. Each array key must
- * correspond to a column name in the table. The update condition is
- * specified by the $where argument and additional binding values can be
- * specified using the $parameter argument.
- * This method uses additional arguments as $parameters. E.g.
- * <code>
- * $gateway->update($data, 'age > ? AND location = ?', $age, $location);
- * </code>
- * @param array new record data.
- * @param string update condition
- * @param array additional binding name-value pairs.
- * @return integer number of records updated.
- */
- public function update($data, $criteria, $parameters=array())
- {
- $args = func_num_args() > 2 ? array_slice(func_get_args(),2) : null;
- $criteria = $this->getCriteria($criteria,$parameters, $args);
- return $this->getCommand()->update($data, $criteria);
- }
-
- /**
- * Inserts a new record into the table. Each array key must
- * correspond to a column name in the table unless a null value is permitted.
- * @param array new record data.
- * @return mixed last insert id if one column contains a serial or sequence,
- * otherwise true if command executes successfully and affected 1 or more rows.
- */
- public function insert($data)
- {
- return $this->getCommand()->insert($data);
- }
-
- /**
- * @return mixed last insert id, null if none is found.
- */
- public function getLastInsertId()
- {
- return $this->getCommand()->getLastInsertId();
- }
-
- /**
- * Create a new TSqlCriteria object from a string $criteria. The $args
- * are additional parameters and are used in place of the $parameters
- * if $parameters is not an array and $args is an arrary.
- * @param string|TSqlCriteria sql criteria
- * @param mixed parameters passed by the user.
- * @param array additional parameters obtained from function_get_args().
- * @return TSqlCriteria criteria object.
- */
- protected function getCriteria($criteria, $parameters, $args)
- {
- if(is_string($criteria))
- {
- $useArgs = !is_array($parameters) && is_array($args);
- return new TSqlCriteria($criteria,$useArgs ? $args : $parameters);
- }
- else if($criteria instanceof TSqlCriteria)
- return $criteria;
- else
- throw new TDbException('dbtablegateway_invalid_criteria');
- }
-
- /**
- * Dynamic find method using parts of method name as search criteria.
- * Method name starting with "findBy" only returns 1 record.
- * Method name starting with "findAllBy" returns 0 or more records.
- * Method name starting with "deleteBy" deletes records by the trail criteria.
- * The condition is taken as part of the method name after "findBy", "findAllBy"
- * or "deleteBy".
- *
- * The following are equivalent:
- * <code>
- * $table->findByName($name)
- * $table->find('Name = ?', $name);
- * </code>
- * <code>
- * $table->findByUsernameAndPassword($name,$pass); // OR may be used
- * $table->findBy_Username_And_Password($name,$pass); // _OR_ may be used
- * $table->find('Username = ? AND Password = ?', $name, $pass);
- * </code>
- * <code>
- * $table->findAllByAge($age);
- * $table->findAll('Age = ?', $age);
- * </code>
- * <code>
- * $table->deleteAll('Name = ?', $name);
- * $table->deleteByName($name);
- * </code>
- * @return mixed single record if method name starts with "findBy", 0 or more records
- * if method name starts with "findAllBy"
- */
- public function __call($method,$args)
- {
- $delete =false;
- if($findOne = substr(strtolower($method),0,6)==='findby')
- $condition = $method[6]==='_' ? substr($method,7) : substr($method,6);
- else if(substr(strtolower($method),0,9)==='findallby')
- $condition = $method[9]==='_' ? substr($method,10) : substr($method,9);
- else if($delete = substr(strtolower($method),0,8)==='deleteby')
- $condition = $method[8]==='_' ? substr($method,9) : substr($method,8);
- else if($delete = substr(strtolower($method),0,11)==='deleteallby')
- $condition = $method[11]==='_' ? substr($method,12) : substr($method,11);
- else
- return null;
-
- $criteria = $this->getCommand()->createCriteriaFromString($method, $condition, $args);
- if($delete)
- return $this->deleteAll($criteria);
- else
- return $findOne ? $this->find($criteria) : $this->findAll($criteria);
- }
-}
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Data.DataGateway
+ */
+
+/**
+ * Loads the data gateway command builder and sql criteria.
+ */
+Prado::using('System.Data.DataGateway.TSqlCriteria');
+Prado::using('System.Data.DataGateway.TDataGatewayCommand');
+
+/**
+ * TTableGateway class provides several find methods to get data from the database
+ * and update, insert, and delete methods.
+ *
+ * Each method maps the input parameters into a SQL call and executes the SQL
+ * against a database connection. The TTableGateway is stateless
+ * (with respect to the data and data objects), as its role is to push data back and forth.
+ *
+ * Example usage:
+ * <code>
+ * //create a connection
+ * $dsn = 'pgsql:host=localhost;dbname=test';
+ * $conn = new TDbConnection($dsn, 'dbuser','dbpass');
+ *
+ * //create a table gateway for table/view named 'address'
+ * $table = new TTableGateway('address', $conn);
+ *
+ * //insert a new row, returns last insert id (if applicable)
+ * $id = $table->insert(array('name'=>'wei', 'phone'=>'111111'));
+ *
+ * $record1 = $table->findByPk($id); //find inserted record
+ *
+ * //finds all records, returns an iterator
+ * $records = $table->findAll();
+ * print_r($records->readAll());
+ *
+ * //update the row
+ * $table->updateByPk($record1, $id);
+ * </code>
+ *
+ * All methods that may return more than one row of data will return an
+ * TDbDataReader iterator.
+ *
+ * The OnCreateCommand event is raised when a command is prepared and parameter
+ * binding is completed. The parameter object is a TDataGatewayEventParameter of which the
+ * {@link TDataGatewayEventParameter::getCommand Command} property can be
+ * inspected to obtain the sql query to be executed.
+ *
+ * The OnExecuteCommand event is raised when a command is executed and the result
+ * from the database was returned. The parameter object is a
+ * TDataGatewayResultEventParameter of which the
+ * {@link TDataGatewayEventParameter::getResult Result} property contains
+ * the data return from the database. The data returned can be changed
+ * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
+ *
+ * <code>
+ * $table->OnCreateCommand[] = 'log_it'; //any valid PHP callback statement
+ * $table->OnExecuteCommand[] = array($obj, 'method_name'); // calls 'method_name' on $obj
+ *
+ * function log_it($sender, $param)
+ * {
+ * var_dump($param); //TDataGatewayEventParameter object.
+ * }
+ * </code>
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @version $Id$
+ * @package System.Data.DataGateway
+ * @since 3.1
+ */
+class TTableGateway extends TComponent
+{
+ private $_command;
+ private $_connection;
+
+ /**
+ * Creates a new generic table gateway for a given table or view name
+ * and a database connection.
+ * @param string|TDbTableInfo table or view name or table information.
+ * @param TDbConnection database connection.
+ */
+ public function __construct($table,$connection)
+ {
+ $this->_connection=$connection;
+ if(is_string($table))
+ $this->setTableName($table);
+ else if($table instanceof TDbTableInfo)
+ $this->setTableInfo($table);
+ else
+ throw new TDbException('dbtablegateway_invalid_table_info');
+ }
+
+ /**
+ * @param TDbTableInfo table or view information.
+ */
+ protected function setTableInfo($tableInfo)
+ {
+ $builder = $tableInfo->createCommandBuilder($this->getDbConnection());
+ $this->initCommandBuilder($builder);
+ }
+
+ /**
+ * Sets up the command builder for the given table.
+ * @param string table or view name.
+ */
+ protected function setTableName($tableName)
+ {
+ Prado::using('System.Data.Common.TDbMetaData');
+ $meta = TDbMetaData::getInstance($this->getDbConnection());
+ $this->initCommandBuilder($meta->createCommandBuilder($tableName));
+ }
+
+ public function getTableInfo()
+ {
+ return $this->getCommand()->getTableInfo();
+ }
+
+ public function getTableName()
+ {
+ return $this->getTableInfo()->getTableName();
+ }
+
+ /**
+ * @param TDbCommandBuilder database specific command builder.
+ */
+ protected function initCommandBuilder($builder)
+ {
+ $this->_command = new TDataGatewayCommand($builder);
+ $this->_command->OnCreateCommand[] = array($this, 'onCreateCommand');
+ $this->_command->OnExecuteCommand[] = array($this, 'onExecuteCommand');
+ }
+
+ /**
+ * Raised when a command is prepared and parameter binding is completed.
+ * The parameter object is TDataGatewayEventParameter of which the
+ * {@link TDataGatewayEventParameter::getCommand Command} property can be
+ * inspected to obtain the sql query to be executed.
+ * @param TDataGatewayCommand originator $sender
+ * @param TDataGatewayEventParameter
+ */
+ public function onCreateCommand($sender, $param)
+ {
+ $this->raiseEvent('OnCreateCommand', $this, $param);
+ }
+
+ /**
+ * Raised when a command is executed and the result from the database was returned.
+ * The parameter object is TDataGatewayResultEventParameter of which the
+ * {@link TDataGatewayEventParameter::getResult Result} property contains
+ * the data return from the database. The data returned can be changed
+ * by setting the {@link TDataGatewayEventParameter::setResult Result} property.
+ * @param TDataGatewayCommand originator $sender
+ * @param TDataGatewayResultEventParameter
+ */
+ public function onExecuteCommand($sender, $param)
+ {
+ $this->raiseEvent('OnExecuteCommand', $this, $param);
+ }
+
+ /**
+ * @return TDataGatewayCommand command builder and executor.
+ */
+ protected function getCommand()
+ {
+ return $this->_command;
+ }
+
+ /**
+ * @return TDbConnection database connection.
+ */
+ public function getDbConnection()
+ {
+ return $this->_connection;
+ }
+
+ /**
+ * Execute arbituary sql command with binding parameters.
+ * @param string SQL query string.
+ * @param array binding parameters, positional or named.
+ * @return array query results.
+ */
+ public function findBySql($sql, $parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ $criteria = $this->getCriteria($sql,$parameters, $args);
+ return $this->getCommand()->findBySql($criteria);
+ }
+
+ /**
+ * Execute arbituary sql command with binding parameters.
+ * @param string SQL query string.
+ * @param array binding parameters, positional or named.
+ * @return TDbDataReader query results.
+ */
+ public function findAllBySql($sql, $parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ $criteria = $this->getCriteria($sql,$parameters, $args);
+ return $this->getCommand()->findAllBySql($criteria);
+ }
+
+ /**
+ * Find one single record that matches the criteria.
+ *
+ * Usage:
+ * <code>
+ * $table->find('username = :name AND password = :pass',
+ * array(':name'=>$name, ':pass'=>$pass));
+ * $table->find('username = ? AND password = ?', array($name, $pass));
+ * $table->find('username = ? AND password = ?', $name, $pass);
+ * //$criteria is of TSqlCriteria
+ * $table->find($criteria); //the 2nd parameter for find() is ignored.
+ * </code>
+ *
+ * @param string|TSqlCriteria SQL condition or criteria object.
+ * @param mixed parameter values.
+ * @return array matching record object.
+ */
+ public function find($criteria, $parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ $criteria = $this->getCriteria($criteria,$parameters, $args);
+ return $this->getCommand()->find($criteria);
+ }
+
+ /**
+ * Accepts same parameters as find(), but returns TDbDataReader instead.
+ * @param string|TSqlCriteria SQL condition or criteria object.
+ * @param mixed parameter values.
+ * @return TDbDataReader matching records.
+ */
+ public function findAll($criteria=null, $parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ if($criteria!==null)
+ $criteria = $this->getCriteria($criteria,$parameters, $args);
+ return $this->getCommand()->findAll($criteria);
+ }
+
+ /**
+ * Find one record using only the primary key or composite primary keys. Usage:
+ *
+ * <code>
+ * $table->findByPk($primaryKey);
+ * $table->findByPk($key1, $key2, ...);
+ * $table->findByPk(array($key1,$key2,...));
+ * </code>
+ *
+ * @param mixed primary keys
+ * @return array matching record.
+ */
+ public function findByPk($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ return $this->getCommand()->findByPk($keys);
+ }
+
+ /**
+ * Similar to findByPk(), but returns TDbDataReader instead.
+ *
+ * For scalar primary keys:
+ * <code>
+ * $table->findAllByPk($key1, $key2, ...);
+ * $table->findAllByPk(array($key1, $key2, ...));
+ * </code>
+ *
+ * For composite keys:
+ * <code>
+ * $table->findAllByPk(array($key1, $key2), array($key3, $key4), ...);
+ * $table->findAllByPk(array(array($key1, $key2), array($key3, $key4), ...));
+ * </code>
+ * @param mixed primary keys
+ * @return TDbDataReader data reader.
+ */
+ public function findAllByPks($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ return $this->getCommand()->findAllByPk($keys);
+ }
+
+ /**
+ * Delete records from the table with condition given by $where and
+ * binding values specified by $parameter argument.
+ * This method uses additional arguments as $parameters. E.g.
+ * <code>
+ * $table->delete('age > ? AND location = ?', $age, $location);
+ * </code>
+ * @param string delete condition.
+ * @param array condition parameters.
+ * @return integer number of records deleted.
+ */
+ public function deleteAll($criteria, $parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ $criteria = $this->getCriteria($criteria,$parameters, $args);
+ return $this->getCommand()->delete($criteria);
+ }
+
+ /**
+ * Delete records by primary key. Usage:
+ *
+ * <code>
+ * $table->deleteByPk($primaryKey); //delete 1 record
+ * $table->deleteByPk($key1,$key2,...); //delete multiple records
+ * $table->deleteByPk(array($key1,$key2,...)); //delete multiple records
+ * </code>
+ *
+ * For composite primary keys (determined from the table definitions):
+ * <code>
+ * $table->deleteByPk(array($key1,$key2)); //delete 1 record
+ *
+ * //delete multiple records
+ * $table->deleteByPk(array($key1,$key2), array($key3,$key4),...);
+ *
+ * //delete multiple records
+ * $table->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. ));
+ * </code>
+ *
+ * @param mixed primary key values.
+ * @return int number of records deleted.
+ */
+ public function deleteByPk($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ return $this->getCommand()->deleteByPk($keys);
+ }
+
+ /**
+ * Alias for deleteByPk()
+ */
+ public function deleteAllByPks($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ return $this->deleteByPk($keys);
+ }
+
+ /**
+ * Find the number of records.
+ * @param string|TSqlCriteria SQL condition or criteria object.
+ * @param mixed parameter values.
+ * @return int number of records.
+ */
+ public function count($criteria=null,$parameters=array())
+ {
+ $args = func_num_args() > 1 ? array_slice(func_get_args(),1) : null;
+ if($criteria!==null)
+ $criteria = $this->getCriteria($criteria,$parameters, $args);
+ return $this->getCommand()->count($criteria);
+ }
+
+ /**
+ * Updates the table with new name-value pair $data. Each array key must
+ * correspond to a column name in the table. The update condition is
+ * specified by the $where argument and additional binding values can be
+ * specified using the $parameter argument.
+ * This method uses additional arguments as $parameters. E.g.
+ * <code>
+ * $gateway->update($data, 'age > ? AND location = ?', $age, $location);
+ * </code>
+ * @param array new record data.
+ * @param string update condition
+ * @param array additional binding name-value pairs.
+ * @return integer number of records updated.
+ */
+ public function update($data, $criteria, $parameters=array())
+ {
+ $args = func_num_args() > 2 ? array_slice(func_get_args(),2) : null;
+ $criteria = $this->getCriteria($criteria,$parameters, $args);
+ return $this->getCommand()->update($data, $criteria);
+ }
+
+ /**
+ * Inserts a new record into the table. Each array key must
+ * correspond to a column name in the table unless a null value is permitted.
+ * @param array new record data.
+ * @return mixed last insert id if one column contains a serial or sequence,
+ * otherwise true if command executes successfully and affected 1 or more rows.
+ */
+ public function insert($data)
+ {
+ return $this->getCommand()->insert($data);
+ }
+
+ /**
+ * @return mixed last insert id, null if none is found.
+ */
+ public function getLastInsertId()
+ {
+ return $this->getCommand()->getLastInsertId();
+ }
+
+ /**
+ * Create a new TSqlCriteria object from a string $criteria. The $args
+ * are additional parameters and are used in place of the $parameters
+ * if $parameters is not an array and $args is an arrary.
+ * @param string|TSqlCriteria sql criteria
+ * @param mixed parameters passed by the user.
+ * @param array additional parameters obtained from function_get_args().
+ * @return TSqlCriteria criteria object.
+ */
+ protected function getCriteria($criteria, $parameters, $args)
+ {
+ if(is_string($criteria))
+ {
+ $useArgs = !is_array($parameters) && is_array($args);
+ return new TSqlCriteria($criteria,$useArgs ? $args : $parameters);
+ }
+ else if($criteria instanceof TSqlCriteria)
+ return $criteria;
+ else
+ throw new TDbException('dbtablegateway_invalid_criteria');
+ }
+
+ /**
+ * Dynamic find method using parts of method name as search criteria.
+ * Method name starting with "findBy" only returns 1 record.
+ * Method name starting with "findAllBy" returns 0 or more records.
+ * Method name starting with "deleteBy" deletes records by the trail criteria.
+ * The condition is taken as part of the method name after "findBy", "findAllBy"
+ * or "deleteBy".
+ *
+ * The following are equivalent:
+ * <code>
+ * $table->findByName($name)
+ * $table->find('Name = ?', $name);
+ * </code>
+ * <code>
+ * $table->findByUsernameAndPassword($name,$pass); // OR may be used
+ * $table->findBy_Username_And_Password($name,$pass); // _OR_ may be used
+ * $table->find('Username = ? AND Password = ?', $name, $pass);
+ * </code>
+ * <code>
+ * $table->findAllByAge($age);
+ * $table->findAll('Age = ?', $age);
+ * </code>
+ * <code>
+ * $table->deleteAll('Name = ?', $name);
+ * $table->deleteByName($name);
+ * </code>
+ * @return mixed single record if method name starts with "findBy", 0 or more records
+ * if method name starts with "findAllBy"
+ */
+ public function __call($method,$args)
+ {
+ $delete =false;
+ if($findOne = substr(strtolower($method),0,6)==='findby')
+ $condition = $method[6]==='_' ? substr($method,7) : substr($method,6);
+ else if(substr(strtolower($method),0,9)==='findallby')
+ $condition = $method[9]==='_' ? substr($method,10) : substr($method,9);
+ else if($delete = substr(strtolower($method),0,8)==='deleteby')
+ $condition = $method[8]==='_' ? substr($method,9) : substr($method,8);
+ else if($delete = substr(strtolower($method),0,11)==='deleteallby')
+ $condition = $method[11]==='_' ? substr($method,12) : substr($method,11);
+ else
+ return null;
+
+ $criteria = $this->getCommand()->createCriteriaFromString($method, $condition, $args);
+ if($delete)
+ return $this->deleteAll($criteria);
+ else
+ return $findOne ? $this->find($criteria) : $this->findAll($criteria);
+ }
+}