-- --------------------------------------------------------
-- Server OS:                    Linux
-- HeidiSQL Version:             12.14.0.7165
-- --------------------------------------------------------



-- Dumping database structure for sr_310_ricards
CREATE DATABASE IF NOT EXISTS `sr_310_ricards` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `sr_310_ricards`;
    RETURN TRUE;
END//
DELIMITER ;
    RETURN TRUE;
END//
DELIMITER ;

    INSERT INTO sesijas(sesijas_id, last_activity, is_logged_in)
    VALUES (LAST_INSERT_ID(), NOW(), FALSE);

    RETURN TRUE;
END//
DELIMITER ;

-- Data exporting was unselected.

-- Dumping structure for function sr_310_ricards.lietotajs_aktivs
DELIMITER //
CREATE FUNCTION `lietotajs_aktivs`(
	`p_username` VARCHAR(50)
) RETURNS tinyint(1)
    DETERMINISTIC
    SQL SECURITY INVOKER
    COMMENT 'parbauda vai lietotajs ir aktivs'
BEGIN
 DECLARE last_time DATETIME;
    DECLARE uid INT;

    IF uid IS NULL THEN
        RETURN FALSE;
    END IF;

    IF TIMESTAMPDIFF(MINUTE, last_time, NOW()) > 5 THEN
        UPDATE sesijas SET is_logged_in = FALSE WHERE sesijas_id = uid;
        RETURN FALSE;
    END IF;

    RETURN TRUE;
END//
DELIMITER ;
    RETURN TRUE;
END//
DELIMITER ;

-- Dumping structure for function sr_310_ricards.parbaude_lietotaja_dati
DELIMITER //
CREATE FUNCTION `parbaude_lietotaja_dati`(
	`p_username` VARCHAR(50),
	`p_password` VARCHAR(255)
) RETURNS tinyint(1)
    DETERMINISTIC
    SQL SECURITY INVOKER
    COMMENT 'parbauda lietotaja vardu un paroli'
BEGIN
 DECLARE uid INT;

    IF uid IS NOT NULL THEN
        UPDATE sesijas
        SET is_logged_in = TRUE,
            last_activity = NOW()
        WHERE sesijas_id = uid;
        RETURN TRUE;
    END IF;

    RETURN FALSE;
END//
DELIMITER ;

-- Dumping structure for table sr_310_ricards.sesijas
CREATE TABLE IF NOT EXISTS `sesijas` (
  `sesijas_id` int NOT NULL,
  `last_activity` datetime NOT NULL,
  `is_logged_in` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`sesijas_id`),
  CONSTRAINT `sesijas_ibfk_1` FOREIGN KEY (`sesijas_id`) REFERENCES `lietotaji` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

