1
0
mirror of synced 2025-01-22 00:01:40 +03:00

Doctrine_Db updates

This commit is contained in:
zYne 2006-11-08 10:18:15 +00:00
parent fef149dd62
commit a0aa16bbea
21 changed files with 217 additions and 295 deletions

View File

@ -27,48 +27,7 @@
* @license LGPL
* @package Doctrine
*/
class Doctrine_DB2 implements Countable, IteratorAggregate {
/**
* A connection operation or selecting a database.
*/
const CONNECT = 1;
/**
* Any general database query that does not fit into the other constants.
*/
const QUERY = 2;
/**
* Adding new data to the database, such as SQL's INSERT.
*/
const INSERT = 4;
/**
* Updating existing information in the database, such as SQL's UPDATE.
*
*/
const UPDATE = 8;
/**
* An operation related to deleting data in the database,
* such as SQL's DELETE.
*/
const DELETE = 16;
/**
* Retrieving information from the database, such as SQL's SELECT.
*/
const SELECT = 32;
/**
* Transactional operation, such as start transaction, commit, or rollback.
*/
const TRANSACTION = 64;
/**
* default DSN
*/
const DSN = "mysql://root:dc34@localhost/test";
class Doctrine_DB2 implements Countable, IteratorAggregate, Doctrine_Adapter_Interface {
/**
* @var array $instances all the instances of this class
*/
@ -77,24 +36,20 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @var array $isConnected whether or not a connection has been established
*/
protected $isConnected = false;
/**
* @var string $dsn data source name
*/
protected $dsn;
/**
* @var string $username database username
*/
protected $username;
/**
* @var string $password database password
*/
protected $password;
/**
* @var PDO $dbh the database handler
*/
protected $dbh;
/**
* @var Doctrine_DB_EventListener_Interface|Doctrine_Overloadable $listener listener for listening events
* @var array $options
*/
protected $options = array('dsn' => null,
'username' => null,
'password' => null,
);
/**
* @var Doctrine_DB_EventListener_Interface|Doctrine_Overloadable $listener
* listener for listening events
*/
protected $listener;
/**
@ -113,13 +68,18 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* constructor
*
* @param string $dsn data source name
* @param string $username database username
* @param string $password database password
* @param string $user database username
* @param string $pass database password
*/
public function __construct($dsn,$username,$password) {
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
public function __construct($dsn, $user, $pass) {
if( ! isset($user)) {
$a = self::parseDSN($dsn);
extract($a);
}
$this->options['dsn'] = $dsn;
$this->options['username'] = $user;
$this->options['password'] = $pass;
$this->listener = new Doctrine_DB_EventListener();
}
@ -139,30 +99,11 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
public function getDBH() {
return $this->dbh;
}
/**
* getDSN
* returns the data source name
*
* @return string
*/
public function getDSN() {
return $this->dsn;
}
/**
* getUsername
*
* @return string
*/
public function getUsername() {
return $this->username;
}
/**
* getPassword
*
* @return string
*/
public function getPassword() {
return $this->password;
public function getOption($name) {
if( ! array_key_exists($name, $this->options))
throw new Doctrine_Db_Exception('Unknown option ' . $name);
return $this->options[$name];
}
/**
* addListener
@ -212,7 +153,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
if($this->isConnected)
return false;
$this->dbh = new PDO($this->dsn,$this->username,$this->password);
$this->dbh = new PDO($this->options['dsn'], $this->options['username'], $this->options['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;
@ -228,25 +169,9 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
* @return
*/
public static function getConnection($dsn = null, $username = null, $password = null) {
$md5 = md5($dsn);
if( ! isset(self::$instances[$md5])) {
if(isset($username)) {
self::$instances[$md5] = new self($dsn, $username, $password);
} else {
if( ! isset($dsn))
$a = self::parseDSN(self::DSN);
else
$a = self::parseDSN($dsn);
extract($a);
self::$instances[$md5] = new self($dsn, $user, $pass);
}
}
return self::$instances[$md5];
return new self($dsn, $username, $password);
}
/**
/**
* driverName
* converts a driver name like (oracle) to appropriate PDO
* driver name (oci8 in the case of oracle)
@ -523,7 +448,7 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
public function getAttribute($attribute) {
$this->connect();
$this->dbh->getAttribute($attribute);
return $this->dbh->getAttribute($attribute);
}
/**
* returns an array of available PDO drivers
@ -561,7 +486,5 @@ class Doctrine_DB2 implements Countable, IteratorAggregate {
*/
public function count() {
return $this->querySequence;
}
}
}

View File

@ -73,11 +73,14 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
/**
* the constructor
*
* @param Doctrine_Manager $manager the manager object
* @param PDO $pdo the database handler
* @param Doctrine_Manager $manager the manager object
* @param PDO|Doctrine_Adapter_Interface $adapter database driver
*/
public function __construct(Doctrine_Manager $manager, PDO $pdo) {
$this->dbh = $pdo;
public function __construct(Doctrine_Manager $manager, $adapter) {
if( ! ($adapter instanceof PDO) && ! in_array('Doctrine_Adapter_Interface', class_implements($adapter)))
throw new Doctrine_Connection_Exception("First argument should be an instance of PDO or implement Doctrine_Adapter_Interface");
$this->dbh = $adapter;
$this->transaction = new Doctrine_Connection_Transaction($this);
$this->unitOfWork = new Doctrine_Connection_UnitOfWork($this);
@ -140,7 +143,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
*
* @return PDO the database handler
*/
public function getDBH() {
public function getDbh() {
return $this->dbh;
}
/**

View File

@ -41,7 +41,7 @@ class Doctrine_Connection_Firebird extends Doctrine_Connection {
* @param Doctrine_Manager $manager
* @param PDO $pdo database handle
*/
public function __construct(Doctrine_Manager $manager, PDO $pdo) {
public function __construct(Doctrine_Manager $manager, $adapter) {
$this->supported = array(
'sequences' => true,
@ -69,7 +69,7 @@ class Doctrine_Connection_Firebird extends Doctrine_Connection {
$this->options['database_path'] = '';
$this->options['database_extension'] = '.gdb';
$this->options['server_version'] = '';
parent::__construct($manager, $pdo);
parent::__construct($manager, $adapter);
}
/**
* Set the transacton isolation level.

View File

@ -37,9 +37,9 @@ class Doctrine_Connection_Informix extends Doctrine_Connection {
* @param Doctrine_Manager $manager
* @param PDO $pdo database handle
*/
public function __construct(Doctrine_Manager $manager, PDO $pdo) {
public function __construct(Doctrine_Manager $manager, $adapter) {
// initialize all driver options
parent::__construct($manager, $pdo);
parent::__construct($manager, $adapter);
}
}

View File

@ -40,7 +40,7 @@ class Doctrine_Connection_Mssql extends Doctrine_Connection {
* @param Doctrine_Manager $manager
* @param PDO $pdo database handle
*/
public function __construct(Doctrine_Manager $manager, PDO $pdo) {
public function __construct(Doctrine_Manager $manager, $adapter) {
// initialize all driver options
$this->supported = array(
'sequences' => 'emulated',
@ -60,7 +60,7 @@ class Doctrine_Connection_Mssql extends Doctrine_Connection {
'prepared_statements' => 'emulated',
);
parent::__construct($manager, $pdo);
parent::__construct($manager, $adapter);
}
/**
* quoteIdentifier

View File

@ -40,10 +40,9 @@ class Doctrine_Connection_Mysql extends Doctrine_Connection_Common {
* @param Doctrine_Manager $manager
* @param PDO $pdo database handle
*/
public function __construct(Doctrine_Manager $manager,PDO $pdo) {
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$this->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS);
public function __construct(Doctrine_Manager $manager, $adapter) {
$adapter->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$this->supported = array(
'sequences' => 'emulated',
'indexes' => true,
@ -65,7 +64,7 @@ class Doctrine_Connection_Mysql extends Doctrine_Connection_Common {
'pattern_escaping' => true
);
parent::__construct($manager,$pdo);
parent::__construct($manager, $adapter);
}
/**

View File

@ -33,7 +33,7 @@ class Doctrine_Connection_Oracle extends Doctrine_Connection {
protected $driverName = 'Oracle';
public function __construct(Doctrine_Manager $manager, PDO $pdo) {
public function __construct(Doctrine_Manager $manager, $adapter) {
$this->supported = array(
'sequences' => true,
'indexes' => true,
@ -63,7 +63,7 @@ class Doctrine_Connection_Oracle extends Doctrine_Connection {
$this->options['default_text_field_length'] = 2000;
$this->options['result_prefetching'] = false;
parent::__construct($manager, $pdo);
parent::__construct($manager, $adapter);
}
/**
* Adds an driver-specific LIMIT clause to the query

View File

@ -40,7 +40,7 @@ class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common {
* @param Doctrine_Manager $manager
* @param PDO $pdo database handle
*/
public function __construct(Doctrine_Manager $manager, PDO $pdo) {
public function __construct(Doctrine_Manager $manager, $adapter) {
// initialize all driver options
$this->supported = array(
'sequences' => true,
@ -65,7 +65,7 @@ class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common {
$this->options['multi_query'] = false;
parent::__construct($manager, $pdo);
parent::__construct($manager, $adapter);
}
/**
* Set the charset on the current connection

View File

@ -41,7 +41,7 @@ class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common {
* @param Doctrine_Manager $manager
* @param PDO $pdo database handle
*/
public function __construct(Doctrine_Manager $manager, PDO $pdo) {
public function __construct(Doctrine_Manager $manager, $adapter) {
$this->supported = array(
'sequences' => 'emulated',
@ -70,7 +70,7 @@ class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common {
$this->options['database_extension'] = '';
$this->options['server_version'] = '';
parent::__construct($manager, $pdo);
parent::__construct($manager, $adapter);
}
/**
* initializes database functions missing in sqlite

View File

@ -54,80 +54,80 @@ class Doctrine_Db_EventListener_Chain extends Doctrine_Access implements Doctrin
$this->listeners[$name] = $listener;
}
public function onQuery(Doctrine_DB2 $dbh, $statement, array $args, $queryId) {
public function onQuery(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onQuery($dbh, $args);
$listener->onQuery($event);
}
}
public function onPreQuery(Doctrine_DB2 $dbh, $statement, array $args) {
public function onPreQuery(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPreQuery($dbh, $args);
$listener->onPreQuery($event);
}
}
public function onPreExec(Doctrine_DB2 $dbh, $statement, array $args) {
public function onPreExec(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPreExec($dbh, $args);
$listener->onPreExec($event);
}
}
public function onExec(Doctrine_DB2 $dbh, $statement, array $args) {
public function onExec(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onExec($dbh, $args);
$listener->onExec($event);
}
}
public function onPrePrepare(Doctrine_DB2 $dbh, $statement, array $args) {
public function onPrePrepare(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPrePrepare($dbh, $args);
$listener->onPrePrepare($event);
}
}
public function onPrepare(Doctrine_DB2 $dbh, $statement, array $args, $queryId) {
public function onPrepare(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPrepare($dbh, $args);
$listener->onPrepare($event);
}
}
public function onPreCommit(Doctrine_DB2 $dbh) {
public function onPreCommit(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPreCommit($dbh);
$listener->onPreCommit($event);
}
}
public function onCommit(Doctrine_DB2 $dbh) {
public function onCommit(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onCommit($dbh);
$listener->onCommit($event);
}
}
public function onPreRollBack(Doctrine_DB2 $dbh) {
public function onPreRollBack(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPreRollBack($dbh);
$listener->onPreRollBack($event);
}
}
public function onRollBack(Doctrine_DB2 $dbh) {
public function onRollBack(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onRollBack($dbh);
$listener->onRollBack($event);
}
}
public function onPreBeginTransaction(Doctrine_DB2 $dbh) {
public function onPreBeginTransaction(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPreBeginTransaction($dbh);
$listener->onPreBeginTransaction($event);
}
}
public function onBeginTransaction(Doctrine_DB2 $dbh) {
public function onBeginTransaction(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onBeginTransaction($dbh);
$listener->onBeginTransaction($event);
}
}
public function onPreExecute(Doctrine_Db_Statement $stmt, array $params) {
public function onPreExecute(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onPreExecute($stmt, $params);
$listener->onPreExecute($event);
}
}
public function onExecute(Doctrine_Db_Statement $stmt, array $params) {
public function onExecute(Doctrine_Db_Event $event) {
foreach($this->listeners as $listener) {
$listener->onExecute($stmt, $params);
$listener->onExecute($event);
}
}
}

View File

@ -55,7 +55,7 @@ class Doctrine_Db_Statement extends PDOStatement {
$this->executed = (bool) $executed;
}
public function execute(array $params) {
public function execute(array $params = array()) {
$event = new Doctrine_Db_Event($this, Doctrine_Db_Event::EXECUTE, $this->queryString);
$this->dbh->getListener()->onPreExecute($event);

View File

@ -94,7 +94,6 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
Doctrine::ATTR_AUTO_TYPE_VLD => true,
Doctrine::ATTR_CREATE_TABLES => true,
Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS,
Doctrine::ATTR_SHORT_ALIASES => false,
);
foreach($attributes as $attribute => $value) {
$old = $this->getAttribute($attribute);
@ -140,12 +139,16 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
* openConnection
* opens a new connection and saves it to Doctrine_Manager->connections
*
* @param PDO $pdo PDO database driver
* @param string $name name of the connection, if empty numeric key is used
* @throws Doctrine_Manager_Exception if trying to bind a connection with an existing name
* @param PDO|Doctrine_Adapter_Interface $adapter database driver
* @param string $name name of the connection, if empty numeric key is used
* @throws Doctrine_Manager_Exception if trying to bind a connection with an existing name
* @return Doctrine_Connection
*/
public function openConnection(PDO $pdo, $name = null) {
public function openConnection($adapter, $name = null) {
if( ! ($adapter instanceof PDO) && ! in_array('Doctrine_Adapter_Interface', class_implements($adapter)))
throw new Doctrine_Manager_Exception("First argument should be an instance of PDO or implement Doctrine_Adapter_Interface");
// initialize the default attributes
$this->setDefaultAttributes();
@ -153,32 +156,32 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
$name = (string) $name;
if(isset($this->connections[$name]))
throw new Doctrine_Manager_Exception("Connection with $name already exists!");
} else {
$name = $this->index;
$this->index++;
}
switch($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)):
switch($adapter->getAttribute(PDO::ATTR_DRIVER_NAME)):
case "mysql":
$this->connections[$name] = new Doctrine_Connection_Mysql($this,$pdo);
$this->connections[$name] = new Doctrine_Connection_Mysql($this, $adapter);
break;
case "sqlite":
$this->connections[$name] = new Doctrine_Connection_Sqlite($this,$pdo);
$this->connections[$name] = new Doctrine_Connection_Sqlite($this, $adapter);
break;
case "pgsql":
$this->connections[$name] = new Doctrine_Connection_Pgsql($this,$pdo);
$this->connections[$name] = new Doctrine_Connection_Pgsql($this, $adapter);
break;
case "oci":
$this->connections[$name] = new Doctrine_Connection_Oracle($this,$pdo);
$this->connections[$name] = new Doctrine_Connection_Oracle($this, $adapter);
break;
case "mssql":
$this->connections[$name] = new Doctrine_Connection_Mssql($this,$pdo);
$this->connections[$name] = new Doctrine_Connection_Mssql($this, $adapter);
break;
case "firebird":
$this->connections[$name] = new Doctrine_Connection_Firebird($this,$pdo);
$this->connections[$name] = new Doctrine_Connection_Firebird($this, $adapter);
break;
case "informix":
$this->connections[$name] = new Doctrine_Connection_Informix($this,$pdo);
$this->connections[$name] = new Doctrine_Connection_Informix($this, $adapter);
break;
endswitch;

View File

@ -218,7 +218,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
if( ! empty($having))
$q .= ' HAVING ' . implode(' AND ',$having);
$params = array_merge($this->params, $params);
$params = array_merge($this->params, $params);
$a = $this->getConnection()->execute($q, $params)->fetch(PDO::FETCH_NUM);
return $a[0];
@ -349,8 +349,6 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$parser->parse($args[0]);
break;
case 'where':
call_user_func_array(array($this, 'addWhere'), $args);
break;
case 'having':
case 'orderby':
case 'groupby':
@ -572,6 +570,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$subquery .= ' FROM ' . $table->getTableName() . ' ' . $alias;
foreach($this->parts['join'] as $parts) {
foreach($parts as $part) {
// preserve LEFT JOINs only if needed
@ -1102,11 +1101,11 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
}
if( ! $fk->isOneToOne()) {
if( ! $loadFields || $table->usesInheritanceMap()) {
$this->subqueryAliases[] = $tname2;
}
$this->needsSubquery = true;
$this->needsSubquery = true;
}
if( ! $loadFields || $fk->getTable()->usesInheritanceMap()) {
$this->subqueryAliases[] = $tname2;
}
if($fk instanceof Doctrine_Relation_Association) {

View File

@ -25,7 +25,7 @@ GLOBAL $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_DEFAULT; // DEFAULT, NUM, ASSOC or BOTH. Default follows native driver default...
function NewDataDictionary(PDO $conn) {
function NewDataDictionary($conn) {
$dbtype = $conn->getAttribute(PDO::ATTR_DRIVER_NAME);

View File

@ -157,8 +157,7 @@ class Doctrine_ConnectionTestCase extends Doctrine_UnitTestCase {
$this->assertTrue(is_numeric($user->Phonenumber[0]->entity_id));
$this->assertTrue($user->Phonenumber->count(), 4);
$this->assertEqual($user->Group->count(), 2);
$this->assertTrue($this->dbh instanceof Doctrine_DB);
$user = $this->objTable->find(5);
@ -303,8 +302,8 @@ class Doctrine_ConnectionTestCase extends Doctrine_UnitTestCase {
$email = $this->connection->create("Email");
$this->assertTrue($email instanceof Email);
}
public function testGetDBH() {
$this->assertTrue($this->connection->getDBH() instanceof PDO);
public function testGetDbh() {
$this->assertTrue($this->connection->getDBH() instanceof Doctrine_Db);
}
public function testCount() {
$this->assertTrue(is_integer(count($this->connection)));

View File

@ -18,21 +18,23 @@ class Doctrine_Db_TestValidListener extends Doctrine_Db_EventListener { }
class Doctrine_Db_TestInvalidListener { }
class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
protected $dbh;
public function prepareData() { }
public function prepareTables() { }
public function init() { }
public function testFetchAll() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$dbh->connect();
$this->dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$this->dbh->connect();
$dbh->query('CREATE TABLE entity (id INTEGER, name TEXT)');
$this->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')");
$this->dbh->query("INSERT INTO entity (id, name) VALUES (1, 'zYne')");
$this->dbh->query("INSERT INTO entity (id, name) VALUES (2, 'John')");
$a = $dbh->fetchAll('SELECT * FROM entity');
$a = $this->dbh->fetchAll('SELECT * FROM entity');
$this->assertEqual($a, array (
@ -49,13 +51,11 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
));
}
public function testFetchOne() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$c = $dbh->fetchOne('SELECT COUNT(1) FROM entity');
$c = $this->dbh->fetchOne('SELECT COUNT(1) FROM entity');
$this->assertEqual($c, 2);
$c = $dbh->fetchOne('SELECT COUNT(1) FROM entity WHERE id = ?', array(1));
$c = $this->dbh->fetchOne('SELECT COUNT(1) FROM entity WHERE id = ?', array(1));
$this->assertEqual($c, 1);
}
@ -64,32 +64,28 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
}
public function testFetchColumn() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$a = $dbh->fetchColumn('SELECT * FROM entity');
$a = $this->dbh->fetchColumn('SELECT * FROM entity');
$this->assertEqual($a, array (
0 => '1',
1 => '2',
));
$a = $dbh->fetchColumn('SELECT * FROM entity WHERE id = ?', array(1));
$a = $this->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');
$a = $this->dbh->fetchArray('SELECT * FROM entity');
$this->assertEqual($a, array (
0 => '1',
1 => 'zYne',
));
$a = $dbh->fetchArray('SELECT * FROM entity WHERE id = ?', array(1));
$a = $this->dbh->fetchArray('SELECT * FROM entity WHERE id = ?', array(1));
$this->assertEqual($a, array (
0 => '1',
@ -97,16 +93,14 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
));
}
public function testFetchRow() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$c = $dbh->fetchRow('SELECT * FROM entity');
$c = $this->dbh->fetchRow('SELECT * FROM entity');
$this->assertEqual($c, array (
'id' => '1',
'name' => 'zYne',
));
$c = $dbh->fetchRow('SELECT * FROM entity WHERE id = ?', array(1));
$c = $this->dbh->fetchRow('SELECT * FROM entity WHERE id = ?', array(1));
$this->assertEqual($c, array (
'id' => '1',
@ -117,62 +111,61 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
}
public function testAddValidEventListener() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_EventListener);
$this->dbh->setListener(new Doctrine_Db_EventListener());
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
try {
$ret = $dbh->addListener(new Doctrine_Db_TestLogger());
$ret = $this->dbh->addListener(new Doctrine_Db_TestLogger());
$this->pass();
$this->assertTrue($ret instanceof Doctrine_Db2);
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
try {
$ret = $dbh->addListener(new Doctrine_Db_TestValidListener());
$ret = $this->dbh->addListener(new Doctrine_Db_TestValidListener());
$this->pass();
$this->assertTrue($ret instanceof Doctrine_Db2);
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
$this->assertTrue($dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
$this->assertTrue($this->dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
try {
$ret = $dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
$ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
$this->pass();
$this->assertTrue($ret instanceof Doctrine_Db2);
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
$this->assertTrue($dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
$this->assertTrue($dbh->getListener()->get('chain') instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
$this->assertTrue($this->dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
$this->assertTrue($this->dbh->getListener()->get('chain') instanceof Doctrine_Db_EventListener_Chain);
// replacing
try {
$ret = $dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
$ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
$this->pass();
$this->assertTrue($ret instanceof Doctrine_Db2);
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
$this->assertTrue($dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
$this->assertTrue($dbh->getListener()->get('chain') instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
$this->assertTrue($this->dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
$this->assertTrue($this->dbh->getListener()->get('chain') instanceof Doctrine_Db_EventListener_Chain);
}
public function testListeningEventsWithSingleListener() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$dbh->connect();
$dbh->setListener(new Doctrine_Db_TestLogger());
$listener = $dbh->getListener();
$stmt = $dbh->prepare('INSERT INTO entity (id) VALUES(?)');
$this->dbh->setListener(new Doctrine_Db_TestLogger());
$listener = $this->dbh->getListener();
$stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)');
$this->assertEqual($listener->pop(), 'onPrepare');
$this->assertEqual($listener->pop(), 'onPrePrepare');
@ -182,19 +175,19 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($listener->pop(), 'onExecute');
$this->assertEqual($listener->pop(), 'onPreExecute');
$dbh->exec('DELETE FROM entity');
$this->dbh->exec('DELETE FROM entity');
$this->assertEqual($listener->pop(), 'onExec');
$this->assertEqual($listener->pop(), 'onPreExec');
$dbh->beginTransaction();
$this->dbh->beginTransaction();
$this->assertEqual($listener->pop(), 'onBeginTransaction');
$this->assertEqual($listener->pop(), 'onPreBeginTransaction');
$dbh->query('INSERT INTO entity (id) VALUES (1)');
$this->dbh->query('INSERT INTO entity (id) VALUES (1)');
$dbh->commit();
$this->dbh->commit();
$this->assertEqual($listener->pop(), 'onCommit');
$this->assertEqual($listener->pop(), 'onPreCommit');
@ -202,18 +195,18 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($listener->pop(), 'onQuery');
$this->assertEqual($listener->pop(), 'onPreQuery');
$dbh->query('DROP TABLE entity');
}
public function testListeningEventsWithListenerChain() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$dbh->connect();
$dbh->addListener(new Doctrine_Db_TestLogger());
$dbh->addListener(new Doctrine_Db_TestLogger());
$this->dbh->query('DROP TABLE entity');
$dbh->query('CREATE TABLE entity (id INT)');
$this->dbh->addListener(new Doctrine_Db_TestLogger());
$this->dbh->addListener(new Doctrine_Db_TestLogger());
$listener = $dbh->getListener()->get(0);
$listener2 = $dbh->getListener()->get(1);
$this->dbh->query('CREATE TABLE entity (id INT)');
$listener = $this->dbh->getListener()->get(0);
$listener2 = $this->dbh->getListener()->get(1);
$this->assertEqual($listener->pop(), 'onQuery');
$this->assertEqual($listener->pop(), 'onPreQuery');
@ -221,7 +214,7 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($listener2->pop(), 'onPreQuery');
$stmt = $dbh->prepare('INSERT INTO entity (id) VALUES(?)');
$stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)');
$this->assertEqual($listener->pop(), 'onPrepare');
$this->assertEqual($listener->pop(), 'onPrePrepare');
@ -237,7 +230,7 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($listener2->pop(), 'onExecute');
$this->assertEqual($listener2->pop(), 'onPreExecute');
$dbh->exec('DELETE FROM entity');
$this->dbh->exec('DELETE FROM entity');
$this->assertEqual($listener->pop(), 'onExec');
$this->assertEqual($listener->pop(), 'onPreExec');
@ -245,7 +238,7 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($listener2->pop(), 'onExec');
$this->assertEqual($listener2->pop(), 'onPreExec');
$dbh->beginTransaction();
$this->dbh->beginTransaction();
$this->assertEqual($listener->pop(), 'onBeginTransaction');
$this->assertEqual($listener->pop(), 'onPreBeginTransaction');
@ -253,9 +246,9 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($listener2->pop(), 'onBeginTransaction');
$this->assertEqual($listener2->pop(), 'onPreBeginTransaction');
$dbh->query('INSERT INTO entity (id) VALUES (1)');
$this->dbh->query('INSERT INTO entity (id) VALUES (1)');
$dbh->commit();
$this->dbh->commit();
$this->assertEqual($listener->pop(), 'onCommit');
$this->assertEqual($listener->pop(), 'onPreCommit');
@ -263,44 +256,42 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($listener->pop(), 'onQuery');
$this->assertEqual($listener->pop(), 'onPreQuery');
$dbh->query('DROP TABLE entity');
$this->dbh->query('DROP TABLE entity');
}
public function testSetValidEventListener() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
try {
$dbh->setListener(new Doctrine_Db_TestLogger());
$this->dbh->setListener(new Doctrine_Db_TestLogger());
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_TestLogger);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestLogger);
try {
$dbh->setListener(new Doctrine_Db_TestValidListener());
$this->dbh->setListener(new Doctrine_Db_TestValidListener());
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_TestValidListener);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestValidListener);
try {
$dbh->setListener(new Doctrine_Db_EventListener_Chain());
$this->dbh->setListener(new Doctrine_Db_EventListener_Chain());
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
try {
$dbh->setListener(new Doctrine_Db_EventListener());
$this->dbh->setListener(new Doctrine_Db_EventListener());
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail();
}
$this->assertTrue($dbh->getListener() instanceof Doctrine_Db_EventListener);
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
}
public function testSetInvalidEventListener() {
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
try {
$dbh->setListener(new Doctrine_Db_TestInvalidListener());
$this->dbh->setListener(new Doctrine_Db_TestInvalidListener());
$this->fail();
} catch(Doctrine_Db_Exception $e) {
$this->pass();
@ -308,19 +299,19 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
}
public function testInvalidDSN() {
try {
$dbh = Doctrine_Db2::getConnection('');
$this->dbh = Doctrine_Db2::getConnection('');
$this->fail();
} catch(Doctrine_Db_Exception $e) {
$this->pass();
}
try {
$dbh = Doctrine_Db2::getConnection('unknown');
$this->dbh = Doctrine_Db2::getConnection('unknown');
$this->fail();
} catch(Doctrine_Db_Exception $e) {
$this->pass();
}
try {
$dbh = Doctrine_Db2::getConnection(0);
$this->dbh = Doctrine_Db2::getConnection(0);
$this->fail();
} catch(Doctrine_Db_Exception $e) {
$this->pass();
@ -328,7 +319,7 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
}
public function testInvalidScheme() {
try {
$dbh = Doctrine_Db2::getConnection('unknown://:memory:');
$this->dbh = Doctrine_Db2::getConnection('unknown://:memory:');
$this->fail();
} catch(Doctrine_Db_Exception $e) {
$this->pass();
@ -336,7 +327,7 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
}
public function testInvalidHost() {
try {
$dbh = Doctrine_Db2::getConnection('mysql://user:password@');
$this->dbh = Doctrine_Db2::getConnection('mysql://user:password@');
$this->fail();
} catch(Doctrine_Db_Exception $e) {
$this->pass();
@ -344,41 +335,41 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
}
public function testInvalidDatabase() {
try {
$dbh = Doctrine_Db2::getConnection('mysql://user:password@host/');
$this->dbh = Doctrine_Db2::getConnection('mysql://user:password@host/');
$this->fail();
} catch(Doctrine_Db_Exception $e) {
$this->pass();
}
}
public function testGetConnectionPdoLikeDSN() {
$dbh = Doctrine_Db2::getConnection('mysql:host=localhost;dbname=test', 'root', 'password');
$this->assertEqual($dbh->getDSN(), 'mysql:host=localhost;dbname=test');
$this->assertEqual($dbh->getUsername(), 'root');
$this->assertEqual($dbh->getPassword(), 'password');
$this->dbh = Doctrine_Db2::getConnection('mysql:host=localhost;dbname=test', 'root', 'password');
$this->assertEqual($this->dbh->getOption('dsn'), 'mysql:host=localhost;dbname=test');
$this->assertEqual($this->dbh->getOption('username'), 'root');
$this->assertEqual($this->dbh->getOption('password'), 'password');
$dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$this->dbh = Doctrine_Db2::getConnection('sqlite::memory:');
$this->assertEqual($dbh->getDSN(), 'sqlite::memory:');
$this->assertEqual($dbh->getUsername(), null);
$this->assertEqual($dbh->getPassword(), null);
$this->assertEqual($this->dbh->getOption('dsn'), 'sqlite::memory:');
$this->assertEqual($this->dbh->getOption('username'), false);
$this->assertEqual($this->dbh->getOption('password'), false);
}
public function testDriverName() {
}
public function testGetConnectionWithPearLikeDSN() {
$dbh = Doctrine_Db2::getConnection('mysql://zYne:password@localhost/test');
$this->assertEqual($dbh->getDSN(), 'mysql:host=localhost;dbname=test');
$this->assertEqual($dbh->getUsername(), 'zYne');
$this->assertEqual($dbh->getPassword(), 'password');
$this->dbh = Doctrine_Db2::getConnection('mysql://zYne:password@localhost/test');
$this->assertEqual($this->dbh->getOption('dsn'), 'mysql:host=localhost;dbname=test');
$this->assertEqual($this->dbh->getOption('username'), 'zYne');
$this->assertEqual($this->dbh->getOption('password'), 'password');
$dbh = Doctrine_Db2::getConnection('sqlite://:memory:');
$this->dbh = Doctrine_Db2::getConnection('sqlite://:memory:');
$this->assertEqual($dbh->getDSN(), 'sqlite::memory:');
$this->assertEqual($dbh->getUsername(), null);
$this->assertEqual($dbh->getPassword(), null);
$this->assertEqual($this->dbh->getOption('dsn'), 'sqlite::memory:');
$this->assertEqual($this->dbh->getOption('username'), false);
$this->assertEqual($this->dbh->getOption('password'), false);
}
}

View File

@ -126,6 +126,7 @@ class Doctrine_Db_Profiler_TestCase extends Doctrine_UnitTestCase {
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail($e->__toString());
$this->dbh->rollback();
}
$this->assertEqual($this->profiler->lastEvent()->getQuery(), null);

View File

@ -47,7 +47,7 @@ class Doctrine_Query_ComponentAlias_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 8);
$this->assertTrue($users[0]->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($q->getQuery(),
"SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p2.id AS p2__id, p2.p AS p2__p, p2.entity_id AS p2__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN p AS p2 ON e2.id = p2.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
"SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id, p2.id AS p2__id, p2.phonenumber AS p2__phonenumber, p2.entity_id AS p2__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
$this->assertEqual($count, count($this->dbh));
}
}

View File

@ -7,7 +7,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
parent::prepareTables();
}
/**
public function testLimitWithOneToOneLeftJoin() {
$q = new Doctrine_Query($this->connection);
$q->from('User(id).Email')->limit(5);
@ -73,6 +73,8 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
public function testLimitWithOneToManyLeftJoinAndOrderBy() {
$q = new Doctrine_Query($this->connection);
$q->from("User(name)")->where("User.Phonenumber.phonenumber LIKE '%123%'")->orderby("User.Email.address")->limit(5);
$users = $q->execute();
$this->assertEqual($users[0]->name, 'Arnold Schwarzenegger');
@ -84,7 +86,6 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(), 5);
}
public function testLimitWithOneToManyInnerJoin() {
$this->query->select('u.id')->from("User u INNER JOIN u.Phonenumber");
$this->query->limit(5);
@ -202,20 +203,20 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
}
public function testLimitAttribute() {
$this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS);
$this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS);
$this->connection->clear();
$q = new Doctrine_Query();
$q->from("User")->where("User.Group.id = ?")->orderby("User.id DESC")->limit(5);
$users = $q->execute(array(3));
print $q;
$this->assertEqual($users->count(), 3);
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id WHERE e2.id = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL)) ORDER BY e.id DESC LIMIT 5");
$this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS);
}
*/
public function testLimitWithNormalManyToMany() {
$coll = new Doctrine_Collection($this->connection->getTable("Photo"));
$tag = new Tag();

View File

@ -33,7 +33,7 @@ class Doctrine_Record_TestCase extends Doctrine_UnitTestCase {
try {
$null->save();
$this->fail();
} catch(Doctrine_Exception $e) {
} catch(Exception $e) {
$this->pass();
$this->connection->rollback();
}

View File

@ -65,18 +65,21 @@ class Doctrine_UnitTestCase extends UnitTestCase {
if($this->manager->count() > 0) {
$this->connection = $this->manager->getConnection(0);
$this->connection->evictTables();
$this->dbh = $this->connection->getDBH();
$this->dbh = $this->connection->getDBH();
$this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
} else {
//$this->dbh = Doctrine_DB::getConnection();
$this->dbh = Doctrine_DB::getConn("sqlite::memory:");
$this->dbh = Doctrine_Db2::getConnection("sqlite::memory:");
//$this->dbh = new PDO("sqlite::memory:");
$this->connection = $this->manager->openConnection($this->dbh);
$this->listener = new Doctrine_EventListener_Debugger();
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
}
$this->connection->setListener(new Doctrine_EventListener());
$this->query = new Doctrine_Query($this->connection);
$this->prepareTables();