Completed
Push — 6.7_race_cond_testing ( 56a653 )
by André
18:41 queued 11s
created

QueryBuilder::createVersionInfoQueryBuilder()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 50
rs 9.0909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the DoctrineDatabase query builder class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase;
10
11
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
12
13
class QueryBuilder
14
{
15
    /**
16
     * Database handler.
17
     *
18
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
19
     */
20
    protected $dbHandler;
21
22
    /**
23
     * The native Doctrine connection.
24
     *
25
     * Meant to be used to transition from eZ/Zeta interface to Doctrine.
26
     *
27
     * @var \Doctrine\DBAL\Connection
28
     */
29
    protected $connection;
30
31
    /**
32
     * Creates a new query builder.
33
     *
34
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
35
     */
36
    public function __construct(DatabaseHandler $dbHandler)
37
    {
38
        $this->dbHandler = $dbHandler;
39
        $this->connection = $dbHandler->getConnection();
40
    }
41
42
    /**
43
     * Creates a select query for full content objects, used by Content `load`.
44
     *
45
     * Creates a select query with all necessary joins to fetch a complete
46
     * content object. Does not apply any WHERE conditions unless
47
     * translations are provided, and does not contain name data as it will
48
     * lead to very large result set {@see createNamesQuery}.
49
     *
50
     * @param string[] $translations
51
     *
52
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
53
     */
54
    public function createFindQuery(array $translations = null)
55
    {
56
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
57
        $query = $this->dbHandler->createSelectQuery();
58
        $query->select(
59
            // Content object
60
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcontentobject'),
61
            $this->dbHandler->aliasedColumn($query, 'contentclass_id', 'ezcontentobject'),
62
            $this->dbHandler->aliasedColumn($query, 'section_id', 'ezcontentobject'),
63
            $this->dbHandler->aliasedColumn($query, 'owner_id', 'ezcontentobject'),
64
            $this->dbHandler->aliasedColumn($query, 'remote_id', 'ezcontentobject'),
65
            $this->dbHandler->aliasedColumn($query, 'current_version', 'ezcontentobject'),
66
            $this->dbHandler->aliasedColumn($query, 'initial_language_id', 'ezcontentobject'),
67
            $this->dbHandler->aliasedColumn($query, 'modified', 'ezcontentobject'),
68
            $this->dbHandler->aliasedColumn($query, 'published', 'ezcontentobject'),
69
            $this->dbHandler->aliasedColumn($query, 'status', 'ezcontentobject'),
70
            $this->dbHandler->aliasedColumn($query, 'name', 'ezcontentobject'),
71
            $this->dbHandler->aliasedColumn($query, 'language_mask', 'ezcontentobject'),
72
            // Content object version
73
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcontentobject_version'),
74
            $this->dbHandler->aliasedColumn($query, 'version', 'ezcontentobject_version'),
75
            $this->dbHandler->aliasedColumn($query, 'modified', 'ezcontentobject_version'),
76
            $this->dbHandler->aliasedColumn($query, 'creator_id', 'ezcontentobject_version'),
77
            $this->dbHandler->aliasedColumn($query, 'created', 'ezcontentobject_version'),
78
            $this->dbHandler->aliasedColumn($query, 'status', 'ezcontentobject_version'),
79
            // @todo: remove ezcontentobject_version.contentobject_id from query as it duplicates ezcontentobject.id
80
            $this->dbHandler->aliasedColumn($query, 'contentobject_id', 'ezcontentobject_version'),
81
            $this->dbHandler->aliasedColumn($query, 'language_mask', 'ezcontentobject_version'),
82
            $this->dbHandler->aliasedColumn($query, 'initial_language_id', 'ezcontentobject_version'),
83
            // Content object fields
84
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcontentobject_attribute'),
85
            $this->dbHandler->aliasedColumn($query, 'contentclassattribute_id', 'ezcontentobject_attribute'),
86
            $this->dbHandler->aliasedColumn($query, 'data_type_string', 'ezcontentobject_attribute'),
87
            $this->dbHandler->aliasedColumn($query, 'language_code', 'ezcontentobject_attribute'),
88
            $this->dbHandler->aliasedColumn($query, 'language_id', 'ezcontentobject_attribute'),
89
            // @todo: remove ezcontentobject_attribute.version from query as it duplicates ezcontentobject_version.version
90
            $this->dbHandler->aliasedColumn($query, 'version', 'ezcontentobject_attribute'),
91
            // Content object field data
92
            $this->dbHandler->aliasedColumn($query, 'data_float', 'ezcontentobject_attribute'),
93
            $this->dbHandler->aliasedColumn($query, 'data_int', 'ezcontentobject_attribute'),
94
            $this->dbHandler->aliasedColumn($query, 'data_text', 'ezcontentobject_attribute'),
95
            $this->dbHandler->aliasedColumn($query, 'sort_key_int', 'ezcontentobject_attribute'),
96
            $this->dbHandler->aliasedColumn($query, 'sort_key_string', 'ezcontentobject_attribute'),
97
            // Content object locations
98
            $this->dbHandler->aliasedColumn($query, 'main_node_id', 'ezcontentobject_tree')
99
        )->from(
100
            $this->dbHandler->quoteTable('ezcontentobject')
101
        )->innerJoin(
102
            $this->dbHandler->quoteTable('ezcontentobject_version'),
103
            $query->expr->eq(
104
                $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version'),
105
                $this->dbHandler->quoteColumn('id', 'ezcontentobject')
106
            )
107
        )->innerJoin(
108
            $this->dbHandler->quoteTable('ezcontentobject_attribute'),
109
            $query->expr->lAnd(
110
                $query->expr->eq(
111
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_attribute'),
112
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version')
113
                ),
114
                $query->expr->eq(
115
                    $this->dbHandler->quoteColumn('version', 'ezcontentobject_attribute'),
116
                    $this->dbHandler->quoteColumn('version', 'ezcontentobject_version')
117
                )
118
            )
119
        )->leftJoin(
120
            $this->dbHandler->quoteTable('ezcontentobject_tree'),
121
            $query->expr->lAnd(
122
                $query->expr->eq(
123
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_tree'),
124
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject')
125
                ),
126
                $query->expr->eq(
127
                    $this->dbHandler->quoteColumn('main_node_id', 'ezcontentobject_tree'),
128
                    $this->dbHandler->quoteColumn('node_id', 'ezcontentobject_tree')
129
                )
130
            )
131
        );
132
133
        if (!empty($translations)) {
134
            $query->where(
135
                $query->expr->in(
136
                    $this->dbHandler->quoteColumn('language_code', 'ezcontentobject_attribute'),
137
                    $translations
138
                )
139
            );
140
        }
141
142
        return $query;
143
    }
144
145
    /**
146
     * Create select query to query content name data.
147
     *
148
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
149
     */
150
    public function createNamesQuery()
151
    {
152
        $query = $this->dbHandler->createSelectQuery();
153
        $query
154
            ->select(
155
                $this->dbHandler->aliasedColumn($query, 'contentobject_id', 'ezcontentobject_name'),
156
                $this->dbHandler->aliasedColumn($query, 'content_version', 'ezcontentobject_name'),
157
                $this->dbHandler->aliasedColumn($query, 'name', 'ezcontentobject_name'),
158
                $this->dbHandler->aliasedColumn($query, 'content_translation', 'ezcontentobject_name')
159
            )
160
            ->from($this->dbHandler->quoteTable('ezcontentobject_name'));
161
162
        return $query;
163
    }
164
165
    /**
166
     * Creates a select query for content relations.
167
     *
168
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
169
     */
170
    public function createRelationFindQuery()
171
    {
172
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
173
        $query = $this->dbHandler->createSelectQuery();
174
        $query->select(
175
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcontentobject_link'),
176
            $this->dbHandler->aliasedColumn($query, 'contentclassattribute_id', 'ezcontentobject_link'),
177
            $this->dbHandler->aliasedColumn($query, 'from_contentobject_id', 'ezcontentobject_link'),
178
            $this->dbHandler->aliasedColumn($query, 'from_contentobject_version', 'ezcontentobject_link'),
179
            $this->dbHandler->aliasedColumn($query, 'relation_type', 'ezcontentobject_link'),
180
            $this->dbHandler->aliasedColumn($query, 'to_contentobject_id', 'ezcontentobject_link')
181
        )->from(
182
            $this->dbHandler->quoteTable('ezcontentobject_link')
183
        );
184
185
        return $query;
186
    }
187
188
    /**
189
     * Creates a select query for content version objects, used for version loading w/o fields.
190
     *
191
     * Creates a select query with all necessary joins to fetch a complete
192
     * content object. Does not apply any WHERE conditions, and does not contain
193
     * name data as it will lead to large result set {@see createNamesQuery}.
194
     *
195
     * @deprecated Move to Doctrine based query builder {@see createVersionInfoQueryBuilder}.
196
     *
197
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
198
     */
199
    public function createVersionInfoFindQuery()
200
    {
201
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
202
        $query = $this->dbHandler->createSelectQuery();
203
        $query->select(
204
            // Content object version
205
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcontentobject_version'),
206
            $this->dbHandler->aliasedColumn($query, 'version', 'ezcontentobject_version'),
207
            $this->dbHandler->aliasedColumn($query, 'modified', 'ezcontentobject_version'),
208
            $this->dbHandler->aliasedColumn($query, 'creator_id', 'ezcontentobject_version'),
209
            $this->dbHandler->aliasedColumn($query, 'created', 'ezcontentobject_version'),
210
            $this->dbHandler->aliasedColumn($query, 'status', 'ezcontentobject_version'),
211
            $this->dbHandler->aliasedColumn($query, 'contentobject_id', 'ezcontentobject_version'),
212
            $this->dbHandler->aliasedColumn($query, 'initial_language_id', 'ezcontentobject_version'),
213
            $this->dbHandler->aliasedColumn($query, 'language_mask', 'ezcontentobject_version'),
214
            // Content main location
215
            $this->dbHandler->aliasedColumn($query, 'main_node_id', 'ezcontentobject_tree'),
216
            // Content object
217
            // @todo: remove ezcontentobject.d from query as it duplicates ezcontentobject_version.contentobject_id
218
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcontentobject'),
219
            $this->dbHandler->aliasedColumn($query, 'contentclass_id', 'ezcontentobject'),
220
            $this->dbHandler->aliasedColumn($query, 'section_id', 'ezcontentobject'),
221
            $this->dbHandler->aliasedColumn($query, 'owner_id', 'ezcontentobject'),
222
            $this->dbHandler->aliasedColumn($query, 'remote_id', 'ezcontentobject'),
223
            $this->dbHandler->aliasedColumn($query, 'current_version', 'ezcontentobject'),
224
            $this->dbHandler->aliasedColumn($query, 'initial_language_id', 'ezcontentobject'),
225
            $this->dbHandler->aliasedColumn($query, 'modified', 'ezcontentobject'),
226
            $this->dbHandler->aliasedColumn($query, 'published', 'ezcontentobject'),
227
            $this->dbHandler->aliasedColumn($query, 'status', 'ezcontentobject'),
228
            $this->dbHandler->aliasedColumn($query, 'name', 'ezcontentobject'),
229
            $this->dbHandler->aliasedColumn($query, 'language_mask', 'ezcontentobject')
230
        )->from(
231
            $this->dbHandler->quoteTable('ezcontentobject_version')
232
        )->innerJoin(
233
            $this->dbHandler->quoteTable('ezcontentobject'),
234
            $query->expr->eq(
235
                $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
236
                $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version')
237
            )
238
        )->leftJoin(
239
            $this->dbHandler->quoteTable('ezcontentobject_tree'),
240
            $query->expr->lAnd(
241
                $query->expr->eq(
242
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_tree'),
243
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version')
244
                ),
245
                $query->expr->eq(
246
                    $this->dbHandler->quoteColumn('main_node_id', 'ezcontentobject_tree'),
247
                    $this->dbHandler->quoteColumn('node_id', 'ezcontentobject_tree')
248
                )
249
            )
250
        );
251
252
        return $query;
253
    }
254
255
    /**
256
     * Create a doctrine query builder with db fields needed to populate VersionInfo.
257
     *
258
     * @param int|null $versionNo Selects current version number if left undefined as null.
259
     *
260
     * @return \Doctrine\DBAL\Query\QueryBuilder
261
     */
262
    public function createVersionInfoQueryBuilder($versionNo = null)
263
    {
264
        $queryBuilder = $this->connection->createQueryBuilder();
265
        $expr = $queryBuilder->expr();
266
        $queryBuilder
267
            ->select(
268
                'c.id AS ezcontentobject_id',
269
                'c.contentclass_id AS ezcontentobject_contentclass_id',
270
                'c.section_id AS ezcontentobject_section_id',
271
                'c.owner_id AS ezcontentobject_owner_id',
272
                'c.remote_id AS ezcontentobject_remote_id',
273
                'c.current_version AS ezcontentobject_current_version',
274
                'c.initial_language_id AS ezcontentobject_initial_language_id',
275
                'c.modified AS ezcontentobject_modified',
276
                'c.published AS ezcontentobject_published',
277
                'c.status AS ezcontentobject_status',
278
                'c.name AS ezcontentobject_name',
279
                'c.language_mask AS ezcontentobject_language_mask',
280
                'v.id AS ezcontentobject_version_id',
281
                'v.version AS ezcontentobject_version_version',
282
                'v.modified AS ezcontentobject_version_modified',
283
                'v.creator_id AS ezcontentobject_version_creator_id',
284
                'v.created AS ezcontentobject_version_created',
285
                'v.status AS ezcontentobject_version_status',
286
                'v.language_mask AS ezcontentobject_version_language_mask',
287
                'v.initial_language_id AS ezcontentobject_version_initial_language_id',
288
                't.main_node_id AS ezcontentobject_tree_main_node_id'
289
            )
290
            ->from('ezcontentobject', 'c')
291
            ->innerJoin(
292
                'c',
293
                'ezcontentobject_version',
294
                'v',
295
                $expr->andX(
296
                    $expr->eq('c.id', 'v.contentobject_id'),
297
                    $expr->eq('v.version', $versionNo ?: 'c.current_version')
298
                )
299
            )
300
            ->leftJoin(
301
                'c',
302
                'ezcontentobject_tree',
303
                't',
304
                $expr->andX(
305
                    $expr->eq('c.id', 't.contentobject_id'),
306
                    $expr->eq('t.node_id', 't.main_node_id')
307
                )
308
            );
309
310
        return $queryBuilder;
311
    }
312
}
313