1
0
mirror of synced 2025-01-18 22:41:43 +03:00

drafting the initial CTI support

This commit is contained in:
zYne 2007-11-10 13:21:40 +00:00
parent c6a0dd655b
commit bd31958035
5 changed files with 171 additions and 76 deletions

View File

@ -514,15 +514,15 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$tmp = array(); $tmp = array();
foreach (array_keys($identifier) as $id) { foreach (array_keys($identifier) as $id) {
$tmp[] = $id . ' = ? '; $tmp[] = $id . ' = ?';
} }
$query = 'DELETE FROM ' $query = 'DELETE FROM '
. $this->conn->quoteIdentifier($table) . $this->quoteIdentifier($table)
. ' WHERE ' . implode(' AND ', $tmp); . ' WHERE ' . implode(' AND ', $tmp);
return $this->conn->exec($query, array_values($identifier)); return $this->exec($query, array_values($identifier));
} }
/** /**

View File

@ -267,7 +267,9 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
$record->preDelete($event); $record->preDelete($event);
$record->getTable()->getRecordListener()->preDelete($event); $table = $record->getTable();
$table->getRecordListener()->preDelete($event);
$state = $record->state(); $state = $record->state();
@ -277,8 +279,15 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
if ( ! $event->skipOperation) { if ( ! $event->skipOperation) {
$record->state(Doctrine_Record::STATE_TDIRTY); $record->state(Doctrine_Record::STATE_TDIRTY);
if (count($table->getOption('joinedParents')) > 0) {
$this->deleteRecord($record); foreach ($table->getOption('joinedParents') as $parent) {
$parentTable = $table->getConnection()->getTable($parent);
$this->conn->delete($parentTable->getTableName(), $record->identifier());
}
}
$this->conn->delete($table->getTableName(), $record->identifier());
$record->state(Doctrine_Record::STATE_TCLEAN); $record->state(Doctrine_Record::STATE_TCLEAN);
} else { } else {
@ -287,11 +296,11 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
} }
$record->getTable()->getRecordListener()->postDelete($event); $table->getRecordListener()->postDelete($event);
$record->postDelete($event); $record->postDelete($event);
$record->getTable()->removeRecord($record); $table->removeRecord($record);
$this->conn->commit(); $this->conn->commit();
@ -507,8 +516,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
} }
/** /**
* update * updates given record
* updates the given record
* *
* @param Doctrine_Record $record record to be updated * @param Doctrine_Record $record record to be updated
* @return boolean whether or not the update was successful * @return boolean whether or not the update was successful
@ -519,60 +527,49 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
$record->preUpdate($event); $record->preUpdate($event);
$record->getTable()->getRecordListener()->preUpdate($event); $table = $record->getTable();
$table->getRecordListener()->preUpdate($event);
if ( ! $event->skipOperation) { if ( ! $event->skipOperation) {
$array = $record->getPrepared(); $identifier = $record->identifier();
if (empty($array)) { if (count($table->getOption('joinedParents')) > 0) {
return false; $dataSet = $this->formatDataSet($record);
}
$set = array(); $component = $table->getComponentName();
foreach ($array as $name => $value) {
if ($value instanceof Doctrine_Expression) {
$set[] = $name . ' = ' . $value->getSql();
unset($array[$name]);
} else {
$set[] = $name . ' = ?'; $classes = $table->getOption('joinedParents');
$classes[] = $component;
foreach ($record as $field => $value) {
if ($value instanceof Doctrine_Record) { if ($value instanceof Doctrine_Record) {
if ( ! $value->exists()) { if ( ! $value->exists()) {
$record->save($this->conn); $value->save();
} }
$array[$name] = $value->getIncremented(); $record->set($field, $value->getIncremented());
$record->set($name, $value->getIncremented());
} }
} }
}
$params = array_values($array); foreach ($classes as $class) {
$id = $record->identifier(); $parentTable = $this->conn->getTable($class);
if ( ! is_array($id)) { $this->conn->update($this->conn->getTable($class)->getTableName(), $dataSet[$class], $identifier);
$id = array($id); }
} else {
$array = $record->getPrepared();
$this->conn->update($table->getTableName(), $array, $identifier);
} }
$id = array_values($id);
$params = array_merge($params, $id);
$sql = 'UPDATE ' . $this->conn->quoteIdentifier($record->getTable()->getTableName())
. ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' = ? AND ', (array) $record->getTable()->getIdentifier())
. ' = ?';
$stmt = $this->conn->prepare($sql);
$stmt->execute($params);
$record->assignIdentifier(true); $record->assignIdentifier(true);
} }
$record->getTable()->getRecordListener()->postUpdate($event); $table->getRecordListener()->postUpdate($event);
$record->postUpdate($event); $record->postUpdate($event);
return true; return true;
} }
/** /**
* inserts a record into database * inserts a record into database
* *
@ -592,24 +589,10 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
if ( ! $event->skipOperation) { if ( ! $event->skipOperation) {
if (count($table->getOption('joinedParents')) > 0) { if (count($table->getOption('joinedParents')) > 0) {
$dataSet = array(); $dataSet = $this->formatDataSet($record);
$component = $table->getComponentName(); $component = $table->getComponentName();
$array = $record->getPrepared();
foreach ($table->getColumns() as $column => $definition) {
if (isset($definition['primary']) && $definition['primary']) {
continue;
}
if (isset($definition['owner'])) {
$dataSet[$definition['owner']][$column] = $array[$column];
} else {
$dataSet[$component][$column] = $array[$column];
}
}
$classes = $table->getOption('joinedParents'); $classes = $table->getOption('joinedParents');
$classes[] = $component; $classes[] = $component;
@ -641,6 +624,30 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
return true; return true;
} }
public function formatDataSet(Doctrine_Record $record)
{
$table = $record->getTable();
$dataSet = array();
$component = $table->getComponentName();
$array = $record->getPrepared();
foreach ($table->getColumns() as $column => $definition) {
if (isset($definition['primary']) && $definition['primary']) {
continue;
}
if (isset($definition['owner'])) {
$dataSet[$definition['owner']][$column] = $array[$column];
} else {
$dataSet[$component][$column] = $array[$column];
}
}
return $dataSet;
}
public function processSingleInsert(Doctrine_Record $record) public function processSingleInsert(Doctrine_Record $record)
{ {
$array = $record->getPrepared(); $array = $record->getPrepared();

View File

@ -453,11 +453,27 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
} }
$sql = array(); $sql = array();
foreach ($fields as $name) { foreach ($fields as $name) {
$name = $table->getColumnName($name); if (($owner = $table->getColumnOwner($name)) !== null &&
$owner !== $table->getComponentName()) {
$sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $name)
. ' AS ' $parent = $this->_conn->getTable($owner);
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
$name = $parent->getColumnName($name);
$parentAlias = $this->getTableAlias($componentAlias . '.' . $parent->getComponentName());
$sql[] = $this->_conn->quoteIdentifier($parentAlias . '.' . $name)
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
} else {
$name = $table->getColumnName($name);
$sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $name)
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
}
} }
$this->_neededTables[] = $tableAlias; $this->_neededTables[] = $tableAlias;
@ -1643,8 +1659,10 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
. ' = ' . ' = '
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $relation->getForeign()); . $this->_conn->quoteIdentifier($foreignAlias . '.' . $relation->getForeign());
} }
} }
$queryPart .= $this->buildInheritanceJoinSql($table->getComponentName(), $componentAlias);
$this->parts['from'][$componentAlias] = $queryPart; $this->parts['from'][$componentAlias] = $queryPart;
if ( ! empty($joinCondition)) { if ( ! empty($joinCondition)) {
$this->_pendingJoinConditions[$componentAlias] = $joinCondition; $this->_pendingJoinConditions[$componentAlias] = $joinCondition;
@ -1708,13 +1726,54 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
if ($this->type === self::SELECT) { if ($this->type === self::SELECT) {
$queryPart .= ' ' . $this->_conn->quoteIdentifier($tableAlias); $queryPart .= ' ' . $this->_conn->quoteIdentifier($tableAlias);
} }
$this->tableAliases[$tableAlias] = $componentAlias;
$queryPart .= $this->buildInheritanceJoinSql($name, $componentAlias);
$this->parts['from'][] = $queryPart; $this->parts['from'][] = $queryPart;
$this->tableAliases[$tableAlias] = $componentAlias;
$this->_aliasMap[$componentAlias] = array('table' => $table, 'map' => null); $this->_aliasMap[$componentAlias] = array('table' => $table, 'map' => null);
return $table; return $table;
} }
public function buildInheritanceJoinSql($name, $componentAlias)
{
// get the connection for the component
$this->_conn = Doctrine_Manager::getInstance()
->getConnectionForComponent($name);
$table = $this->_conn->getTable($name);
$tableName = $table->getTableName();
// get the short alias for this table
$tableAlias = $this->getTableAlias($componentAlias, $tableName);
$queryPart = '';
foreach ($table->getOption('joinedParents') as $parent) {
$parentTable = $this->_conn->getTable($parent);
$parentAlias = $componentAlias . '.' . $parent;
// get the short alias for the parent table
$parentTableAlias = $this->getTableAlias($parentAlias, $parentTable->getTableName());
$queryPart .= ' LEFT JOIN ' . $this->_conn->quoteIdentifier($parentTable->getTableName())
. ' ' . $this->_conn->quoteIdentifier($parentTableAlias) . ' ON ';
foreach ((array) $table->getIdentifier() as $identifier) {
$column = $table->getColumnName($identifier);
$queryPart .= $this->_conn->quoteIdentifier($tableAlias)
. '.' . $this->_conn->quoteIdentifier($column)
. ' = ' . $this->_conn->quoteIdentifier($parentTableAlias)
. '.' . $this->_conn->quoteIdentifier($column);
}
}
return $queryPart;
}
/** /**
* count * count

View File

@ -239,6 +239,10 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
{ {
return $this->_oid; return $this->_oid;
} }
public function oid()
{
return $this->_oid;
}
/** /**
* isValid * isValid
@ -1041,7 +1045,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
* @param Doctrine_Connection $conn optional connection parameter * @param Doctrine_Connection $conn optional connection parameter
* @throws Doctrine_Connection_Exception if some of the key values was null * @throws Doctrine_Connection_Exception if some of the key values was null
* @throws Doctrine_Connection_Exception if there were no key fields * @throws Doctrine_Connection_Exception if there were no key fields
* @throws PDOException if something fails at PDO level * @throws Doctrine_Connection_Exception if something fails at database level
* @return integer number of rows affected * @return integer number of rows affected
*/ */
public function replace(Doctrine_Connection $conn = null) public function replace(Doctrine_Connection $conn = null)
@ -1066,6 +1070,16 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
} }
return $a; return $a;
} }
public function modifiedFields()
{
$a = array();
foreach ($this->_modified as $k => $v) {
$a[$v] = $this->_data[$v];
}
return $a;
}
/** /**
* getPrepared * getPrepared
@ -1472,7 +1486,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
if (isset($values[$value])) { if (isset($values[$value])) {
$this->set($value, $values[$value]); $this->set($value, $values[$value]);
} }
} catch(Exception $e) { } catch(Doctrine_Exception $e) {
// silence all exceptions // silence all exceptions
} }
} }
@ -1522,12 +1536,6 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
return $this->_node; return $this->_node;
} }
public function unshiftFilter(Doctrine_Record_Filter $filter)
{
return $this->_table->unshiftFilter($filter);
}
/** /**
* revert * revert
* reverts this record to given version, this method only works if versioning plugin * reverts this record to given version, this method only works if versioning plugin
@ -1552,7 +1560,10 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
return $this; return $this;
} }
public function unshiftFilter(Doctrine_Record_Filter $filter)
{
return $this->_table->unshiftFilter($filter);
}
/** /**
* unlink * unlink
* removes links from this record to given records * removes links from this record to given records

View File

@ -265,7 +265,11 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
if ($parent === $class->getName()) { if ($parent === $class->getName()) {
continue; continue;
} }
$ref = new ReflectionClass($parent);
if ($ref->isAbstract()) {
continue;
}
$table = $this->_conn->getTable($parent); $table = $this->_conn->getTable($parent);
$found = false; $found = false;
@ -401,6 +405,20 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
} }
} }
public function getColumnOwner($column)
{
if (isset($this->_columns[$column]['owner'])) {
return $this->_columns[$column]['owner'];
} else {
return $this->getComponentName();
}
}
public function isInheritedColumn($column)
{
return (isset($this->_columns[$column]['owner']));
}
public function isIdentifier($identifier) public function isIdentifier($identifier)
{ {
return ($identifier === $this->_identifier || return ($identifier === $this->_identifier ||