From 4ba05468a024f225b352cb716df85753f22b4c0d Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 25 Dec 2011 18:57:54 -0500 Subject: [PATCH] ... --- framework/db/dao/ColumnSchema.php | 2 +- framework/db/dao/Command.php | 11 +- framework/db/dao/Connection.php | 3 +- framework/db/dao/Query.php | 4 + framework/db/dao/QueryBuilder.php | 17 +- framework/db/dao/Schema.php | 22 +- framework/db/dao/TableSchema.php | 10 - framework/db/dao/Transaction.php | 3 +- framework/db/dao/mysql/QueryBuilder.php | 32 +-- framework/db/dao/mysql/Schema.php | 10 +- tests/unit/data/mysql.sql | 12 +- tests/unit/framework/db/dao/CommandTest.php | 339 ++++++++++++---------------- 12 files changed, 201 insertions(+), 264 deletions(-) diff --git a/framework/db/dao/ColumnSchema.php b/framework/db/dao/ColumnSchema.php index f7566b9..2c4d8ff 100644 --- a/framework/db/dao/ColumnSchema.php +++ b/framework/db/dao/ColumnSchema.php @@ -32,7 +32,7 @@ class ColumnSchema extends \yii\base\Component public $allowNull; /** * @var string logical type of this column. Possible logic types include: - * string, text, boolean, smallint, integer, bigint, float, decimal, datetime, timestamp, time, date, binary + * string, text, boolean, smallint, integer, bigint, float, decimal, datetime, timestamp, time, date, binary, money */ public $type; /** diff --git a/framework/db/dao/Command.php b/framework/db/dao/Command.php index 3715390..0159a78 100644 --- a/framework/db/dao/Command.php +++ b/framework/db/dao/Command.php @@ -163,8 +163,7 @@ class Command extends \yii\base\Component $sql = $this->getSql(); try { $this->pdoStatement = $this->connection->pdo->prepare($sql); - } - catch (\Exception $e) { + } catch (\Exception $e) { \Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __CLASS__); $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; throw new Exception($e->getMessage(), (int)$e->getCode(), $errorInfo); @@ -289,8 +288,7 @@ class Command extends \yii\base\Component \Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); } return $n; - } - catch (Exception $e) { + } catch (\Exception $e) { if ($this->connection->enableProfiling) { \Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); } @@ -360,7 +358,7 @@ class Command extends \yii\base\Component */ public function queryScalar($params = array()) { - $result = $this->queryInternal('fetchColumn', $params); + $result = $this->queryInternal('fetchColumn', $params, 0); if (is_resource($result) && get_resource_type($result) === 'stream') { return stream_get_contents($result); } else { @@ -452,8 +450,7 @@ class Command extends \yii\base\Component } return $result; - } - catch (Exception $e) { + } catch (\Exception $e) { if ($db->enableProfiling) { \Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); } diff --git a/framework/db/dao/Connection.php b/framework/db/dao/Connection.php index efdf53a..a9855bc 100644 --- a/framework/db/dao/Connection.php +++ b/framework/db/dao/Connection.php @@ -61,8 +61,7 @@ use yii\db\Exception; * $connection->createCommand($sql2)->execute(); * // ... executing other SQL statements ... * $transaction->commit(); - * } - * catch(Exception $e) { + * } catch(Exception $e) { * $transaction->rollBack(); * } * ~~~ diff --git a/framework/db/dao/Query.php b/framework/db/dao/Query.php index 6c30acb..fec9672 100644 --- a/framework/db/dao/Query.php +++ b/framework/db/dao/Query.php @@ -77,6 +77,10 @@ class Query extends \yii\base\Object public $union; + /** + * @param Connection $connection + * @return string + */ public function getSql($connection) { return $connection->getQueryBuilder()->build($this); diff --git a/framework/db/dao/QueryBuilder.php b/framework/db/dao/QueryBuilder.php index 8aad9cc..ad1c3cb 100644 --- a/framework/db/dao/QueryBuilder.php +++ b/framework/db/dao/QueryBuilder.php @@ -22,22 +22,9 @@ class QueryBuilder extends \yii\base\Object { /** * @var array the abstract column types mapped to physical column types. + * Child classes should override this property to declare possible type mappings. */ - public $typeMap = array( - 'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'string' => 'varchar(255)', - 'text' => 'text', - 'integer' => 'int(11)', - 'float' => 'float', - 'decimal' => 'decimal', - 'datetime' => 'datetime', - 'timestamp' => 'timestamp', - 'time' => 'time', - 'date' => 'date', - 'binary' => 'blob', - 'boolean' => 'tinyint(1)', - 'money' => 'decimal(19,4)', - ); + public $typeMap = array(); /** * @var Connection the database connection. */ diff --git a/framework/db/dao/Schema.php b/framework/db/dao/Schema.php index 6f45a5c..dbd76fc 100644 --- a/framework/db/dao/Schema.php +++ b/framework/db/dao/Schema.php @@ -4,7 +4,7 @@ * * @author Qiang Xue * @link http://www.yiiframework.com/ - * @copyright Copyright © 2008-2011 Yii Software LLC + * @copyright Copyright © 2008-2012 Yii Software LLC * @license http://www.yiiframework.com/license/ */ @@ -20,6 +20,22 @@ use yii\db\Exception; */ abstract class Schema extends \yii\base\Object { + const TYPE_PK = 'pk'; + const TYPE_STRING = 'string'; + const TYPE_TEXT = 'text'; + const TYPE_SMALLINT = 'smallint'; + const TYPE_INTEGER = 'integer'; + const TYPE_BIGINT = 'bigint'; + const TYPE_FLOAT = 'float'; + const TYPE_DECIMAL = 'decimal'; + const TYPE_DATETIME = 'datetime'; + const TYPE_TIMESTAMP = 'timestamp'; + const TYPE_TIME = 'time'; + const TYPE_DATE = 'date'; + const TYPE_BINARY = 'binary'; + const TYPE_BOOLEAN = 'boolean'; + const TYPE_MONEY = 'money'; + /** * @var \yii\db\dao\Connection the database connection */ @@ -38,7 +54,7 @@ abstract class Schema extends \yii\base\Object /** * Constructor. - * @param CDbConnection $connection database connection. + * @param Connection $connection database connection. */ public function __construct($connection) { @@ -48,7 +64,7 @@ abstract class Schema extends \yii\base\Object /** * Obtains the metadata for the named table. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name. - * @return CDbTableSchema table metadata. Null if the named table does not exist. + * @return TableSchema table metadata. Null if the named table does not exist. */ public function getTableSchema($name) { diff --git a/framework/db/dao/TableSchema.php b/framework/db/dao/TableSchema.php index 6ad64db..cfd475a 100644 --- a/framework/db/dao/TableSchema.php +++ b/framework/db/dao/TableSchema.php @@ -15,16 +15,6 @@ namespace yii\db\dao; * * It may be extended by different DBMS driver to provide DBMS-specific table metadata. * - * TableSchema provides the following information about a table: - * - * * @author Qiang Xue * @since 2.0 */ diff --git a/framework/db/dao/Transaction.php b/framework/db/dao/Transaction.php index 0e815f0..fc261b1 100644 --- a/framework/db/dao/Transaction.php +++ b/framework/db/dao/Transaction.php @@ -27,8 +27,7 @@ use yii\db\Exception; * $connection->createCommand($sql2)->execute(); * //.... other SQL executions * $transaction->commit(); - * } - * catch(Exception $e) { + * } catch(Exception $e) { * $transaction->rollBack(); * } * ~~~ diff --git a/framework/db/dao/mysql/QueryBuilder.php b/framework/db/dao/mysql/QueryBuilder.php index 0c4791c..e5b241b 100644 --- a/framework/db/dao/mysql/QueryBuilder.php +++ b/framework/db/dao/mysql/QueryBuilder.php @@ -21,22 +21,22 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder /** * @var array the abstract column types mapped to physical column types. */ - public $columnTypes = array( - 'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'string' => 'varchar(255)', - 'text' => 'text', - 'smallint' => 'smallint', - 'integer' => 'int(11)', - 'bigint'=> 'bigint', - 'boolean' => 'tinyint(1)', - 'float' => 'float', - 'decimal' => 'decimal', - 'money' => 'decimal(19,4)', - 'datetime' => 'datetime', - 'timestamp' => 'timestamp', - 'time' => 'time', - 'date' => 'date', - 'binary' => 'blob', + public $typeMap = array( + Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + Schema::TYPE_STRING => 'varchar(255)', + Schema::TYPE_TEXT => 'text', + Schema::TYPE_SMALLINT => 'smallint(6)', + Schema::TYPE_INTEGER => 'int(11)', + Schema::TYPE_BIGINT => 'bigint(20)', + Schema::TYPE_FLOAT => 'float', + Schema::TYPE_DECIMAL => 'decimal', + Schema::TYPE_DATETIME => 'datetime', + Schema::TYPE_TIMESTAMP => 'timestamp', + Schema::TYPE_TIME => 'time', + Schema::TYPE_DATE => 'date', + Schema::TYPE_BINARY => 'blob', + Schema::TYPE_BOOLEAN => 'tinyint(1)', + Schema::TYPE_MONEY => 'decimal(19,4)', ); /** diff --git a/framework/db/dao/mysql/Schema.php b/framework/db/dao/mysql/Schema.php index a98510b..0f28e98 100644 --- a/framework/db/dao/mysql/Schema.php +++ b/framework/db/dao/mysql/Schema.php @@ -10,6 +10,8 @@ namespace yii\db\dao\mysql; +use yii\db\dao\TableSchema; + /** * Schema is the class for retrieving metadata information from a MySQL database (version 4.1.x and 5.x). * @@ -43,7 +45,7 @@ class Schema extends \yii\db\dao\Schema /** * Loads the metadata for the specified table. * @param string $name table name - * @return TableSchema driver dependent table metadata. Null if the table does not exist. + * @return \yii\db\dao\TableSchema driver dependent table metadata. Null if the table does not exist. */ protected function loadTableSchema($name) { @@ -58,7 +60,7 @@ class Schema extends \yii\db\dao\Schema /** * Generates various kinds of table names. - * @param CMysqlTableSchema $table the table instance + * @param \yii\db\dao\TableSchema $table the table instance * @param string $name the unquoted table name */ protected function resolveTableNames($table, $name) @@ -96,7 +98,7 @@ class Schema extends \yii\db\dao\Schema /** * Collects the table column metadata. - * @param CMysqlTableSchema $table the table metadata + * @param \yii\db\dao\TableSchema $table the table metadata * @return boolean whether the table exists in the database */ protected function findColumns($table) @@ -128,7 +130,7 @@ class Schema extends \yii\db\dao\Schema /** * Collects the foreign key column details for the given table. - * @param CMysqlTableSchema $table the table metadata + * @param \yii\db\dao\TableSchema $table the table metadata */ protected function findConstraints($table) { diff --git a/tests/unit/data/mysql.sql b/tests/unit/data/mysql.sql index 7b77af2..ae5ac94 100644 --- a/tests/unit/data/mysql.sql +++ b/tests/unit/data/mysql.sql @@ -43,18 +43,18 @@ CREATE TABLE yii_post ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, title VARCHAR(128) NOT NULL, - create_time TIMESTAMP NOT NULL, + create_time INTEGER NOT NULL, author_id INTEGER NOT NULL, content TEXT, CONSTRAINT FK_post_author FOREIGN KEY (author_id) REFERENCES yii_user (id) ON DELETE CASCADE ON UPDATE RESTRICT ) TYPE=INNODB; -INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 1','2000-01-01',1,'content 1'); -INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 2','2000-01-02',2,'content 2'); -INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 3','2000-01-03',2,'content 3'); -INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 4','2000-01-04',2,'content 4'); -INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 5','2000-01-05',3,'content 5'); +INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 1','1324854194',1,'content 1'); +INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 2','1324855194',2,'content 2'); +INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 3','1324856194',2,'content 3'); +INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 4','1324857194',2,'content 4'); +INSERT INTO yii_post (title, create_time, author_id, content) VALUES ('post 5','1324858194',3,'content 5'); CREATE TABLE yii_comment ( diff --git a/tests/unit/framework/db/dao/CommandTest.php b/tests/unit/framework/db/dao/CommandTest.php index fb2b561..2641e83 100644 --- a/tests/unit/framework/db/dao/CommandTest.php +++ b/tests/unit/framework/db/dao/CommandTest.php @@ -5,6 +5,7 @@ namespace yiiunit\framework\db\dao; use yii\db\dao\Connection; use yii\db\dao\Command; use yii\db\dao\Query; +use yii\db\dao\DataReader; class CommandTest extends \yiiunit\MysqlTestCase { @@ -15,7 +16,7 @@ class CommandTest extends \yiiunit\MysqlTestCase $command = $db->createCommand(); $this->assertEquals("SELECT *\nFROM ", $command->sql); - $sql='SELECT * FROM posts'; + $sql = 'SELECT * FROM yii_post'; $command = $db->createCommand($sql); $this->assertEquals($sql, $command->sql); @@ -23,7 +24,7 @@ class CommandTest extends \yiiunit\MysqlTestCase $command = $db->createCommand($query); $this->assertEquals($query, $command->query); - $query = array('select'=>'id', 'from'=>'posts'); + $query = array('select' => 'id', 'from' => 'yii_post'); $command = $db->createCommand($query); $this->assertEquals($query, $command->query->toArray()); } @@ -44,248 +45,190 @@ class CommandTest extends \yiiunit\MysqlTestCase function testGetSetSql() { + $db = $this->getConnection(false); - } - - function testPrepare() - { - - } - - function testBindParam() - { + $sql = 'SELECT * FROM yii_user'; + $command = $db->createCommand($sql); + $this->assertEquals($sql, $command->sql); + $sql2 = 'SELECT * FROM yii_yii_post'; + $command->sql = $sql2; + $this->assertEquals($sql2, $command->sql); } - function testBindValue() + function testPrepareCancel() { + $db = $this->getConnection(false); + $command = $db->createCommand('SELECT * FROM yii_user'); + $this->assertEquals(null, $command->pdoStatement); + $command->prepare(); + $this->assertNotEquals(null, $command->pdoStatement); + $command->cancel(); + $this->assertEquals(null, $command->pdoStatement); } function testExecute() { + $db = $this->getConnection(); - } - - function testQuery() - { - - } - - function testQueryRow() - { - - } + $sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (\'test comment\', 1, 1)'; + $command = $db->createCommand($sql); + $this->assertEquals(1, $command->execute()); - function testQueryAll() - { + $sql = 'SELECT COUNT(*) FROM yii_comment WHERE content=\'test comment\''; + $command = $db->createCommand($sql); + $this->assertEquals(1, $command->queryScalar()); + $command = $db->createCommand('bad SQL'); + $this->setExpectedException('\yii\db\Exception'); + $command->execute(); } - function testQueryColumn() + function testQuery() { + $db = $this->getConnection(); - } + // query + $sql = 'SELECT * FROM yii_post'; + $reader = $db->createCommand($sql)->query(); + $this->assertTrue($reader instanceof DataReader); - function testQueryScalar() - { + // queryAll + $rows = $db->createCommand('SELECT * FROM yii_post')->queryAll(); + $this->assertEquals(5, count($rows)); + $row = $rows[2]; + $this->assertEquals(3, $row['id']); + $this->assertEquals($row['title'], 'post 3'); - } + $rows = $db->createCommand('SELECT * FROM yii_post WHERE id=10')->queryAll(); + $this->assertEquals(array(), $rows); - function testFetchMode() - { - - } + // queryRow + $sql = 'SELECT * FROM yii_post'; + $row = $db->createCommand($sql)->queryRow(); + $this->assertEquals(1, $row['id']); + $this->assertEquals('post 1', $row['title'], 'post 1'); - /* - function testPrepare() - { - $sql='SELECT title FROM posts'; - $command=$db->createCommand($sql); - $this->assertEquals($command->pdoStatement,null); + $sql = 'SELECT * FROM yii_post'; + $command = $db->createCommand($sql); $command->prepare(); - $this->assertTrue($command->pdoStatement instanceof PDOStatement); - $this->assertEquals($command->queryScalar(),'post 1'); + $row = $command->queryRow(); + $this->assertEquals(1, $row['id']); + $this->assertEquals('post 1', $row['title']); - $command->text='Bad SQL'; - $this->setExpectedException('CException'); - $command->prepare(); - } + $sql = 'SELECT * FROM yii_post WHERE id=10'; + $command = $db->createCommand($sql); + $this->assertFalse($command->queryRow()); - function testCancel() - { - $sql='SELECT title FROM posts'; - $command=$db->createCommand($sql); - $command->prepare(); - $this->assertTrue($command->pdoStatement instanceof PDOStatement); - $command->cancel(); - $this->assertEquals($command->pdoStatement,null); - } + // queryColumn + $sql = 'SELECT * FROM yii_post'; + $column = $db->createCommand($sql)->queryColumn(); + $this->assertEquals(range(1, 5), $column); - function testExecute() - { - $sql='INSERT INTO comments(content,post_id,author_id) VALUES (\'test comment\', 1, 1)'; - $command=$db->createCommand($sql); - $this->assertEquals($command->execute(),1); - $this->assertEquals($command->execute(),1); - $command=$db->createCommand('SELECT * FROM comments WHERE content=\'test comment\''); - $this->assertEquals($command->execute(),0); - $command=$db->createCommand('SELECT COUNT(*) FROM comments WHERE content=\'test comment\''); - $this->assertEquals($command->queryScalar(),2); - - $command=$db->createCommand('bad SQL'); - $this->setExpectedException('CException'); - $command->execute(); - } + $command = $db->createCommand('SELECT id FROM yii_post WHERE id=10'); + $this->assertEquals(array(), $command->queryColumn()); - function testQuery() - { - $sql='SELECT * FROM posts'; - $reader=$db->createCommand($sql)->query(); - $this->assertTrue($reader instanceof CDbDataReader); + // queryScalar + $sql = 'SELECT * FROM yii_post'; + $this->assertEquals($db->createCommand($sql)->queryScalar(), 1); - $sql='SELECT * FROM posts'; - $command=$db->createCommand($sql); + $sql = 'SELECT id FROM yii_post'; + $command = $db->createCommand($sql); $command->prepare(); - $reader=$command->query(); - $this->assertTrue($reader instanceof CDbDataReader); + $this->assertEquals(1, $command->queryScalar()); + + $command = $db->createCommand('SELECT id FROM yii_post WHERE id=10'); + $this->assertFalse($command->queryScalar()); - $command=$db->createCommand('bad SQL'); - $this->setExpectedException('CException'); + $command = $db->createCommand('bad SQL'); + $this->setExpectedException('\yii\db\Exception'); $command->query(); } - function testBindParam() + function testBindParamValue() { - $sql='INSERT INTO posts(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); - $command->execute(); - - $sql='SELECT create_time FROM posts WHERE title=:title'; - $command=$db->createCommand($sql); - $command->bindParam(':title',$title); - $this->assertEquals($command->queryScalar(),$createTime); - - $sql='INSERT INTO types (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'; - $floatCol=1.23; - $blobCol="\x10\x11\x12"; - $numericCol='1.23'; - $boolCol=false; - $command->bindParam(':int_col',$intCol); - $command->bindParam(':char_col',$charCol); - $command->bindParam(':float_col',$floatCol); - $command->bindParam(':blob_col',$blobCol); - $command->bindParam(':numeric_col',$numericCol); - $command->bindParam(':bool_col',$boolCol); - $this->assertEquals(1,$command->execute()); - - $sql='SELECT * FROM types'; - $row=$db->createCommand($sql)->queryRow(); - $this->assertEquals($row['int_col'],$intCol); - $this->assertEquals($row['char_col'],$charCol); - $this->assertEquals($row['float_col'],$floatCol); - $this->assertEquals($row['blob_col'],$blobCol); - $this->assertEquals($row['numeric_col'],$numericCol); - } + $db = $this->getConnection(); - function testBindValue() - { - $sql='INSERT INTO comments(content,post_id,author_id) VALUES (:content, 1, 1)'; - $command=$db->createCommand($sql); - $command->bindValue(':content','test comment'); + // 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); $command->execute(); - $sql='SELECT post_id FROM comments WHERE content=:content'; - $command=$db->createCommand($sql); - $command->bindValue(':content','test comment'); - $this->assertEquals($command->queryScalar(),1); - } - - function testQueryAll() - { - $rows=$db->createCommand('SELECT * FROM posts')->queryAll(); - $this->assertEquals(count($rows),5); - $row=$rows[2]; - $this->assertEquals($row['id'],3); - $this->assertEquals($row['title'],'post 3'); - - $rows=$db->createCommand('SELECT * FROM posts WHERE id=10')->queryAll(); - $this->assertEquals($rows,array()); - } - - function testQueryRow() - { - $sql='SELECT * FROM posts'; - $row=$db->createCommand($sql)->queryRow(); - $this->assertEquals($row['id'],1); - $this->assertEquals($row['title'],'post 1'); - - $sql='SELECT * FROM posts'; - $command=$db->createCommand($sql); - $command->prepare(); - $row=$command->queryRow(); - $this->assertEquals($row['id'],1); - $this->assertEquals($row['title'],'post 1'); - - $sql='SELECT * FROM posts WHERE id=10'; - $command=$db->createCommand($sql); - $this->assertFalse($command->queryRow()); - - $command=$db->createCommand('bad SQL'); - $this->setExpectedException('CException'); - $command->queryRow(); - } + $sql = 'SELECT create_time FROM yii_post WHERE title=:title'; + $command = $db->createCommand($sql); + $command->bindParam(':title', $title); + $this->assertEquals($createTime, $command->queryScalar()); - function testQueryColumn() - { - $sql='SELECT * FROM posts'; - $column=$db->createCommand($sql)->queryColumn(); - $this->assertEquals($column,range(1,5)); + $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)'; + $command = $db->createCommand($sql); + $intCol = 123; + $charCol = 'abc'; + $floatCol = 1.23; + $blobCol = "\x10\x11\x12"; + $numericCol = '1.23'; + $boolCol = false; + $command->bindParam(':int_col', $intCol); + $command->bindParam(':char_col', $charCol); + $command->bindParam(':float_col', $floatCol); + $command->bindParam(':blob_col', $blobCol); + $command->bindParam(':numeric_col', $numericCol); + $command->bindParam(':bool_col', $boolCol); + $this->assertEquals(1, $command->execute()); + + $sql = 'SELECT * FROM yii_type'; + $row = $db->createCommand($sql)->queryRow(); + $this->assertEquals($intCol, $row['int_col']); + $this->assertEquals($charCol, $row['char_col']); + $this->assertEquals($floatCol, $row['float_col']); + $this->assertEquals($blobCol, $row['blob_col']); + $this->assertEquals($numericCol, $row['numeric_col']); + + // bindValue + $sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (:content, 1, 1)'; + $command = $db->createCommand($sql); + $command->bindValue(':content', 'test comment'); + $command->execute(); - $command=$db->createCommand('SELECT id FROM posts WHERE id=10'); - $this->assertEquals($command->queryColumn(),array()); + $sql = 'SELECT post_id FROM yii_comment WHERE content=:content'; + $command = $db->createCommand($sql); + $command->bindValue(':content', 'test comment'); + $this->assertEquals(1, $command->queryScalar()); - $command=$db->createCommand('bad SQL'); - $this->setExpectedException('CException'); - $command->queryColumn(); + // bind value via query or execute method + $sql = 'INSERT INTO yii_comment(content,post_id,author_id) VALUES (:content, 1, 1)'; + $command = $db->createCommand($sql); + $command->execute(array(':content' => 'test comment2')); + $sql = 'SELECT post_id FROM yii_comment WHERE content=:content'; + $command = $db->createCommand($sql); + $this->assertEquals(1, $command->queryScalar(array(':content' => 'test comment2'))); } - function testQueryScalar() + function testFetchMode() { - $sql='SELECT * FROM posts'; - $this->assertEquals($db->createCommand($sql)->queryScalar(),1); - - $sql='SELECT id FROM posts'; - $command=$db->createCommand($sql); - $command->prepare(); - $this->assertEquals($command->queryScalar(),1); - - $command=$db->createCommand('SELECT id FROM posts WHERE id=10'); - $this->assertFalse($command->queryScalar()); - - $command=$db->createCommand('bad SQL'); - $this->setExpectedException('CException'); - $command->queryScalar(); - } + $db = $this->getConnection(); - function testFetchMode(){ - $sql='SELECT * FROM posts'; - $command=$db->createCommand($sql); + // default: FETCH_ASSOC + $sql = 'SELECT * FROM yii_post'; + $command = $db->createCommand($sql); $result = $command->queryRow(); - $this->assertTrue(is_array($result)); + $this->assertTrue(is_array($result) && isset($result['id'])); - $sql='SELECT * FROM posts'; - $command=$db->createCommand($sql); - $command->setFetchMode(PDO::FETCH_OBJ); + // FETCH_OBJ, customized via fetchMode property + $sql = 'SELECT * FROM yii_post'; + $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'; + $command = $db->createCommand($sql); + $result = $command->queryRow(array(), \PDO::FETCH_NUM); + $this->assertTrue(is_array($result) && isset($result[0])); } - */ } \ No newline at end of file