Completed
Push — master ( deea5c...9cc936 )
by Tim
16s
created

SqlStatements::__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\Utils;
22
23
/**
24
 * Utility class with the SQL statements to use.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class SqlStatements extends AbstractSqlStatements
33
{
34
35
    /**
36
     * The SQL statement to load all available categories.
37
     *
38
     * @var string
39
     */
40
    const CATEGORIES = 'categories';
41
42
    /**
43
     * The SQL statement to load the root categories.
44
     *
45
     * @var string
46
     */
47
    const ROOT_CATEGORIES = 'root_categories';
48
49
    /**
50
     * The SQL statement to load the category varchars for a list of entity IDs.
51
     *
52
     * @var string
53
     */
54
    const CATEGORY_VARCHARS_BY_ENTITY_IDS = 'category_varchars.by.entity_ids';
55
56
    /**
57
     * The SQL statement to load the stores.
58
     *
59
     * @var string
60
     */
61
    const STORES = 'stores';
62
63
    /**
64
     * The SQL statement to load the default store.
65
     *
66
     * @var string
67
     */
68
    const STORE_DEFAULT = 'store_default';
69
70
    /**
71
     * The SQL statement to load the store websites.
72
     *
73
     * @var string
74
     */
75
    const STORE_WEBSITES = 'store_websites';
76
77
    /**
78
     * The SQL statement to load the store groups.
79
     *
80
     * @var string
81
     */
82
    const STORE_GROUPS = 'store_groups';
83
84
    /**
85
     * The SQL statement to load the tax classes.
86
     *
87
     * @var string
88
     */
89
    const TAX_CLASSES = 'tax_classes';
90
91
    /**
92
     * The SQL statement to load all available link types.
93
     *
94
     * @var string
95
     */
96
    const LINK_TYPES = 'link_types';
97
98
    /**
99
     * The SQL statement to load all available link types.
100
     *
101
     * @var string
102
     */
103
    const LINK_ATTRIBUTES = 'link_attributes';
104
105
    /**
106
     * The SQL statement to load all available EAV entity types.
107
     *
108
     * @var string
109
     */
110
    const EAV_ENTITY_TYPES = 'eav_entity_types';
111
112
    /**
113
     * The SQL statement to load the EAV attribute set.
114
     *
115
     * @var string
116
     */
117
    const EAV_ATTRIBUTE_SET = 'eav_attribute_set';
118
119
    /**
120
     * The SQL statement to load the EAV attribute group.
121
     *
122
     * @var string
123
     */
124
    const EAV_ATTRIBUTE_GROUP = 'eav_attribute_group';
125
126
    /**
127
     * The SQL statement to load the attribute sets for a specific entity type.
128
     *
129
     * @var string
130
     */
131
    const EAV_ATTRIBUTE_SETS_BY_ENTITY_TYPE_ID = 'eav_attribute_sets.by.entity_type_id';
132
133
    /**
134
     * The SQL statement to load the EAV attribute groups for a specific attribute set ID.
135
     *
136
     * @var string
137
     */
138
    const EAV_ATTRIBUTE_GROUPS_BY_ATTRIBUTE_SET_ID = 'eav_attribute_groups.by.attribute_set_id';
139
140
    /**
141
     * The SQL statement to load EAV attributes by entity type ID and attribute set name.
142
     *
143
     * @var string
144
     */
145
    const EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_SET_NAME = 'eav_attributes.by.entity_type_id.and.attribute_set_name';
146
147
    /**
148
     * The SQL statement to load EAV attributes by attribute option value and store ID.
149
     *
150
     * @var string
151
     */
152
    const EAV_ATTRIBUTES_BY_OPTION_VALUE_AND_STORE_ID = 'eav_attributes.by.option_value.and.store_id';
153
154
    /**
155
     * The SQL statement to load EAV attributes by passed is user defined flag.
156
     *
157
     * @var string
158
     */
159
    const EAV_ATTRIBUTES_BY_IS_USER_DEFINED = 'eav_attributes.by.is_user_defined';
160
161
    /**
162
     * The SQL statement to load EAV attributes by passed entity type ID and is user defined flag.
163
     *
164
     * @var string
165
     */
166
    const EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_IS_USER_DEFINED = 'eav_attributes.by.entity_type_id.and.is_user_defined';
167
168
    /**
169
     * The SQL statement to load the EAV attribute option value by its attribute code, store ID and value.
170
     *
171
     * @var string
172
     */
173
    const EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE = 'eav_attribute_option_value.by.attribute_code.and.store_id.and.value';
174
175
    /**
176
     * The SQL statement to load the Magento 2 configuration.
177
     *
178
     * @var string
179
     */
180
    const CORE_CONFIG_DATA = 'core_config_data';
181
182
    /**
183
     * The SQL statement to load the URL rewrites for the passed entity type and ID.
184
     *
185
     * @var string
186
     */
187
    const URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID = 'url_rewrites.by.entity_type.and.entity_id';
188
189
    /**
190
     * The SQL statement to remove a existing URL rewrite.
191
     *
192
     * @var string
193
     */
194
    const DELETE_URL_REWRITE = 'delete.url_rewrite';
195
196
    /**
197
     * The SQL statement to remove all existing URL rewrites related with the SKU passed as parameter.
198
     *
199
     * @var string
200
     */
201
    const DELETE_URL_REWRITE_BY_SKU = 'delete.url_rewrite.by.sku';
202
203
    /**
204
     * The SQL statement to remove all existing URL rewrites related with the category path passed as parameter.
205
     *
206
     * @var string
207
     */
208
    const DELETE_URL_REWRITE_BY_PATH = 'delete.url_rewrite.by.path';
209
210
    /**
211
     * The SQL statement to create new URL rewrites.
212
     *
213
     * @var string
214
     */
215
    const CREATE_URL_REWRITE = 'create.url_rewrite';
216
217
    /**
218
     * The SQL statement to update an existing URL rewrite.
219
     *
220
     * @var string
221
     */
222
    const UPDATE_URL_REWRITE = 'update.url_rewrite';
223
224
    /**
225
     * The SQL statements.
226
     *
227
     * @var array
228
     */
229
    private $statements = array(
230
        SqlStatements::CATEGORIES =>
231
            'SELECT t0.*,
232
                    (SELECT `value`
233
                       FROM eav_attribute t1, catalog_category_entity_varchar t2
234
                      WHERE t1.attribute_code = \'name\'
235
                        AND t1.entity_type_id = 3
236
                        AND t2.attribute_id = t1.attribute_id
237
                        AND t2.store_id = 0
238
                        AND t2.entity_id = t0.entity_id) AS name,
239
                    (SELECT `value`
240
                       FROM eav_attribute t1, catalog_category_entity_varchar t2
241
                      WHERE t1.attribute_code = \'url_key\'
242
                        AND t1.entity_type_id = 3
243
                        AND t2.attribute_id = t1.attribute_id
244
                        AND t2.store_id = 0
245
                        AND t2.entity_id = t0.entity_id) AS url_key,
246
                    (SELECT `value`
247
                       FROM eav_attribute t1, catalog_category_entity_varchar t2
248
                      WHERE t1.attribute_code = \'url_path\'
249
                        AND t1.entity_type_id = 3
250
                        AND t2.attribute_id = t1.attribute_id
251
                        AND t2.store_id = 0
252
                        AND t2.entity_id = t0.entity_id) AS url_path
253
               FROM catalog_category_entity AS t0',
254
        SqlStatements::ROOT_CATEGORIES =>
255
            'SELECT t2.code, t0.*
256
               FROM catalog_category_entity t0
257
         INNER JOIN store_group t1
258
                 ON t1.root_category_id = t0.entity_id
259
         INNER JOIN store t2
260
                 ON t2.group_id = t1.group_id',
261
        SqlStatements::ROOT_CATEGORIES =>
262
            'SELECT t2.code, t0.*
263
               FROM catalog_category_entity t0
264
         INNER JOIN store_group t1
265
                 ON t1.root_category_id = t0.entity_id
266
         INNER JOIN store t2
267
                 ON t2.group_id = t1.group_id',
268
        SqlStatements::CATEGORY_VARCHARS_BY_ENTITY_IDS =>
269
            'SELECT t1.*
270
               FROM catalog_category_entity_varchar AS t1
271
         INNER JOIN eav_attribute AS t2
272
                 ON t2.entity_type_id = 3
273
                AND t2.attribute_code = \'name\'
274
                AND t1.attribute_id = t2.attribute_id
275
                AND t1.store_id = 0
276
                AND t1.entity_id IN (?)',
277
        SqlStatements::STORES =>
278
            'SELECT t1.* FROM store AS t1',
279
        SqlStatements::STORE_DEFAULT =>
280
            'SELECT t0.*
281
               FROM store t0
282
         INNER JOIN store_group t1
283
                 ON t1.default_store_id = t0.store_id
284
         INNER JOIN store_website t2
285
                 ON t1.website_id = t2.website_id
286
                AND t2.is_default = 1;',
287
        SqlStatements::STORE_WEBSITES =>
288
            'SELECT t1.* FROM store_website AS t1',
289
        SqlStatements::STORE_GROUPS =>
290
            'SELECT t1.* FROM store_group AS t1',
291
        SqlStatements::TAX_CLASSES =>
292
            'SELECT t1.* FROM tax_class AS t1',
293
        SqlStatements::LINK_TYPES =>
294
            'SELECT t1.* FROM catalog_product_link_type AS t1',
295
        SqlStatements::LINK_ATTRIBUTES =>
296
            'SELECT t1.* FROM catalog_product_link_attribute AS t1',
297
        SqlStatements::EAV_ENTITY_TYPES =>
298
            'SELECT t1.* FROM eav_entity_type AS t1',
299
        SqlStatements::EAV_ATTRIBUTE_SET =>
300
            'SELECT t1.*
301
               FROM eav_attribute_set AS t1
302
              WHERE attribute_set_id = ?',
303
        SqlStatements::EAV_ATTRIBUTE_GROUP =>
304
            'SELECT t1.*
305
               FROM eav_attribute_group AS t1
306
              WHERE attribute_group_id = :attribute_group_id',
307
        SqlStatements::EAV_ATTRIBUTE_SETS_BY_ENTITY_TYPE_ID =>
308
            'SELECT t1.*
309
               FROM eav_attribute_set AS t1
310
              WHERE entity_type_id = ?',
311
        SqlStatements::EAV_ATTRIBUTE_GROUPS_BY_ATTRIBUTE_SET_ID =>
312
            'SELECT *
313
               FROM eav_attribute_group
314
              WHERE attribute_set_id = :attribute_set_id',
315
        SqlStatements::EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_SET_NAME =>
316
            'SELECT t3.*
317
               FROM eav_attribute AS t3
318
         INNER JOIN eav_entity_type AS t0
319
                 ON t0.entity_type_id = :entity_type_id
320
         INNER JOIN eav_attribute_set AS t1
321
                 ON t1.attribute_set_name = :attribute_set_name
322
                AND t1.entity_type_id = t0.entity_type_id
323
         INNER JOIN eav_entity_attribute AS t2
324
                 ON t2.attribute_set_id = t1.attribute_set_id
325
                AND t3.attribute_id = t2.attribute_id',
326
        SqlStatements::EAV_ATTRIBUTES_BY_OPTION_VALUE_AND_STORE_ID =>
327
            'SELECT t1.*
328
               FROM eav_attribute AS t1
329
         INNER JOIN eav_attribute_option_value AS t2
330
                 ON t2.value = :value
331
                AND t2.store_id = :store_id
332
         INNER JOIN eav_attribute_option AS t3
333
                 ON t3.option_id = t2.option_id
334
                AND t1.attribute_id = t3.attribute_id',
335
        SqlStatements::EAV_ATTRIBUTES_BY_IS_USER_DEFINED =>
336
            'SELECT * FROM eav_attribute WHERE is_user_defined = :is_user_defined',
337
        SqlStatements::EAV_ATTRIBUTES_BY_ENTITY_TYPE_ID_AND_IS_USER_DEFINED =>
338
            'SELECT *
339
               FROM eav_attribute
340
              WHERE entity_type_id = :entity_type_id
341
                AND is_user_defined = :is_user_defined',
342
        SqlStatements::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE =>
343
            'SELECT t3.*
344
               FROM eav_attribute t1,
345
                    eav_attribute_option t2,
346
                    eav_attribute_option_value t3
347
              WHERE t1.attribute_code = :attribute_code
348
                AND t3.store_id = :store_id
349
                AND t3.value = :value
350
                AND t2.attribute_id = t1.attribute_id
351
                AND t2.option_id = t3.option_id',
352
        SqlStatements::CORE_CONFIG_DATA =>
353
            'SELECT * FROM core_config_data',
354
        SqlStatements::URL_REWRITES_BY_ENTITY_TYPE_AND_ENTITY_ID =>
355
            'SELECT *
356
               FROM url_rewrite
357
              WHERE entity_type = :entity_type
358
                AND entity_id = :entity_id',
359
        SqlStatements::DELETE_URL_REWRITE =>
360
            'DELETE
361
               FROM url_rewrite
362
              WHERE url_rewrite_id = :url_rewrite_id',
363
        SqlStatements::DELETE_URL_REWRITE_BY_SKU =>
364
            'DELETE url_rewrite
365
               FROM url_rewrite
366
         INNER JOIN catalog_product_entity
367
              WHERE catalog_product_entity.sku = :sku
368
                AND url_rewrite.entity_id = catalog_product_entity.entity_id',
369
        SqlStatements::DELETE_URL_REWRITE_BY_PATH =>
370
            'DELETE url_rewrite
371
               FROM url_rewrite
372
         INNER JOIN catalog_category_entity
373
              WHERE catalog_category_entity.path = :path
374
                AND url_rewrite.entity_id = catalog_category_entity.entity_id
375
                AND url_rewrite.entity_type = \'category\'',
376
        SqlStatements::CREATE_URL_REWRITE =>
377
            'INSERT
378
               INTO url_rewrite
379
                    (entity_type,
380
                     entity_id,
381
                     request_path,
382
                     target_path,
383
                     redirect_type,
384
                     store_id,
385
                     description,
386
                     is_autogenerated,
387
                     metadata)
388
             VALUES (:entity_type,
389
                     :entity_id,
390
                     :request_path,
391
                     :target_path,
392
                     :redirect_type,
393
                     :store_id,
394
                     :description,
395
                     :is_autogenerated,
396
                     :metadata)',
397
        SqlStatements::UPDATE_URL_REWRITE =>
398
            'UPDATE url_rewrite
399
                SET entity_type = :entity_type,
400
                    entity_id = :entity_id,
401
                    request_path = :request_path,
402
                    target_path = :target_path,
403
                    redirect_type = :redirect_type,
404
                    store_id = :store_id,
405
                    description = :description,
406
                    is_autogenerated = :is_autogenerated,
407
                    metadata = :metadata
408
              WHERE url_rewrite_id = :url_rewrite_id'
409
    );
410
411
    /**
412
     * Initialize the the SQL statements.
413
     */
414 2
    public function __construct()
415
    {
416
417
        // merge the class statements
418 2
        foreach ($this->statements as $key => $statement) {
419 2
            $this->preparedStatements[$key] = $statement;
420
        }
421 2
    }
422
}
423