-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcedureAndTrigger.sql
80 lines (64 loc) · 2.2 KB
/
ProcedureAndTrigger.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
--Stored Procedure to get game name, number of upvotes, upvote score, number of comments, and all comments and their usernames:
DELIMITER //
CREATE PROCEDURE RetrieveGameRatings(IN game_ID INT)
BEGIN
DECLARE totalRatings INT;
DECLARE averageRating INT;
DECLARE totalComments INT;
DECLARE gameNameOne VARCHAR(255);
DECLARE commentTextOne VARCHAR(2000);
DECLARE userNameOne VARCHAR(255);
DECLARE count INT DEFAULT 0;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE commentCursor CURSOR FOR
(SELECT CommentText, Username
FROM Comments
WHERE GameID = game_ID);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP TABLE IF EXISTS NewTable;
CREATE TABLE NewTable(
text VARCHAR(2000),
user VARCHAR(255)
);
OPEN commentCursor;
cloop: LOOP
FETCH commentCursor INTO commentTextOne, userNameOne;
SET count = count + 1;
IF done THEN
LEAVE cloop;
END IF;
INSERT INTO NewTable VALUES(commentTextOne, userNameOne);
END LOOP cloop;
CLOSE commentCursor;
SELECT G.GameName, COUNT(C.CommentText) AS comments
INTO gameNameOne, totalComments
FROM GameInfo G NATURAL JOIN Comments C
WHERE G.GameID = game_ID
GROUP BY C.GameID;
SELECT COUNT(Vote) AS Total, SUM(Vote) AS average
INTO totalRatings, averageRating
FROM Rating R
GROUP BY R.GameID
HAVING R.GameID = game_ID;
IF gameNameOne IS NULL THEN
SELECT GameName INTO gameNameOne FROM GameInfo WHERE GameID = game_ID;
END IF;
IF averageRating IS NULL THEN SET averageRating = 0; END IF;
IF totalComments IS NULL THEN SET totalComments = 0; END IF;
IF totalRatings IS NULL THEN SET totalRatings = 0; END IF;
SELECT totalRatings AS TotalRatings, averageRating AS AverageRating, totalComments AS TotalComments, gameNameOne AS GameName;
SELECT text AS Comments, user AS UserName FROM NewTable ;
END //
DELIMITER ;
--Trigger to censor word "potty" in comments:
DELIMITER //
CREATE TRIGGER replace_potty_comment
BEFORE INSERT ON Comments
FOR EACH ROW
BEGIN
IF NEW.CommentText LIKE '%potty%' THEN
SET NEW.CommentText = REPLACE(NEW.CommentText, 'potty', '*****');
END IF;
END;
//
DELIMITER ;