Commit c6bd1147 authored by zYne's avatar zYne

Refactored Doctrine_Connection

parent c5f7572c
......@@ -223,16 +223,38 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$query = 'DELETE FROM '. $table . ' WHERE ' . implode(' AND ', $condition);
$affectedRows = $this->dbh->exec($query);
$insert = implode(', ', array_keys($values));
$query = 'INSERT INTO ' . $table . ' (' . $insert . ') VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')';
$stmt = $this->dbh->prepare($query);
$stmt->execute($values);
$this->insert($table, $values);
$affectedRows++;
return $affectedRows;
}
/**
* Inserts a table row with specified data.
*
* @param string $table The table to insert data into.
* @param array $values An associateve array containing column-value pairs.
* @return boolean
*/
public function insert($table, array $values = array()) {
if(empty($values))
return false;
// column names are specified as array keys
$cols = array_keys($values);
// build the statement
$query = "INSERT INTO $table "
. '(' . implode(', ', $cols) . ') '
. 'VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')';
// prepare and execute the statement
$stmt = $this->dbh->prepare($query);
$stmt->execute(array_values($values));
return true;
}
/**
* returns the next value in the given sequence
*
......@@ -427,36 +449,14 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
* saves all the records from all tables
* this operation is isolated using a transaction
*
* @throws PDOException if something went wrong at database level
* @return void
*/
public function flush() {
$this->beginTransaction();
$this->saveAll();
$this->unitOfWork->saveAll();
$this->commit();
}
/**
* saveAll
* persists all the records from all tables
*
* @return void
*/
private function saveAll() {
$tree = $this->unitOfWork->buildFlushTree($this->tables);
foreach($tree as $name) {
$table = $this->tables[$name];
foreach($table->getRepository() as $record) {
$this->save($record);
}
}
foreach($tree as $name) {
$table = $this->tables[$name];
foreach($table->getRepository() as $record) {
$this->unitOfWork->saveAssociations($record);
}
}
}
/**
* clear
* clears all repositories
......@@ -503,6 +503,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
/**
* beginTransaction
* starts a new transaction
*
* this method can be listened by onPreBeginTransaction and onBeginTransaction
* listener methods
*
* @return void
*/
public function beginTransaction() {
......@@ -579,6 +583,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$record->getTable()->getListener()->onDelete($record);
$this->commit();
return true;
}
/**
......
......@@ -282,7 +282,7 @@ class Doctrine_Connection_Transaction implements Countable, IteratorAggregate {
/**
* inserts a record into database
*
* @param Doctrine_Record $record
* @param Doctrine_Record $record record to be inserted
* @return boolean
*/
public function insert(Doctrine_Record $record) {
......@@ -298,24 +298,15 @@ class Doctrine_Connection_Transaction implements Countable, IteratorAggregate {
$keys = $table->getPrimaryKeys();
$seq = $record->getTable()->getSequenceName();
if( ! empty($seq)) {
$id = $this->getNextID($seq);
$id = $this->nextId($seq);
$name = $record->getTable()->getIdentifier();
$array[$name] = $id;
}
$strfields = join(", ", array_keys($array));
$strvalues = substr(str_repeat("?, ", count($array)), 0, -2);
$sql = "INSERT INTO ".$record->getTable()->getTableName()." (".$strfields.") VALUES (".$strvalues.")";
$stmt = $this->conn->getDBH()->prepare($sql);
$stmt->execute(array_values($array));
$this->conn->insert($table->getTableName(), $array);
if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
$id = $this->conn->getDBH()->lastInsertID();
......
......@@ -142,6 +142,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
* saveRelated
* saves all related records to $record
*
* @throws PDOException if something went wrong at database level
* @param Doctrine_Record $record
*/
public function saveRelated(Doctrine_Record $record) {
......@@ -185,6 +186,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
* 3, 4 and 5, this method would first destroy the associations to 1 and 2 and then
* save new associations to 4 and 5
*
* @throws PDOException if something went wrong at database level
* @param Doctrine_Record $record
* @return void
*/
......@@ -200,6 +202,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
* deletes all related composites
* this method is always called internally when a record is deleted
*
* @throws PDOException if something went wrong at database level
* @return void
*/
public function deleteComposites(Doctrine_Record $record) {
......@@ -213,6 +216,35 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
endswitch;
}
}
/**
* saveAll
* persists all the pending records from all tables
*
* @throws PDOException if something went wrong at database level
* @return void
*/
public function saveAll() {
// get the flush tree
$tree = $this->buildFlushTree($this->conn->getTables());
// save all records
foreach($tree as $name) {
$table = $this->conn->getTable($name);
foreach($table->getRepository() as $record) {
$this->conn->save($record);
}
}
// save all associations
foreach($tree as $name) {
$table = $this->conn->getTable($name);
foreach($table->getRepository() as $record) {
$this->saveAssociations($record);
}
}
}
public function getIterator() { }
public function count() { }
......
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