summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--framework/Data/TDbCommand.php7
-rw-r--r--framework/Data/TDbConnection.php8
-rw-r--r--framework/Data/TDbDataReader.php6
-rw-r--r--tests/unit/Data/TDbCommandTest.php49
-rw-r--r--tests/unit/Data/TDbConnectionTest.php11
5 files changed, 68 insertions, 13 deletions
diff --git a/framework/Data/TDbCommand.php b/framework/Data/TDbCommand.php
index 3d0e367a..df34207c 100644
--- a/framework/Data/TDbCommand.php
+++ b/framework/Data/TDbCommand.php
@@ -22,6 +22,9 @@
* (such as select), use {@link query} or its convenient versions {@link queryRow}
* and {@link queryScalar}.
*
+ * If an SQL statement returns results (such as a SELECT SQL), the results
+ * can be accessed via the returned {@link TDbDataReader}.
+ *
* TDbCommand supports SQL statment preparation and parameter binding.
* Call {@link bindParameter} to bind a PHP variable to a parameter in SQL.
* Call {@link bindValue} to bind a value to an SQL parameter.
@@ -79,7 +82,7 @@ class TDbCommand extends TComponent
/**
* @return PDOStatement the underlying PDOStatement for this command
- * It could be null if the statement is not created yet.
+ * It could be null if the statement is not prepared yet.
*/
public function getPdoStatement()
{
@@ -99,7 +102,7 @@ class TDbCommand extends TComponent
{
try
{
- $this->_statement=$this->getConnection()->getPdoInstance()->prepare($sql);
+ $this->_statement=$this->getConnection()->getPdoInstance()->prepare($this->getText());
}
catch(Exception $e)
{
diff --git a/framework/Data/TDbConnection.php b/framework/Data/TDbConnection.php
index cfb4fd72..9ca57a4a 100644
--- a/framework/Data/TDbConnection.php
+++ b/framework/Data/TDbConnection.php
@@ -20,7 +20,7 @@ Prado::using('System.Data.TDbCommand');
*
* TDbConnection works together with {@link TDbCommand}, {@link TDbDataReader}
* and {@link TDbTransaction} to provide data access to various DBMS
- * in a common set of APIs, thanks to the {@link http://www.php.net/manual/en/ref.pdo.php PDO}
+ * in a common set of APIs. They are a thin wrapper of the {@link http://www.php.net/manual/en/ref.pdo.php PDO}
* PHP extension.
*
* To establish a connection, set {@link setActive Active} to true after
@@ -53,9 +53,9 @@ Prado::using('System.Data.TDbCommand');
*
* To use transaction, do like the following:
* <code>
+ * $transaction=$connection->beginTransaction();
* try
* {
- * $transaction=$connection->beginTransaction();
* $connection->createCommand($sql1)->execute();
* $connection->createCommand($sql2)->execute();
* //.... other SQL executions
@@ -136,8 +136,8 @@ class TDbConnection extends TComponent
{
try
{
- $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_pdo=new PDO($this->getConnectionString(),$this->getUsername(),$this->getPassword(),$this->_attributes);
+ $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_active=true;
}
catch(PDOException $e)
@@ -454,7 +454,7 @@ class TDbConnection extends TComponent
*/
public function setAttribute($name,$value)
{
- if($this->getActive())
+ if($this->_pdo instanceof PDO)
$this->_pdo->setAttribute($name,$value);
else
$this->_attributes[$name]=$value;
diff --git a/framework/Data/TDbDataReader.php b/framework/Data/TDbDataReader.php
index 32b86c1d..ad3132b0 100644
--- a/framework/Data/TDbDataReader.php
+++ b/framework/Data/TDbDataReader.php
@@ -18,14 +18,16 @@
* To read the current row of data, call {@link read}. The method {@link readAll}
* returns all the rows in a single array.
*
- * TDbDataReader implements Iterator interface and thus can be used in foreach like the following:
+ * One can also retrieve the rows of data in TDbDataReader by using foreach:
* <code>
* foreach($reader as $row)
* // $row represents a row of data
* </code>
+ * Since TDbDataReader is a forward-only stream, you can only traverse it once.
*
* It is possible to use a specific mode of data fetching by setting
- * {@link setFetchMode FetchMode}. See {@link
+ * {@link setFetchMode FetchMode}. See {@link http://www.php.net/manual/en/function.pdostatement-setfetchmode.php}
+ * for more details.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id $
diff --git a/tests/unit/Data/TDbCommandTest.php b/tests/unit/Data/TDbCommandTest.php
index f0c02f75..ab94cb3f 100644
--- a/tests/unit/Data/TDbCommandTest.php
+++ b/tests/unit/Data/TDbCommandTest.php
@@ -19,7 +19,8 @@ class TDbCommandTest extends PHPUnit2_Framework_TestCase
$this->_connection=new TDbConnection('sqlite:'.TEST_DB_FILE);
$this->_connection->Active=true;
$this->_connection->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute();
- $this->_connection->createCommand('INSERT TABLE foo (name) VALUES (\'my name\')')->execute;
+ $this->_connection->createCommand('INSERT INTO foo (name) VALUES (\'my name\')')->execute();
+ $this->_connection->createCommand('INSERT INTO foo (name) VALUES (\'my name 2\')')->execute();
}
public function tearDown()
@@ -36,16 +37,54 @@ class TDbCommandTest extends PHPUnit2_Framework_TestCase
public function testSetText()
{
- $sql='SELECT * FROM foo';
- $newSql='SELECT id FROM foo';
+ $sql='SELECT name FROM foo';
$command=$this->_connection->createCommand($sql);
- $command->query()->read();
+ $row=$command->query()->read();
+ $this->assertEquals($row['name'],'my name');
+
+ $newSql='SELECT id FROM foo';
$command->Text=$newSql;
$this->assertEquals($command->Text,$newSql);
$row=$command->query()->read();
- $this->assertEquals($row['id'],1);
+
+ $this->assertEquals($row['id'],'1');
}
+ public function testConnection()
+ {
+ $sql='SELECT name FROM foo';
+ $command=$this->_connection->createCommand($sql);
+ $this->assertTrue($command->Connection instanceof TDbConnection);
+ }
+
+ public function testPrepare()
+ {
+ $sql='SELECT name FROM foo';
+ $command=$this->_connection->createCommand($sql);
+ $this->assertTrue($command->PdoStatement===null);
+ $command->prepare();
+ $this->assertTrue($command->PdoStatement instanceof PDOStatement);
+
+ try
+ {
+ $command->Text='Bad SQL';
+ $command->prepare();
+ $this->fail('Expected exception is not raised');
+ }
+ catch(TDbException $e)
+ {
+ }
+ }
+
+ public function testCancel()
+ {
+ $sql='SELECT name FROM foo';
+ $command=$this->_connection->createCommand($sql);
+ $command->prepare();
+ $this->assertTrue($command->PdoStatement instanceof PDOStatement);
+ $command->cancel();
+ $this->assertEquals($command->PdoStatement,null);
+ }
/*
public function testActive()
{
diff --git a/tests/unit/Data/TDbConnectionTest.php b/tests/unit/Data/TDbConnectionTest.php
index 21d6730b..6c1e71aa 100644
--- a/tests/unit/Data/TDbConnectionTest.php
+++ b/tests/unit/Data/TDbConnectionTest.php
@@ -38,12 +38,23 @@ class TDbConnectionTest extends PHPUnit2_Framework_TestCase
$this->_connection2->Active=true;
$this->assertTrue($this->_connection2->Active);
$pdo=$this->_connection2->PdoInstance;
+ $this->assertTrue($pdo instanceof PDO);
// test setting Active repeatedly doesn't re-connect DB
$this->_connection2->Active=true;
$this->assertTrue($pdo===$this->_connection2->PdoInstance);
$this->_connection2->Active=false;
$this->assertFalse($this->_connection2->Active);
+
+ try
+ {
+ $connection=new TDbConnection('unknown:'.TEST_DB_FILE);
+ $connection->Active=true;
+ $this->fail('Expected exception is not raised');
+ }
+ catch(TDbException $e)
+ {
+ }
}
public function testCreateCommand()