-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalermo_Francesco_programs.sql
185 lines (133 loc) · 4.9 KB
/
Palermo_Francesco_programs.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
/**********************************************************
****** Stored Programs for Assn.2, 2019 *******************
********** Francesco Palermo and 45539669 ************************
******************* Date **********************************
** I declare that the code provided below is my own work **
******* Any help received is duely acknowledged here ******
**********************************************************/
/********* Trigger TR_OVERDUE ************/
drop trigger if exists tr_overdue;
delimiter //
CREATE TRIGGER tr_overdue
BEFORE UPDATE ON invoice
FOR EACH ROW
BEGIN
DECLARE msg VARCHAR(250);
set msg = CONCAT('Invoice with number: ',NEW.INVOICENO,' is now overdue!');
IF (NEW.STATUS ='OVERDUE') THEN
-- INSERTING ROWS TO THE TABLE alert
INSERT INTO alerts (message_date,origin,message) VALUES (now(),current_user(),msg);
END IF;
END//
DELIMITER ;
/************* Helper Functions/Procedures used, two functions for example ****************/
drop function if exists rate_on_date;
delimiter //
CREATE FUNCTION rate_on_date(staff_id INT, given_date DATE)
RETURNS FLOAT
DETERMINISTIC
BEGIN
-- declare a local variable that store the total hour_rate on a given date
DECLARE hourly_rate FLOAT DEFAULT 0;
-- return the total salary of a given staff (staff_id) on any particular date (given_date)
SELECT sg.HOURLYRATE INTO hourly_rate
FROM workson wo, staffongrade sog, salarygrade sg
WHERE wo.STAFFNO = sog.STAFFNO AND sog.GRADE = sg.GRADE
AND wo.STAFFNO = staff_id AND wo.WDATE = given_date AND given_date >= sog.STARTDATE and (given_date <= sog.FINISHDATE OR sog.FINISHDATE IS NULL);
RETURN hourly_rate;
END //
DELIMITER ;
-- create function cost_of_campaign (camp_id int) returns float
-- retuens the total cost incurred due to any given campaigh (camp_id)
drop function if exists cost_of_campaign ;
delimiter //
CREATE FUNCTION cost_of_campaign(camp_id INT)
RETURNS FLOAT
DETERMINISTIC
BEGIN
-- declare a local variable that store the total cost of a campaign
DECLARE v_finito FLOAT DEFAULT 0;
DECLARE costo_totale FLOAT DEFAULT 0;
DECLARE c_hour FLOAT DEFAULT 0;
DECLARE c_rate FLOAT DEFAULT 0;
-- declare a cursor
DECLARE hour_rate CURSOR FOR
SELECT HOUR,rate_on_date(STAFFNO,WDATE)
FROM workson
WHERE CAMPAIGN_NO = camp_id;
-- delare the handler
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finito = 1;
-- OPEN THE CURSOR AND LOOP TO GET THE TOTAL
OPEN hour_rate;
WHILE (v_finito=0) DO
FETCH hour_rate INTO c_hour,c_rate;
IF (v_finito=0) THEN
SET costo_totale = costo_totale+(c_hour*c_rate);
END IF;
END WHILE;
CLOSE hour_rate;
RETURN costo_totale;
END//
delimiter ;
/************ Procedure SP_FINISH_CAMPAIGN******************/
drop procedure if exists sp_finish_campaign;
delimiter //
CREATE PROCEDURE sp_finish_campaign (in c_title varchar(30))
BEGIN
DECLARE v_campaign_count INT DEFAULT 0;
DECLARE v_costof_camp FLOAT DEFAULT 0;
-- count the number of campaign with the supplied c_title
-- the count will be 1 if if the campaign exists
SELECT COUNT(*) INTO v_campaign_count
FROM campaign
WHERE TITLE=c_title;
-- if the campaign exists update CAMPAIGNFINISHDATE AND ACTUALCOST
IF (v_campaign_count = 1) THEN
-- UPDATE THE campaignfinishdate TO THE CURRENT DATE
UPDATE campaign
SET CAMPAIGNFINISHDATE = now()
WHERE TITLE = c_title;
-- UPDATE THE actual cost
SELECT cost_of_campaign(CAMPAIGN_NO) INTO v_costof_camp
FROM campaign
WHERE TITLE=c_title;
UPDATE campaign
SET ACTUALCOST = v_costof_camp
WHERE TITLE = c_title;
ELSE
SIGNAL SQLSTATE '45000'
-- which means “unhandled user-defined exception.”
SET MESSAGE_TEXT = 'ERROR! Campaign title does not exist';
END IF;
END//
delimiter ;
/************ Procedure SYNC_INVOICE******************/
drop procedure if exists sync_invoice;
delimiter //
CREATE PROCEDURE sync_invoice()
BEGIN
-- declare a local variable
DECLARE v_invoiceno INT DEFAULT 0;
DECLARE date_diff INT DEFAULT 0;
DECLARE dateissued DATE;
DECLARE v_status VARCHAR(20);
DECLARE v_finito FLOAT DEFAULT 0;
-- declare a cursor
DECLARE c_invoice CURSOR FOR
SELECT INVOICENO,DATEISSUED,STATUS,DATEDIFF(now(),DATEISSUED)
FROM invoice
WHERE (DATEDIFF(now(),DATEISSUED) >30 AND STATUS = 'UNPAID');
-- delare the handler
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finito = 1;
-- OPEN THE CURSOR AND LOOP THROUGH THE ROWS
OPEN c_invoice;
WHILE (v_finito=0) DO
FETCH c_invoice INTO v_invoiceno,dateissued,v_status,date_diff;
IF (v_finito=0) THEN
UPDATE invoice
SET invoice.STATUS= 'OVERDUE'
WHERE INVOICENO=v_invoiceno;
END IF;
END WHILE;
CLOSE c_invoice;
END//