-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sql
79 lines (62 loc) · 1.98 KB
/
database.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
CREATE DATABASE gymdb;
SET GLOBAL event_scheduler = ON;
DELIMITER //
CREATE PROCEDURE procedure_check_suscription()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE idsuscription VARCHAR(36);
DECLARE endtime DATE;
DECLARE cur1 CURSOR FOR SELECT id, end FROM gymdb.suscription WHERE state = 'active' AND concept = "Mensual";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO idsuscription, endtime;
IF done THEN
LEAVE read_loop;
END IF;
IF (DATEDIFF(endtime, CURRENT_DATE()) < 0) THEN
UPDATE suscription SET suscription.state = 'inactive', suscription.days = 0 WHERE suscription.id = idsuscription;
ELSE
UPDATE suscription SET suscription.days = IF(suscription.days = 0, 0,(suscription.days-1)) WHERE suscription.id = idsuscription;
END IF;
END LOOP;
CLOSE cur1;
END; //
DELIMITER ;
/*Falta modificarlo*/
DELIMITER //
CREATE PROCEDURE procedure_check_suscription_tiquetera()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE idsuscription VARCHAR(36);
DECLARE endtime DATE;
DECLARE daysSuscription INT;
DECLARE cur1 CURSOR FOR SELECT id, end, days FROM gymdb.suscription WHERE state = 'active' AND concept ='Tiquetera';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO idsuscription, endtime, daysSuscription;
IF done THEN
LEAVE read_loop;
END IF;
IF (DATEDIFF(endtime, CURRENT_DATE()) < 0 OR daysSuscription = 0) THEN
UPDATE
suscription
SET
suscription.state = 'inactive',
suscription.days = 0 ,
suscription.end = CURRENT_TIMESTAMP()
WHERE
suscription.id = idsuscription;
END IF;
END LOOP;
CLOSE cur1;
END; //
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE CHEKEAR()
BEGIN
CALL procedure_check_suscription();
CALL procedure_check_suscription_tiquetera();
END; $$
DELIMITER ;