Completed
Pull Request — master (#128)
by Tim
04:34
created

SqlStatementRepository::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
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 eav_attribute t1, 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.entity_id = t0.entity_id) AS name,
52
                    (SELECT `value`
53
                       FROM eav_attribute t1, 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.entity_id = t0.entity_id) AS url_key,
59
                    (SELECT `value`
60
                       FROM eav_attribute t1, 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.entity_id = t0.entity_id) AS url_path,
66
                    (SELECT `value`
67
                       FROM eav_attribute t1, catalog_category_entity_int 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.entity_id = t0.entity_id) AS is_anchor
73
               FROM catalog_category_entity AS t0',
74
        SqlStatementKeys::ROOT_CATEGORIES =>
75
            'SELECT t2.code, t0.*
76
               FROM catalog_category_entity t0
77
         INNER JOIN store_group t1
78
                 ON t1.root_category_id = t0.entity_id
79
         INNER JOIN 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 catalog_category_entity AS t0
88
          LEFT JOIN catalog_category_entity_varchar AS name_store
89
                 ON name_store.attribute_id = (
90
                        SELECT attribute_id FROM 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 catalog_category_entity_varchar AS name_default
96
                 ON name_default.attribute_id = (
97
                        SELECT attribute_id FROM 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 catalog_category_entity_varchar AS url_key_store
103
                 ON url_key_store.attribute_id = (
104
                        SELECT attribute_id FROM 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 catalog_category_entity_varchar AS url_key_default
110
                 ON url_key_default.attribute_id = (
111
                        SELECT attribute_id FROM 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 catalog_category_entity_varchar AS url_path_store
117
                 ON url_path_store.attribute_id = (
118
                        SELECT attribute_id FROM 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 catalog_category_entity_varchar AS url_path_default
124
                 ON url_path_default.attribute_id = (
125
                        SELECT attribute_id FROM 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 catalog_category_entity_int AS is_anchor_store
131
                 ON is_anchor_store.attribute_id = (
132
                        SELECT attribute_id FROM 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 catalog_category_entity_int AS is_anchor_default
138
                 ON is_anchor_default.attribute_id = (
139
                        SELECT attribute_id FROM 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 catalog_category_entity t0
147
         INNER JOIN store_group t1
148
                 ON t1.root_category_id = t0.entity_id
149
         INNER JOIN store t2
150
                 ON t2.group_id = t1.group_id',
151
        SqlStatementKeys::CATEGORY_VARCHARS_BY_ENTITY_IDS =>
152
            'SELECT t1.*
153
               FROM catalog_category_entity_varchar AS t1
154
         INNER JOIN 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 store AS t1',
162
        SqlStatementKeys::STORE_DEFAULT =>
163
            'SELECT t0.*
164
               FROM store t0
165
         INNER JOIN store_group t1
166
                 ON t1.default_store_id = t0.store_id
167
         INNER JOIN 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 store_website AS t1',
172
        SqlStatementKeys::STORE_GROUPS =>
173
            'SELECT t1.* FROM store_group AS t1',
174
        SqlStatementKeys::TAX_CLASSES =>
175
            'SELECT t1.* FROM tax_class AS t1',
176
        SqlStatementKeys::LINK_TYPES =>
177
            'SELECT t1.* FROM catalog_product_link_type AS t1',
178
        SqlStatementKeys::LINK_ATTRIBUTES =>
179
            'SELECT t1.* FROM catalog_product_link_attribute AS t1',
180
        SqlStatementKeys::EAV_ENTITY_TYPES =>
181
            'SELECT t1.* FROM eav_entity_type AS t1',
182
        SqlStatementKeys::EAV_ATTRIBUTE_SET =>
183
            'SELECT t1.*
184
               FROM eav_attribute_set AS t1
185
              WHERE attribute_set_id = ?',
186
        SqlStatementKeys::EAV_ATTRIBUTE_GROUP =>
187
            'SELECT t1.*
188
               FROM eav_attribute_group AS t1
189
              WHERE attribute_group_id = :attribute_group_id',
190
        SqlStatementKeys::EAV_ATTRIBUTE_SETS_BY_ENTITY_TYPE_ID =>
191
            'SELECT t1.*
192
               FROM eav_attribute_set AS t1
193
              WHERE entity_type_id = ?',
194
        SqlStatementKeys::EAV_ATTRIBUTE_GROUPS_BY_ATTRIBUTE_SET_ID =>
195
            'SELECT *
196
               FROM eav_attribute_group
197
              WHERE attribute_set_id = :attribute_set_id',
198
        SqlStatementKeys::EAV_ATTRIBUTE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE =>
199
            'SELECT *
200
               FROM eav_attribute
201
              WHERE entity_type_id = :entity_type_id
202
                AND attribute_code = :attribute_code',
203
        SqlStatementKeys::EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_SET_NAME =>
204
            'SELECT t3.*
205
               FROM eav_attribute AS t3
206
         INNER JOIN eav_entity_type AS t0
207
                 ON t0.entity_type_id = :entity_type_id
208
         INNER JOIN eav_attribute_set AS t1
209
                 ON t1.attribute_set_name = :attribute_set_name
210
                AND t1.entity_type_id = t0.entity_type_id
211
         INNER JOIN eav_entity_attribute AS t2
212
                 ON t2.attribute_set_id = t1.attribute_set_id
213
                AND t3.attribute_id = t2.attribute_id',
214
        SqlStatementKeys::EAV_ATTRIBUTES_BY_OPTION_VALUE_AND_STORE_ID =>
215
            'SELECT t1.*
216
               FROM eav_attribute AS t1
217
         INNER JOIN eav_attribute_option_value AS t2
218
                 ON t2.value = :value
219
                AND t2.store_id = :store_id
220
         INNER JOIN eav_attribute_option AS t3
221
                 ON t3.option_id = t2.option_id
222
                AND t1.attribute_id = t3.attribute_id',
223
        SqlStatementKeys::EAV_ATTRIBUTES_BY_IS_USER_DEFINED =>
224
            'SELECT * FROM eav_attribute WHERE is_user_defined = :is_user_defined',
225
        SqlStatementKeys::EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_IS_USER_DEFINED =>
226
            'SELECT *
227
               FROM eav_attribute
228
              WHERE entity_type_id = :entity_type_id
229
                AND is_user_defined = :is_user_defined',
230
        SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE =>
231
            'SELECT t3.*
232
               FROM eav_attribute t1,
233
                    eav_attribute_option t2,
234
                    eav_attribute_option_value t3
235
              WHERE t1.attribute_code = :attribute_code
236
                AND t3.store_id = :store_id
237
                AND t3.value = :value
238
                AND t2.attribute_id = t1.attribute_id
239
                AND t2.option_id = t3.option_id',
240
        SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID =>
241
            'SELECT t1.*
242
               FROM eav_attribute_option_value t1
243
              WHERE t1.option_id = :option_id
244
                AND t1.store_id = :store_id',
245
        SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUES =>
246
            'SELECT t3.*, t1.attribute_code
247
               FROM eav_attribute t1,
248
                    eav_attribute_option t2,
249
                    eav_attribute_option_value t3
250
              WHERE t2.option_id = t3.option_id
251
                AND t1.attribute_id = t2.attribute_id',
252
        SqlStatementKeys::CORE_CONFIG_DATA =>
253
            'SELECT * FROM core_config_data',
254
        SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID =>
255
            'SELECT *
256
               FROM url_rewrite
257
              WHERE entity_type = :entity_type
258
                AND entity_id = :entity_id',
259
        SqlStatementKeys::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID_AND_STORE_ID =>
260
            'SELECT *
261
               FROM url_rewrite
262
              WHERE entity_type = :entity_type
263
                AND entity_id = :entity_id
264
                AND store_id = :store_id',
265
        SqlStatementKeys::DELETE_URL_REWRITE =>
266
            'DELETE
267
               FROM url_rewrite
268
              WHERE url_rewrite_id = :url_rewrite_id',
269
        SqlStatementKeys::DELETE_URL_REWRITE_BY_SKU =>
270
            'DELETE url_rewrite
271
               FROM url_rewrite
272
         INNER JOIN catalog_product_entity
273
              WHERE catalog_product_entity.sku = :sku
274
                AND url_rewrite.entity_id = catalog_product_entity.entity_id',
275
        SqlStatementKeys::DELETE_URL_REWRITE_BY_PATH =>
276
            'DELETE url_rewrite
277
               FROM url_rewrite
278
         INNER JOIN catalog_category_entity
279
              WHERE catalog_category_entity.path = :path
280
                AND url_rewrite.entity_id = catalog_category_entity.entity_id
281
                AND url_rewrite.entity_type = \'category\'',
282
        SqlStatementKeys::DELETE_URL_REWRITE_BY_CATEGORY_ID =>
283
            'DELETE t1.*
284
               FROM url_rewrite t1
285
         INNER JOIN catalog_url_rewrite_product_category t2
286
              WHERE t2.category_id = :category_id
287
                AND t1.url_rewrite_id = t2.url_rewrite_id',
288
        SqlStatementKeys::CREATE_URL_REWRITE =>
289
            'INSERT
290
               INTO url_rewrite
291
                    (entity_type,
292
                     entity_id,
293
                     request_path,
294
                     target_path,
295
                     redirect_type,
296
                     store_id,
297
                     description,
298
                     is_autogenerated,
299
                     metadata)
300
             VALUES (:entity_type,
301
                     :entity_id,
302
                     :request_path,
303
                     :target_path,
304
                     :redirect_type,
305
                     :store_id,
306
                     :description,
307
                     :is_autogenerated,
308
                     :metadata)',
309
        SqlStatementKeys::UPDATE_URL_REWRITE =>
310
            'UPDATE url_rewrite
311
                SET entity_type = :entity_type,
312
                    entity_id = :entity_id,
313
                    request_path = :request_path,
314
                    target_path = :target_path,
315
                    redirect_type = :redirect_type,
316
                    store_id = :store_id,
317
                    description = :description,
318
                    is_autogenerated = :is_autogenerated,
319
                    metadata = :metadata
320
              WHERE url_rewrite_id = :url_rewrite_id',
321
        SqlStatementKeys::CREATE_STORE =>
322
            'INSERT
323
               INTO store
324
                    (code,
325
                     website_id,
326
                     group_id,
327
                     name,
328
                     sort_order,
329
                     is_active)
330
             VALUES (:code,
331
                     :website_id,
332
                     :group_id,
333
                     :name,
334
                     :sort_order,
335
                     :is_active)',
336
        SqlStatementKeys::UPDATE_STORE =>
337
            'UPDATE store
338
                SET code = :code,
339
                    website_id = :website_id,
340
                    group_id = :group_id,
341
                    name = :name,
342
                    sort_order = :sort_order,
343
                    is_active = :is_active
344
              WHERE store_id = :store_id',
345
        SqlStatementKeys::CREATE_STORE_GROUP =>
346
            'INSERT
347
               INTO store_group
348
                    (website_id,
349
                     name,
350
                     root_category_id,
351
                     default_store_id)
352
             VALUES (:website_id,
353
                     :name,
354
                     :root_category_id,
355
                     :default_store_id)',
356
        SqlStatementKeys::UPDATE_STORE_GROUP =>
357
            'UPDATE store_group
358
                SET website_id = :website_id,
359
                    name = :name,
360
                    root_category_id = :root_category_id,
361
                    default_store_id = :default_store_id
362
              WHERE group_id = :group_id',
363
        SqlStatementKeys::CREATE_STORE_WEBSITE =>
364
            'INSERT
365
               INTO store_website
366
                    (code,
367
                     name,
368
                     sort_order,
369
                     default_group_id,
370
                     is_default)
371
             VALUES (:code,
372
                     :name,
373
                     :sort_order,
374
                     :default_group_id,
375
                     :is_default)',
376
        SqlStatementKeys::UPDATE_STORE_WEBSITE =>
377
            'UPDATE store_website
378
                SET code = :code,
379
                    name = :name,
380
                    sort_order = :sort_order,
381
                    default_group_id = :default_group_id,
382
                    is_default = :is_default
383
              WHERE website_id = :website_id',
384
        SqlStatementKeys::IMAGE_TYPES =>
385
            'SELECT main_table.attribute_code
386
               FROM eav_attribute AS main_table
387
         INNER JOIN eav_entity_type AS entity_type
388
                 ON main_table.entity_type_id = entity_type.entity_type_id
389
          LEFT JOIN eav_entity_attribute
390
                 ON main_table.attribute_id = eav_entity_attribute.attribute_id
391
         INNER JOIN catalog_eav_attribute AS additional_table
392
                 ON main_table.attribute_id = additional_table.attribute_id
393
              WHERE (entity_type_code = \'catalog_product\')
394
                AND (frontend_input = \'media_image\')
395
           GROUP BY main_table.attribute_code'
396
    );
397
398
    /**
399
     * Initialize the the SQL statements.
400
     */
401 2
    public function __construct()
402
    {
403
404
        // merge the class statements
405 2
        foreach ($this->statements as $key => $statement) {
406 2
            $this->preparedStatements[$key] = $statement;
407
        }
408 2
    }
409
}
410