From 8eef3f44c4cc2fcbe6946890cfff93056a0ccfd0 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 11 Jun 2007 23:25:46 +0000 Subject: [PATCH] updated doc blocks, added savepoint as optional transaction parameter --- lib/Doctrine/Connection.php | 124 ++++++++++++++++++++++++--------- lib/Doctrine/Export.php | 6 +- lib/Doctrine/Export/Mysql.php | 2 +- lib/Doctrine/Export/Sqlite.php | 2 +- lib/Doctrine/Table.php | 2 +- lib/Doctrine/Transaction.php | 35 +++++++--- 6 files changed, 124 insertions(+), 47 deletions(-) diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index 045ce6a5a..006392845 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -113,7 +113,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun 'import' => false, 'sequence' => false, 'unitOfWork' => false, - 'formatter' => false + 'formatter' => false, + 'util' => false, ); /** * @var array $properties an array of connection properties @@ -233,6 +234,59 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return $this->dbh; } + /** + * connect + * connects into database + * + * @return boolean + */ + public function connect() + { + /** + if ($this->isConnected) { + return false; + } + + $event = new Doctrine_Db_Event($this, Doctrine_Db_Event::CONNECT); + + $this->listener->onPreConnect($event); + + $e = explode(':', $this->options['dsn']); + $found = false; + + if (extension_loaded('pdo')) { + if (in_array($e[0], PDO::getAvailableDrivers())) { + $this->dbh = new PDO($this->options['dsn'], $this->options['username'], $this->options['password']); + $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $found = true; + } + } + + if ( ! $found) { + $class = 'Doctrine_Adapter_' . ucwords($e[0]); + + if (class_exists($class)) { + $this->dbh = new $class($this->options['dsn'], $this->options['username'], $this->options['password']); + } else { + throw new Doctrine_Connection_Exception("Couldn't locate driver named " . $e[0]); + } + } + + + foreach($this->pendingAttributes as $attr => $value) { + // some drivers don't support setting this so we just skip it + if($attr == Doctrine::ATTR_DRIVER_NAME) { + continue; + } + $this->dbh->setAttribute($attr, $value); + } + + $this->isConnected = true; + + $this->listener->onConnect($event); + return true; + */ + } /** * converts given driver name * @@ -762,17 +816,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * addTable * adds a Doctrine_Table object into connection registry * - * @param $objTable a Doctrine_Table object to be added into registry + * @param $table a Doctrine_Table object to be added into registry * @return boolean */ - public function addTable(Doctrine_Table $objTable) + public function addTable(Doctrine_Table $table) { - $name = $objTable->getComponentName(); + $name = $table->getComponentName(); if (isset($this->tables[$name])) { return false; } - $this->tables[$name] = $objTable; + $this->tables[$name] = $table; return true; } /** @@ -850,40 +904,55 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } /** * beginTransaction - * starts a new transaction + * Start a transaction or set a savepoint. * - * this method can be listened by onPreBeginTransaction and onBeginTransaction - * listener methods + * if trying to set a savepoint and there is no active transaction + * a new transaction is being started * - * @return void + * Listeners: onPreTransactionBegin, onTransactionBegin + * + * @param string $savepoint name of a savepoint to set + * @throws Doctrine_Transaction_Exception if the transaction fails at database level + * @return integer current transaction nesting level */ - public function beginTransaction() + public function beginTransaction($savepoint = null) { - $this->transaction->beginTransaction(); + $this->transaction->beginTransaction($savepoint); } /** - * commits the current transaction - * if lockmode is optimistic this method starts a transaction - * and commits it instantly + * commit + * Commit the database changes done during a transaction that is in + * progress or release a savepoint. This function may only be called when + * auto-committing is disabled, otherwise it will fail. * - * @return void + * Listeners: onPreTransactionCommit, onTransactionCommit + * + * @param string $savepoint name of a savepoint to release + * @throws Doctrine_Transaction_Exception if the transaction fails at PDO level + * @throws Doctrine_Validator_Exception if the transaction fails due to record validations + * @return boolean false if commit couldn't be performed, true otherwise */ - public function commit() + public function commit($savepoint = null) { - $this->transaction->commit(); + $this->transaction->commit($savepoint); } /** * rollback - * rolls back all transactions + * Cancel any database changes done during a transaction or since a specific + * savepoint that is in progress. This function may only be called when + * auto-committing is disabled, otherwise it will fail. Therefore, a new + * transaction is implicitly started after canceling the pending changes. * - * this method also listens to onPreTransactionRollback and onTransactionRollback - * eventlisteners + * this method can be listened with onPreTransactionRollback and onTransactionRollback + * eventlistener methods * - * @return void + * @param string $savepoint name of a savepoint to rollback to + * @throws Doctrine_Transaction_Exception if the rollback operation fails at database level + * @return boolean false if rollback couldn't be performed, true otherwise */ - public function rollback() + public function rollback($savepoint = null) { - $this->transaction->rollback(); + $this->transaction->rollback($savepoint); } /** * returns a string representation of this object @@ -893,14 +962,5 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { return Doctrine_Lib::getConnectionAsString($this); } - /** - * Enter description here... - * - * @param unknown_type $name - */ - public function getIndexName($name) - { - return $this->formatter->getIndexName($name); - } } diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index a14cdcad1..ff86037f3 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -66,7 +66,7 @@ class Doctrine_Export extends Doctrine_Connection_Module */ public function dropIndex($table, $name) { - $name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true); + $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true); return $this->conn->exec('DROP INDEX ' . $name); } /** @@ -80,7 +80,7 @@ class Doctrine_Export extends Doctrine_Connection_Module public function dropConstraint($table, $name, $primary = false) { $table = $this->conn->quoteIdentifier($table, true); - $name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true); + $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true); return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name); } /** @@ -226,7 +226,7 @@ class Doctrine_Export extends Doctrine_Connection_Module public function createConstraint($table, $name, $definition) { $table = $this->conn->quoteIdentifier($table, true); - $name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true); + $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true); $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name; if (!empty($definition['primary'])) { $query .= ' PRIMARY KEY'; diff --git a/lib/Doctrine/Export/Mysql.php b/lib/Doctrine/Export/Mysql.php index 830210431..dd55c904e 100644 --- a/lib/Doctrine/Export/Mysql.php +++ b/lib/Doctrine/Export/Mysql.php @@ -579,7 +579,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export public function dropIndex($table, $name) { $table = $this->conn->quoteIdentifier($table, true); - $name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true); + $name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true); return $this->conn->exec('DROP INDEX ' . $name . ' ON ' . $table); } /** diff --git a/lib/Doctrine/Export/Sqlite.php b/lib/Doctrine/Export/Sqlite.php index b54a7d948..0d1994934 100644 --- a/lib/Doctrine/Export/Sqlite.php +++ b/lib/Doctrine/Export/Sqlite.php @@ -68,7 +68,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export public function createIndexSql($table, $name, array $definition) { $table = $this->conn->quoteIdentifier($table, true); - $name = $this->conn->getIndexName($name); + $name = $this->conn->formatter->getIndexName($name); $query = 'CREATE INDEX ' . $name . ' ON ' . $table; $query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')'; diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 354640ed6..e312e849b 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -462,7 +462,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable */ public function addIndex($index, array $definition) { - $index = $this->conn->getIndexName($index); + $index = $this->conn->formatter->getIndexName($index); $this->options['indexes'][$index] = $definition; } /** diff --git a/lib/Doctrine/Transaction.php b/lib/Doctrine/Transaction.php index dd827c232..e3d7fb87a 100644 --- a/lib/Doctrine/Transaction.php +++ b/lib/Doctrine/Transaction.php @@ -207,10 +207,13 @@ class Doctrine_Transaction extends Doctrine_Connection_Module * Listeners: onPreTransactionBegin, onTransactionBegin * * @param string $savepoint name of a savepoint to set + * @throws Doctrine_Transaction_Exception if the transaction fails at database level * @return integer current transaction nesting level */ public function beginTransaction($savepoint = null) { + $this->conn->connect(); + if ( ! is_null($savepoint)) { $this->beginTransaction(); @@ -221,7 +224,11 @@ class Doctrine_Transaction extends Doctrine_Connection_Module if ($this->transactionLevel == 0) { $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($this->conn); - $this->conn->getDbh()->beginTransaction(); + try { + $this->conn->getDbh()->beginTransaction(); + } catch(Exception $e) { + throw new Doctrine_Transaction_Exception($e->getMessage()); + } $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($this->conn); } @@ -240,14 +247,17 @@ class Doctrine_Transaction extends Doctrine_Connection_Module * Listeners: onPreTransactionCommit, onTransactionCommit * * @param string $savepoint name of a savepoint to release - * @throws Doctrine_Transaction_Exception if the transaction fails at PDO level + * @throws Doctrine_Transaction_Exception if the transaction fails at database level * @throws Doctrine_Validator_Exception if the transaction fails due to record validations * @return boolean false if commit couldn't be performed, true otherwise */ public function commit($savepoint = null) { - if ($this->transactionLevel == 0) + $this->conn->connect(); + + if ($this->transactionLevel == 0) { return false; + } if ( ! is_null($savepoint)) { $this->transactionLevel = $this->removeSavePoints($savepoint); @@ -263,7 +273,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module } catch(Exception $e) { $this->rollback(); - throw new Doctrine_Transaction_Exception($e->__toString()); + throw new Doctrine_Transaction_Exception($e->getMessage()); } if ( ! empty($this->invalid)) { $this->rollback(); @@ -299,16 +309,20 @@ class Doctrine_Transaction extends Doctrine_Connection_Module * auto-committing is disabled, otherwise it will fail. Therefore, a new * transaction is implicitly started after canceling the pending changes. * - * this method listens to onPreTransactionRollback and onTransactionRollback + * this method can be listened with onPreTransactionRollback and onTransactionRollback * eventlistener methods * - * @param string $savepoint name of a savepoint to rollback to + * @param string $savepoint name of a savepoint to rollback to + * @throws Doctrine_Transaction_Exception if the rollback operation fails at database level * @return boolean false if rollback couldn't be performed, true otherwise */ public function rollback($savepoint = null) { - if ($this->transactionLevel == 0) + $this->conn->connect(); + + if ($this->transactionLevel == 0) { return false; + } $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionRollback($this->conn); @@ -321,8 +335,11 @@ class Doctrine_Transaction extends Doctrine_Connection_Module $this->deteles = array(); $this->transactionLevel = 0; - - $this->conn->getDbh()->rollback(); + try { + $this->conn->getDbh()->rollback(); + } catch (Exception $e) { + throw new Doctrine_Transaction_Exception($e->getMessage()); + } } $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionRollback($this->conn);