-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignmentDueProcedure.sql
49 lines (40 loc) · 1.61 KB
/
assignmentDueProcedure.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
DELIMITER $$
DROP PROCEDURE IF EXISTS `webwork`.`assignment_due` $$
CREATE PROCEDURE `webwork`.`assignment_due` ()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tname TEXT DEFAULT '';
DECLARE course_name TEXT;
DECLARE query_str TEXT DEFAULT '';
DECLARE studentCount INT;
DECLARE cur CURSOR FOR SELECT table_name FROM `INFORMATION_SCHEMA`.`TABLES` as t WHERE table_schema = 'webwork' AND t.`table_name` LIKE "%_set";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP TABLE IF EXISTS `tmp_assignments`;
CREATE TEMPORARY TABLE `tmp_assignments` (
`course` VARCHAR(255),
`set_id` VARCHAR(255),
`open_date` bigint(20),
`due_date` bigint(20),
`answer_date` bigint(20),
`student_count` int(8)
);
OPEN cur;
FETCH cur INTO tname;
table_loop:LOOP
FETCH cur INTO tname;
SET course_name = LEFT(tname, LENGTH(tname)-4);
SET @query_str = concat('SELECT count(*) INTO @studentCount FROM `', course_name, '_user`');
PREPARE stmt1 FROM @query_str;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET @query_str = concat('INSERT INTO tmp_assignments SELECT "', course_name, '", CONVERT(`set_id` USING utf8), `open_date`, `due_date`, `answer_date`, ? FROM `', tname, '`');
PREPARE stmt2 FROM @query_str;
EXECUTE stmt2 USING @studentCount;
DEALLOCATE PREPARE stmt2;
IF done THEN
LEAVE table_loop;
END IF;
END LOOP;
CLOSE cur;
SELECT `course`, `set_id`, FROM_UNIXTIME(`open_date`) as `open_date`, FROM_UNIXTIME(`due_date`) as `due_date`, `student_count` from `tmp_assignments` WHERE `due_date` > UNIX_TIMESTAMP() ORDER BY `due_date` DESC;
END $$