Completed
Push — ezp_30797 ( b39740...a92ef0 )
by
unknown
17:49
created

QueryBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 303
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 303
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 5

6 Methods

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