summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxue <>2006-11-25 02:27:26 +0000
committerxue <>2006-11-25 02:27:26 +0000
commitd877676a3b2432fccfc5ce97e42008f9ce20c94d (patch)
tree1f477fe9393bde0da8ded400b7c6513d1b2c3102
parent7280a2301a3d3dd251d8302c3f7999fa09780d42 (diff)
Further bug fixes.
-rw-r--r--framework/Data/TDbCommand.php4
-rw-r--r--tests/unit/Data/TDbCommandTest.php152
2 files changed, 103 insertions, 53 deletions
diff --git a/framework/Data/TDbCommand.php b/framework/Data/TDbCommand.php
index df34207c..7ae32d17 100644
--- a/framework/Data/TDbCommand.php
+++ b/framework/Data/TDbCommand.php
@@ -222,7 +222,7 @@ class TDbCommand extends TComponent
if($this->_statement instanceof PDOStatement)
$this->_statement->execute();
else
- $this->_statement=$this->getConnection()->getPdoInstance()->query($sql);
+ $this->_statement=$this->getConnection()->getPdoInstance()->query($this->getText());
$result=$this->_statement->fetch($fetchAssociative ? PDO::FETCH_ASSOC : PDO::FETCH_NUM);
$this->_statement->closeCursor();
return $result;
@@ -247,7 +247,7 @@ class TDbCommand extends TComponent
if($this->_statement instanceof PDOStatement)
$this->_statement->execute();
else
- $this->_statement=$this->getConnection()->getPdoInstance()->query($sql);
+ $this->_statement=$this->getConnection()->getPdoInstance()->query($this->getText());
$result=$this->_statement->fetchColumn();
$this->_statement->closeCursor();
if($result!==false)
diff --git a/tests/unit/Data/TDbCommandTest.php b/tests/unit/Data/TDbCommandTest.php
index ab94cb3f..40d871e3 100644
--- a/tests/unit/Data/TDbCommandTest.php
+++ b/tests/unit/Data/TDbCommandTest.php
@@ -85,86 +85,136 @@ class TDbCommandTest extends PHPUnit2_Framework_TestCase
$command->cancel();
$this->assertEquals($command->PdoStatement,null);
}
-/*
- public function testActive()
- {
- $this->assertFalse($this->_connection2->Active);
- $this->_connection2->Active=true;
- $this->assertTrue($this->_connection2->Active);
- $pdo=$this->_connection2->PdoInstance;
- // test setting Active repeatedly doesn't re-connect DB
- $this->_connection2->Active=true;
- $this->assertTrue($pdo===$this->_connection2->PdoInstance);
+ public function testBindParameter()
+ {
+ $sql='INSERT INTO foo (id,name) VALUES (3,:name)';
+ $command=$this->_connection->createCommand($sql);
+ $name='new name';
+ $command->bindParameter(':name',$name);
+ $command->execute();
+ $insertedName=$this->_connection->createCommand('SELECT name FROM foo WHERE id=3')->queryScalar();
+ $this->assertEquals($name,$insertedName);
+ }
- $this->_connection2->Active=false;
- $this->assertFalse($this->_connection2->Active);
+ public function testBindValue()
+ {
+ $sql='INSERT INTO foo (id,name) VALUES (3,:name)';
+ $command=$this->_connection->createCommand($sql);
+ $command->bindValue(':name','new name');
+ $command->execute();
+ $insertedName=$this->_connection->createCommand('SELECT name FROM foo WHERE id=3')->queryScalar();
+ $this->assertEquals('new name',$insertedName);
}
- public function testCreateCommand()
+ public function testExecute()
{
- $sql='CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))';
+ // test unprepared SQL execution
+ $sql='INSERT INTO foo (name) VALUES (\'new name\')';
+ $command=$this->_connection->createCommand($sql);
+ $command->execute();
+ $command->execute();
+ $count=$this->_connection->createCommand('SELECT COUNT(id) AS id_count FROM foo')->queryScalar();
+ $this->assertEquals('4',$count);
+
+ // test prepared SQL execution
+ $sql='INSERT INTO foo (name) VALUES (\'new name\')';
+ $command=$this->_connection->createCommand($sql);
+ $command->prepare();
+ $command->execute();
+ $command->execute();
+ $count=$this->_connection->createCommand('SELECT COUNT(id) AS id_count FROM foo')->queryScalar();
+ $this->assertEquals('6',$count);
+
+ // test exception raising
try
{
- $this->_connection2->createCommand($sql);
+ $command=$this->_connection->createCommand('bad SQL');
+ $command->execute();
$this->fail('Expected exception is not raised');
}
catch(TDbException $e)
{
}
-
- $command=$this->_connection->createCommand($sql);
- $this->assertTrue($command instanceof TDbCommand);
}
- public function testBeginTransaction()
+ public function testQuery()
{
- $sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')';
- $transaction=$this->_connection->beginTransaction();
+ // test unprepared SQL query
+ $sql='SELECT * FROM foo';
+ $reader=$this->_connection->createCommand($sql)->query();
+ $this->assertTrue($reader instanceof TDbDataReader);
+
+ // test unprepared SQL query
+ $sql='SELECT * FROM foo';
+ $command=$this->_connection->createCommand($sql);
+ $command->prepare();
+ $reader=$command->query();
+ $this->assertTrue($reader instanceof TDbDataReader);
+
+ // test exception raising
try
{
- $this->_connection->createCommand($sql)->execute();
- $this->_connection->createCommand($sql)->execute();
- $this->fail('Expected exception not raised');
- $transaction->commit();
+ $command=$this->_connection->createCommand('bad SQL');
+ $command->query();
+ $this->fail('Expected exception is not raised');
}
- catch(Exception $e)
+ catch(TDbException $e)
{
- $transaction->rollBack();
- $reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
- $this->assertFalse($reader->read());
}
}
- public function testLastInsertID()
+ public function testQueryRow()
{
- $sql='INSERT INTO foo(name) VALUES (\'my name\')';
- $this->_connection->createCommand($sql)->execute();
- $value=$this->_connection->LastInsertID;
- $this->assertEquals($this->_connection->LastInsertID,'1');
- }
+ // test unprepared SQL query
+ $sql='SELECT * FROM foo';
+ $row=$this->_connection->createCommand($sql)->queryRow();
+ $this->assertTrue($row['id']==='1' && $row['name']==='my name');
- public function testQuoteString()
- {
- $str="this is 'my' name";
- $expectedStr="'this is ''my'' name'";
- $this->assertEquals($expectedStr,$this->_connection->quoteString($str));
- }
+ // test unprepared SQL query
+ $sql='SELECT * FROM foo';
+ $command=$this->_connection->createCommand($sql);
+ $command->prepare();
+ $row=$command->queryRow();
+ $this->assertTrue($row['id']==='1' && $row['name']==='my name');
- public function testColumnNameCase()
- {
- $this->assertEquals(TDbColumnCaseMode::Preserved,$this->_connection->ColumnCase);
- $this->_connection->ColumnCase=TDbColumnCaseMode::LowerCase;
- $this->assertEquals(TDbColumnCaseMode::LowerCase,$this->_connection->ColumnCase);
+ // test exception raising
+ try
+ {
+ $command=$this->_connection->createCommand('bad SQL');
+ $command->queryRow();
+ $this->fail('Expected exception is not raised');
+ }
+ catch(TDbException $e)
+ {
+ }
}
- public function testNullConversion()
+ public function testQueryScalar()
{
- $this->assertEquals(TDbNullConversionMode::Preserved,$this->_connection->NullConversion);
- $this->_connection->NullConversion=TDbNullConversionMode::NullToEmptyString;
- $this->assertEquals(TDbNullConversionMode::NullToEmptyString,$this->_connection->NullConversion);
+ // test unprepared SQL query
+ $sql='SELECT * FROM foo';
+ $id=$this->_connection->createCommand($sql)->queryScalar();
+ $this->assertTrue($id==='1');
+
+ // test unprepared SQL query
+ $sql='SELECT * FROM foo';
+ $command=$this->_connection->createCommand($sql);
+ $command->prepare();
+ $row=$command->queryScalar();
+ $this->assertTrue($id==='1');
+
+ // test exception raising
+ try
+ {
+ $command=$this->_connection->createCommand('bad SQL');
+ $command->queryScalar();
+ $this->fail('Expected exception is not raised');
+ }
+ catch(TDbException $e)
+ {
+ }
}
- */
}
?> \ No newline at end of file