Commit 2f37d093 by Qiang Xue

...

parent 95135f11
......@@ -179,7 +179,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an iterator.
*/
public function fromArray($data)
public function copyFrom($data)
{
if (is_array($data) || $data instanceof \Traversable) {
if ($this->_d !== array()) {
......
......@@ -342,27 +342,4 @@ class Object
return $object;
}
/**
* Configures the object properties with the specified array.
* @param array $array name-value pairs to be used to initialize the properties of this object.
* @return Object the object itself
*/
public function fromArray($array)
{
foreach ($array as $name => $value) {
$this->$name = $value;
}
return $this;
}
/**
* Returns the object in terms of an array.
* The default implementation will return the result of PHP function `get_object_vars()`.
* @return array the array representation of this object.
*/
public function toArray()
{
return get_object_vars($this);
}
}
......@@ -240,7 +240,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an object implementing `Traversable`.
*/
public function fromArray($data)
public function copyFrom($data)
{
if (is_array($data) || $data instanceof \Traversable) {
if ($this->_c > 0) {
......
<?php
/**
* ActiveQueryBuilder class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
/**
* ActiveQueryBuilder is ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveQueryBuilder extends \yii\base\Object
{
}
\ No newline at end of file
......@@ -10,6 +10,8 @@
namespace yii\db\dao;
use yii\db\Exception;
/**
* TableSchema represents the metadata of a database table.
*
......@@ -82,4 +84,18 @@ class TableSchema extends \yii\base\Object
{
return array_keys($this->columns);
}
public function fixPrimaryKey($keys)
{
if (!is_array($keys)) {
$keys = array($keys);
}
foreach ($keys as $key) {
if (isset($this->columns[$key])) {
$this->columns[$key]->isPrimaryKey = true;
} else {
throw new Exception("Primary key '$key' cannot be found in table '{$this->name}'.");
}
}
}
}
......@@ -14,6 +14,8 @@ namespace yii\util;
/**
* Text helper
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class Text
......@@ -23,9 +25,9 @@ class Text
* @param string $name the word to be pluralized
* @return string the pluralized word
*/
public function pluralize($name)
public static function pluralize($name)
{
$rules=array(
$rules = array(
'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/(m)an$/i' => '\1en',
......@@ -33,11 +35,17 @@ class Text
'/(r)y$/i' => '\1ies',
'/s$/' => 's',
);
foreach($rules as $rule=>$replacement)
foreach ($rules as $rule => $replacement)
{
if(preg_match($rule,$name))
return preg_replace($rule,$replacement,$name);
if (preg_match($rule, $name)) {
return preg_replace($rule, $replacement, $name);
}
}
return $name.'s';
return $name . 's';
}
public static function dd($value)
{
return trim(strtolower(str_replace(array('-', '_', '.'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $value))));
}
}
......@@ -86,14 +86,14 @@ class DictionaryTest extends \yiiunit\TestCase
public function testFromArray()
{
$array=array('key3'=>$this->item3,'key4'=>$this->item1);
$this->dictionary->fromArray($array);
$this->dictionary->copyFrom($array);
$this->assertEquals(2, $this->dictionary->getCount());
$this->assertEquals($this->item3, $this->dictionary['key3']);
$this->assertEquals($this->item1, $this->dictionary['key4']);
$this->setExpectedException('yii\base\Exception');
$this->dictionary->fromArray($this);
$this->dictionary->copyFrom($this);
}
public function testMergeWith()
......
......@@ -117,7 +117,6 @@ class ObjectTest extends \yiiunit\TestCase
$this->assertTrue(empty($this->object->Text));
}
public function testEvaluateExpression()
{
$object = new NewObject;
......
......@@ -113,10 +113,10 @@ class VectorTest extends \yiiunit\TestCase
public function testFromArray()
{
$array=array($this->item3,$this->item1);
$this->vector->fromArray($array);
$this->vector->copyFrom($array);
$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1);
$this->setExpectedException('yii\base\Exception');
$this->vector->fromArray($this);
$this->vector->copyFrom($this);
}
public function testMergeWith()
......
......@@ -18,33 +18,33 @@ class CommandTest extends \yiiunit\MysqlTestCase
$this->assertEquals(null, $command->sql);
// string
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql);
// Query object
$query = new Query;
$query->select('id')->from('tbl_user');
$query->select('id')->from('tbl_customer');
$command = $db->createCommand($query);
$this->assertEquals("SELECT `id` FROM `tbl_user`", $command->sql);
$this->assertEquals("SELECT `id` FROM `tbl_customer`", $command->sql);
// array
$command = $db->createCommand(array(
'select' => 'name',
'from' => 'tbl_user',
'from' => 'tbl_customer',
));
$this->assertEquals("SELECT `name` FROM `tbl_user`", $command->sql);
$this->assertEquals("SELECT `name` FROM `tbl_customer`", $command->sql);
}
function testGetSetSql()
{
$db = $this->getConnection(false);
$sql = 'SELECT * FROM yii_user';
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql);
$sql2 = 'SELECT * FROM yii_yii_post';
$sql2 = 'SELECT * FROM tbl_order';
$command->sql = $sql2;
$this->assertEquals($sql2, $command->sql);
}
......@@ -53,7 +53,7 @@ class CommandTest extends \yiiunit\MysqlTestCase
{
$db = $this->getConnection(false);
$command = $db->createCommand('SELECT * FROM yii_user');
$command = $db->createCommand('SELECT * FROM tbl_customer');
$this->assertEquals(null, $command->pdoStatement);
$command->prepare();
$this->assertNotEquals(null, $command->pdoStatement);
......@@ -65,11 +65,11 @@ class CommandTest extends \yiiunit\MysqlTestCase
{
$db = $this->getConnection();
$sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (\'test comment\', 1, 1)';
$sql = 'INSERT INTO tbl_customer(email, name , address) VALUES (\'user4@example.com\', \'user4\', \'address4\')';
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->execute());
$sql = 'SELECT COUNT(*) FROM yii_comment WHERE content=\'test comment\'';
$sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\'';
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->queryScalar());
......@@ -83,55 +83,55 @@ class CommandTest extends \yiiunit\MysqlTestCase
$db = $this->getConnection();
// query
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer';
$reader = $db->createCommand($sql)->query();
$this->assertTrue($reader instanceof DataReader);
// queryAll
$rows = $db->createCommand('SELECT * FROM yii_post')->queryAll();
$this->assertEquals(5, count($rows));
$rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll();
$this->assertEquals(3, count($rows));
$row = $rows[2];
$this->assertEquals(3, $row['id']);
$this->assertEquals($row['title'], 'post 3');
$this->assertEquals('user3', $row['name']);
$rows = $db->createCommand('SELECT * FROM yii_post WHERE id=10')->queryAll();
$rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll();
$this->assertEquals(array(), $rows);
// queryRow
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
$row = $db->createCommand($sql)->queryRow();
$this->assertEquals(1, $row['id']);
$this->assertEquals('post 1', $row['title'], 'post 1');
$this->assertEquals('user1', $row['name']);
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
$command = $db->createCommand($sql);
$command->prepare();
$row = $command->queryRow();
$this->assertEquals(1, $row['id']);
$this->assertEquals('post 1', $row['title']);
$this->assertEquals('user1', $row['name']);
$sql = 'SELECT * FROM yii_post WHERE id=10';
$sql = 'SELECT * FROM tbl_customer WHERE id=10';
$command = $db->createCommand($sql);
$this->assertFalse($command->queryRow());
// queryColumn
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer';
$column = $db->createCommand($sql)->queryColumn();
$this->assertEquals(range(1, 5), $column);
$this->assertEquals(range(1, 3), $column);
$command = $db->createCommand('SELECT id FROM yii_post WHERE id=10');
$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
$this->assertEquals(array(), $command->queryColumn());
// queryScalar
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
$this->assertEquals($db->createCommand($sql)->queryScalar(), 1);
$sql = 'SELECT id FROM yii_post';
$sql = 'SELECT id FROM tbl_customer ORDER BY id';
$command = $db->createCommand($sql);
$command->prepare();
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT id FROM yii_post WHERE id=10');
$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
$this->assertFalse($command->queryScalar());
$command = $db->createCommand('bad SQL');
......@@ -144,20 +144,22 @@ class CommandTest extends \yiiunit\MysqlTestCase
$db = $this->getConnection();
// bindParam
$sql = 'INSERT INTO yii_post(title,create_time,author_id) VALUES (:title, :create_time, 1)';
$command = $db->createCommand($sql);
$title = 'test title';
$createTime = time();
$command->bindParam(':title', $title);
$command->bindParam(':create_time', $createTime);
$sql = 'INSERT INTO tbl_customer(email,name,address) VALUES (:email, :name, :address)';
$command = $db->createCommand($sql);
$email = 'user4@example.com';
$name = 'user4';
$address = 'address4';
$command->bindParam(':email', $email);
$command->bindParam(':name', $name);
$command->bindParam(':address', $address);
$command->execute();
$sql = 'SELECT create_time FROM yii_post WHERE title=:title';
$sql = 'SELECT name FROM tbl_customer WHERE email=:email';
$command = $db->createCommand($sql);
$command->bindParam(':title', $title);
$this->assertEquals($createTime, $command->queryScalar());
$command->bindParam(':email', $email);
$this->assertEquals($name, $command->queryScalar());
$sql = 'INSERT INTO yii_type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)';
$sql = 'INSERT INTO tbl_type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)';
$command = $db->createCommand($sql);
$intCol = 123;
$charCol = 'abc';
......@@ -173,7 +175,7 @@ class CommandTest extends \yiiunit\MysqlTestCase
$command->bindParam(':bool_col', $boolCol);
$this->assertEquals(1, $command->execute());
$sql = 'SELECT * FROM yii_type';
$sql = 'SELECT * FROM tbl_type';
$row = $db->createCommand($sql)->queryRow();
$this->assertEquals($intCol, $row['int_col']);
$this->assertEquals($charCol, $row['char_col']);
......@@ -182,23 +184,23 @@ class CommandTest extends \yiiunit\MysqlTestCase
$this->assertEquals($numericCol, $row['numeric_col']);
// bindValue
$sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (:content, 1, 1)';
$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
$command = $db->createCommand($sql);
$command->bindValue(':content', 'test comment');
$command->bindValue(':email', 'user5@example.com');
$command->execute();
$sql = 'SELECT post_id FROM yii_comment WHERE content=:content';
$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
$command = $db->createCommand($sql);
$command->bindValue(':content', 'test comment');
$this->assertEquals(1, $command->queryScalar());
$command->bindValue(':name', 'user5');
$this->assertEquals('user5@example.com', $command->queryScalar());
// bind value via query or execute method
$sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (:content, 1, 1)';
$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user6\', \'address6\')';
$command = $db->createCommand($sql);
$command->execute(array(':content' => 'test comment2'));
$sql = 'SELECT post_id FROM yii_comment WHERE content=:content';
$command->execute(array(':email' => 'user6@example.com'));
$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->queryScalar(array(':content' => 'test comment2')));
$this->assertEquals('user5@example.com', $command->queryScalar(array(':name' => 'user5')));
}
function testFetchMode()
......@@ -206,20 +208,20 @@ class CommandTest extends \yiiunit\MysqlTestCase
$db = $this->getConnection();
// default: FETCH_ASSOC
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$result = $command->queryRow();
$this->assertTrue(is_array($result) && isset($result['id']));
// FETCH_OBJ, customized via fetchMode property
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$command->fetchMode = \PDO::FETCH_OBJ;
$result = $command->queryRow();
$this->assertTrue(is_object($result));
// FETCH_NUM, customized in query method
$sql = 'SELECT * FROM yii_post';
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$result = $command->queryRow(array(), \PDO::FETCH_NUM);
$this->assertTrue(is_array($result) && isset($result[0]));
......
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