forked from mmariani/mediacore-community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_triggers.sql
179 lines (163 loc) · 5.2 KB
/
setup_triggers.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
-- XXX: If your config.ini file has a db_table_prefix set, you'll have to
-- manually prepend it to all table names in this class. This is far
-- from ideal, it's true, and we hope to improve this in the future.
DELIMITER //
-- After Media is Inserted
-- Create a new Search row --
DROP TRIGGER IF EXISTS media_ai//
CREATE TRIGGER media_ai
AFTER INSERT ON media FOR EACH ROW
BEGIN
INSERT INTO media_fulltext
SET `media_id` = NEW.`id`,
`title` = NEW.`title`,
`subtitle` = NEW.`subtitle`,
`description_plain` = NEW.`description_plain`,
`notes` = NEW.`notes`,
`author_name` = NEW.`author_name`,
`tags` = '',
`categories` = '';
END;//
-- After Media is Updated
-- Copies changes to the corresponding Search row
DROP TRIGGER IF EXISTS media_au//
CREATE TRIGGER media_au
AFTER UPDATE ON media FOR EACH ROW
BEGIN
UPDATE media_fulltext
SET `media_id` = NEW.`id`,
`title` = NEW.`title`,
`subtitle` = NEW.`subtitle`,
`description_plain` = NEW.`description_plain`,
`notes` = NEW.`notes`,
`author_name` = NEW.`author_name`
WHERE media_id = OLD.id;
END;//
--
-- Deletes the corresponding Search row
DROP TRIGGER IF EXISTS media_ad//
CREATE TRIGGER media_ad
AFTER DELETE ON media FOR EACH ROW
BEGIN
DELETE FROM media_fulltext WHERE media_id = OLD.id;
END;//
-- Add Tag to Media
-- Append the tag to the Search row's tag TEXT column
DROP TRIGGER IF EXISTS media_tags_ai//
CREATE TRIGGER media_tags_ai
AFTER INSERT ON media_tags FOR EACH ROW
BEGIN
UPDATE media_fulltext
SET tags = CONCAT(tags, ', ', (SELECT name FROM tags WHERE id = NEW.tag_id))
WHERE media_id = NEW.media_id;
END;//
-- Remove Tag From Media
-- Remove the tag from the Search row's tag TEXT column
DROP TRIGGER IF EXISTS media_tags_ad//
CREATE TRIGGER media_tags_ad
AFTER DELETE ON media_tags FOR EACH ROW
BEGIN
UPDATE media_fulltext
SET tags = TRIM(', ' FROM REPLACE(
CONCAT(', ', tags, ', '),
CONCAT(', ', (SELECT name FROM tags WHERE id = OLD.tag_id), ', '),
', '))
WHERE media_id = OLD.media_id;
END;//
-- Rename Tag
-- Update all Search row's which use this Tag
DROP TRIGGER IF EXISTS tags_au//
CREATE TRIGGER tags_au
AFTER UPDATE ON tags FOR EACH ROW
BEGIN
IF OLD.name != NEW.name THEN
UPDATE media_fulltext
SET tags = TRIM(', ' FROM REPLACE(
CONCAT(', ', tags, ', '),
CONCAT(', ', OLD.name, ', '),
CONCAT(', ', NEW.name, ', ')))
WHERE media_id IN (SELECT media_id FROM media_tags
WHERE tag_id = OLD.id);
END IF;
END;//
-- Delete Tag
-- Remove the tag from all Search row's which use this Tag
DROP TRIGGER IF EXISTS tags_ad//
CREATE TRIGGER tags_ad
AFTER DELETE ON tags FOR EACH ROW
BEGIN
UPDATE media_fulltext
SET tags = TRIM(', ' FROM REPLACE(
CONCAT(', ', tags, ', '),
CONCAT(', ', OLD.name, ', '),
', '))
WHERE media_id IN (SELECT media_id FROM media_tags
WHERE media_id = OLD.id);
END;//
-- Add Category to Media
-- Append the Category to the Search row's Category TEXT column
DROP TRIGGER IF EXISTS media_categories_ai//
CREATE TRIGGER media_categories_ai
AFTER INSERT ON media_categories FOR EACH ROW
BEGIN
UPDATE media_fulltext
SET categories = CONCAT(categories, ', ', (SELECT name FROM categories WHERE id = NEW.category_id))
WHERE media_id = NEW.media_id;
END;//
-- Remove Category From Media
-- Remove the Category from the Search row's Category TEXT column
DROP TRIGGER IF EXISTS media_categories_ad//
CREATE TRIGGER media_categories_ad
AFTER DELETE ON media_categories FOR EACH ROW
BEGIN
UPDATE media_fulltext
SET categories = TRIM(', ' FROM REPLACE(
CONCAT(', ', categories, ', '),
CONCAT(', ', (SELECT name FROM categories WHERE id = OLD.category_id), ', '),
', '))
WHERE media_id = OLD.media_id;
END;//
-- Rename Category
-- Update all Search row's which use this Category
DROP TRIGGER IF EXISTS categories_au//
CREATE TRIGGER categories_au
AFTER UPDATE ON categories FOR EACH ROW
BEGIN
IF OLD.name != NEW.name THEN
UPDATE media_fulltext
SET categories = TRIM(', ' FROM REPLACE(
CONCAT(', ', categories, ', '),
CONCAT(', ', OLD.name, ', '),
CONCAT(', ', NEW.name, ', ')))
WHERE media_id IN (SELECT media_id FROM media_categories
WHERE category_id = OLD.id);
END IF;
END;//
-- Delete Category
-- Remove the Category from all Search row's which use this Category
DROP TRIGGER IF EXISTS categories_ad//
CREATE TRIGGER categories_ad
AFTER DELETE ON categories FOR EACH ROW
BEGIN
UPDATE media_fulltext
SET categories = TRIM(', ' FROM REPLACE(
CONCAT(', ', categories, ', '),
CONCAT(', ', OLD.name, ', '),
', '))
WHERE media_id IN (SELECT media_id FROM media_categories
WHERE category_id = OLD.id);
END;//
DELIMITER ;
INSERT INTO media_fulltext (`media_id`, `title`, `subtitle`, `description_plain`, `notes`, `author_name`, `tags`, `categories`)
SELECT id AS media_id, title, subtitle, description_plain, notes, author_name
, (SELECT GROUP_CONCAT(t.name SEPARATOR ', ')
FROM media_tags j
LEFT JOIN tags t ON j.tag_id = t.id
WHERE j.media_id = m.id
GROUP BY j.media_id) AS tags
, (SELECT GROUP_CONCAT(t.name SEPARATOR ', ')
FROM media_categories j
LEFT JOIN categories t ON j.category_id = t.id
WHERE j.media_id = m.id
GROUP BY j.media_id) AS categories
FROM media AS m;