Commit 537c8e49 authored by romanb's avatar romanb

[2.0] Implemented DQL bulk DELETE support for Class Table Inheritance. Other...

[2.0] Implemented DQL bulk DELETE support for Class Table Inheritance. Other cleanups, refactorings and docblock additions.
parent 61555c78
version=2.0
build.dir=build
dist.dir=dir
\ No newline at end of file
dist.dir=dist
report.dir=reports
\ No newline at end of file
......@@ -8,20 +8,32 @@
<property file="build.properties" />
<!--
Fileset for artifacts shared across all distributed packages.
-->
<fileset id="shared-artifacts" dir=".">
<include name="LICENSE"/>
<include name="COPYRIGHT"/>
<include name="CHANGELOG"/>
</fileset>
<!--
Fileset for the sources of the Doctrine Common package.
-->
<fileset id="common-sources" dir=".">
<include name="lib/Doctrine/Common/**"/>
</fileset>
<!--
Fileset for the sources of the Doctrine DBAL package.
-->
<fileset id="dbal-sources" dir=".">
<include name="lib/Doctrine/DBAL/**"/>
</fileset>
<!--
Fileset for the sources of the Doctrine ORM package.
-->
<fileset id="orm-sources" dir=".">
<include name="lib/Doctrine/ORM/**"/>
</fileset>
......@@ -29,32 +41,38 @@
<target name="clean">
<delete dir="${build.dir}" includeemptydirs="true" />
<delete dir="${dist.dir}" includeemptydirs="true" />
<delete dir="${report.dir}" includeemptydirs="true" />
</target>
<target name="prepare" depends="clean">
<echo msg="Creating build directory: build" />
<echo msg="Creating build directory: ${build.dir}" />
<mkdir dir="${build.dir}" />
<echo msg="Creating distribution directory: dist" />
<echo msg="Creating distribution directory: ${dist.dir}" />
<mkdir dir="${dist.dir}" />
<echo msg="Creating report directory: ${report.dir}" />
<mkdir dir="${report.dir}" />
</target>
<target name="build-common">
<copy todir="build/common">
<copy todir="${build.dir}/common">
<fileset refid="shared-artifacts"/>
<fileset refid="common-sources"/>
</copy>
</target>
<target name="build-dbal">
<copy todir="build/dbal">
<copy todir="${build.dir}/dbal">
<fileset refid="shared-artifacts"/>
<fileset refid="common-sources"/>
<fileset refid="dbal-sources"/>
</copy>
</target>
<!--
Builds all packages, preparing them for distribution.
-->
<target name="build-all" depends="prepare, build-common, build-dbal">
<copy todir="build/all">
<copy todir="${build.dir}/all">
<fileset refid="shared-artifacts"/>
<fileset refid="common-sources"/>
<fileset refid="dbal-sources"/>
......@@ -62,9 +80,15 @@
</copy>
</target>
<target name="test">
<target name="prepare-test">
<mkdir dir="${build.dir}/logs"/>
<mkdir dir="reports/tests"/>
<mkdir dir="${report.dir}/tests"/>
</target>
<!--
Runs the full test suite.
-->
<target name="test" depends="prepare-test">
<phpunit printsummary="true" haltonfailure="true">
<formatter todir="${build.dir}/logs" type="xml"/>
<batchtest classpath="tests">
......@@ -76,14 +100,20 @@
<!-- <phpunitreport infile="build/logs/testsuites.xml" format="frames" todir="reports/tests" />-->
</target>
<!--
Distributes the Doctrine Common package.
-->
<target name="dist-common">
<tar destfile="./dist/Doctrine2-common.tar.gz" compression="gzip">
<tar destfile="${dist.dir}/Doctrine2-common.tar.gz" compression="gzip">
<fileset dir="${build.dir}/common">
<include name="**" />
</fileset>
</tar>
</target>
<!--
Distributes the Doctrine DBAL package.
-->
<target name="dist-dbal">
<tar destfile="${dist.dir}/Doctrine2-dbal.tar.gz" compression="gzip">
<fileset dir="${build.dir}/dbal">
......@@ -92,14 +122,16 @@
</tar>
</target>
<!--
DEFAULT TARGET
Tests, builds and distributes the full Doctrine package (Common+DBAL+ORM).
-->
<target name="dist-all" depends="test, build-all, dist-common, dist-dbal">
<tar destfile="${dist.dir}/Doctrine2-all.tar.gz" compression="gzip">
<fileset dir="${build.dir}/all">
<include name="**" />
</fileset>
</tar>
</target>
</project>
......@@ -35,23 +35,6 @@ use Doctrine\Common\DoctrineException;
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Lukas Smith <smith@pooteeweet.org> (MDB2 library)
* @author Roman Borschel <roman@code-factory.org>
* @todo
* 1) REPLICATION SUPPORT
* Replication support should be tackled at this layer (DBAL).
* There can be options that look like:
* 'slaves' => array(
* 'slave1' => array(
* user, pass etc.
* ),
* 'slave2' => array(
* user, pass etc.
* )),
* 'slaveConnectionResolver' => new MySlaveConnectionResolver(),
* 'masters' => array(...),
* 'masterConnectionResolver' => new MyMasterConnectionResolver()
*
* Doctrine\DBAL could ship with a simple standard broker that uses a primitive
* round-robin approach to distribution. User can provide its own brokers.
*/
class Connection
{
......@@ -451,9 +434,9 @@ class Connection
$params = array_merge(array_values($data), array_values($identifier));
$sql = 'UPDATE ' . $this->quoteIdentifier($tableName)
. ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
. ' = ?';
. ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
. ' = ?';
return $this->exec($sql, $params);
}
......@@ -528,7 +511,6 @@ class Connection
*/
public function quote($input, $type = null)
{
$this->connect();
return $this->_conn->quote($input, $type);
}
......
......@@ -17,7 +17,7 @@ interface Driver
* @param string $username The username to use when connecting.
* @param string $password The password to use when connecting.
* @param array $driverOptions The driver options to use when connecting.
* @return Doctrine::DBAL::Connection The database connection.
* @return Doctrine\DBAL\Driver\Connection The database connection.
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = array());
......@@ -25,7 +25,7 @@ interface Driver
* Gets the DatabasePlatform instance that provides all the metadata about
* the platform this driver connects to.
*
* @return Doctrine::DBAL::DatabasePlatform The database platform.
* @return Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
*/
public function getDatabasePlatform();
......@@ -39,9 +39,9 @@ interface Driver
public function getSchemaManager(Connection $conn);
/**
* Get the name of the driver
* Gets the name of the driver.
*
* @return string The name of the driver
* @return string The name of the driver.
*/
public function getName();
......
......@@ -23,7 +23,7 @@ namespace Doctrine\DBAL\Driver;
/**
* Connection interface.
* Drivers must implement this interface.
* Driver connections must implement this interface.
*
* This resembles the PDO interface.
*
......@@ -31,14 +31,14 @@ namespace Doctrine\DBAL\Driver;
*/
interface Connection
{
public function prepare($prepareString);
public function query();
public function quote($input);
public function exec($statement);
public function lastInsertId();
public function beginTransaction();
public function commit();
public function rollBack();
public function errorCode();
public function errorInfo();
function prepare($prepareString);
function query();
function quote($input);
function exec($statement);
function lastInsertId();
function beginTransaction();
function commit();
function rollBack();
function errorCode();
function errorInfo();
}
\ No newline at end of file
......@@ -29,7 +29,7 @@ use \PDO;
*
* @since 2.0
*/
class PDOConnection extends PDO implements \Doctrine\DBAL\Driver\Connection
class PDOConnection extends PDO implements Connection
{
public function __construct($dsn, $user = null, $password = null, array $options = null)
{
......
......@@ -27,7 +27,7 @@ namespace Doctrine\DBAL\Driver;
*
* @since 2.0
*/
class PDOStatement extends \PDOStatement implements \Doctrine\DBAL\Driver\Statement
class PDOStatement extends \PDOStatement implements Statement
{
private function __construct() {}
}
\ No newline at end of file
......@@ -46,7 +46,7 @@ interface Statement
* @param integer $type Data type of the parameter, specified by the PDO::PARAM_* constants.
* @return boolean Returns TRUE on success or FALSE on failure
*/
public function bindColumn($column, &$param, $type = null);
function bindColumn($column, &$param, $type = null);
/**
* Binds a value to a corresponding named or positional
......@@ -61,7 +61,7 @@ interface Statement
*
* @return boolean Returns TRUE on success or FALSE on failure.
*/
public function bindValue($param, $value, $type = null);
function bindValue($param, $value, $type = null);
/**
* Binds a PHP variable to a corresponding named or question mark placeholder in the
......@@ -89,7 +89,7 @@ interface Statement
* @param mixed $driverOptions
* @return boolean Returns TRUE on success or FALSE on failure.
*/
public function bindParam($column, &$variable, $type = null, $length = null, $driverOptions = array());
function bindParam($column, &$variable, $type = null, $length = null, $driverOptions = array());
/**
* closeCursor
......@@ -97,7 +97,7 @@ interface Statement
*
* @return boolean Returns TRUE on success or FALSE on failure.
*/
public function closeCursor();
function closeCursor();
/**
* columnCount
......@@ -107,7 +107,7 @@ interface Statement
* by the PDOStatement object. If there is no result set,
* this method should return 0.
*/
public function columnCount();
function columnCount();
/**
* errorCode
......@@ -116,7 +116,7 @@ interface Statement
* @see Doctrine_Adapter_Interface::errorCode()
* @return string error code string
*/
public function errorCode();
function errorCode();
/**
* errorInfo
......@@ -125,7 +125,7 @@ interface Statement
* @see Doctrine_Adapter_Interface::errorInfo()
* @return array error info array
*/
public function errorInfo();
function errorInfo();
/**
* Executes a prepared statement
......@@ -141,7 +141,7 @@ interface Statement
* bound parameters in the SQL statement being executed.
* @return boolean Returns TRUE on success or FALSE on failure.
*/
public function execute($params = null);
function execute($params = null);
/**
* fetch
......@@ -170,7 +170,7 @@ interface Statement
*
* @return mixed
*/
public function fetch($fetchStyle = Query::HYDRATE_BOTH,
function fetch($fetchStyle = Query::HYDRATE_BOTH,
$cursorOrientation = Query::HYDRATE_ORI_NEXT,
$cursorOffset = null);
......@@ -187,7 +187,7 @@ interface Statement
*
* @return array
*/
public function fetchAll($fetchStyle = Query::HYDRATE_BOTH);
function fetchAll($fetchStyle = Query::HYDRATE_BOTH);
/**
* fetchColumn
......@@ -200,7 +200,7 @@ interface Statement
*
* @return string returns a single column in the next row of a result set.
*/
public function fetchColumn($columnIndex = 0);
function fetchColumn($columnIndex = 0);
/**
* fetchObject
......@@ -215,7 +215,7 @@ interface Statement
* @return mixed an instance of the required class with property names that correspond
* to the column names or FALSE in case of an error.
*/
public function fetchObject($className = 'stdClass', $args = array());
function fetchObject($className = 'stdClass', $args = array());
/**
* getAttribute
......@@ -225,7 +225,7 @@ interface Statement
* @see Doctrine::ATTR_* constants
* @return mixed the attribute value
*/
public function getAttribute($attribute);
function getAttribute($attribute);
/**
* getColumnMeta
......@@ -243,7 +243,7 @@ interface Statement
* precision The numeric precision of this column. Normally 0 for types other than floating point decimals.
* pdo_type The type of this column as represented by the PDO::PARAM_* constants.
*/
public function getColumnMeta($column);
function getColumnMeta($column);
/**
* nextRowset
......@@ -256,7 +256,7 @@ interface Statement
*
* @return boolean Returns TRUE on success or FALSE on failure.
*/
public function nextRowset();
function nextRowset();
/**
* rowCount
......@@ -270,7 +270,7 @@ interface Statement
*
* @return integer Returns the number of rows.
*/
public function rowCount();
function rowCount();
/**
* setAttribute
......@@ -280,7 +280,7 @@ interface Statement
* @param mixed $value the value of given attribute
* @return boolean Returns TRUE on success or FALSE on failure.
*/
public function setAttribute($attribute, $value);
function setAttribute($attribute, $value);
/**
* setFetchMode
......@@ -289,5 +289,5 @@ interface Statement
* @param integer $mode The fetch mode must be one of the Query::HYDRATE_* constants.
* @return boolean Returns 1 on success or FALSE on failure.
*/
public function setFetchMode($mode, $arg1);
function setFetchMode($mode, $arg1);
}
\ No newline at end of file
......@@ -94,7 +94,8 @@ final class DriverManager
* @param Doctrine\Common\EventManager The event manager to use.
* @return Doctrine\DBAL\Connection
*/
public static function getConnection(array $params,
public static function getConnection(
array $params,
Configuration $config = null,
EventManager $eventManager = null)
{
......@@ -107,10 +108,10 @@ final class DriverManager
}
// check for existing pdo object
if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
throw DoctrineException::invalidPDOInstance();
} else if (isset($params['pdo'])) {
$params['driver'] = $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
$params['driver'] = $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
} else {
self::_checkParams($params);
}
......
......@@ -1504,6 +1504,10 @@ abstract class AbstractPlatform
}
/**
* Gets the format string, as accepted by the date() function, that describes
* the format of a stored datetime value of this platform.
*
* @return string The format string.
* TODO: We need to get the specific format for each dbms and override this
* function for each platform
*/
......@@ -1512,11 +1516,23 @@ abstract class AbstractPlatform
return 'Y-m-d H:i:s';
}
/**
* Gets the format string, as accepted by the date() function, that describes
* the format of a stored date value of this platform.
*
* @return string The format string.
*/
public function getDateFormatString()
{
return 'Y-m-d';
}
/**
* Gets the format string, as accepted by the date() function, that describes
* the format of a stored time value of this platform.
*
* @return string The format string.
*/
public function getTimeFormatString()
{
return 'H:i:s';
......@@ -1535,7 +1551,7 @@ abstract class AbstractPlatform
}
/**
* Get the platform name for this instance
* Gets the name of the platform.
*
* @return string
*/
......
This diff is collapsed.
......@@ -2,6 +2,8 @@
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Type that maps an SQL boolean to a PHP boolean.
*
......@@ -9,17 +11,17 @@ namespace Doctrine\DBAL\Types;
*/
class BooleanType extends Type
{
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getBooleanDeclarationSql();
}
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $platform->convertBooleans($value);
}
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return (bool) $value;
}
......
......@@ -2,6 +2,8 @@
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Type that maps an SQL DATETIME to a PHP DateTime object.
*
......@@ -14,17 +16,17 @@ class DateTimeType extends Type
return 'DateTime';
}
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getDateTimeTypeDeclarationSql($fieldDeclaration);
}
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value->format($platform->getDateTimeFormatString());
}
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
}
......
......@@ -2,6 +2,8 @@
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Type that maps an SQL DATETIME to a PHP Date object.
*
......@@ -14,17 +16,17 @@ class DateType extends Type
return 'Date';
}
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getDateTypeDeclarationSql($fieldDeclaration);
}
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value->format($platform->getDateFormatString());
}
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return \DateTime::createFromFormat($platform->getDateFormatString(), $value);
}
......
......@@ -2,6 +2,8 @@
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Type that maps an SQL DATETIME to a PHP Date object.
*
......@@ -17,7 +19,7 @@ class TimeType extends Type
/**
* {@inheritdoc}
*/
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getTimeTypeDeclarationSql($fieldDeclaration);
}
......@@ -27,7 +29,7 @@ class TimeType extends Type
*
* @override
*/
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value->format($platform->getTimeFormatString());
}
......@@ -37,7 +39,7 @@ class TimeType extends Type
*
* @override
*/
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return \DateTime::createFromFormat($platform->getTimeFormatString(), $value);
}
......
......@@ -24,7 +24,10 @@ abstract class Type
const CODE_STR = 2;
const CODE_LOB = 3;
/** Map of already instantiated type objects. One instance per type (flyweight). */
private static $_typeObjects = array();
/** The map of supported doctrine mapping types. */
private static $_typesMap = array(
'array' => 'Doctrine\DBAL\Types\ArrayType',
'object' => 'Doctrine\DBAL\Types\ObjectType',
......@@ -45,25 +48,63 @@ abstract class Type
/* Prevent instantiation and force use of the factory method. */
private function __construct() {}
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
/**
* Converts a value from its PHP representation to its database representation
* of this type.
*
* @param mixed $value The value to convert.
* @param AbstractPlatform $platform The currently used database platform.
* @return mixed The database representation of the value.
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value;
}
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
/**
* Converts a value from its database representation to its PHP representation
* of this type.
*
* @param mixed $value The value to convert.
* @param AbstractPlatform $platform The currently used database platform.
* @return mixed The PHP representation of the value.
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value;
}
public function getDefaultLength(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
/**
* Gets the default length of this type.
*
* @todo Needed?
*/
public function getDefaultLength(AbstractPlatform $platform)
{
return null;
}
/**
* Gets the SQL declaration snippet for a field of this type.
*
* @param array $fieldDeclaration The field declaration.
* @param AbstractPlatform $platform The currently used database platform.
*/
abstract public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform);
abstract public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform);
/**
* Gets the name of this type.
*
* @return string
* @todo Needed?
*/
abstract public function getName();
/**
* Gets the type code of this type.
*
* @return integer
*/
public function getTypeCode()
{
return self::CODE_STR;
......
......@@ -264,7 +264,7 @@ class EntityManager
/**
* @todo Implementation.
*/
public function createCriteria()
public function createQueryBuilder()
{
//...
}
......@@ -405,7 +405,7 @@ class EntityManager
* overriding any local changes that have not yet been persisted.
*
* @param object $entity
* @todo Implemntation
* @todo Implementation
*/
public function refresh($entity)
{
......@@ -559,7 +559,7 @@ class EntityManager
}
/**
*
* Gets the proxy generated used by the EntityManager to create entity proxies.
*/
public function getProxyGenerator()
{
......@@ -591,8 +591,6 @@ class EntityManager
$eventManager = new EventManager();
}
$em = new EntityManager($conn, $config, $eventManager);
return $em;
return new EntityManager($conn, $config, $eventManager);
}
}
\ No newline at end of file
......@@ -1121,6 +1121,16 @@ final class ClassMetadata
{
return $this->primaryTable['name'];
}
/**
* Gets the table name to use for temporary identifier tables of this class.
*
* @return string
*/
public function getTemporaryIdTableName()
{
return $this->primaryTable['name'] . '_id_tmp';
}
public function getInheritedFields()
{
......
......@@ -143,12 +143,12 @@ class StandardEntityPersister
$params = array();
foreach ($insertData[$primaryTableName] as $value) {
$params[$paramIndex] = $value;
$stmt->bindValue($paramIndex++, $value/*, Type::getType()*/);
$stmt->bindValue($paramIndex++, $value);
}
$sqlLogger->logSql($this->_class->insertSql, $params);
} else {
foreach ($insertData[$primaryTableName] as $value) {
$stmt->bindValue($paramIndex++, $value/*, Type::getType()*/);
$stmt->bindValue($paramIndex++, $value);
}
}
......@@ -201,8 +201,8 @@ class StandardEntityPersister
public function delete($entity)
{
$id = array_combine(
$this->_class->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity)
$this->_class->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity)
);
$this->_conn->delete($this->_class->primaryTable['name'], $id);
}
......@@ -238,11 +238,12 @@ class StandardEntityPersister
}
/**
* Gets the table name to use for temporary identifier tables.
* Gets the table name to use for temporary identifier tables of the class
* persisted by this persister.
*/
public function getTemporaryIdTableName()
{
//...
return $this->_class->primaryTable['name'] . '_id_tmp';
}
/**
......@@ -378,8 +379,7 @@ class StandardEntityPersister
} else {
// Inject collection
$this->_class->reflFields[$field]->setValue(
$entity, new PersistentCollection($this->_em,
$this->_em->getClassMetadata($assoc->targetEntityName)
$entity, new PersistentCollection($this->_em, $this->_em->getClassMetadata($assoc->targetEntityName)
));
}
}
......@@ -410,7 +410,7 @@ class StandardEntityPersister
}
return 'SELECT ' . $columnList . ' FROM ' . $this->_class->getTableName()
. ' WHERE ' . $conditionSql;
. ' WHERE ' . $conditionSql;
}
/**
......
......@@ -68,17 +68,18 @@ abstract class AbstractExecutor implements \Serializable
$isDeleteStatement = $AST instanceof \Doctrine\ORM\Query\AST\DeleteStatement;
$isUpdateStatement = $AST instanceof \Doctrine\ORM\Query\AST\UpdateStatement;
if ($isUpdateStatement || $isDeleteStatement) {
// TODO: Inspect the $AST and create the proper executor like so (pseudo-code):
/*
if (primaryClassInFromClause->isMultiTable()) {
if ($isDeleteStatement) {
return new Doctrine_ORM_Query_SqlExecutor_MultiTableDelete($AST);
} else {
return new Doctrine_ORM_Query_SqlExecutor_MultiTableUpdate($AST);
}
} else ...
*/
if ($isDeleteStatement) {
$primaryClass = $sqlWalker->getEntityManager()->getClassMetadata(
$AST->getDeleteClause()->getAbstractSchemaName()
);
if ($primaryClass->isInheritanceTypeJoined()) {
return new MultiTableDeleteExecutor($AST, $sqlWalker);
} else {
return new SingleTableDeleteUpdateExecutor($AST, $sqlWalker);
}
} else if ($isUpdateStatement) {
//TODO: Check whether to pick MultiTableUpdateExecutor instead
return new SingleTableDeleteUpdateExecutor($AST, $sqlWalker);
} else {
return new SingleSelectExecutor($AST, $sqlWalker);
......
......@@ -30,37 +30,98 @@ namespace Doctrine\ORM\Query\Exec;
* @link http://www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @todo For a good implementation that uses temporary tables see the Hibernate sources:
* (org.hibernate.hql.ast.exec.MultiTableDeleteExecutor).
*/
class MultiTableDeleteExecutor extends AbstractExecutor
{
public function __construct($AST)
private $_createTempTableSql;
private $_dropTempTableSql;
private $_insertSql;
/**
* Initializes a new <tt>MultiTableDeleteExecutor</tt>.
*
* @param Node $AST The root AST node of the DQL query.
* @param SqlWalker $sqlWalker The walker used for SQL generation from the AST.
*/
public function __construct(\Doctrine\ORM\Query\AST\Node $AST, $sqlWalker)
{
$em = $sqlWalker->getEntityManager();
$conn = $em->getConnection();
$primaryClass = $sqlWalker->getEntityManager()->getClassMetadata(
$AST->getDeleteClause()->getAbstractSchemaName()
);
$rootClass = $em->getClassMetadata($primaryClass->rootEntityName);
$tempTable = $rootClass->getTemporaryIdTableName();
$idColumnNames = $rootClass->getIdentifierColumnNames();
$idColumnList = implode(', ', $idColumnNames);
// 1. Create a INSERT INTO temptable ... VALUES ( SELECT statement where the SELECT statement
// selects the identifiers and uses the WhereClause of the $AST ).
$this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')'
. ' SELECT ' . $idColumnList . ' FROM ' . $conn->quoteIdentifier($rootClass->primaryTable['name']) . ' t0';
// Append WHERE clause, if there is one.
if ($AST->getWhereClause()) {
$sqlWalker->setSqlTableAlias($rootClass->primaryTable['name'] . $AST->getDeleteClause()->getAliasIdentificationVariable(), 't0');
$this->_insertSql .= $sqlWalker->walkWhereClause($AST->getWhereClause());
}
// 2. Create ID subselect statement used in DELETE .... WHERE ... IN (subselect)
$idSubselect = 'SELECT ' . $idColumnList . ' FROM ' . $tempTable;
// 3. Create and store DELETE statements
/*$subselect = 'SELECT id1, id2 FROM temptable';
foreach ($tableNames as $tableName) {
$this->_sqlStatements[] = 'DELETE FROM ' . $tableName . ' WHERE (id1, id2) IN (subselect)';
}*/
// in $this->_sqlStatements
}
$classNames = array_merge($primaryClass->parentClasses, array($primaryClass->name), $primaryClass->subClasses);
foreach (array_reverse($classNames) as $className) {
$tableName = $em->getClassMetadata($className)->primaryTable['name'];
$this->_sqlStatements[] = 'DELETE FROM ' . $conn->quoteIdentifier($tableName)
. ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')';
}
// 4. Store DDL for temporary identifier table.
$columnDefinitions = array();
foreach ($idColumnNames as $idColumnName) {
$columnDefinitions[$idColumnName] = array(
'notnull' => true,
'type' => \Doctrine\DBAL\Types\Type::getType($rootClass->getTypeOfColumn($idColumnName))
);
}
$this->_createTempTableSql = 'CREATE TEMPORARY TABLE ' . $tempTable . ' ('
. $conn->getDatabasePlatform()->getColumnDeclarationListSql($columnDefinitions)
. ', PRIMARY KEY(' . $idColumnList . '))';
$this->_dropTempTableSql = 'DROP TABLE ' . $tempTable;
}
/**
* Executes all sql statements.
*
* @param Doctrine_Connection $conn The database connection that is used to execute the queries.
* @param Doctrine\DBAL\Connection $conn The database connection that is used to execute the queries.
* @param array $params The parameters.
* @override
*/
public function execute(\Doctrine\DBAL\Connection $conn, array $params)
{
// 1. Create temporary id table if necessary
$numDeleted = 0;
// Create temporary id table
$conn->exec($this->_createTempTableSql);
// Insert identifiers
$conn->exec($this->_insertSql, $params);
//...
}
// Execute DELETE statements
for ($i=0, $count=count($this->_sqlStatements); $i<$count; ++$i) {
if ($i == $count-1) {
$numDeleted = $conn->exec($this->_sqlStatements[$i]);
} else {
$conn->exec($this->_sqlStatements[$i]);
}
}
// Drop temporary table
$conn->exec($this->_dropTempTableSql);
return $numDeleted;
}
}
\ No newline at end of file
......@@ -85,12 +85,24 @@ class SqlWalker implements TreeWalker
}
/**
* Gets the Connection used by the walker.
*
* @return Connection
*/
public function getConnection()
{
return $this->_em->getConnection();
}
/**
* Gets the EntityManager used by the walker.
*
* @return EntityManager
*/
public function getEntityManager()
{
return $this->_em;
}
/**
* Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
......@@ -1203,7 +1215,25 @@ class SqlWalker implements TreeWalker
}
return $this->_dqlToSqlAliasMap[$tableName];
}
/**
* Forces the SqlWalker to use a specific alias for a table name, rather than
* generating an alias on its own.
*
* @param string $tableName
* @param string $alias
*/
public function setSqlTableAlias($tableName, $alias)
{
$this->_dqlToSqlAliasMap[$tableName] = $alias;
}
/**
* Gets an SQL column alias for a column name.
*
* @param string $columnName
* @return string
*/
public function getSqlColumnAlias($columnName)
{
return trim($columnName, '`') . $this->_aliasCounter++;
......
......@@ -412,9 +412,7 @@ class UnitOfWork implements PropertyChangedListener
$coll = new PersistentCollection($this->_em, $this->_em->getClassMetadata($assoc->targetEntityName),
$actualData[$name] ? $actualData[$name] : array());
$coll->setOwner($entity, $assoc);
if ( ! $coll->isEmpty()) {
$coll->setDirty(true);
}
$coll->setDirty( ! $coll->isEmpty());
$class->reflFields[$name]->setValue($entity, $coll);
$actualData[$name] = $coll;
}
......@@ -425,8 +423,7 @@ class UnitOfWork implements PropertyChangedListener
// (only has an id). These result in an INSERT.
$this->_originalEntityData[$oid] = $actualData;
$this->_entityChangeSets[$oid] = array_map(
function($e) { return array(null, $e); },
$actualData
function($e) { return array(null, $e); }, $actualData
);
} else {
// Entity is "fully" MANAGED: it was already fully persisted before
......@@ -519,7 +516,6 @@ class UnitOfWork implements PropertyChangedListener
$data[$name] = $refProp->getValue($entry);
$changeSet[$name] = array(null, $data[$name]);
if (isset($targetClass->associationMappings[$name])) {
//echo "RECURSING INTO $name" . PHP_EOL;
//TODO: Prevent infinite recursion
$this->_computeAssociationChanges($targetClass->associationMappings[$name], $data[$name]);
}
......@@ -1300,8 +1296,9 @@ class UnitOfWork implements PropertyChangedListener
$id = array($data[$class->identifier[0]]);
$idHash = $id[0];
}
$entity = $this->tryGetByIdHash($idHash, $class->rootEntityName);
if ($entity) {
if (isset($this->_identityMap[$class->rootEntityName][$idHash])) {
$entity = $this->_identityMap[$class->rootEntityName][$idHash];
$oid = spl_object_hash($entity);
$overrideLocalChanges = false;
//$overrideLocalChanges = $query->getHint('doctrine.refresh');
......
......@@ -65,6 +65,13 @@ class ClassTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals(100000, $entities[0]->getSalary());
$this->_em->clear();
//TODO: Test bulk UPDATE
$query = $this->_em->createQuery("delete from Doctrine\Tests\Models\Company\CompanyPerson p");
$numDeleted = $query->execute();
$this->assertEquals(2, $numDeleted);
}
public function testMultiLevelUpdateAndFind() {
......
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