Commit da4b7afe by Qiang Xue

...

parent 0ba6b4bd
......@@ -188,8 +188,7 @@ class Object
*/
public function hasProperty($name, $checkVar = true)
{
return $this->canGetProperty($name, false) || $this->canSetProperty($name, false)
|| $checkVar && property_exists($this, $name);
return $this->canGetProperty($name, false) || $this->canSetProperty($name, false) || $checkVar && property_exists($this, $name);
}
/**
......
......@@ -3,6 +3,7 @@
namespace yii\db\ar;
use yii\db\Exception;
use yii\db\dao\TableSchema;
/**
* ActiveMetaData represents the meta-data for an Active Record class.
......@@ -28,19 +29,16 @@ class ActiveMetaData
public function __construct($modelClass)
{
$tableName = $modelClass::tableName();
$table = $modelClass::getDbConnection()->getDriver()->getTableSchema($tableName);
if ($table === null) {
$this->table = $modelClass::getDbConnection()->getDriver()->getTableSchema($tableName);
if ($this->table === null) {
throw new Exception("Unable to find table '$tableName' for ActiveRecord class '$modelClass'.");
}
if ($table->primaryKey === null) {
$primaryKey = $modelClass::primaryKey();
if ($primaryKey !== null) {
$table->fixPrimaryKey($primaryKey);
} else {
$this->table->fixPrimaryKey($primaryKey);
} elseif ($this->table->primaryKey === null) {
throw new Exception("The table '$tableName' for ActiveRecord class '$modelClass' does not have a primary key.");
}
}
$this->table = $table;
foreach ($modelClass::relations() as $name => $config) {
$this->addRelation($name, $config);
......
......@@ -12,6 +12,7 @@ namespace yii\db\ar;
use yii\base\VectorIterator;
use yii\db\dao\Query;
use yii\db\Exception;
/**
* ActiveFinder.php is ...
......@@ -44,32 +45,28 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
$this->query = new Query;
}
public function all($refresh = false)
public function all()
{
if ($this->records === null || $refresh) {
if ($this->records === null) {
$this->records = $this->findRecords();
}
return $this->records;
}
public function one($refresh = false, $limitOne = true)
public function one($limitOne = true)
{
if ($this->records === null || $refresh) {
if ($this->records === null) {
if ($limitOne) {
$this->limit(1);
}
$this->records = $this->findRecords();
}
if (isset($this->records[0])) {
return $this->records[0];
} else {
return null;
}
return isset($this->records[0]) ? $this->records[0] : null;
}
public function exists()
{
return $this->select(array('1'))->asArray(true)->one() !== null;
}
public function asArray($value = true)
......@@ -81,6 +78,10 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
public function with()
{
$this->with = func_get_args();
if (isset($this->with[0]) && is_array($this->with[0])) {
// the parameter is given as an array
$this->with = $this->with[0];
}
return $this;
}
......@@ -649,6 +650,11 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
return $this;
}
public function getParams()
{
return $this->query->params;
}
/**
* Sets the parameters to be bound to the query.
* @param array list of query parameter values indexed by parameter placeholders.
......@@ -677,21 +683,20 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
return $this;
}
public function joinWith()
{
// todo: inner join with one or multiple relations
}
protected function findRecords()
{
/*
* public $with;
*/
if (!empty($this->with)) {
// todo: handle findBySql() and limit cases
$this->initRelationalQuery();
}
if ($this->sql === null) {
if ($this->query->from === null) {
$modelClass = $this->modelClass;
$tableName = $modelClass::tableName();
if ($this->tableAlias !== null) {
$tableName .= ' ' . $this->tableAlias;
}
$this->query->from = array($tableName);
}
$this->initFrom($this->query);
$command = $this->query->createCommand($this->getDbConnection());
$this->sql = $command->getSql();
} else {
......@@ -731,14 +736,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
protected function performCountQuery()
{
if ($this->sql === null) {
if ($this->query->from === null) {
$modelClass = $this->modelClass;
$tableName = $modelClass::tableName();
if ($this->tableAlias !== null) {
$tableName .= ' ' . $this->tableAlias;
}
$this->query->from = array($tableName);
}
$this->initFrom($this->query);
$this->query->select = 'COUNT(*)';
$command = $this->query->createCommand($this->getDbConnection());
$this->sql = $command->getSql();
......@@ -748,4 +746,79 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
}
return $command->queryScalar();
}
protected function initFrom($query)
{
if ($query->from === null) {
$modelClass = $this->modelClass;
$tableName = $modelClass::tableName();
if ($this->tableAlias !== null) {
$tableName .= ' ' . $this->tableAlias;
}
$query->from = array($tableName);
}
}
protected function initRelationalQuery()
{
$joinTree = new JoinElement(null, null);
$joinCount = 0;
$this->buildJoinTree($joinTree, $this->with, $joinCount);
$query = new Query;
foreach ($joinTree->children as $child) {
$child->buildQuery($query);
}
}
/**
* @param JoinElement $parent
* @param array|string $with
* @param integer $joinCount
* @param array $config
* @return null|JoinElement
* @throws \yii\db\Exception
*/
protected function buildJoinTree($parent, $with, &$joinCount, $config = array())
{
if (is_array($with)) {
foreach ($with as $name => $value) {
if (is_string($value)) {
$this->buildJoinTree($parent, $value, $joinCount);
} elseif (is_string($name) && is_array($value)) {
$this->buildJoinTree($parent, $name, $joinCount, $value);
}
}
return null;
}
if (($pos = strrpos($with, '.')) !== false) {
$parent = $this->buildJoinTree($parent, substr($with, 0, $pos), $joinCount);
$with = substr($with, $pos + 1);
}
if (isset($parent->children[$with])) {
$child = $parent->children[$with];
} else {
$modelClass = $parent->relation->modelClass;
$relations = $modelClass::getMetaData()->relations;
if (!isset($relations[$with])) {
throw new Exception("$modelClass has no relation named '$with'.");
}
$relation = clone $relations[$with];
if ($relation->tableAlias === null) {
$relation->tableAlias = 't' . ($joinCount++);
}
$child = new JoinElement($parent, $relation);
}
foreach ($config as $name => $value) {
$child->relation->$name = $value;
}
if (!empty($child->relation->with)) {
$this->buildJoinTree($child, $child->relation->with, $joinCount);
}
return $child;
}
}
......@@ -68,10 +68,10 @@ abstract class ActiveRecord extends \yii\base\Model
} elseif ($q !== null) {
// query by primary key
$primaryKey = static::getMetaData()->table->primaryKey;
if (is_string($primaryKey)) {
$query->where(array($primaryKey => $q));
if (count($primaryKey) === 1) {
$query->where(array($primaryKey[0] => $q));
} else {
throw new Exception('Composite primary keys require multiple column values.');
throw new Exception('Multiple values are required to query by composite keys.');
}
}
return $query;
......@@ -1112,20 +1112,14 @@ abstract class ActiveRecord extends \yii\base\Model
public function getPrimaryKey()
{
$table = static::getMetaData()->table;
if (is_string($table->primaryKey)) {
return $this->{$table->primaryKey};
}
elseif (is_array($table->primaryKey))
{
if (count($table->primaryKey) === 1) {
return $this->{$table->primaryKey[0]};
} else {
$values = array();
foreach ($table->primaryKey as $name)
{
foreach ($table->primaryKey as $name) {
$values[$name] = $this->$name;
}
return $values;
} else
{
return null;
}
}
......@@ -1138,14 +1132,11 @@ abstract class ActiveRecord extends \yii\base\Model
public function setPrimaryKey($value)
{
$this->_pk = $this->getPrimaryKey();
$table = $this->getMetaData()->tableSchema;
if (is_string($table->primaryKey)) {
$this->{$table->primaryKey} = $value;
}
elseif (is_array($table->primaryKey))
{
foreach ($table->primaryKey as $name)
{
$table = $this->getMetaData()->table;
if (count($table->primaryKey) === 1) {
$this->{$table->primaryKey[0]} = $value;
} else {
foreach ($table->primaryKey as $name) {
$this->$name = $value[$name];
}
}
......
......@@ -2,42 +2,66 @@
namespace yii\db\ar;
class ActiveRelation extends \yii\db\dao\BaseQuery
class ActiveRelation extends \yii\base\Object
{
public $name;
public $modelClass;
public $hasMany;
public $joinType;
public $alias;
public $tableAlias;
public $on;
public $via;
public $index;
public $with;
public $scopes;
public function mergeWith($relation)
{
parent::mergeWith($relation);
if ($relation->joinType !== null) {
$this->joinType = $relation->joinType;
}
if ($relation->alias !== null) {
$this->alias = $relation->alias;
}
if ($relation->on !== null) {
if (!empty($this->on)) {
$this->on = "({$this->on}) AND ({$relation->on})";
} else {
$this->on = $relation->on;
}
}
if ($relation->via !== null) {
$this->via = $relation->via;
}
if ($relation->index !== null) {
$this->index = $relation->index;
}
// todo: with, scopes
}
/**
* @var string|array the columns being selected. This refers to the SELECT clause in a SQL
* statement. It can be either a string (e.g. `'id, name'`) or an array (e.g. `array('id', 'name')`).
* If not set, if means all columns.
* @see select()
*/
public $select;
/**
* @var string|array query condition. This refers to the WHERE clause in a SQL statement.
* For example, `age > 31 AND team = 1`.
* @see where()
*/
public $where;
/**
* @var integer maximum number of records to be returned. If not set or less than 0, it means no limit.
*/
public $limit;
/**
* @var integer zero-based offset from where the records are to be returned. If not set or
* less than 0, it means starting from the beginning.
*/
public $offset;
/**
* @var string|array how to sort the query results. This refers to the ORDER BY clause in a SQL statement.
* It can be either a string (e.g. `'id ASC, name DESC'`) or an array (e.g. `array('id ASC', 'name DESC')`).
*/
public $orderBy;
/**
* @var string|array how to group the query results. This refers to the GROUP BY clause in a SQL statement.
* It can be either a string (e.g. `'company, department'`) or an array (e.g. `array('company', 'department')`).
*/
public $groupBy;
/**
* @var string|array how to join with other tables. This refers to the JOIN clause in a SQL statement.
* It can either a string (e.g. `'LEFT JOIN tbl_user ON tbl_user.id=author_id'`) or an array (e.g.
* `array('LEFT JOIN tbl_user ON tbl_user.id=author_id', 'LEFT JOIN tbl_team ON tbl_team.id=team_id')`).
* @see join()
*/
public $join;
/**
* @var string|array the condition to be applied in the GROUP BY clause.
* It can be either a string or an array. Please refer to [[where()]] on how to specify the condition.
*/
public $having;
/**
* @var array list of query parameter values indexed by parameter placeholders.
* For example, `array(':name'=>'Dan', ':age'=>31)`.
*/
public $params;
}
<?php
/**
* ActiveQuery class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
use yii\base\VectorIterator;
use yii\db\dao\Query;
use yii\db\Exception;
class JoinElement extends \yii\base\Object
{
/**
* @var ActiveRelation
*/
public $relation;
/**
* @var JoinElement
*/
public $parent;
/**
* @var JoinElement[]
*/
public $children = array();
public $columnAliases = array(); // alias => original name
public $pkAlias = array(); // original name => alias
public $records;
public $relatedRecords;
public function __construct($parent, $relation)
{
if ($parent !== null) {
$this->parent = $parent;
$this->relation = $relation;
$parent->children[$relation->name] = $this;
}
}
public function populateData($row)
{
$pk = array();
foreach ($this->pkAlias as $alias) {
if (isset($row[$alias])) {
$pk[] = $row[$alias];
} else {
return null;
}
}
$pk = count($pk) === 1 ? $pk[0] : serialize($pk);
// create active record
if (isset($this->records[$pk])) {
$record = $this->records[$pk];
} else {
$attributes = array();
foreach ($row as $alias => $value) {
if (isset($this->columnAliases[$alias])) {
$attributes[$this->columnAliases[$alias]] = $value;
}
}
$modelClass = $this->relation->modelClass;
$record = $modelClass::populateRecord($attributes);
foreach ($this->children as $child) {
if ($child->relation->select !== false) {
$record->initRelation($child->relation);
}
}
$this->records[$pk] = $record;
}
// populate child records
foreach ($this->children as $child) {
if ($child->relation->select === false) {
continue;
}
$childRecord = $child->populateData($row);
if ($childRecord === null) {
continue;
}
if ($child->relation->hasMany) {
$fpk = serialize($childRecord->getPrimaryKey());
if (isset($this->relatedRecords[$pk][$child->relation->name][$fpk])) {
continue;
}
$this->relatedRecords[$pk][$child->relation->name][$fpk] = true;
}
$record->addRelatedRecord($child->relation, $childRecord);
}
return $record;
}
public function buildQuery($query)
{
$tokens = array(
'@.' => $this->relation->tableAlias,
'?.' => $this->parent->relation->tableAlias,
);
foreach ($this->buildSelect() as $column) {
$query->select[] = strtr($column, $tokens);
}
if ($this->relation->where !== null) {
$query->where[] = strtr($this->relation->where, $tokens);
}
if ($this->relation->having !== null) {
$query->having[] = strtr($this->relation->having, $tokens);
}
/*
* joinType;
on;
via;
orderby
groupby
join
params
*/
foreach ($this->children as $child) {
$child->buildQuery($query);
}
}
public function buildSelect()
{
$modelClass = $this->relation->modelClass;
$tableSchema = $modelClass::getMetaData()->table;
$select = $this->relation->select;
$columns = array();
$columnCount = 0;
if (empty($select) || $select === '*') {
foreach ($tableSchema->columns as $column) {
$alias = $this->tableAlias . '_' . ($columnCount++);
$columns[] = "{$column->name} AS $alias";
$this->columnAliases[$alias] = $column->name;
if ($column->isPrimaryKey) {
$this->pkAlias[$column->name] = $alias;
}
}
} else {
if (is_string($select)) {
$select = explode(',', $select);
}
foreach ($tableSchema->primaryKey as $column) {
$alias = $this->tableAlias . '_' . ($columnCount++);
$columns[] = "$column AS $alias";
$this->pkAlias[$column] = $alias;
}
foreach ($select as $column) {
$column = trim($column);
if (preg_match('/^(.*?)\s+AS\s+(\w+)$/im', $column, $matches)) {
// if the column is already aliased
$this->columnAliases[$matches[2]] = $matches[2];
$columns[] = $column;
} elseif (!isset($this->pkAlias[$column])) {
$alias = $this->tableAlias . '_' . ($columnCount++);
$columns[] = "$column AS $alias";
$this->columnAliases[$alias] = $column;
}
}
}
return $columns;
}
}
\ No newline at end of file
......@@ -377,7 +377,7 @@ class Query extends \yii\base\Object
/**
* Sets the SELECT part of the query.
* @param mixed $columns the columns to be selected. Defaults to '*', meaning all columns.
* @param string|array $columns the columns to be selected. Defaults to '*', meaning all columns.
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. array('id', 'name')).
* Columns can contain table prefixes (e.g. "tbl_user.id") and/or column aliases (e.g. "tbl_user.id AS user_id").
* The method will automatically quote the column names unless a column contains some parenthesis
......@@ -882,7 +882,7 @@ class Query extends \yii\base\Object
* The merging is done according to the following rules:
*
* - [[select]]: the union of both queries' [[select]] property values.
* - [[selectOption]], [[distinct]], [[limit]], [[offset]]: the new query
* - [[selectOption]], [[distinct]], [[from]], [[limit]], [[offset]]: the new query
* takes precedence over this query.
* - [[where]], [[having]]: the new query's corresponding property value
* will be 'AND' together with the existing one.
......@@ -913,6 +913,10 @@ class Query extends \yii\base\Object
$this->distinct = $query->distinct;
}
if ($query->from !== null) {
$this->from = $query->from;
}
if ($query->limit !== null) {
$this->limit = $query->limit;
}
......
......@@ -41,9 +41,9 @@ class TableSchema extends \yii\base\Object
*/
public $quotedName;
/**
* @var string|array primary key name of this table. If composite key, an array of key names is returned.
* @var array primary keys of this table.
*/
public $primaryKey;
public $primaryKey = array();
/**
* @var string sequence name for the primary key. Null if no sequence.
*/
......@@ -85,11 +85,20 @@ class TableSchema extends \yii\base\Object
return array_keys($this->columns);
}
/**
* Manually specifies the primary key for this table.
* @param string|array $keys the primary key (can be composite)
* @throws \yii\db\Exception if the specified key cannot be found in the table.
*/
public function fixPrimaryKey($keys)
{
if (!is_array($keys)) {
$keys = array($keys);
}
$this->primaryKey = $keys;
foreach ($this->columns as $column) {
$column->isPrimaryKey = false;
}
foreach ($keys as $key) {
if (isset($this->columns[$key])) {
$this->columns[$key]->isPrimaryKey = true;
......
......@@ -212,13 +212,7 @@ class Driver extends \yii\db\dao\Driver
$column = $this->createColumn($column);
$table->columns[$column->name] = $column;
if ($column->isPrimaryKey) {
if ($table->primaryKey === null) {
$table->primaryKey = $column->name;
} elseif (is_string($table->primaryKey)) {
$table->primaryKey = array($table->primaryKey, $column->name);
} else {
$table->primaryKey[] = $column->name;
}
if ($column->autoIncrement) {
$table->sequenceName = '';
}
......
......@@ -108,18 +108,12 @@ class Driver extends \yii\db\dao\Driver
$column = $this->createColumn($column);
$table->columns[$column->name] = $column;
if ($column->isPrimaryKey) {
if ($table->primaryKey === null) {
$table->primaryKey = $column->name;
} elseif (is_string($table->primaryKey)) {
$table->primaryKey = array($table->primaryKey, $column->name);
} else {
$table->primaryKey[] = $column->name;
}
}
}
if (is_string($table->primaryKey) && !strncasecmp($table->columns[$table->primaryKey]->dbType, 'int', 3)) {
if (count($table->primaryKey) === 1 && !strncasecmp($table->columns[$table->primaryKey[0]]->dbType, 'int', 3)) {
$table->sequenceName = '';
$table->columns[$table->primaryKey]->autoIncrement = true;
$table->columns[$table->primaryKey[0]]->autoIncrement = true;
}
return true;
......
......@@ -23,23 +23,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
/**
* @var array mapping from abstract column types (keys) to physical column types (values).
*/
public $typeMap = array(
Driver::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
Driver::TYPE_STRING => 'varchar(255)',
Driver::TYPE_TEXT => 'text',
Driver::TYPE_SMALLINT => 'smallint',
Driver::TYPE_INTEGER => 'integer',
Driver::TYPE_BIGINT => 'bigint',
Driver::TYPE_FLOAT => 'float',
Driver::TYPE_DECIMAL => 'decimal',
Driver::TYPE_DATETIME => 'datetime',
Driver::TYPE_TIMESTAMP => 'timestamp',
Driver::TYPE_TIME => 'time',
Driver::TYPE_DATE => 'date',
Driver::TYPE_BINARY => 'blob',
Driver::TYPE_BOOLEAN => 'tinyint(1)',
Driver::TYPE_MONEY => 'decimal(19,4)',
);
public $typeMap = array(Driver::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', Driver::TYPE_STRING => 'varchar(255)', Driver::TYPE_TEXT => 'text', Driver::TYPE_SMALLINT => 'smallint', Driver::TYPE_INTEGER => 'integer', Driver::TYPE_BIGINT => 'bigint', Driver::TYPE_FLOAT => 'float', Driver::TYPE_DECIMAL => 'decimal', Driver::TYPE_DATETIME => 'datetime', Driver::TYPE_TIMESTAMP => 'timestamp', Driver::TYPE_TIME => 'time', Driver::TYPE_DATE => 'date', Driver::TYPE_BINARY => 'blob', Driver::TYPE_BOOLEAN => 'tinyint(1)', Driver::TYPE_MONEY => 'decimal(19,4)',);
/**
* Resets the sequence value of a table's primary key.
......@@ -53,9 +37,8 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
{
if ($table->sequenceName !== null) {
if ($value === null) {
$value = $this->connection->createCommand("SELECT MAX(`{$table->primaryKey}`) FROM {$table->quotedName}")->queryScalar();
}
else {
$value = $this->connection->createCommand("SELECT MAX(`{$table->primaryKey[0]}`) FROM {$table->quotedName}")->queryScalar();
} else {
$value = (int)$value - 1;
}
try {
......
......@@ -6,7 +6,7 @@ class MysqlTestCase extends TestCase
{
function __construct()
{
if(!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
$this->markTestSkipped('pdo and pdo_mysql extensions are required.');
}
}
......
......@@ -62,13 +62,13 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals('user2', $customer->name);
// find by attributes
$customer = Customer::find(array('name'=>'user2'))->one();
$customer = Customer::find(array('name' => 'user2'))->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(2, $customer->id);
// find by Query
$query = new Query;
$query->where('id=:id', array(':id'=>2));
$query->where('id=:id', array(':id' => 2));
$customer = Customer::find($query)->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
......@@ -89,7 +89,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals(3, count($customers));
// find with parameter binding
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id'=>2))->one();
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
......@@ -116,7 +116,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals(3, $customer->id);
$this->assertEquals(null, $customer->name);
}
/*
/*
public function testGetSql()
{
// sql for all
......@@ -140,10 +140,10 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
public function testMisc()
{
// Customer::exists()
// Customer::updateAll()
// Customer::updateCounters()
// Customer::deleteAll()
// Customer::exists()
// Customer::updateAll()
// Customer::updateCounters()
// Customer::deleteAll()
}
public function testInsert()
......@@ -171,5 +171,5 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
{
}
*/
*/
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment