Completed
Push — 15.x ( 74665b )
by Tim
02:57
created

SqlStatementRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.9666
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\SqlStatements
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Repositories;
22
23
use TechDivision\Import\Utils\SqlStatementKeys;
24
25
/**
26
 * Repository class with the SQL statements to use.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class SqlStatementRepository extends AbstractSqlStatementRepository
35
{
36
37
    /**
38
     * The SQL statements.
39
     *
40
     * @var array
41
     */
42
    private $statements = array(
43
        SqlStatementKeys::CATEGORIES =>
44
            'SELECT t0.*,
45
                    (SELECT `value`
46
                       FROM ${table:eav_attribute} t1, ${table:catalog_category_entity_varchar} t2
47
                      WHERE t1.attribute_code = \'name\'
48
                        AND t1.entity_type_id = 3
49
                        AND t2.attribute_id = t1.attribute_id
50
                        AND t2.store_id = 0
51
                        AND t2.${pk:entity_id} = t0.${pk:entity_id}) AS name,
52
                    (SELECT `value`
53
                       FROM ${table:eav_attribute} t1, ${table:catalog_category_entity_varchar} t2
54
                      WHERE t1.attribute_code = \'url_key\'
55
                        AND t1.entity_type_id = 3
56
                        AND t2.attribute_id = t1.attribute_id
57
                        AND t2.store_id = 0
58
                        AND t2.${pk:entity_id} = t0.${pk:entity_id}) AS url_key,
59
                    (SELECT `value`
60
                       FROM ${table:eav_attribute} t1, ${table:catalog_category_entity_varchar} t2
61
                      WHERE t1.attribute_code = \'url_path\'
62
                        AND t1.entity_type_id = 3
63
                        AND t2.attribute_id = t1.attribute_id
64
                        AND t2.store_id = 0
65
                        AND t2.${pk:entity_id} = t0.${pk:entity_id}) AS url_path,
66
                    (SELECT `value`
67
                       FROM ${table:eav_attribute} t1, ${table:catalog_category_entity_varchar} t2
68
                      WHERE t1.attribute_code = \'is_anchor\'
69
                        AND t1.entity_type_id = 3
70
                        AND t2.attribute_id = t1.attribute_id
71
                        AND t2.store_id = 0
72
                        AND t2.${pk:entity_id} = t0.${pk:entity_id}) AS is_anchor
73
               FROM ${table:catalog_category_entity} AS t0',
74
        SqlStatementKeys::ROOT_CATEGORIES =>
75
            'SELECT t2.code, t0.*
76
               FROM ${table:catalog_category_entity} t0
77
         INNER JOIN ${table:store_group} t1
78
                 ON t1.root_category_id = t0.entity_id
79
         INNER JOIN ${table:store} t2
80
                 ON t2.group_id = t1.group_id',
81
        SqlStatementKeys::CATEGORIES_BY_STORE_VIEW =>
82
            'SELECT t0.*,
83
                 IF (name_store.value_id > 0, name_store.value, name_default.value) AS name,
84
                 IF (url_key_store.value_id > 0, url_key_store.value, url_key_default.value) AS url_key,
85
                 IF (url_path_store.value_id > 0, url_path_store.value, url_path_default.value) AS url_path,
86
                 IF (is_anchor_store.value_id > 0, is_anchor_store.value, is_anchor_default.value) AS is_anchor
87
               FROM ${table:catalog_category_entity} AS t0
88
          LEFT JOIN ${table:catalog_category_entity_varchar} AS name_store
89
                 ON name_store.attribute_id = (
90
                        SELECT attribute_id FROM ${table:eav_attribute}
91
                        WHERE attribute_code = \'name\' AND entity_type_id = 3
92
                    )
93
                    AND name_store.store_id = :store_id
94
                    AND name_store.entity_id = t0.entity_id
95
          LEFT JOIN ${table:catalog_category_entity_varchar} AS name_default
96
                 ON name_default.attribute_id = (
97
                        SELECT attribute_id FROM ${table:eav_attribute}
98
                        WHERE attribute_code = \'name\' AND entity_type_id = 3
99
                    )
100
                    AND name_default.store_id = 0
101
                    AND name_default.entity_id = t0.entity_id
102
          LEFT JOIN ${table:catalog_category_entity_varchar} AS url_key_store
103
                 ON url_key_store.attribute_id = (
104
                        SELECT attribute_id FROM ${table:eav_attribute}
105
                        WHERE attribute_code = \'url_key\' AND entity_type_id = 3
106
                    )
107
                    AND url_key_store.store_id = :store_id
108
                    AND url_key_store.entity_id = t0.entity_id
109
          LEFT JOIN ${table:catalog_category_entity_varchar} AS url_key_default
110
                 ON url_key_default.attribute_id = (
111
                        SELECT attribute_id FROM ${table:eav_attribute}
112
                        WHERE attribute_code = \'url_key\' AND entity_type_id = 3
113
                    )
114
                    AND url_key_default.store_id = 0
115
                    AND url_key_default.entity_id = t0.entity_id
116
          LEFT JOIN ${table:catalog_category_entity_varchar} AS url_path_store
117
                 ON url_path_store.attribute_id = (
118
                        SELECT attribute_id FROM ${table:eav_attribute}
119
                        WHERE attribute_code = \'url_path\' AND entity_type_id = 3
120
                    )
121
                    AND url_path_store.store_id = :store_id
122
                    AND url_path_store.entity_id = t0.entity_id
123
          LEFT JOIN ${table:catalog_category_entity_varchar} AS url_path_default
124
                 ON url_path_default.attribute_id = (
125
                        SELECT attribute_id FROM ${table:eav_attribute}
126
                        WHERE attribute_code = \'url_path\' AND entity_type_id = 3
127
                    )
128
                    AND url_path_default.store_id = 0
129
                    AND url_path_default.entity_id = t0.entity_id
130
          LEFT JOIN ${table:catalog_category_entity_int} AS is_anchor_store
131
                 ON is_anchor_store.attribute_id = (
132
                        SELECT attribute_id FROM ${table:eav_attribute}
133
                        WHERE attribute_code = \'is_anchor\' AND entity_type_id = 3
134
                    )
135
                    AND is_anchor_store.store_id = :store_id
136
                    AND is_anchor_store.entity_id = t0.entity_id
137
          LEFT JOIN ${table:catalog_category_entity_int} AS is_anchor_default
138
                 ON is_anchor_default.attribute_id = (
139
                        SELECT attribute_id FROM ${table:eav_attribute}
140
                        WHERE attribute_code = \'is_anchor\' AND entity_type_id = 3
141
                    )
142
                    AND is_anchor_default.store_id = 0
143
                    AND is_anchor_default.entity_id = t0.entity_id',
144
        SqlStatementKeys::ROOT_CATEGORIES =>
145
            'SELECT t2.code, t0.*
146
               FROM ${table:catalog_category_entity} t0
147
         INNER JOIN ${table:store_group} t1
148
                 ON t1.root_category_id = t0.entity_id
149
         INNER JOIN ${table:store} t2
150
                 ON t2.group_id = t1.group_id',
151
        SqlStatementKeys::CATEGORY_VARCHARS_BY_ENTITY_IDS =>
152
            'SELECT t1.*
153
               FROM ${table:catalog_category_entity_varchar} AS t1
154
         INNER JOIN ${table:eav_attribute} AS t2
155
                 ON t2.entity_type_id = 3
156
                AND t2.attribute_code = \'name\'
157
                AND t1.attribute_id = t2.attribute_id
158
                AND t1.store_id = 0
159
                AND t1.entity_id IN (?)',
160
        SqlStatementKeys::STORES =>
161
            'SELECT t1.* FROM ${table:store} AS t1',
162
        SqlStatementKeys::STORE_DEFAULT =>
163
            'SELECT t0.*
164
               FROM ${table:store} t0
165
         INNER JOIN ${table:store_group} t1
166
                 ON t1.default_store_id = t0.store_id
167
         INNER JOIN ${table:store_website} t2
168
                 ON t1.website_id = t2.website_id
169
                AND t2.is_default = 1;',
170
        SqlStatementKeys::STORE_WEBSITES =>
171
            'SELECT t1.* FROM ${table:store_website} AS t1',
172
        SqlStatementKeys::STORE_GROUPS =>
173
            'SELECT t1.* FROM ${table:store_group} AS t1',
174
        SqlStatementKeys::TAX_CLASSES =>
175
            'SELECT t1.* FROM ${table:tax_class} AS t1',
176
        SqlStatementKeys::LINK_TYPES =>
177
            'SELECT t1.* FROM ${table:catalog_product_link_type} AS t1',
178
        SqlStatementKeys::LINK_ATTRIBUTES =>
179
            'SELECT t1.* FROM ${table:catalog_product_link_attribute} AS t1',
180
        SqlStatementKeys::EAV_ENTITY_TYPES =>
181
            'SELECT t1.* FROM ${table:eav_entity_type} AS t1',
182
        SqlStatementKeys::EAV_ENTITY_TYPE_BY_ENTITY_TYPE_CODE =>
183
            'SELECT *
184
               FROM ${table:eav_entity_type}
185
              WHERE entity_type_code = :entity_type_code',
186
        SqlStatementKeys::EAV_ATTRIBUTE_SET =>
187
            'SELECT t1.*
188
               FROM ${table:eav_attribute_set} AS t1
189
              WHERE attribute_set_id = ?',
190
        SqlStatementKeys::EAV_ATTRIBUTE_GROUP =>
191
            'SELECT t1.*
192
               FROM ${table:eav_attribute_group} AS t1
193
              WHERE attribute_group_id = :attribute_group_id',
194
        SqlStatementKeys::EAV_ATTRIBUTE_GROUP_BY_ENTITY_TYPE_CODE_AND_ATTRIBUTE_SET_NAME_AND_ATTRIBUTE_GROUP_NAME =>
195
            'SELECT t1.*
196
               FROM ${table:eav_attribute_group} AS t1
197
         INNER JOIN ${table:eav_entity_type} t3
198
                 ON t3.entity_type_code = :entity_type_code
199
         INNER JOIN ${table:eav_attribute_set} t2
200
                 ON t2.attribute_set_name = :attribute_set_name
201
                AND t2.entity_type_id = t3.entity_type_id
202
                AND t1.attribute_set_id = t2.attribute_set_id
203
              WHERE attribute_group_name = :attribute_group_name',
204
        SqlStatementKeys::EAV_ATTRIBUTE_SETS_BY_ENTITY_TYPE_ID =>
205
            'SELECT t1.*
206
               FROM ${table:eav_attribute_set} AS t1
207
              WHERE entity_type_id = ?',
208
        SqlStatementKeys::EAV_ATTRIBUTE_SET_BY_ENTITY_TYPE_CODE_AND_ATTRIBUTE_SET_NAME =>
209
            'SELECT t1.*
210
               FROM ${table:eav_attribute_set} AS t1
211
         INNER JOIN ${table:eav_entity_type} t2
212
                 ON t2.entity_type_code = :entity_type_code
213
                AND t1.entity_type_id = t2.entity_type_id
214
              WHERE t1.attribute_set_name = :attribute_set_name',
215
        SqlStatementKeys::EAV_ATTRIBUTE_SET_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_SET_NAME =>
216
            'SELECT *
217
               FROM ${table:eav_attribute_set}
218
              WHERE entity_type_id = :entity_type_id
219
                AND attribute_set_name = :attribute_set_name',
220
        SqlStatementKeys::EAV_ATTRIBUTE_GROUPS_BY_ATTRIBUTE_SET_ID =>
221
            'SELECT *
222
               FROM ${table:eav_attribute_group}
223
              WHERE attribute_set_id = :attribute_set_id',
224
        SqlStatementKeys::EAV_ATTRIBUTE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE =>
225
            'SELECT *
226
               FROM ${table:eav_attribute}
227
              WHERE entity_type_id = :entity_type_id
228
                AND attribute_code = :attribute_code',
229
        SqlStatementKeys::EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_SET_NAME =>
230
            'SELECT t3.*
231
               FROM ${table:eav_attribute} AS t3
232
         INNER JOIN ${table:eav_entity_type} AS t0
233
                 ON t0.entity_type_id = :entity_type_id
234
         INNER JOIN ${table:eav_attribute_set} AS t1
235
                 ON t1.attribute_set_name = :attribute_set_name
236
                AND t1.entity_type_id = t0.entity_type_id
237
         INNER JOIN ${table:eav_entity_attribute} AS t2
238
                 ON t2.attribute_set_id = t1.attribute_set_id
239
                AND t3.attribute_id = t2.attribute_id',
240
        SqlStatementKeys::EAV_ATTRIBUTES_BY_OPTION_VALUE_AND_STORE_ID =>
241
            'SELECT t1.*
242
               FROM ${table:eav_attribute} AS t1
243
         INNER JOIN ${table:eav_attribute_option_value} AS t2
244
                 ON t2.value = BINARY :value
245
                AND t2.store_id = :store_id
246
         INNER JOIN ${table:eav_attribute_option} AS t3
247
                 ON t3.option_id = t2.option_id
248
                AND t1.attribute_id = t3.attribute_id',
249
        SqlStatementKeys::EAV_ATTRIBUTES_BY_IS_USER_DEFINED =>
250
            'SELECT * FROM ${table:eav_attribute} WHERE is_user_defined = :is_user_defined',
251
        SqlStatementKeys::EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_IS_USER_DEFINED =>
252
            'SELECT *
253
               FROM ${table:eav_attribute}
254
              WHERE entity_type_id = :entity_type_id
255
                AND is_user_defined = :is_user_defined',
256
        SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE =>
257
            'SELECT t3.*
258
               FROM ${table:eav_attribute} t1,
259
                    ${table:eav_attribute_option} t2,
260
                    ${table:eav_attribute_option_value} t3
261
              WHERE t1.attribute_code = :attribute_code
262
                AND t3.store_id = :store_id
263
                AND t3.value = BINARY :value
264
                AND t2.attribute_id = t1.attribute_id
265
                AND t2.option_id = t3.option_id',
266
        SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE =>
267
            'SELECT t3.*
268
               FROM ${table:eav_attribute} t1,
269
                    ${table:eav_attribute_option} t2,
270
                    ${table:eav_attribute_option_value} t3
271
              WHERE t1.attribute_code = :attribute_code
272
                AND t1.entity_type_id = :entity_type_id
273
                AND t3.store_id = :store_id
274
                AND t3.value = BINARY :value
275
                AND t2.attribute_id = t1.attribute_id
276
                AND t2.option_id = t3.option_id',
277
        SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID =>
278
            'SELECT t1.*
279
               FROM ${table:eav_attribute_option_value} t1
280
              WHERE t1.option_id = :option_id
281
                AND t1.store_id = :store_id',
282
        SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUES =>
283
            'SELECT t3.*, t1.attribute_code
284
               FROM ${table:eav_attribute} t1,
285
                    ${table:eav_attribute_option} t2,
286
                    ${table:eav_attribute_option_value} t3
287
              WHERE t2.option_id = t3.option_id
288
                AND t1.attribute_id = t2.attribute_id',
289
        SqlStatementKeys::CORE_CONFIG_DATA =>
290
            'SELECT * FROM ${table:core_config_data}',
291
        SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID =>
292
            'SELECT *
293
               FROM ${table:url_rewrite}
294
              WHERE entity_type = :entity_type
295
                AND entity_id = :entity_id',
296
        SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID_AND_STORE_ID =>
297
            'SELECT *
298
               FROM ${table:url_rewrite}
299
              WHERE entity_type = :entity_type
300
                AND entity_id = :entity_id
301
                AND store_id = :store_id',
302
        SqlStatementKeys::DELETE_URL_REWRITE =>
303
            'DELETE
304
               FROM ${table:url_rewrite}
305
              WHERE url_rewrite_id = :url_rewrite_id',
306
        SqlStatementKeys::DELETE_URL_REWRITE_BY_SKU =>
307
            'DELETE t1.*
308
               FROM ${table:url_rewrite} t1
309
         INNER JOIN ${table:catalog_product_entity} t2
310
              WHERE t2.sku = :sku
311
                AND t1.entity_id = t2.entity_id',
312
        SqlStatementKeys::DELETE_URL_REWRITE_BY_PATH =>
313
            'DELETE t1.*
314
               FROM ${table:url_rewrite} t1
315
         INNER JOIN ${table:catalog_category_entity} t2
316
              WHERE t2.path = :path
317
                AND t1.entity_id = t2.entity_id
318
                AND t1.entity_type = \'category\'',
319
        SqlStatementKeys::DELETE_URL_REWRITE_BY_CATEGORY_ID =>
320
            'DELETE t1.*
321
               FROM ${table:url_rewrite} t1
322
         INNER JOIN ${table:catalog_url_rewrite_product_category} t2
323
              WHERE t2.category_id = :category_id
324
                AND t1.url_rewrite_id = t2.url_rewrite_id',
325
        SqlStatementKeys::CREATE_URL_REWRITE =>
326
            'INSERT
327
               INTO ${table:url_rewrite}
328
                    (entity_type,
329
                     entity_id,
330
                     request_path,
331
                     target_path,
332
                     redirect_type,
333
                     store_id,
334
                     description,
335
                     is_autogenerated,
336
                     metadata)
337
             VALUES (:entity_type,
338
                     :entity_id,
339
                     :request_path,
340
                     :target_path,
341
                     :redirect_type,
342
                     :store_id,
343
                     :description,
344
                     :is_autogenerated,
345
                     :metadata)',
346
        SqlStatementKeys::UPDATE_URL_REWRITE =>
347
            'UPDATE ${table:url_rewrite}
348
                SET entity_type = :entity_type,
349
                    entity_id = :entity_id,
350
                    request_path = :request_path,
351
                    target_path = :target_path,
352
                    redirect_type = :redirect_type,
353
                    store_id = :store_id,
354
                    description = :description,
355
                    is_autogenerated = :is_autogenerated,
356
                    metadata = :metadata
357
              WHERE url_rewrite_id = :url_rewrite_id',
358
        SqlStatementKeys::CREATE_STORE =>
359
            'INSERT
360
               INTO ${table:store}
361
                    (code,
362
                     website_id,
363
                     group_id,
364
                     name,
365
                     sort_order,
366
                     is_active)
367
             VALUES (:code,
368
                     :website_id,
369
                     :group_id,
370
                     :name,
371
                     :sort_order,
372
                     :is_active)',
373
        SqlStatementKeys::UPDATE_STORE =>
374
            'UPDATE ${table:store}
375
                SET code = :code,
376
                    website_id = :website_id,
377
                    group_id = :group_id,
378
                    name = :name,
379
                    sort_order = :sort_order,
380
                    is_active = :is_active
381
              WHERE store_id = :store_id',
382
        SqlStatementKeys::CREATE_STORE_GROUP =>
383
            'INSERT
384
               INTO ${table:store_group}
385
                    (website_id,
386
                     name,
387
                     root_category_id,
388
                     default_store_id)
389
             VALUES (:website_id,
390
                     :name,
391
                     :root_category_id,
392
                     :default_store_id)',
393
        SqlStatementKeys::UPDATE_STORE_GROUP =>
394
            'UPDATE ${table:store_group}
395
                SET website_id = :website_id,
396
                    name = :name,
397
                    root_category_id = :root_category_id,
398
                    default_store_id = :default_store_id
399
              WHERE group_id = :group_id',
400
        SqlStatementKeys::CREATE_STORE_WEBSITE =>
401
            'INSERT
402
               INTO ${table:store_website}
403
                    (code,
404
                     name,
405
                     sort_order,
406
                     default_group_id,
407
                     is_default)
408
             VALUES (:code,
409
                     :name,
410
                     :sort_order,
411
                     :default_group_id,
412
                     :is_default)',
413
        SqlStatementKeys::UPDATE_STORE_WEBSITE =>
414
            'UPDATE ${table:store_website}
415
                SET code = :code,
416
                    name = :name,
417
                    sort_order = :sort_order,
418
                    default_group_id = :default_group_id,
419
                    is_default = :is_default
420
              WHERE website_id = :website_id',
421
        SqlStatementKeys::IMAGE_TYPES =>
422
            'SELECT main_table.attribute_code,
423
                    main_table.attribute_id,
424
                    main_table.is_user_defined
425
               FROM ${table:eav_attribute} AS main_table
426
         INNER JOIN ${table:eav_entity_type} AS entity_type
427
                 ON main_table.entity_type_id = entity_type.entity_type_id
428
          LEFT JOIN ${table:eav_entity_attribute}
429
                 ON main_table.attribute_id = ${table:eav_entity_attribute}.attribute_id
430
         INNER JOIN ${table:catalog_eav_attribute} AS additional_table
431
                 ON main_table.attribute_id = additional_table.attribute_id
432
              WHERE (entity_type_code = \'catalog_product\')
433
                AND (frontend_input = \'media_image\')
434
           GROUP BY main_table.attribute_code,
435
                    main_table.attribute_id,
436
                    main_table.is_user_defined
437
           ORDER BY main_table.is_user_defined,
438
                    main_table.attribute_id',
439
        SqlStatementKeys::CUSTOMER_GROUPS =>
440
            'SELECT * FROM ${table:customer_group}'
441
    );
442
443
    /**
444
     * Initializes the SQL statement repository with the primary key and table prefix utility.
445
     *
446
     * @param \TechDivision\Import\Utils\SqlCompilerInterface[] $compilers The array with the compiler instances
447
     */
448
    public function __construct(\IteratorAggregate $compilers)
449
    {
450
451
        // pass primary key + table prefix utility to parent instance
452
        parent::__construct($compilers);
453
454
        // compile the SQL statements
455
        $this->compile($this->statements);
456
    }
457
}
458