-
Notifications
You must be signed in to change notification settings - Fork 0
/
creation_script.sql
449 lines (408 loc) · 12.5 KB
/
creation_script.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
CREATE DATABASE shutter;
USE shutter;
CREATE TABLE user
(
username VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
password VARCHAR(50),
email VARCHAR(50),
name VARCHAR(50),
biography VARCHAR(100),
created_date DATETIME,
birthdate DATE,
profile_picture VARCHAR(2000),
file_id VARCHAR(36),
PRIMARY KEY (username)
);
CREATE TABLE publication
(
publication_id VARCHAR(36) NOT NULL,
poster_username VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
description VARCHAR(200),
picture VARCHAR(2000),
created_date DATETIME,
rating INT default 0,
file_id VARCHAR(36),
PRIMARY KEY (publication_id),
FOREIGN KEY (poster_username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE gallery
(
gallery_id VARCHAR(36) NOT NULL,
creator_username VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
description VARCHAR(200),
created_date DATETIME,
private BIT(1),
rating INT default 0,
title VARCHAR(50),
PRIMARY KEY (gallery_id),
FOREIGN KEY (creator_username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE tag
(
value VARCHAR(50) NOT NULL,
nb_publications INT DEFAULT 0,
PRIMARY KEY (value)
);
CREATE TABLE comment
(
comment_id VARCHAR(36) NOT NULL,
commenter_username VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
publication_id VARCHAR(36) NOT NULL,
message VARCHAR(200),
created_date DATETIME,
rating INT default 0,
PRIMARY KEY (comment_id),
FOREIGN KEY (commenter_username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (publication_id) REFERENCES publication (publication_id) ON DELETE CASCADE ON UPDATE CASCADE
);
/* Relationship */
CREATE TABLE follow
(
follower_username VARCHAR(36) COLLATE utf8mb4_bin NOT NULL,
followed_username VARCHAR(36) COLLATE utf8mb4_bin NOT NULL,
PRIMARY KEY (follower_username, followed_username),
FOREIGN KEY (follower_username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (followed_username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE identify
(
publication_id VARCHAR(36) NOT NULL,
tag_value VARCHAR(50) NOT NULL,
PRIMARY KEY (publication_id, tag_value),
FOREIGN KEY (publication_id) REFERENCES publication (publication_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (tag_value) REFERENCES tag (value)
);
CREATE TABLE save
(
gallery_id VARCHAR(36) NOT NULL,
publication_id VARCHAR(36) NOT NULL,
PRIMARY KEY (gallery_id, publication_id),
FOREIGN KEY (gallery_id) REFERENCES gallery (gallery_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (publication_id) REFERENCES publication (publication_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE rate_gallery
(
username VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
gallery_id VARCHAR(36) NOT NULL,
rating BIT(1),
PRIMARY KEY (username, gallery_id),
FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (gallery_id) REFERENCES gallery (gallery_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE rate_publication
(
username VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
publication_id VARCHAR(36) NOT NULL,
rating BIT(1),
PRIMARY KEY (username, publication_id),
FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (publication_id) REFERENCES publication (publication_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE rate_comment
(
username VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
comment_id VARCHAR(36) NOT NULL,
rating BIT(1),
PRIMARY KEY (username, comment_id),
FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (comment_id) REFERENCES comment (comment_id) ON DELETE CASCADE ON UPDATE CASCADE
);
/*Index*/
CREATE INDEX publication_Index ON publication (created_date DESC);
CREATE INDEX comment_Index ON comment (publication_id, created_date ASC);
CREATE INDEX gallery_Index On gallery (creator_username DESC);
CREATE INDEX rate_comment_Index ON rate_comment (comment_id);
CREATE INDEX rate_gallery_Index On rate_gallery (gallery_id);
CREATE INDEX rate_publication_Index On rate_publication (publication_id);
CREATE INDEX save_Index On save (gallery_id);
CREATE INDEX identify_Index On identify (publication_id);
/* Function */
DELIMITER //
/* Function to get the rating of a user on a comment*/
CREATE FUNCTION get_user_comment_rating(
p_username VARCHAR(50),
p_comment_id VARCHAR(36)
)
RETURNS BIT(1)
BEGIN
DECLARE v_rating INT;
SELECT rating
INTO v_rating
FROM rate_comment
WHERE BINARY username = p_username
AND comment_id = p_comment_id;
RETURN v_rating;
END//
DELIMITER ;
DELIMITER //
/* Function to get the rating of a user on a publication*/
CREATE FUNCTION get_user_publication_rating(
p_username VARCHAR(50),
p_publication_id VARCHAR(36)
)
RETURNS BIT(1)
BEGIN
DECLARE v_rating INT;
SELECT rating
INTO v_rating
FROM rate_publication
WHERE BINARY username = p_username
AND publication_id = p_publication_id;
RETURN v_rating;
END//
DELIMITER ;
DELIMITER //
/* Function to get the rating of a user on a gallery*/
CREATE FUNCTION get_user_gallery_rating(
p_username VARCHAR(50),
p_gallery_id VARCHAR(36)
)
RETURNS BIT(1)
BEGIN
DECLARE v_rating INT;
SELECT rating
INTO v_rating
FROM rate_gallery
WHERE BINARY username = p_username
AND gallery_id = p_gallery_id;
RETURN v_rating;
END//
DELIMITER ;
/* Procedure */
DELIMITER //
/* Procedure to update the rating of a comment*/
CREATE PROCEDURE update_comment_rating_procedure(IN comment_id_var VARCHAR(36))
BEGIN
DECLARE total_rating INT;
SET total_rating = (SELECT SUM(CASE WHEN rc.rating = 1 THEN 1 WHEN rc.rating = 0 THEN -1 ELSE 0 END)
FROM rate_comment rc
WHERE comment_id_var = rc.comment_id);
IF total_rating IS NULL THEN
SET total_rating = 0;
END IF;
UPDATE comment c
SET c.rating = total_rating
WHERE c.comment_id = comment_id_var;
END//
DELIMITER ;
DELIMITER //
/* Procedure to update the rating of a publication*/
CREATE PROCEDURE update_publication_rating_procedure(IN publication_id_var VARCHAR(36))
BEGIN
DECLARE total_rating INT;
SET total_rating = (SELECT SUM(CASE WHEN rp.rating = 1 THEN 1 WHEN rp.rating = 0 THEN -1 ELSE 0 END)
FROM rate_publication rp
WHERE publication_id_var = rp.publication_id);
IF total_rating IS NULL THEN
SET total_rating = 0;
END IF;
UPDATE publication p
SET p.rating = total_rating
WHERE p.publication_id = publication_id_var;
END//
DELIMITER ;
DELIMITER //
/* Procedure to update the rating of a gallery*/
CREATE PROCEDURE update_gallery_rating_procedure(IN gallery_id_var VARCHAR(36))
BEGIN
DECLARE total_rating INT;
SET total_rating = (SELECT SUM(CASE WHEN rg.rating = 1 THEN 1 WHEN rg.rating = 0 THEN -1 ELSE 0 END)
FROM rate_gallery rg
WHERE gallery_id_var = rg.gallery_id);
IF total_rating IS NULL THEN
SET total_rating = 0;
END IF;
UPDATE gallery g
SET g.rating = total_rating
WHERE g.gallery_id = gallery_id_var;
END//
DELIMITER ;
DELIMITER //
/* Procedure to update the count of a tag. if count = 0 delete tag*/
CREATE PROCEDURE count_tag(IN tag_value_var VARCHAR(50))
BEGIN
DECLARE tag_count INT;
SELECT COUNT(*) INTO tag_count FROM identify WHERE tag_value = tag_value_var;
IF tag_count = 0 THEN
DELETE FROM tag WHERE value = tag_value_var;
ELSE
UPDATE tag SET tag.nb_publications = tag_count WHERE value = tag_value_var;
END IF;
END //
DELIMITER ;
/*publication triggers*/
DELIMITER //
/* Trigger after delete because trigger don't work on cascade to update tag count*/
CREATE TRIGGER delete_publication
AFTER DELETE
ON publication
FOR EACH ROW
BEGIN
DECLARE tag_value VARCHAR(50);
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE cur CURSOR FOR SELECT t.value FROM tag t;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop:
LOOP
FETCH cur INTO tag_value;
IF done THEN
LEAVE read_loop;
END IF;
CALL count_tag(tag_value);
end loop;
CLOSE cur;
END//
DELIMITER ;
/* user trigger */
DELIMITER //
/* Trigger after delete because trigger don't work on cascade to update tag count*/
CREATE TRIGGER delete_user
AFTER DELETE
ON user
FOR EACH ROW
BEGIN
DECLARE tag_value VARCHAR(50);
DECLARE done bit(1) DEFAULT FALSE;
DECLARE cur CURSOR FOR SELECT t.value FROM tag t;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop:
LOOP
FETCH cur INTO tag_value;
IF done THEN
LEAVE read_loop;
END IF;
CALL count_tag(tag_value);
end loop;
CLOSE cur;
END//
DELIMITER ;
/* identify trigger */
DELIMITER //
/* Trigger Before insert to add new tag if its doesn't exist*/
CREATE TRIGGER add_tag_value_before_insert
BEFORE INSERT
ON identify
FOR EACH ROW
BEGIN
DECLARE tag_value_var VARCHAR(50);
SELECT t.value INTO tag_value_var FROM tag t WHERE t.value = NEW.tag_value;
IF tag_value_var IS NULL THEN
INSERT INTO tag (value, nb_publications) VALUES (NEW.tag_value, 1);
end if;
END//
DELIMITER ;
DELIMITER //
/* Trigger After insert to update tag count*/
CREATE TRIGGER add_tag_value_after_trigger
AFTER INSERT
ON identify
FOR EACH ROW
BEGIN
CALL count_tag(NEW.tag_value);
END//
DELIMITER ;
DELIMITER //
/* Trigger After Delete to update tag count*/
CREATE TRIGGER update_tag_count
AFTER DELETE
ON identify
FOR EACH ROW
BEGIN
CALL count_tag(OLD.tag_value);
END//
DELIMITER ;
/* rate_comment Trigger */
DELIMITER //
/* Trigger After insert to update comment rating*/
CREATE TRIGGER update_comment_rating_insert
AFTER INSERT
ON rate_comment
FOR EACH ROW
BEGIN
CALL update_comment_rating_procedure(NEW.comment_id);
END//
DELIMITER ;
DELIMITER //
/* Trigger After update to update comment rating*/
CREATE TRIGGER update_comment_rating_update
AFTER UPDATE
ON rate_comment
FOR EACH ROW
BEGIN
CALL update_comment_rating_procedure(NEW.comment_id);
END//
DELIMITER ;
DELIMITER //
/* Trigger After delete to update comment rating*/
CREATE TRIGGER update_comment_rating_delete
AFTER DELETE
ON rate_comment
FOR EACH ROW
BEGIN
CALL update_comment_rating_procedure(OLD.comment_id);
END//
DELIMITER ;
/* rate_publication trigger*/
DELIMITER //
/* Trigger After insert to update publication rating*/
CREATE TRIGGER update_publication_rating_insert
AFTER INSERT
ON rate_publication
FOR EACH ROW
BEGIN
CALL update_publication_rating_procedure(NEW.publication_id);
END//
DELIMITER ;
DELIMITER //
/* Trigger After update to update publication rating*/
CREATE TRIGGER update_publication_rating_update
AFTER UPDATE
ON rate_publication
FOR EACH ROW
BEGIN
CALL update_publication_rating_procedure(NEW.publication_id);
END//
DELIMITER ;
DELIMITER //
/* Trigger After delete to update publication rating*/
CREATE TRIGGER update_publication_rating_delete
AFTER DELETE
ON rate_publication
FOR EACH ROW
BEGIN
CALL update_publication_rating_procedure(OLD.publication_id);
END//
DELIMITER ;
/* rate_gallery trigger*/
DELIMITER //
/* Trigger After insert to update gallery rating*/
CREATE TRIGGER update_gallery_rating_insert
AFTER INSERT
ON rate_gallery
FOR EACH ROW
BEGIN
CALL update_gallery_rating_procedure(NEW.gallery_id);
END//
DELIMITER ;
DELIMITER //
/* Trigger After update to update gallery rating*/
CREATE TRIGGER update_gallery_rating_update
AFTER UPDATE
ON rate_gallery
FOR EACH ROW
BEGIN
CALL update_gallery_rating_procedure(NEW.gallery_id);
END//
DELIMITER ;
DELIMITER //
/* Trigger After delete to update gallery rating*/
CREATE TRIGGER update_gallery_rating_delete
AFTER DELETE
ON rate_gallery
FOR EACH ROW
BEGIN
CALL update_gallery_rating_procedure(OLD.gallery_id);
END//
DELIMITER ;