Completed
Pull Request — master (#6365)
by Daniel Tome
10:44
created

MultiTableDeleteExecutor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 13
dl 0
loc 110
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 53 4
B execute() 0 26 3
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Query\Exec;
21
22
use Doctrine\DBAL\Connection;
23
use Doctrine\DBAL\Types\Type;
24
use Doctrine\ORM\Query\AST;
25
use Doctrine\ORM\Utility\PersisterHelper;
26
27
/**
28
 * Executes the SQL statements for bulk DQL DELETE statements on classes in
29
 * Class Table Inheritance (JOINED).
30
 *
31
 * @author      Roman Borschel <[email protected]>
32
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
33
 * @link        http://www.doctrine-project.org
34
 * @since       2.0
35
 */
36
class MultiTableDeleteExecutor extends AbstractSqlExecutor
37
{
38
    /**
39
     * @var string
40
     */
41
    private $_createTempTableSql;
42
43
    /**
44
     * @var string
45
     */
46
    private $_dropTempTableSql;
47
48
    /**
49
     * @var string
50
     */
51
    private $_insertSql;
52
53
    /**
54
     * Initializes a new <tt>MultiTableDeleteExecutor</tt>.
55
     *
56
     * Internal note: Any SQL construction and preparation takes place in the constructor for
57
     *                best performance. With a query cache the executor will be cached.
58
     *
59
     * @param \Doctrine\ORM\Query\AST\Node  $AST       The root AST node of the DQL query.
60
     * @param \Doctrine\ORM\Query\SqlWalker $sqlWalker The walker used for SQL generation from the AST.
61
     */
62
    public function __construct(AST\Node $AST, $sqlWalker)
63
    {
64
        $em             = $sqlWalker->getEntityManager();
65
        $conn           = $em->getConnection();
66
        $platform       = $conn->getDatabasePlatform();
67
        $quoteStrategy  = $em->getConfiguration()->getQuoteStrategy();
68
69
        $primaryClass       = $em->getClassMetadata($AST->deleteClause->abstractSchemaName);
0 ignored issues
show
Bug introduced by
The property deleteClause does not seem to exist in Doctrine\ORM\Query\AST\Node.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
        $primaryDqlAlias    = $AST->deleteClause->aliasIdentificationVariable;
71
        $rootClass          = $em->getClassMetadata($primaryClass->rootEntityName);
72
73
        $tempTable      = $platform->getTemporaryTableName($rootClass->getTemporaryIdTableName());
74
        $idColumnNames  = $rootClass->getIdentifierColumnNames();
75
        $idColumnList   = implode(', ', $idColumnNames);
76
77
        // 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause()
78
        $sqlWalker->setSQLTableAlias($primaryClass->getTableName(), 't0', $primaryDqlAlias);
79
80
        $this->_insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')'
81
                . ' SELECT t0.' . implode(', t0.', $idColumnNames);
82
83
        $rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $primaryDqlAlias);
84
        $fromClause = new AST\FromClause([new AST\IdentificationVariableDeclaration($rangeDecl, null, [])]);
85
        $this->_insertSql .= $sqlWalker->walkFromClause($fromClause);
86
87
        // Append WHERE clause, if there is one.
88
        if ($AST->whereClause) {
89
            $this->_insertSql .= $sqlWalker->walkWhereClause($AST->whereClause);
0 ignored issues
show
Bug introduced by
The property whereClause does not seem to exist in Doctrine\ORM\Query\AST\Node.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
90
        }
91
92
        // 2. Create ID subselect statement used in DELETE ... WHERE ... IN (subselect)
93
        $idSubselect = 'SELECT ' . $idColumnList . ' FROM ' . $tempTable;
94
95
        // 3. Create and store DELETE statements
96
        $classNames = array_merge($primaryClass->parentClasses, [$primaryClass->name], $primaryClass->subClasses);
97
        foreach (array_reverse($classNames) as $className) {
98
            $tableName = $quoteStrategy->getTableName($em->getClassMetadata($className), $platform);
99
            $this->_sqlStatements[] = 'DELETE FROM ' . $tableName
100
                    . ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')';
101
        }
102
103
        // 4. Store DDL for temporary identifier table.
104
        $columnDefinitions = [];
105
        foreach ($idColumnNames as $idColumnName) {
106
            $columnDefinitions[$idColumnName] = [
107
                'notnull' => true,
108
                'type'    => Type::getType(PersisterHelper::getTypeOfColumn($idColumnName, $rootClass, $em)),
109
            ];
110
        }
111
        $this->_createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
112
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
113
        $this->_dropTempTableSql = $platform->getDropTemporaryTableSQL($tempTable);
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function execute(Connection $conn, array $params, array $types)
120
    {
121
        // Create temporary id table
122
        $conn->executeUpdate($this->_createTempTableSql);
123
124
        try {
125
            // Insert identifiers
126
            $numDeleted = $conn->executeUpdate($this->_insertSql, $params, $types);
127
128
            // Execute DELETE statements
129
            foreach ($this->_sqlStatements as $sql) {
130
                $conn->executeUpdate($sql);
131
            }
132
        } catch (\Exception $exception) {
133
            // FAILURE! Drop temporary table to avoid possible collisions
134
            $conn->executeUpdate($this->_dropTempTableSql);
135
136
            // Re-throw exception
137
            throw $exception;
138
        }
139
140
        // Drop temporary table
141
        $conn->executeUpdate($this->_dropTempTableSql);
142
143
        return $numDeleted;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $numDeleted; (integer) is incompatible with the return type declared by the abstract method Doctrine\ORM\Query\Exec\...actSqlExecutor::execute of type Doctrine\DBAL\Driver\Statement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
144
    }
145
}
146