/
category.sql
705 lines (465 loc) · 16.1 KB
/
category.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
-- Categories
-- get all categories
CREATE PROCEDURE getCategories(
-- variables
IN language_id INT,
IN taxonomy_id INT,
IN site_id INT,
IN post_id INT,
IN product_id INT,
IN search CHAR,
IN type CHAR,
IN post_type CHAR,
-- pagination
IN start INT,
IN limit INT,
-- return array of categories for categories query
OUT fetch_all,
-- return categories count for count query
OUT fetch_one
)
BEGIN
SELECT DISTINCT categories.taxonomy_item_id, categories.*, tc.*, tc.content as content, tc.name as name, categories.taxonomy_item_id as array_key
@IF isset(:post_id)
THEN
,pt.post_id as checked
END @IF
@IF isset(:product_id)
THEN
,pt.product_id as checked
END @IF
FROM taxonomy_item AS categories
INNER JOIN taxonomy_to_site t2s ON (categories.taxonomy_item_id = t2s.taxonomy_item_id AND t2s.site_id = :site_id)
INNER JOIN taxonomy_item_content tc ON (categories.taxonomy_item_id = tc.taxonomy_item_id AND tc.language_id = :language_id)
INNER JOIN taxonomy t ON (categories.taxonomy_id = t.taxonomy_id)
@IF isset(:post_id) AND :type == "categories"
THEN
-- LEFT JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
END @IF
@IF isset(:post_id) THEN
@IF :type == "tags"
THEN
INNER JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
@ELSE
LEFT JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
END @IF
END @IF
@IF isset(:product_id) THEN
@IF :type == "tags"
THEN
INNER JOIN product_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.product_id = :product_id)
@ELSE
LEFT JOIN product_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.product_id = :product_id)
END @IF
END @IF
WHERE
tc.language_id = :language_id AND t2s.site_id = :site_id
@IF isset(:search)
THEN
AND tc.name LIKE :search
END @IF
@IF isset(:type)
THEN
AND t.type = :type
END @IF
@IF isset(:post_type)
THEN
AND t.post_type = :post_type
END @IF
@IF isset(:parent_id)
THEN
AND categories.parent_id = :parent_id
END @IF
@IF isset(:taxonomy_id)
THEN
AND categories.taxonomy_id = :taxonomy_id
END @IF
ORDER BY categories.parent_id, categories.sort_order, categories.taxonomy_item_id
@IF isset(:limit) && !empty(:limit)
THEN
@SQL_LIMIT(:start, :limit)
END @IF
;
SELECT count(*) FROM (
@SQL_COUNT(categories.taxonomy_item_id, taxonomy_item) -- this takes previous query removes limit and replaces select columns with parameter taxonomy_item_id
) as count;
END-- get all categories
CREATE PROCEDURE getCategoriesPages(
-- variables
IN language_id INT,
IN taxonomy_id INT,
IN site_id INT,
IN post_id INT,
IN search CHAR,
IN type CHAR,
-- pagination
IN start INT,
IN limit INT,
-- return array of categories for categories query
OUT affected_rows,
-- return array of categories for categories query
OUT fetch_all,
-- return categories count for count query
OUT fetch_one
)
BEGIN
-- SET group_concat_max_len = 10000;
SELECT DISTINCT categories.taxonomy_item_id, @taxonomy_item_id := categories.taxonomy_item_id, categories.*, tc.*, tc.content as content, categories.taxonomy_item_id as array_key
@IF isset(:post_id)
THEN
,pt.post_id as checked
END @IF
--,(SELECT CONCAT('[', GROUP_CONCAT('{"post_id":"', pc.post_id, '","slug":"' , pc.slug,'","sort_order":"' , p.sort_order, '","name":"' , pc.name, '"}'), ']')
,(SELECT JSON_ARRAYAGG( JSON_OBJECT('post_id', pc.post_id, 'slug', pc.slug, 'sort_order', p.sort_order, 'name', pc.name) )
FROM post_content pc
LEFT JOIN post p ON (pc.post_id = p.post_id)
LEFT JOIN post_to_taxonomy_item ptt ON (ptt.taxonomy_item_id = @taxonomy_item_id AND ptt.post_id = p.post_id)
WHERE ptt.taxonomy_item_id = @taxonomy_item_id ORDER by p.sort_order
) AS posts
FROM taxonomy_item AS categories
INNER JOIN taxonomy_to_site t2s ON (categories.taxonomy_item_id = t2s.taxonomy_item_id AND t2s.site_id = :site_id)
INNER JOIN taxonomy_item_content tc ON (categories.taxonomy_item_id = tc.taxonomy_item_id AND tc.language_id = :language_id)
INNER JOIN taxonomy t ON (categories.taxonomy_id = t.taxonomy_id)
@IF isset(:post_id) AND :type == "categories"
THEN
-- LEFT JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
END @IF
@IF isset(:post_id) AND :type == "tags"
THEN
INNER JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
@ELSE
LEFT JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
END @IF
WHERE
tc.language_id = :language_id AND t2s.site_id = :site_id
@IF isset(:search)
THEN
AND tc.name LIKE :search
END @IF
@IF isset(:type)
THEN
AND t.type = :type
END @IF
@IF isset(:taxonomy_id)
THEN
AND categories.taxonomy_id = :taxonomy_id
END @IF
ORDER BY categories.parent_id, categories.sort_order, categories.taxonomy_item_id
@IF isset(:limit) && !empty(:limit)
THEN
@SQL_LIMIT(:start, :limit)
END @IF
;
SELECT count(*) FROM (
@SQL_COUNT(categories.taxonomy_item_id, taxonomy_item) -- this takes previous query removes limit and replaces select columns with parameter taxonomy_item_id
) as count;
END
-- get one taxonomy_item
CREATE PROCEDURE getCategory(
IN taxonomy_item_id INT,
IN post_id INT,
IN product_id INT,
IN post_type CHAR,
IN type CHAR,
IN slug CHAR,
OUT fetch_row
)
BEGIN
-- taxonomy_item
SELECT *
FROM taxonomy_item as _ -- (underscore) _ means that data will be kept in main array
@IF isset(:post_id) THEN
@IF :type == "tags"
THEN
INNER JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
@ELSE
LEFT JOIN post_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.post_id = :post_id)
END @IF
END @IF
@IF isset(:product_id) THEN
@IF :type == "tags"
THEN
INNER JOIN product_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.product_id = :product_id)
@ELSE
LEFT JOIN product_to_taxonomy_item pt ON (categories.taxonomy_item_id = pt.taxonomy_item_id AND pt.product_id = :product_id)
END @IF
END @IF
WHERE
1 = 1
@IF isset(:taxonomy_item_id)
THEN
AND _.taxonomy_item_id = :taxonomy_item_id
END @IF
@IF isset(:post_type)
THEN
AND _.post_type = :post_type
END @IF
@IF isset(:post_id)
THEN
,pt.post_id as checked
END @IF
@IF isset(:product_id)
THEN
,pt.product_id as checked
END @IF
LIMIT 1;
-- content
SELECT *, language_id as _ -- (underscore) _ column means that this column (language_id) value will be used as array key when adding row to result array
FROM taxonomy_item_content
WHERE taxonomy_item_id = :taxonomy_item_id;
-- SELECT *,taxonomy_item_option_id as _
-- FROM taxonomy_item_option WHERE taxonomy_item_id = :taxonomy_item_id;
--@EACH(taxonomy_item_option, taxonomy_item_option_value)
-- SELECT *, taxonomy_item_option_value_id as _ FROM taxonomy_item_option_value pov
-- WHERE taxonomy_item_option_id = :taxonomy_item_option[taxonomy_item_option_id];
END
CREATE PROCEDURE getCategoryBySlug(
IN taxonomy_item_id INT,
IN parent_id INT,
IN language_id INT,
IN post_type CHAR,
IN slug CHAR,
OUT fetch_row
)
BEGIN
-- content
SELECT *, _.name as name -- (underscore) _ column means that this column (language_id) value will be used as array key when adding row to result array
FROM taxonomy_item_content AS _
LEFT JOIN taxonomy_item as ti ON (_.taxonomy_item_id = ti.taxonomy_item_id)
LEFT JOIN taxonomy t ON (ti.taxonomy_id = t.taxonomy_id)
WHERE 1 = 1
@IF isset(:taxonomy_item_id)
THEN
AND _.taxonomy_item_id = :taxonomy_item_id
END @IF
@IF isset(:slug)
THEN
AND slug = :slug
END @IF
@IF isset(:parent_id)
THEN
AND parent_id = :parent_id
END @IF
@IF isset(:language_id)
THEN
AND language_id = :language_id
END @IF
@IF isset(:post_type)
THEN
AND t.post_type = :post_type
END @IF
LIMIT 1;
-- images
-- SELECT *, taxonomy_item_image_id as _
-- FROM taxonomy_item_image as images
-- WHERE taxonomy_item_id = :taxonomy_item_id;
-- SELECT *,taxonomy_item_option_id as _
-- FROM taxonomy_item_option WHERE taxonomy_item_id = :taxonomy_item_id;
-- @EACH(taxonomy_item_option, taxonomy_item_option_value)
-- SELECT *, taxonomy_item_option_value_id as _ FROM taxonomy_item_option_value pov
-- WHERE taxonomy_item_option_id = :taxonomy_item_option[taxonomy_item_option_id];
END
-- Edit taxonomy_item
CREATE PROCEDURE editCategory(
IN taxonomy_item ARRAY,
IN taxonomy_item_content ARRAY,
IN taxonomy_item_id INT,
OUT insert_id,
OUT affected_rows
)
BEGIN
DELETE FROM taxonomy_item_content WHERE taxonomy_item_id = :taxonomy_item_id;
@FILTER(:taxonomy_item_content, taxonomy_item_content)
@EACH(:taxonomy_item_content)
INSERT INTO taxonomy_item_content
( @KEYS(:each), taxonomy_item_id, meta_title, meta_description, meta_keywords )
VALUES ( :each, :taxonomy_item_id, '', '', '' );
-- SELECT * FROM taxonomy_item_option WHERE taxonomy_item_id = :taxonomy_item_id;
-- allow only table fields and set defaults for missing values
@FILTER(:taxonomy_item, taxonomy_item)
UPDATE taxonomy_item
SET @LIST(:taxonomy_item)
WHERE taxonomy_item_id = :taxonomy_item_id
END
-- Add new taxonomy_item
CREATE PROCEDURE addCategory(
IN taxonomy_item ARRAY,
IN taxonomy_item_content ARRAY,
IN site_id INT,
OUT insert_id,
OUT insert_id,
OUT insert_id
)
BEGIN
-- allow only table fields and set defaults for missing values
:taxonomy_item = @FILTER(:taxonomy_item, taxonomy_item)
:taxonomy_item_content = @FILTER(:taxonomy_item_content, taxonomy_item_content)
INSERT INTO taxonomy_item
( @KEYS(:taxonomy_item) )
VALUES ( :taxonomy_item );
INSERT INTO taxonomy_item_content
( taxonomy_item_id, @KEYS(:taxonomy_item_content) )
VALUES ( @result.taxonomy_item, :taxonomy_item_content );
INSERT INTO taxonomy_to_site
( taxonomy_item_id, site_id )
VALUES ( @result.taxonomy_item, :site_id );
END
CREATE PROCEDURE getCategoriesAllLanguages(
-- variables
IN language_id INT,
IN user_group_id INT,
IN site_id INT,
IN taxonomy_id INT,
IN search CHAR,
-- pagination
IN start INT,
IN limit INT,
-- return array of categories for categories query
OUT fetch_all,
-- return categories count for count query
OUT fetch_one
)
BEGIN
SELECT *,
(
SELECT
-- CONCAT('[', GROUP_CONCAT(
-- '{"language_id":"', tc.language_id,
-- '","name":"' , tc.name,
-- '","slug":"' , tc.slug,
-- '","content":"' , tc.content,
-- '","meta_title":"' , tc.meta_title,
-- '","meta_description":"' , tc.meta_description,
-- '","meta_keywords":"' , tc.meta_keywords, '"}'), ']')
JSON_ARRAYAGG( JSON_OBJECT(
'language_id', tc.language_id,
'name' , tc.name,
'slug' , tc.slug,
'content' , tc.content,
'meta_title' , tc.meta_title,
'meta_description' , tc.meta_description,
'meta_keywords' , tc.meta_keywords) )
FROM taxonomy_item_content as tc
WHERE
tc.taxonomy_item_id = categories.taxonomy_item_id GROUP BY tc.taxonomy_item_id
) as languages
FROM taxonomy_item AS categories
LEFT JOIN taxonomy_to_site t2s ON (categories.taxonomy_item_id = t2s.taxonomy_item_id)
LEFT JOIN taxonomy ON (categories.taxonomy_id = taxonomy.taxonomy_id)
WHERE
t2s.site_id = :site_id
@IF isset(:search)
THEN
AND tc.name LIKE :search
END @IF
@IF isset(:taxonomy_id)
THEN
AND categories.taxonomy_id LIKE :taxonomy_id
END @IF
@IF isset(:type)
THEN
AND taxonomy.type LIKE :type
END @IF
@IF isset(:post_type)
THEN
AND taxonomy.post_type LIKE :post_type
END @IF
ORDER BY categories.parent_id, categories.sort_order, categories.taxonomy_item_id
@IF isset(:limit) && !empty(:limit)
THEN
@SQL_LIMIT(:start, :limit)
END @IF
;
SELECT count(*) FROM (
@SQL_COUNT(categories.taxonomy_item_id, taxonomy_item) -- this takes previous query removes limit and replaces select columns with parameter product_id
) as count;
END
-- Edit menu
CREATE PROCEDURE editTaxonomyItem(
IN taxonomy_item ARRAY,
IN taxonomy_item_id INT,
OUT insert_id
)
BEGIN
-- allow only table fields and set defaults for missing values
:taxonomy_item_content_data = @FILTER(:taxonomy_item.taxonomy_item_content, taxonomy_item_content)
@EACH(:taxonomy_item_content_data)
INSERT INTO taxonomy_item_content
( @KEYS(:each), taxonomy_item_id)
VALUES ( :each, :taxonomy_item_id)
ON DUPLICATE KEY UPDATE @LIST(:each);
-- allow only table fields and set defaults for missing values
@FILTER(:taxonomy_item, taxonomy_item)
UPDATE taxonomy_item
SET @LIST(:taxonomy_item)
WHERE taxonomy_item_id = :taxonomy_item_id;
END
-- Add new menu
CREATE PROCEDURE addTaxonomyItem(
IN taxonomy_item ARRAY,
IN site_id INT,
OUT insert_id,
OUT insert_id
)
BEGIN
-- allow only table fields and set defaults for missing values
:taxonomy_item_content_data = @FILTER(:taxonomy_item.taxonomy_item_content, taxonomy_item_content)
:taxonomy_item_data = @FILTER(:taxonomy_item, taxonomy_item)
INSERT INTO taxonomy_item
( @KEYS(:taxonomy_item_data) )
VALUES ( :taxonomy_item_data );
INSERT INTO taxonomy_to_site
( taxonomy_item_id , site_id )
VALUES ( @result.taxonomy_item, :site_id );
@EACH(:taxonomy_item_content_data)
INSERT INTO taxonomy_item_content
( taxonomy_item_id, @KEYS(:each) )
VALUES ( @result.taxonomy_item, :each );
END
-- Reorder menu items
CREATE PROCEDURE updateTaxonomyItems(
IN taxonomy_items ARRAY,
OUT insert_id
)
BEGIN
:taxonomy_item_data = @FILTER(:taxonomy_items, taxonomy_item)
@EACH(:taxonomy_item_data)
UPDATE taxonomy_item
SET @LIST(:each)
WHERE taxonomy_item_id = :each.taxonomy_item_id;
END
-- Delete menu item
CREATE PROCEDURE deleteTaxonomyItem(
IN taxonomy_item_id INT,
OUT insert_id
)
BEGIN
-- delete taxonomy_item_content
DELETE FROM taxonomy_item_content WHERE taxonomy_item_id IN (
WITH RECURSIVE tree AS (
SELECT taxonomy_item_id,
parent_id
FROM taxonomy_item
WHERE taxonomy_item_id = :taxonomy_item_id
UNION ALL
SELECT p.taxonomy_item_id,
p.parent_id
FROM taxonomy_item p
JOIN tree t ON t.taxonomy_item_id = p.parent_id
)
SELECT taxonomy_item_id FROM tree);
-- delete taxonomy_item
DELETE FROM taxonomy_item WHERE taxonomy_item_id IN (
WITH RECURSIVE tree AS (
SELECT taxonomy_item_id,
parent_id
FROM taxonomy_item
WHERE taxonomy_item_id = :taxonomy_item_id
UNION ALL
SELECT p.taxonomy_item_id,
p.parent_id
FROM taxonomy_item p
JOIN tree t ON t.taxonomy_item_id = p.parent_id
)
SELECT taxonomy_item_id FROM tree);
END