From 03f362a40a8dd39f8c8b4bf816334922b7b264e4 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 9 Jan 2007 10:42:06 +0000 Subject: add TActiveRecord::findAllByPks() --- framework/Data/ActiveRecord/Vendor/TDbMetaData.php | 92 +++++++++------------- .../Data/ActiveRecord/Vendor/TDbMetaDataCommon.php | 26 ++++-- 2 files changed, 56 insertions(+), 62 deletions(-) (limited to 'framework/Data/ActiveRecord/Vendor') diff --git a/framework/Data/ActiveRecord/Vendor/TDbMetaData.php b/framework/Data/ActiveRecord/Vendor/TDbMetaData.php index efb7c467..38a82aef 100644 --- a/framework/Data/ActiveRecord/Vendor/TDbMetaData.php +++ b/framework/Data/ActiveRecord/Vendor/TDbMetaData.php @@ -136,6 +136,43 @@ abstract class TDbMetaData extends TComponent return implode(' AND ', $criteria); } + /** + * Construct a "pk IN ('key1', 'key2', ...)" criteria. + * @param TDbConnection database connection. + * @param array values for IN predicate + * @param string SQL string for primary keys IN a list. + */ + protected function getCompositeKeysCriteria($conn, $values) + { + $count = count($this->getPrimaryKeys()); + if($count===0) + throw new TActiveRecordException('ar_no_primary_key_found',$this->getTableName()); + if(!is_array($values) || count($values) === 0) + throw new TActiveRecordException('ar_missing_pk_values', $this->getTableName()); + if($count>1 && !is_array($values[0])) + $values = array($values); + if($count > 1 && count($values[0]) !== $count) + throw new TActiveRecordException('ar_pk_value_count_mismatch', $this->getTableName()); + + $columns = array(); + foreach($this->getPrimaryKeys() as $key) + $columns[] = $this->getColumn($key)->getName(); + return '('.implode(', ',$columns).') IN '.$this->quoteTuple($conn, $values); + } + + /** + * @param TDbConnection database connection. + * @param array values + * @return string quoted recursive tuple values, e.g. "('val1', 'val2')". + */ + protected function quoteTuple($conn, $array) + { + $data = array(); + foreach($array as $k=>$v) + $data[] = is_array($v) ? $this->quoteTuple($conn, $v) : $conn->quoteString($v); + return '('.implode(', ', $data).')'; + } + /** * Bind a list of variables in the command. The named parameters is taken * from the values of the $keys parameter. The bind value is taken from the @@ -308,60 +345,5 @@ x * @param array name value pairs of columns for update. return implode(', ', $fields); } - /** - * @param TDbConnection database connection - * @param array primary key values. - * @return string delete criteria for multiple scalar primary keys. - */ - protected function getDeleteInPkCriteria($conn, $keys) - { - $pk = $this->getPrimaryKeys(); - $column = $this->getColumn($pk[0])->getName(); - $values = array(); - foreach($keys as $key) - { - if(is_array($key)) - { - throw new TActiveRecordException('ar_primary_key_is_scalar', - $this->getTableName(),$column,'array('.implode(', ',$key).')'); - } - $values[] = $conn->quoteString($key); - } - $pks = implode(', ', $values); - return "$column IN ($pks)"; - } - - /** - * @param TDbConnection database connection - * @param array primary key values. - * @return string delete criteria for multiple composite primary keys. - */ - protected function getDeleteMultiplePkCriteria($conn,$pks) - { - //check for 1 set composite keys - if(count($pks)>0 && !is_array($pks[0])) - $pks = array($pks); - $conditions=array(); - foreach($pks as $keys) - $conditions[] = $this->getDeleteCompositeKeyCondition($conn,$keys); - return implode(' OR ', $conditions); - } - - /** - * @return string delete criteria for 1 composite key. - */ - protected function getDeleteCompositeKeyCondition($conn,$keys) - { - $condition=array(); - $index = 0; - foreach($this->getPrimarykeys() as $pk) - { - $name = $this->getColumn($pk)->getName(); - $value = isset($keys[$pk]) ? $keys[$pk] : $keys[$index]; - $condition[] = "$name = ".$conn->quoteString($value); - $index++; - } - return '('.implode(' AND ', $condition).')'; - } } ?> \ No newline at end of file diff --git a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php index fffdb6fb..7f7dad8b 100644 --- a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php +++ b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php @@ -40,6 +40,24 @@ abstract class TDbMetaDataCommon extends TDbMetaData return $command; } + /** + * SQL database command for finding records by a list of primary keys. + * @param TDbConnection database connection. + * @param array list of primary keys to match. + * @return TDbCommand find by list of primary keys command. + */ + public function getFindInPksCommand($conn, $keys) + { + $conn->setActive(true); + $columns = $this->getSelectionColumns(); + $table = $this->getTableName(); + $criteria = $this->getCompositeKeysCriteria($conn,$keys); + $sql = "SELECT {$columns} FROM {$table} WHERE {$criteria}"; + $command = $conn->createCommand($sql); + $command->prepare(); + return $command; + } + /** * SQL database command for finding records using a criteria object. * @param TDbConnection database connection. @@ -160,14 +178,8 @@ abstract class TDbMetaDataCommon extends TDbMetaData public function getDeleteByPkCommand($conn,$keys) { $conn->setActive(true); - $numKeys = count($this->getPrimaryKeys()); $table = $this->getTableName(); - if($numKeys===0) - throw new TActiveRecordException('ar_no_primary_key_found',$table); - if($numKeys===1) - $criteria = $this->getDeleteInPkCriteria($conn,$keys); - else - $criteria = $this->getDeleteMultiplePkCriteria($conn,$keys); + $criteria = $this->getCompositeKeysCriteria($conn, $keys); $sql = "DELETE FROM {$table} WHERE {$criteria}"; $command = $conn->createCommand($sql); $command->prepare(); -- cgit v1.2.3