-
Notifications
You must be signed in to change notification settings - Fork 0
/
procedures.sql
238 lines (175 loc) · 5.46 KB
/
procedures.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
USE cooking_contest_ntua;
-- TRIGGERS
DROP TRIGGER IF EXISTS check_tips_count;
DELIMITER //
CREATE TRIGGER check_tips_count
BEFORE INSERT ON tips
FOR EACH ROW
BEGIN
DECLARE tips_count INT;
SELECT COUNT(*)
INTO tips_count
FROM tips
WHERE type_meal_id = NEW.type_meal_id;
IF tips_count >= 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Maximum of 3 tips allowed per meal';
END IF;
END//
DELIMITER ;
DROP TRIGGER IF EXISTS check_step_count;
DELIMITER //
CREATE TRIGGER check_step_count
BEFORE INSERT ON steps_recipes
FOR EACH ROW
BEGIN
DECLARE c INT;
SELECT count(*)
INTO c
FROM steps_recipes
WHERE step_counter + 1 = NEW.step_counter AND recipe_id = NEW.recipe_id;
IF NEW.step_counter <> 1 THEN
IF c = 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'You have skipped a step';
END IF;
END IF;
END//
DELIMITER ;
-- VIEWS
drop VIEW recipe_food_group;
CREATE VIEW recipe_food_group
(recipe_name,recipe_id, food_group_name,food_group_id)
AS
SELECT
r.recipe_name,
r.recipe_id,
f.food_group_name,
f.food_group_id
FROM
cooking_ingredients AS i
INNER JOIN recipes AS r ON i.ingredients_id = r.ingredients_id
INNER JOIN food_group AS f ON i.food_group_id = f.food_group_id;
create view co_re as
(
select rr.recipe_id, rr.cooking_or_pastry, rr.recipe_name, rr.recipe_description, rr.time_preparation, rr.time_execution, rr.quantity, rr.ingredients_id, rr.ethnic_id, rr.image, rr.image_caption
from cooker_recipes as cr inner join recipes as rr
on cr.recipe_id = rr.recipe_id
where cr.cooker_id = 1
);
create view my_cook as
(
select *
from cooker
where cooker_id = 1
);
-- Procedure
DELIMITER //
CREATE PROCEDURE CHECK_IF_COMPETITION_IS_CORRECT(IN episode INT, IN season INT, OUT flag BOOLEAN)
BEGIN
DECLARE x INT;
DECLARE y INT;
DECLARE z INT;
DECLARE e INT;
DECLARE cr INT;
DECLARE cee INT;
SELECT COUNT(DISTINCT cooker_id) INTO x
FROM episode_expansion
WHERE season_year = season AND episode_id = episode;
SELECT COUNT(DISTINCT recipe_id) INTO y
FROM episode_expansion
WHERE season_year = season AND episode_id = episode AND is_judge = 0;
SELECT COUNT(DISTINCT r.ethnic_id) INTO z
FROM episode_expansion ee
JOIN recipes r ON r.recipe_id = ee.recipe_id
WHERE season_year = season AND episode_id = episode AND is_judge = 0;
SELECT COUNT(DISTINCT episode_id) INTO e
FROM episode_expansion
WHERE season_year = season;
SELECT COUNT(*) INTO cr
FROM (
SELECT DISTINCT ee.cooker_id, ee.recipe_id
FROM episode_expansion ee
WHERE ee.season_year = season AND ee.episode_id = episode AND ee.is_judge = 0
) AS ce
JOIN cooker_recipes cr ON (ce.cooker_id = cr.cooker_id AND ce.recipe_id = cr.recipe_id);
SELECT COUNT(*) INTO cee
FROM (
SELECT DISTINCT cr.cooker_id, r.ethnic_id
FROM (
SELECT DISTINCT ee.cooker_id, ee.recipe_id
FROM episode_expansion ee
WHERE ee.season_year = season AND ee.episode_id = episode AND ee.is_judge = 0
) AS cr
JOIN recipes r ON r.recipe_id = cr.recipe_id
) AS ce
JOIN cooker_ethnic e ON (ce.cooker_id = e.cooker_id AND ce.ethnic_id = e.ethnic_id);
IF x <> 13 OR y <> 10 OR e <> 10 OR z <> 10 OR cee <> 10 OR cr <> 10 THEN
SET flag = FALSE;
ELSE
SET flag = TRUE;
END IF;
END //
DELIMITER ;
DROP PROCEDURE IF EXISTS DYNAMIC_CALORIES_CALULATOR;
DELIMITER //
CREATE PROCEDURE DYNAMIC_CALORIES_CALULATOR(IN rec_id int ,OUT calories_overall FLOAT)
BEGIN
select sum(total_calories_per_ingr) into calories_overall
from (
SELECT re.recipe_id as recipe_id, ci.ingredients_id as ingredients_id, (ni.carbonhydrates * 4 * ri.quantity / re.quantity) +
(ni.lipids * 9 * ri.quantity / re.quantity) +
(ni.proteins * 4 * ri.quantity / re.quantity) AS total_calories_per_ingr
FROM nutritions_info AS ni inner join cooking_ingredients as ci ON ni.ingredients_id = ci.ingredients_id
inner join recipes_ingredients as ri on ci.ingredients_id = ri.ingredients_id
inner join recipes as re on re.recipe_id = ri.recipe_id
) as calorie_subquery
where recipe_id = rec_id
group by recipe_id;
END //
DELIMITER ;
DROP PROCEDURE CHECK_AT_LEAST_ONE_STEP;
DELIMITER //
CREATE PROCEDURE CHECK_AT_LEAST_ONE_STEP(OUT flag BOOL)
BEGIN
DECLARE cc INT;
SELECT COUNT(*)
INTO cc
FROM recipes AS re
LEFT JOIN steps_recipes AS sr
ON re.recipe_id = sr.recipe_id
WHERE sr.recipe_id IS NULL;
IF cc > 0 THEN
SET flag = FALSE;
ELSE
SET flag = TRUE;
END IF;
END //
DELIMITER ;
DROP PROCEDURE CHECK_AT_LEAST_ONE_COOKER;
DELIMITER //
CREATE PROCEDURE CHECK_AT_LEAST_ONE_COOKER(OUT flag BOOL)
BEGIN
DECLARE cc INT;
SELECT COUNT(*)
INTO cc
FROM recipes AS re
LEFT JOIN cooker_recipes AS cr
ON re.recipe_id = cr.recipe_id
WHERE cr.recipe_id IS NULL;
IF cc > 0 THEN
SET flag = FALSE;
ELSE
SET flag = TRUE;
END IF;
END //
DELIMITER ;
-- Autharazition
create user 'admin'@'%' identified by 'admin';
grant all privileges on cooking_contest_ntua.* to 'admin'@'%';
flush privileges;
create user 'cooker1'@'%' identified by 'cooker1';
grant insert on cooking_contest_ntua.recipes to 'cooker1'@'%';
grant all privileges on cooking_contest_ntua.co_re to 'cooker1'@'%';
grant all privileges on cooking_contest_ntua.my_cook to 'cooker1'@'%';
flush privileges;