Commit 8eef3f44 authored by zYne's avatar zYne

updated doc blocks, added savepoint as optional transaction parameter

parent 8573d33f
...@@ -113,7 +113,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -113,7 +113,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
'import' => false, 'import' => false,
'sequence' => false, 'sequence' => false,
'unitOfWork' => false, 'unitOfWork' => false,
'formatter' => false 'formatter' => false,
'util' => false,
); );
/** /**
* @var array $properties an array of connection properties * @var array $properties an array of connection properties
...@@ -233,6 +234,59 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -233,6 +234,59 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
{ {
return $this->dbh; 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 * converts given driver name
* *
...@@ -762,17 +816,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -762,17 +816,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
* addTable * addTable
* adds a Doctrine_Table object into connection registry * 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 * @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])) { if (isset($this->tables[$name])) {
return false; return false;
} }
$this->tables[$name] = $objTable; $this->tables[$name] = $table;
return true; return true;
} }
/** /**
...@@ -850,40 +904,55 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -850,40 +904,55 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
} }
/** /**
* beginTransaction * beginTransaction
* starts a new transaction * Start a transaction or set a savepoint.
* *
* this method can be listened by onPreBeginTransaction and onBeginTransaction * if trying to set a savepoint and there is no active transaction
* listener methods * 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 * commit
* if lockmode is optimistic this method starts a transaction * Commit the database changes done during a transaction that is in
* and commits it instantly * 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 * 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 * this method can be listened with onPreTransactionRollback and onTransactionRollback
* eventlisteners * 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 * returns a string representation of this object
...@@ -893,14 +962,5 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -893,14 +962,5 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
{ {
return Doctrine_Lib::getConnectionAsString($this); return Doctrine_Lib::getConnectionAsString($this);
} }
/**
* Enter description here...
*
* @param unknown_type $name
*/
public function getIndexName($name)
{
return $this->formatter->getIndexName($name);
}
} }
...@@ -66,7 +66,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -66,7 +66,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
*/ */
public function dropIndex($table, $name) 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); return $this->conn->exec('DROP INDEX ' . $name);
} }
/** /**
...@@ -80,7 +80,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -80,7 +80,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
public function dropConstraint($table, $name, $primary = false) public function dropConstraint($table, $name, $primary = false)
{ {
$table = $this->conn->quoteIdentifier($table, true); $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); return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name);
} }
/** /**
...@@ -226,7 +226,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -226,7 +226,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
public function createConstraint($table, $name, $definition) public function createConstraint($table, $name, $definition)
{ {
$table = $this->conn->quoteIdentifier($table, true); $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; $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name;
if (!empty($definition['primary'])) { if (!empty($definition['primary'])) {
$query .= ' PRIMARY KEY'; $query .= ' PRIMARY KEY';
......
...@@ -579,7 +579,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export ...@@ -579,7 +579,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
public function dropIndex($table, $name) public function dropIndex($table, $name)
{ {
$table = $this->conn->quoteIdentifier($table, true); $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); return $this->conn->exec('DROP INDEX ' . $name . ' ON ' . $table);
} }
/** /**
......
...@@ -68,7 +68,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export ...@@ -68,7 +68,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
public function createIndexSql($table, $name, array $definition) public function createIndexSql($table, $name, array $definition)
{ {
$table = $this->conn->quoteIdentifier($table, true); $table = $this->conn->quoteIdentifier($table, true);
$name = $this->conn->getIndexName($name); $name = $this->conn->formatter->getIndexName($name);
$query = 'CREATE INDEX ' . $name . ' ON ' . $table; $query = 'CREATE INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')'; $query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
......
...@@ -462,7 +462,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -462,7 +462,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
*/ */
public function addIndex($index, array $definition) public function addIndex($index, array $definition)
{ {
$index = $this->conn->getIndexName($index); $index = $this->conn->formatter->getIndexName($index);
$this->options['indexes'][$index] = $definition; $this->options['indexes'][$index] = $definition;
} }
/** /**
......
...@@ -207,10 +207,13 @@ class Doctrine_Transaction extends Doctrine_Connection_Module ...@@ -207,10 +207,13 @@ class Doctrine_Transaction extends Doctrine_Connection_Module
* Listeners: onPreTransactionBegin, onTransactionBegin * Listeners: onPreTransactionBegin, onTransactionBegin
* *
* @param string $savepoint name of a savepoint to set * @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 * @return integer current transaction nesting level
*/ */
public function beginTransaction($savepoint = null) public function beginTransaction($savepoint = null)
{ {
$this->conn->connect();
if ( ! is_null($savepoint)) { if ( ! is_null($savepoint)) {
$this->beginTransaction(); $this->beginTransaction();
...@@ -221,7 +224,11 @@ class Doctrine_Transaction extends Doctrine_Connection_Module ...@@ -221,7 +224,11 @@ class Doctrine_Transaction extends Doctrine_Connection_Module
if ($this->transactionLevel == 0) { if ($this->transactionLevel == 0) {
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($this->conn); $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($this->conn);
try {
$this->conn->getDbh()->beginTransaction(); $this->conn->getDbh()->beginTransaction();
} catch(Exception $e) {
throw new Doctrine_Transaction_Exception($e->getMessage());
}
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($this->conn); $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($this->conn);
} }
...@@ -240,14 +247,17 @@ class Doctrine_Transaction extends Doctrine_Connection_Module ...@@ -240,14 +247,17 @@ class Doctrine_Transaction extends Doctrine_Connection_Module
* Listeners: onPreTransactionCommit, onTransactionCommit * Listeners: onPreTransactionCommit, onTransactionCommit
* *
* @param string $savepoint name of a savepoint to release * @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 * @throws Doctrine_Validator_Exception if the transaction fails due to record validations
* @return boolean false if commit couldn't be performed, true otherwise * @return boolean false if commit couldn't be performed, true otherwise
*/ */
public function commit($savepoint = null) public function commit($savepoint = null)
{ {
if ($this->transactionLevel == 0) $this->conn->connect();
if ($this->transactionLevel == 0) {
return false; return false;
}
if ( ! is_null($savepoint)) { if ( ! is_null($savepoint)) {
$this->transactionLevel = $this->removeSavePoints($savepoint); $this->transactionLevel = $this->removeSavePoints($savepoint);
...@@ -263,7 +273,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module ...@@ -263,7 +273,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module
} catch(Exception $e) { } catch(Exception $e) {
$this->rollback(); $this->rollback();
throw new Doctrine_Transaction_Exception($e->__toString()); throw new Doctrine_Transaction_Exception($e->getMessage());
} }
if ( ! empty($this->invalid)) { if ( ! empty($this->invalid)) {
$this->rollback(); $this->rollback();
...@@ -299,16 +309,20 @@ class Doctrine_Transaction extends Doctrine_Connection_Module ...@@ -299,16 +309,20 @@ class Doctrine_Transaction extends Doctrine_Connection_Module
* auto-committing is disabled, otherwise it will fail. Therefore, a new * auto-committing is disabled, otherwise it will fail. Therefore, a new
* transaction is implicitly started after canceling the pending changes. * 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 * 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 * @return boolean false if rollback couldn't be performed, true otherwise
*/ */
public function rollback($savepoint = null) public function rollback($savepoint = null)
{ {
if ($this->transactionLevel == 0) $this->conn->connect();
if ($this->transactionLevel == 0) {
return false; return false;
}
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionRollback($this->conn); $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionRollback($this->conn);
...@@ -321,8 +335,11 @@ class Doctrine_Transaction extends Doctrine_Connection_Module ...@@ -321,8 +335,11 @@ class Doctrine_Transaction extends Doctrine_Connection_Module
$this->deteles = array(); $this->deteles = array();
$this->transactionLevel = 0; $this->transactionLevel = 0;
try {
$this->conn->getDbh()->rollback(); $this->conn->getDbh()->rollback();
} catch (Exception $e) {
throw new Doctrine_Transaction_Exception($e->getMessage());
}
} }
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionRollback($this->conn); $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionRollback($this->conn);
......
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