USE iot;

-- DROP TABLE IF EXISTS `zero_folder_relevancy`;
-- DROP TABLE IF EXISTS `zero_folder_entity`;

--  ///  zero_folder_entity BEGIN 

CREATE TABLE IF NOT EXISTS `zero_folder_entity` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`features` JSON NOT NULL,
	`name` VARCHAR(128) NOT NULL,
	`mine_type` VARCHAR(96) NOT NULL,
	`counter` INT(8) NOT NULL DEFAULT 0,
	`bytes` LONGBLOB NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `zero_folder_entity` ADD PRIMARY KEY pk_zero_folder_entity_id (`id`);
ALTER TABLE `zero_folder_entity` ADD KEY idx_zero_folder_entity_create_time (`create_time`);
ALTER TABLE `zero_folder_entity` ADD KEY idx_zero_folder_entity_update_time (`update_time`);
ALTER TABLE `zero_folder_entity` ADD KEY idx_zero_folder_entity_name (`name`);

DROP TRIGGER IF EXISTS `zero_folder_entity_uuid`;
DELIMITER $
CREATE TRIGGER `zero_folder_entity_uuid`
BEFORE INSERT
ON `zero_folder_entity`
FOR EACH ROW
BEGIN
	IF new.id = '-'
    THEN 
		SET new.id = (SELECT uuid());
	END IF;
END$
DELIMITER ;

DROP TRIGGER IF EXISTS `zero_folder_entity_update_time`;
DELIMITER $
CREATE TRIGGER `zero_folder_entity_update_time`
BEFORE UPDATE
ON `zero_folder_entity`
FOR EACH ROW
BEGIN
	SET new.update_time = (SELECT now());
END$
DELIMITER ;

--  ///  zero_folder_entity END 

--  ///  zero_folder_relevancy BEGIN 

CREATE TABLE IF NOT EXISTS `zero_folder_relevancy` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`entity_id` VARCHAR(36) NOT NULL,
	`relevancy_id` VARCHAR(36) NOT NULL,
	`directory` VARCHAR(256) NOT NULL,
	`path` VARCHAR(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `zero_folder_relevancy` ADD PRIMARY KEY pk_zero_folder_relevancy_id (`id`);
ALTER TABLE `zero_folder_relevancy` ADD KEY idx_zero_folder_relevancy_create_time (`create_time`);
ALTER TABLE `zero_folder_relevancy` ADD KEY idx_zero_folder_relevancy_id (`relevancy_id`);
ALTER TABLE `zero_folder_relevancy` ADD KEY idx_zero_folder_relevancy_directory (`directory`);
ALTER TABLE `zero_folder_relevancy` ADD UNIQUE KEY uk_zero_folder_relevancy_path (`path`);
ALTER TABLE `zero_folder_relevancy` ADD CONSTRAINT fk_zero_folder_relevancy_id
	FOREIGN KEY (`entity_id`) 
 		REFERENCES zero_folder_entity(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

DROP TRIGGER IF EXISTS `zero_folder_relevancy_uuid`;
DELIMITER $
CREATE TRIGGER `zero_folder_relevancy_uuid`
BEFORE INSERT
ON `zero_folder_relevancy`
FOR EACH ROW
BEGIN
	IF new.id = '-'
    THEN 
		SET new.id = (SELECT uuid());
	END IF;
END$
DELIMITER ;

DROP TRIGGER IF EXISTS `zero_folder_relevancy_iafter`;
DELIMITER $
CREATE TRIGGER `zero_folder_relevancy_iafter`
AFTER INSERT
ON `zero_folder_relevancy`
FOR EACH ROW
BEGIN
	UPDATE zero_folder_entity SET `counter` = `counter` + 1 WHERE ID = new.entity_id;
END$
DELIMITER ;

DROP TRIGGER IF EXISTS `zero_folder_relevancy_dafter`;
DELIMITER $
CREATE TRIGGER `zero_folder_relevancy_dafter`
AFTER DELETE
ON `zero_folder_relevancy`
FOR EACH ROW
BEGIN
	UPDATE zero_folder_entity SET `counter` = `counter` - 1 WHERE ID = old.entity_id;
	IF (SELECT `counter` FROM zero_folder_entity WHERE ID = old.entity_id) <= 0
    THEN 
		DELETE FROM zero_folder_entity WHERE ID = old.entity_id;
	END IF;
END$
DELIMITER ;

--  ///  zero_folder_relevancy END 

--  ///  zero_folder_view BEGIN 

DROP VIEW IF EXISTS zero_folder_view;
CREATE VIEW zero_folder_view AS
SELECT 
	a.id AS ref,
	a.relevancy_id, 
	a.directory, 
	a.path, 
	b.id, 
	b.create_time, 
	b.update_time, 
	b.features, 
	b.name, 
	b.mine_type
FROM 
	zero_folder_relevancy a ,
	zero_folder_entity b 
WHERE 
	b.ID = a.entity_id;

--  ///  zero_folder_view END 

--  ///  zero_folder_data_view BEGIN 

DROP VIEW IF EXISTS zero_folder_data_view;
CREATE VIEW zero_folder_data_view AS
SELECT 
	a.id AS ref,
	a.relevancy_id, 
	a.directory, 
	a.path, 
	b.id, 
	b.create_time, 
	b.update_time, 
	b.features, 
	b.name, 
	b.mine_type, 
	b.bytes
FROM 
	zero_folder_relevancy a ,
	zero_folder_entity b 
WHERE 
	b.ID = a.entity_id;

--  ///  zero_folder_data_view END 