Commit bed3a371 authored by zYne's avatar zYne

added fetch* methods to new Doctrine_DB

parent a1d3e137
......@@ -36,14 +36,6 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @var array $instances all the instances of this class
*/
protected static $instances = array();
/**
* @var array $queries all the executed queries
*/
protected $queries = array();
/**
* @var array $exectimes execution times of the executed queries
*/
protected $exectimes = array();
/**
* @var array $isConnected whether or not a connection has been established
*/
......@@ -104,12 +96,16 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
}
/**
* getUsername
*
* @return string
*/
public function getUsername() {
return $this->username;
}
/**
* getPassword
*
* @return string
*/
public function getPassword() {
return $this->password;
......@@ -165,7 +161,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
$this->dbh = new PDO($this->dsn,$this->username,$this->password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("Doctrine_DB_Statement", array($this)));
$this->isConnected = true;
return true;
}
......@@ -303,6 +299,8 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @param string $statement
*/
public function prepare($statement) {
$this->connect();
$args = func_get_args();
$this->listener->onPrePrepare($this, $args);
......@@ -319,14 +317,17 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @param string $statement
* @return Doctrine_DB_Statement|boolean
*/
public function query($statement, $fetchMode = null, $arg = null, $arg2 = null) {
$args = func_get_args();
$this->listener->onPreQuery($this, $args);
public function query($statement, array $params = array()) {
$this->connect();
$stmt = $this->dbh->query($statement, $fetchMode, $arg, $arg2);
$this->listener->onPreQuery($this, $params);
if( ! empty($params))
$stmt = $this->dbh->query($statement)->execute($params);
else
$stmt = $this->dbh->query($statement);
$this->listener->onQuery($this, $args);
$this->listener->onQuery($this, $params);
return $stmt;
}
......@@ -350,6 +351,8 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @return integer
*/
public function exec($statement) {
$this->connect();
$args = func_get_args();
$this->listener->onPreExec($this, $args);
......@@ -362,15 +365,37 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
}
/**
* fetchAll
*
* @return array
*/
public function fetchAssoc($statement, $params = array()) {
if( ! $params)
$this->query($statement);
public function fetchAll($statement, array $params = array()) {
return $this->query($statement, $params)->fetchAll(PDO::FETCH_ASSOC);
}
public function fetchOne($statement, array $params = array()) {
return current($this->query($statement, $params)->fetch(PDO::FETCH_NUM));
}
public function fetchRow($statement, array $params = array()) {
return $this->query($statement, $params)->fetch(PDO::FETCH_ASSOC);
}
public function fetchArray($statement, array $params = array()) {
return $this->query($statement, $params)->fetch(PDO::FETCH_NUM);
}
public function fetchColumn($statement, array $params = array()) {
return $this->query($statement, $params)->fetchAll(PDO::FETCH_COLUMN);
}
public function fetchAssoc($statement, array $params = array()) {
return $this->query($statement, $params)->fetchAll(PDO::FETCH_ASSOC);
}
public function fetchBoth($statement, array $params = array()) {
return $this->query($statement, $params)->fetchAll(PDO::FETCH_BOTH);
}
/**
* lastInsertId
*
*
* @return integer
*/
public function lastInsertId() {
$this->connect();
......
......@@ -39,6 +39,6 @@ class Doctrine_DB_Statement extends PDOStatement {
$this->dbh->getListener()->onExecute($this, $params);
return $ret;
return $this;
}
}
......@@ -22,6 +22,100 @@ class Doctrine_DB_TestCase extends Doctrine_UnitTestCase {
public function prepareTables() { }
public function init() { }
public function testFetchAll() {
$dbh = Doctrine_DB2::getConnection('sqlite::memory:');
$dbh->connect();
$dbh->query('CREATE TABLE entity (id INTEGER, name TEXT)');
$dbh->query("INSERT INTO entity (id, name) VALUES (1, 'zYne')");
$dbh->query("INSERT INTO entity (id, name) VALUES (2, 'John')");
$a = $dbh->fetchAll('SELECT * FROM entity');
$this->assertEqual($a, array (
0 =>
array (
'id' => '1',
'name' => 'zYne',
),
1 =>
array (
'id' => '2',
'name' => 'John',
),
));
}
public function testFetchOne() {
$dbh = Doctrine_DB2::getConnection('sqlite::memory:');
$c = $dbh->fetchOne('SELECT COUNT(1) FROM entity');
$this->assertEqual($c, 2);
$c = $dbh->fetchOne('SELECT COUNT(1) FROM entity WHERE id = ?', array(1));
$this->assertEqual($c, 1);
}
public function testFetchAssoc() {
}
public function testFetchColumn() {
$dbh = Doctrine_DB2::getConnection('sqlite::memory:');
$a = $dbh->fetchColumn('SELECT * FROM entity');
$this->assertEqual($a, array (
0 => '1',
1 => '2',
));
$a = $dbh->fetchColumn('SELECT * FROM entity WHERE id = ?', array(1));
$this->assertEqual($a, array (
0 => '1',
));
}
public function testFetchArray() {
$dbh = Doctrine_DB2::getConnection('sqlite::memory:');
$a = $dbh->fetchArray('SELECT * FROM entity');
$this->assertEqual($a, array (
0 => '1',
1 => 'zYne',
));
$a = $dbh->fetchArray('SELECT * FROM entity WHERE id = ?', array(1));
$this->assertEqual($a, array (
0 => '1',
1 => 'zYne',
));
}
public function testFetchRow() {
$dbh = Doctrine_DB2::getConnection('sqlite::memory:');
$c = $dbh->fetchRow('SELECT * FROM entity');
$this->assertEqual($c, array (
'id' => '1',
'name' => 'zYne',
));
$c = $dbh->fetchRow('SELECT * FROM entity WHERE id = ?', array(1));
$this->assertEqual($c, array (
'id' => '1',
'name' => 'zYne',
));
}
public function testFetchPairs() {
}
public function testAddValidEventListener() {
$dbh = Doctrine_DB2::getConnection('sqlite::memory:');
......@@ -77,14 +171,7 @@ class Doctrine_DB_TestCase extends Doctrine_UnitTestCase {
$dbh = Doctrine_DB2::getConnection('sqlite::memory:');
$dbh->connect();
$dbh->setListener(new Doctrine_DB_TestLogger());
$dbh->query('CREATE TABLE entity (id INT)');
$listener = $dbh->getListener();
$this->assertEqual($listener->pop(), 'onQuery');
$this->assertEqual($listener->pop(), 'onPreQuery');
$stmt = $dbh->prepare('INSERT INTO entity (id) VALUES(?)');
$this->assertEqual($listener->pop(), 'onPrepare');
......
......@@ -38,7 +38,7 @@ error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests");
$test->addTestCase(new Doctrine_DB_TestCase());
/**
$test->addTestCase(new Doctrine_ConnectionTestCase());
$test->addTestCase(new Doctrine_RecordTestCase());
......@@ -74,11 +74,11 @@ $test->addTestCase(new Doctrine_ValueHolder_TestCase());
$test->addTestCase(new Doctrine_RawSql_TestCase());
$test->addTestCase(new Doctrine_Query_Limit_TestCase());
*/
//$test->addTestCase(new Doctrine_SchemaTestCase());
//$test->addTestCase(new Doctrine_ImportTestCase());
/**
$test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_Query_ReferenceModel_TestCase());
......@@ -94,7 +94,7 @@ $test->addTestCase(new Doctrine_RelationAccessTestCase());
$test->addTestCase(new Doctrine_EventListener_Chain_TestCase());
$test->addTestCase(new Doctrine_DataDict_Sqlite_TestCase());
*/
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
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