From b09acac59340fca37b5149cc0354093b7d7ab015 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 25 Nov 2006 03:48:22 +0000 Subject: DB layer is completed. --- framework/Data/TDbDataReader.php | 42 +++-- tests/unit/Data/TDbCommandTest.php | 3 +- tests/unit/Data/TDbDataReaderTest.php | 291 +++++++++++++++++++++------------- 3 files changed, 209 insertions(+), 127 deletions(-) diff --git a/framework/Data/TDbDataReader.php b/framework/Data/TDbDataReader.php index ad3132b0..1d846839 100644 --- a/framework/Data/TDbDataReader.php +++ b/framework/Data/TDbDataReader.php @@ -80,17 +80,39 @@ class TDbDataReader extends TComponent implements Iterator } /** - * Advances the reader to the next record in a result set. - * @return array|false the current record, false if no more row available + * Advances the reader to the next row in a result set. + * @return array|false the current row, false if no more row available */ public function read() { return $this->_statement->fetch(); } + /** + * Returns a single column from the next row of a result set. + * @param int zero-based column index + * @return mixed|false the column of the current row, false if no more row available + */ + public function readColumn($columnIndex) + { + return $this->_statement->fetchColumn($columnIndex); + } + + /** + * Returns a single column from the next row of a result set. + * @param string class name of the object to be created and populated + * @param array list of column names whose values are to be passed as parameters in the constructor of the class being created + * @return mixed|false the populated object, false if no more row of data available + */ + public function readObject($className,$fields) + { + return $this->_statement->fetchObject($className,$fields); + } + /** * Reads the whole result set into an array. - * @return array|false the result set (each array element represents a row of data), false if no data is available. + * @return array the result set (each array element represents a row of data). + * An empty array will be returned if the result contains no row. */ public function readAll() { @@ -125,17 +147,10 @@ class TDbDataReader extends TComponent implements Iterator return $this->_closed; } - /** - * @return boolean whether the result contains any row of data - */ - public function getHasRows() - { - return $this->getRowCount()>0; - } - /** * @return int number of rows contained in the result. - * Note, some DBMS may not give a meaningful count. + * Note, most DBMS may not give a meaningful count. + * In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows. */ public function getRowCount() { @@ -143,7 +158,8 @@ class TDbDataReader extends TComponent implements Iterator } /** - * @return int the number of columns in the result set, 0 if the result is empty + * @return int the number of columns in the result set. + * Note, even there's no row in the reader, this still gives correct column number. */ public function getColumnCount() { diff --git a/tests/unit/Data/TDbCommandTest.php b/tests/unit/Data/TDbCommandTest.php index 40d871e3..9023e6b6 100644 --- a/tests/unit/Data/TDbCommandTest.php +++ b/tests/unit/Data/TDbCommandTest.php @@ -112,7 +112,8 @@ class TDbCommandTest extends PHPUnit2_Framework_TestCase // test unprepared SQL execution $sql='INSERT INTO foo (name) VALUES (\'new name\')'; $command=$this->_connection->createCommand($sql); - $command->execute(); + $n=$command->execute(); + $this->assertEquals($n,1); $command->execute(); $count=$this->_connection->createCommand('SELECT COUNT(id) AS id_count FROM foo')->queryScalar(); $this->assertEquals('4',$count); diff --git a/tests/unit/Data/TDbDataReaderTest.php b/tests/unit/Data/TDbDataReaderTest.php index 21d6730b..68f9bfe8 100644 --- a/tests/unit/Data/TDbDataReaderTest.php +++ b/tests/unit/Data/TDbDataReaderTest.php @@ -1,114 +1,179 @@ -_connection1=new TDbConnection('sqlite:'.TEST_DB_FILE); - $this->_connection1->Active=true; - $this->_connection1->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute(); - $this->_connection2=new TDbConnection('sqlite:'.TEST_DB_FILE2); - } - - public function tearDown() - { - $this->_connection1=null; - $this->_connection2=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); - - $this->_connection2->Active=false; - $this->assertFalse($this->_connection2->Active); - } - - public function testCreateCommand() - { - $sql='CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))'; - try - { - $this->_connection2->createCommand($sql); - $this->fail('Expected exception is not raised'); - } - catch(TDbException $e) - { - } - - $command=$this->_connection1->createCommand($sql); - $this->assertTrue($command instanceof TDbCommand); - } - - public function testBeginTransaction() - { - $sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')'; - $transaction=$this->_connection1->beginTransaction(); - try - { - $this->_connection1->createCommand($sql)->execute(); - $this->_connection1->createCommand($sql)->execute(); - $this->fail('Expected exception not raised'); - $transaction->commit(); - } - catch(Exception $e) - { - $transaction->rollBack(); - $reader=$this->_connection1->createCommand('SELECT * FROM foo')->query(); - $this->assertFalse($reader->read()); - } - } - - public function testLastInsertID() - { - $sql='INSERT INTO foo(name) VALUES (\'my name\')'; - $this->_connection1->createCommand($sql)->execute(); - $value=$this->_connection1->LastInsertID; - $this->assertEquals($this->_connection1->LastInsertID,'1'); - } - - public function testQuoteString() - { - $str="this is 'my' name"; - $expectedStr="'this is ''my'' name'"; - $this->assertEquals($expectedStr,$this->_connection1->quoteString($str)); - } - - public function testColumnNameCase() - { - $this->assertEquals(TDbColumnCaseMode::Preserved,$this->_connection1->ColumnCase); - $this->_connection1->ColumnCase=TDbColumnCaseMode::LowerCase; - $this->assertEquals(TDbColumnCaseMode::LowerCase,$this->_connection1->ColumnCase); - } - - public function testNullConversion() - { - $this->assertEquals(TDbNullConversionMode::Preserved,$this->_connection1->NullConversion); - $this->_connection1->NullConversion=TDbNullConversionMode::NullToEmptyString; - $this->assertEquals(TDbNullConversionMode::NullToEmptyString,$this->_connection1->NullConversion); - } -} - +param=$param; + } + + public function getName() + { + return $this->_name; + } + + public function setName($value) + { + $this->_name=$value; + } +} + +/** + * @package System.Data.PDO + */ +class TDbDataReaderTest extends PHPUnit2_Framework_TestCase +{ + private $_connection; + + public function setUp() + { + @unlink(TEST_DB_FILE); + $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 INTO foo (name) VALUES (\'my name\')')->execute(); + $this->_connection->createCommand('INSERT INTO foo (name) VALUES (\'my name 2\')')->execute(); + } + + public function tearDown() + { + $this->_connection=null; + } + + public function testRead() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $row=$reader->read(); + $this->assertTrue($row['id']==='1' && $row['name']==='my name'); + $row=$reader->read(); + $this->assertTrue($row['id']==='2' && $row['name']==='my name 2'); + $row=$reader->read(); + $this->assertFalse($row); + } + + public function testReadColumn() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $this->assertEquals($reader->readColumn(0),'1'); + $this->assertEquals($reader->readColumn(1),'my name 2'); + $this->assertFalse($reader->readColumn(0)); + } + + public function testReadObject() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $object=$reader->readObject('FooRecord',array('object')); + $this->assertEquals($object->id,'1'); + $this->assertEquals($object->Name,'my name'); + $this->assertEquals($object->param,'object'); + } + + public function testReadAll() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $rows=$reader->readAll(); + $this->assertEquals(count($rows),2); + $row=$rows[0]; + $this->assertTrue($row['id']==='1' && $row['name']==='my name'); + $row=$rows[1]; + $this->assertTrue($row['id']==='2' && $row['name']==='my name 2'); + + $reader=$this->_connection->createCommand('SELECT * FROM foo WHERE id=3')->query(); + $rows=$reader->readAll(); + $this->assertEquals($rows,array()); + } + + public function testClose() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $row=$reader->read(); + $this->assertFalse($reader->IsClosed); + $reader->close(); + $this->assertTrue($reader->IsClosed); + try + { + $reader->read(); + $this->fail('Expected exception is not raised'); + } + catch(Exception $e) + { + } + } + + public function testRowCount() + { + // unable to test because SQLite doesn't support row count + } + + public function testColumnCount() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $this->assertEquals($reader->ColumnCount,2); + + $reader=$this->_connection->createCommand('SELECT * FROM foo WHERE id=11')->query(); + $this->assertEquals($reader->ColumnCount,2); + } + + public function testForeach() + { + $ids=array(); + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + foreach($reader as $row) + $ids[]=$row['id']; + $this->assertEquals(count($ids),2); + $this->assertTrue($ids[0]==='1' && $ids[1]==='2'); + + try + { + foreach($reader as $row) + $ids[]=$row['id']; + $this->fail('Expected exception is not raised'); + } + catch(TDbException $e) + { + } + } + + public function testFetchMode() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + + $reader->FetchMode=PDO::FETCH_NUM; + $row=$reader->read(); + $this->assertFalse(isset($row['id'])); + $this->assertTrue(isset($row[0])); + + $reader->FetchMode=PDO::FETCH_ASSOC; + $row=$reader->read(); + $this->assertTrue(isset($row['id'])); + $this->assertFalse(isset($row[0])); + } + + public function testBindColumn() + { + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $reader->bindColumn(1,$id); + $reader->bindColumn(2,$name); + $reader->read(); + $this->assertEquals($id,'1'); + $this->assertEquals($name,'my name'); + $reader->read(); + $this->assertEquals($id,'2'); + $this->assertEquals($name,'my name 2'); + $reader->read(); + $this->assertEquals($id,'2'); + $this->assertEquals($name,'my name 2'); + } +} + ?> \ No newline at end of file -- cgit v1.2.3