Добрый день
Я создаю приложение Duty Schedule для нашей горячей линии технической поддержки, используя Qt + MySQL.
На каждый месяц необходимо ежемесячно заполнять календарную таблицу, содержащую дату и знак выходного или рабочего дня.
Структура таблицы следующая:
- CREATE TABLE `calendar` (
- `calendarID` int(10) unsigned NOT NULL AUTO_INCREMENT,
- `monthID` int(10) unsigned NOT NULL,
- `date` date NOT NULL,
- `iswork` tinyint(3) unsigned NOT NULL,
- PRIMARY KEY (`calendarID`),
- KEY `month` (`monthID`)
- ) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4
Процедура была написана без проверки входящих значений.
Февраль високосный год обрабатывается правильно.
Вызов
- call filling_the_calendar(2,2016);
- CREATE DEFINER=`root`@`localhost` PROCEDURE `filling_the_calendar`(IN cur_month INT, cur_year INT)
- BEGIN
- -- We declare variables the current day, the first day of the month, the first day of the next month.
- DECLARE cur_day, first_day, next_month DATE;
- --Month ID, working day or not
- DECLARE monthID, iswork INT;
- /*Variable initialization*/
- SET first_day = CONCAT(cur_year,'-',cur_month,'-01');
- SET monthID = cur_year * 100 + cur_month;
- SET next_month = DATE_ADD(first_day, INTERVAL 1 MONTH);
- SET cur_day = first_day;
- /*Going through all the days of the month */
- WHILE cur_day < next_month DO
- /*If the day of the week is Saturday or Sunday they are not working*/
- IF (WEEKDAY(cur_day) = 6 OR WEEKDAY(cur_day) = 5) THEN
- SET iswork = 0;
- ELSE
- SET iswork = 1;
- END IF;
- /*add record to table*/
- INSERT INTO calendar (`monthID`, `date`, `iswork`)
- VALUES (monthID, cur_day, iswork);
- /*Increase the day*/
- SET cur_day = DATE_ADD(cur_day, INTERVAL 1 DAY);
- END WHILE;
- END