/*
 * phpMyEdit - instant MySQL table editor and code generator
 *
 * examples/phpMyEditMetaDB.sql - phpMyEdit Metadata system tables
 * _______________________________________________________________
 *
 * Copyright (c) 1999-2002 John McCreesh <jpmcc@users.sourceforge.net>
 * Copyright (c) 2001-2002 Jim Kraai <jkraai@users.sourceforge.net>
 * Versions 5.0 and higher developed by Ondrej Jombik <nepto@php.net>
 * Versions 5.7.2 and higher developed by Yves De Billoëz
 * Copyright (c) 2002-2006 Platon Group, http://platon.sk/
 * All rights reserved.
 *
 * See README.md file for more information about this software.
 * See COPYING file for license information.
 *
 * Download the latest version from
 * https://sourceforge.net/projects/phpmariaedit/
 */

/* $Platon: phpMyEdit/examples/phpMyEditMetaDB.sql,v 5.7.6 2024-01-15 12:57:07 Yves $ */

/* generic stored procedures */

DROP PROCEDURE IF EXISTS `pme_protectsysupdate`;
DROP PROCEDURE IF EXISTS `pme_protectsysdelete`;

DELIMITER $$

CREATE PROCEDURE pme_protectsysupdate(IN `old_sc` VARCHAR(8), IN `new_sc` VARCHAR(8))
BEGIN
    IF `old_sc` = 'S' AND `new_sc` = 'S' THEN
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT ='CANNOT UPDATE SYSTEM RECORDS';
    END IF;
END
$$

CREATE PROCEDURE pme_protectsysdelete(IN `old_sc` VARCHAR(8))
BEGIN
    IF `old_sc` = 'S' THEN
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT ='CANNOT DELETE SYSTEM RECORDS';
    END IF;
END
$$

DELIMITER ;

/* delete all tables for safe execution of script */


/* pme_statuscodes table */


DROP TRIGGER IF EXISTS `pme_statuscodes_before_insert`;
DROP TRIGGER IF EXISTS `pme_statuscodes_before_update`;

CREATE TABLE `pme_statuscodes` (
    `code` CHAR(1) NOT NULL,
    `status_name` VARCHAR(60) NOT NULL,
    `status_description` TEXT,
    `status_code` CHAR(1) NOT NULL,
    `creation_date` TIMESTAMP DEFAULT current_timestamp(),
    `modification_date` TIMESTAMP DEFAULT current_timestamp()
);

ALTER TABLE `pme_statuscodes`
    ADD `rowid` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST;

ALTER TABLE `pme_statuscodes`
    COMMENT = 'different status codes for the other tables record validity';

ALTER TABLE `pme_statuscodes`
    ADD UNIQUE KEY `code_ukey` (`code`);

INSERT INTO `pme_statuscodes`
    (`code`, `status_name`, `status_description`, `status_code`)
VALUES
    ('V', 'Valid', 'Indicates record has been validated', 'S'),
    ('P', 'Partially valid', 'Indicates record has been partially validated', 'S'),
    ('D', 'Deleted', 'Indicates record has been soft-deleted', 'S'),
    ('M', 'Modified', 'Indicates record has been modified', 'S'),
    ('C', 'Created', 'Indicates record has been created', 'S'),
    ('I', 'Initial', 'Indicates record has been set initially by the system', 'S'),
    ('X', 'Invalid', 'The record status is invalid (not validated or corrupted)', 'S'),
    ('S', 'System', 'Indicates record is a system record and should not be modified', 'S');

-- do not allow user-modification of system table

DELIMITER $$
CREATE TRIGGER `pme_statuscodes_before_insert`
    BEFORE INSERT ON `pme_statuscodes` FOR EACH ROW
BEGIN
    SET NEW.`status_code` = 'X';
END
$$

CREATE TRIGGER `pme_statuscodes_before_update`
    BEFORE UPDATE ON `pme_statuscodes` FOR EACH ROW
BEGIN
    IF OLD.`status_code` = 'S' THEN
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT ='CANNOT UPDATE SYSTEM RECORDS';
    END IF;
    SET NEW.`modification_date` := current_timestamp();
    SET NEW.`status_code` = 'X';
END
$$

CREATE TRIGGER `pme_statuscodes_before_delete`
    BEFORE DELETE ON `pme_statuscodes` FOR EACH ROW
BEGIN
    IF OLD.`status_code` = 'S' THEN
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT ='CANNOT DELETE SYSTEM RECORDS';
    END IF;
END
$$

DELIMITER ;

-- self referencing statuscode, can only be activated after having inserted the records

ALTER TABLE `pme_statuscodes`
    ADD CONSTRAINT `FK_statuscodes_statuscodes`
    FOREIGN KEY (`status_code`) REFERENCES `pme_statuscodes`(`code`);

/* languages table */

CREATE TABLE `pme_languages` (
    `la_sequence` INTEGER,
    `la_code` CHAR(2) NOT NULL,
    `la_alpha3code` CHAR(3),
    `la_webcode` CHAR(10),
    `la_name` VARCHAR(60) NOT NULL,
    `la_name_en` VARCHAR(60),
    `la_oldcodepage` VARCHAR(30),
    `la_img` BLOB,
    `la_l1_speakers` NUMERIC,
    `la_l2_speakers` NUMERIC,
    `la_translationfile` VARCHAR(250) COMMENT 'php language file',
    `la_oldtranslationfile` VARCHAR(250) COMMENT 'php non utf8 secondary language file',
    `status_code` char(1) DEFAULT 'C',
    `creation_date` TIMESTAMP DEFAULT current_timestamp(),
    `modification_date` TIMESTAMP DEFAULT current_timestamp()
);

ALTER TABLE `pme_languages`
    ADD `rowid` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY FIRST;

ALTER TABLE `pme_languages`
    COMMENT = 'languages supported by the system';

ALTER TABLE `pme_languages`
    ADD UNIQUE KEY `la_webcode_ukey` (`la_webcode`);

DROP TRIGGER IF EXISTS `languages_before_insert`;
DROP TRIGGER IF EXISTS `languages_before_update`;

DELIMITER $$

CREATE TRIGGER `languages_before_insert`
    BEFORE INSERT ON `pme_languages` FOR EACH ROW
BEGIN
    IF (NEW.`la_name_en` IS NULL) THEN SET NEW.`la_name_en` = NEW.`la_name`; END IF;
    IF (NEW.`la_code` IS NULL) THEN SET NEW.`la_code` = LEFT(NEW.`la_webcode`,2); END IF;
    SET NEW.`la_webcode` = LOWER(NEW.`la_webcode`);
    SET NEW.`la_name_en` = CONCAT(UPPER(LEFT(NEW.`la_name_en`,1)), RIGHT(NEW.`la_name_en`,LENGTH(NEW.`la_name_en`) - 1));
END
$$

CREATE TRIGGER `languages_before_update`
    BEFORE UPDATE ON `pme_languages` FOR EACH ROW
BEGIN
    SET NEW.`modification_date` := current_timestamp();
    IF (NEW.`la_name_en` IS NULL) THEN SET NEW.`la_name_en` = NEW.`la_name`; END IF;
    IF (NEW.`la_code` IS NULL) THEN SET NEW.`la_code` = LEFT(NEW.`la_webcode`,2); END IF;
    IF NEW.`la_webcode` != OLD.`la_webcode` THEN
        SET NEW.`la_webcode` = LOWER(NEW.`la_webcode`);
    END IF;
    IF NEW.`la_name_en` != OLD.`la_name_en` THEN
        SET NEW.`la_name_en` = CONCAT(UPPER(LEFT(NEW.`la_name_en`,1)), RIGHT(NEW.`la_name_en`,LENGTH(NEW.`la_name_en`) - 1));
    END IF;
END
$$

DELIMITER ;

INSERT INTO `pme_languages`
    (`la_code`, `la_webcode`, `la_name`, `la_name_en`, `la_oldcodepage`,`la_translationfile`)
VALUES
    ('EN', 'en-gb', 'English', 'English (British)', 'iso-8859-1', ''),
    ('EN', 'en', 'English', 'English', 'iso-8859-1', ''),
    ('EN', 'en-us', 'English', 'English (United States)', 'iso-8859-1', ''),
    ('HI', 'hi', 'हिन्दी', 'Hindi', '', ''),
    ('FR', 'fr', 'Français', 'French', 'iso-8859-1', ''),
    ('RU', 'ru', 'Русский', 'Russian', 'iso-8859-5', '');

INSERT INTO `pme_languages`
    (`la_sequence`, `la_code`, `la_webcode`, `la_name`, `la_name_en`, `la_oldcodepage`)
VALUES
    (999, 'XX', 'und', '--', 'Undetermined', '');

-- following depends on triggers to fill in other fields

INSERT INTO `pme_languages`
    (`la_webcode`, `la_name`,`la_translationfile`)
VALUES
    ('CZ', 'czech', ''),
    ('DE', 'german (standard)', ''),
    ('DK', 'danish', ''),
    ('EL', 'greek (hellenic)', ''),
    ('ES', 'spanish', ''),
    ('ES-AR', 'spanish (argentinian)', ''),
    ('ES-MX', 'spanish (mexican)', ''),
    ('EU', 'basque', ''),
    ('ID', 'indonesian', ''),
    ('IT', 'italian (standard)', ''),
    ('JP', 'japanese', ''),
    ('NL', 'dutch (standard)', ''),
    ('PL', 'polish', ''),
    ('PT', 'portuguese (standard)', ''),
    ('PT-BR', 'portuguese (brazilian)', ''),
    ('SE', 'swedish', ''),
    ('SK', 'slovak', ''),
    ('TR', 'turkish', ''),
    ('ZH', 'chinese (traditional)', ''),
    ('ZH-SG', 'chinese (Singapore)', '');

DROP TRIGGER IF EXISTS `pme_users_before_insert`;
DROP TRIGGER IF EXISTS `pme_users_before_update`;

DELIMITER $$
    END IF;
    SET NEW.`user_code` = UPPER(NEW.`user_code`);
END
$$
    IF NEW.`status_code` = OLD.`status_code` THEN
        /* set status code only if it has not been changed by the
           statement
        */
        SET NEW.`status_code` = 'M';
    ELSEIF (NEW.`status_code` IS NULL) OR (NEW.`status_code` = '') THEN
        SET NEW.`status_code` = 'M';
    END IF;
    IF NEW.`user_code` != OLD.`user_code` THEN
        SET NEW.`user_code` = UPPER(NEW.`user_code`);
    END IF;
END
$$

DELIMITER ;

/* symbols table */

CREATE TABLE `pme_symbols` (
    `rowid` int(10) UNSIGNED NOT NULL,
    `sy_name` char(10) NOT NULL,
    `sy_code` varchar(255) NOT NULL,
    `sy_value` varchar(255) NOT NULL,
    `status_code` char(1) DEFAULT 'C',
    `creation_date` timestamp NOT NULL DEFAULT current_timestamp(),
    `modification_date` timestamp NOT NULL DEFAULT current_timestamp()
);

ALTER TABLE `pme_symbols`
    ADD PRIMARY KEY (`rowid`);

ALTER TABLE `pme_symbols`
    MODIFY `rowid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `pme_symbols`
    MODIFY COLUMN `modification_date` timestamp NOT NULL DEFAULT current_timestamp()
    ON UPDATE current_timestamp();

ALTER TABLE `pme_symbols`
    COMMENT = 'table to store key/value pairs';

ALTER TABLE `pme_symbols`
    ADD UNIQUE KEY `name_code_ukey` (`sy_name`, `sy_code`);

ALTER TABLE `pme_symbols`
    ADD CONSTRAINT `FK_symbols_statuscodes`
    FOREIGN KEY (`status_code`) REFERENCES `pme_statuscodes`(`code`);

-- meta system data upload

INSERT INTO `pme_symbols`
    (`sy_name`, `sy_code`, `sy_value`)
VALUES
    ('SYMBOL', 'PME_TABLE', 'name of phpMyEditMeta system table');

INSERT INTO `pme_symbols`
    (`sy_name`, `sy_code`, `sy_value`)
VALUES
    ('PME_TABLE', 'pme_languages', ''),
    ('PME_TABLE', 'pme_users', ''),
    ('PME_TABLE', 'pme_statuscodes', ''),
    ('PME_TABLE', 'pme_symbols', '');

/* reports table */

DROP TRIGGER IF EXISTS `pme_reports_before_insert`;
DROP TRIGGER IF EXISTS `pme_reports_before_update`;
DROP TRIGGER IF EXISTS `pme_reports_before_delete`;

DROP PROCEDURE IF EXISTS `pme_reports_recalcsections`;

CREATE TABLE `pme_reports` (
    `rowid` int(10) UNSIGNED NOT NULL,
    `re_code` char(10) NOT NULL COMMENT 'code',
    `re_name` varchar(255) NOT NULL COMMENT 'name of the report',
    `re_menuname` varchar(255) COMMENT 'name of menu-entry',
    `re_title` varchar(255) COMMENT 'title of the report (printed on top if set)',
    `re_section_cnt` INT(10) DEFAULT 0,
    `re_run_cnt` INT(10) DEFAULT 0,
    `status_code` char(1) DEFAULT 'C' COMMENT 'valid codes: see table pme_statuscodes',
    `creation_date` timestamp NOT NULL DEFAULT current_timestamp(),
    `modification_date` timestamp NOT NULL DEFAULT current_timestamp()
);

ALTER TABLE `pme_reports`
    ADD PRIMARY KEY (`rowid`);

ALTER TABLE `pme_reports`
    MODIFY `rowid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `pme_reports`
    MODIFY COLUMN `modification_date` timestamp NOT NULL DEFAULT current_timestamp()
    ON UPDATE current_timestamp();

ALTER TABLE `pme_reports`
    COMMENT = 'table for reports that can be used';

ALTER TABLE `pme_reports`
    ADD UNIQUE KEY `ukey_re_code` (`re_code`);

DELIMITER $$
END
$$

CREATE TRIGGER `pme_reports_before_insert`
    BEFORE INSERT ON `pme_reports` FOR EACH ROW
BEGIN
    IF (NEW.`re_menuname` IS NULL) OR (NEW.`re_menuname` = '') THEN
        SET NEW.`re_menuname` = NEW.`re_name`;
    END IF;
END
$$
    IF (NEW.`re_menuname` IS NULL) OR (NEW.`re_menuname` = '') THEN
        SET NEW.`re_menuname` = OLD.`re_menuname`;
    END IF;
END
$$
END
$$

DELIMITER ;

/* report details table */

DROP TRIGGER IF EXISTS `pme_repdetails_before_insert`;
DROP TRIGGER IF EXISTS `pme_repdetails_before_update`;
DROP TRIGGER IF EXISTS `pme_repdetails_before_delete`;

DROP TRIGGER IF EXISTS `pme_repdetails_after_insert`;
DROP TRIGGER IF EXISTS `pme_repdetails_after_update`;
DROP TRIGGER IF EXISTS `pme_repdetails_after_delete`;

CREATE TABLE `pme_repdetails` (
    `rowid` int(10) UNSIGNED NOT NULL,
    `rd_reportcode` char(10) NOT NULL COMMENT 'code',
    `rd_sequence` INT(10) DEFAULT 1 COMMENT 'sequence number of section in report',
    `rd_title` CHAR(250) COMMENT 'title of section (printed on top if set)',
    `rd_sql` TEXT COMMENT 'sql statement',
    `status_code` char(1) DEFAULT 'C' COMMENT 'valid codes: see table pme_statuscodes',
    `creation_date` timestamp NOT NULL DEFAULT current_timestamp(),
    `modification_date` timestamp NOT NULL DEFAULT current_timestamp()
);

ALTER TABLE `pme_repdetails`
    ADD PRIMARY KEY (`rowid`);

ALTER TABLE `pme_repdetails`
    MODIFY `rowid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `pme_repdetails`
    COMMENT = 'table for report - sections';

ALTER TABLE `pme_repdetails`
    ADD CONSTRAINT `FK_pme_repdetails_reportcode`
    FOREIGN KEY (`rd_reportcode`) REFERENCES `pme_reports`(`re_code`);

DELIMITER $$
END
$$
END
$$
END
$$
    END IF;
END
$$
END
$$

DELIMITER ;

INSERT INTO `pme_reports`
    (`re_code`, `re_name`, `re_title`)
VALUES
    ('EXAMPLE1', 'Example report', 'Statistics on reports in the system');

INSERT INTO `pme_repdetails`
    (`rd_reportcode`, `rd_sequence`, `rd_title`, `rd_sql`)
VALUES
    ('EXAMPLE1', 1, "Reports", "SELECT COUNT(`re_code`) AS 'Number of reports' FROM `pme_reports`"),
    ('EXAMPLE1', 2, "Report sections", "SELECT COUNT(`rd_sequence`) AS 'Number of SQL statements' FROM `pme_repdetails`");

/* eof */
