en | fr  | Contact  | Print  | Share

FrameBeurk File

Fmbk/Module/Beurk/_sql/moduleBeurk.sql

# FrameBeurk V0.65 Copyright ToolOscope 2016. Licence CeCILL-C.
# initialisation du module Beurk
#

#
# Structure de la table `Beurk_Compteur`
-- cette table contient la dernière Id créée pour chaque compteur
-- compteurs :    '1' pour les noeuds et relations
--                '2' pour l historique des actions (à faire, peut-être)

CREATE TABLE IF NOT EXISTS Beurk_Compteur (
  IdCompteur INT unsigned NOT NULL auto_increment,
  IdCourante INT unsigned NOT NULL,
  PRIMARY KEY (IdCompteur)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

# --------------------------------------------------------

#
# Structure de la table `Beurk_Terminal`
-- Les terminaux sont les libellés qui sont utilisés "en dur" dans le code de FrameBeurk (ex : action "detail", entité "Section"...)

CREATE TABLE IF NOT EXISTS Beurk_Terminal (
  IdTerminal INT unsigned NOT NULL,
  Terminal varchar(32) collate utf8_unicode_ci NOT NULL default '',
  PRIMARY KEY (IdTerminal),
  KEY (Terminal)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

# --------------------------------------------------------

#
# Structure de la table `Beurk_Lien`
-- cette table porte les relations entre entités et/ou relations (les noeuds peuvent être des relations et vice-versa)

CREATE TABLE IF NOT EXISTS Beurk_Lien (
  IdNoeud INT unsigned NOT NULL,
  IdRelation INT unsigned NOT NULL default '0',
  IdLie INT unsigned NOT NULL default '0',
  PRIMARY KEY  (IdNoeud, IdRelation, IdLie),
  UNIQUE KEY  (IdLie, IdRelation, IdNoeud)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
# --------------------------------------------------------

#
# Structure de la table `Beurk_Noeud`
-- colonnes communes à toutes les entités

CREATE TABLE IF NOT EXISTS Beurk_Noeud (
  IdNoeud INT unsigned NOT NULL,
  Libelle varchar(128) collate utf8_unicode_ci NOT NULL default '',
  Donnees text collate utf8_unicode_ci NOT NULL default '',
  NoVersion int unsigned NOT NULL default 0,
  IdEtat INT unsigned NOT NULL,
  PRIMARY KEY  (IdNoeud),
  FULLTEXT KEY (Libelle, Donnees)                                        -- index FULLTEXT géré sur innoDB à partir de la version
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
# --------------------------------------------------------

#
# Structure de la table `Beurk_Historique`
-- historique des actions sur les noeuds

CREATE TABLE IF NOT EXISTS Beurk_Historique (
  IdNoeud INT unsigned NOT NULL,
  NoVersion int unsigned NOT NULL,
  IdEtat INT unsigned NOT NULL,
  IdUser INT unsigned NOT NULL,
  AdresseIp varchar(45) collate utf8_unicode_ci NOT NULL,
  IdActMaj INT unsigned NOT NULL,
  IdMaj INT unsigned NOT NULL,
  TsAction bigint(20) NOT NULL,
  PRIMARY KEY  (IdNoeud, NoVersion)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
# --------------------------------------------------------

#
# Structure de la table `Beurk_Clef`
-- permet d'utiliser un jeton pour executer une action

CREATE TABLE IF NOT EXISTS Beurk_Clef (
  IdClef INT unsigned NOT NULL,
  Clef varchar(64) collate utf8_unicode_ci NOT NULL default '',
  adresseEmail varchar(64) collate utf8_unicode_ci NOT NULL,
  IdMaj INT unsigned NOT NULL default '0',
  IdActMaj INT unsigned NOT NULL default '0',
  NbrUtilisations INT NOT NULL default '0',
  Donnees text collate utf8_unicode_ci NOT NULL,
  IdEtat INT unsigned NOT NULL,
  NoVersion INT unsigned NOT NULL,
  PRIMARY KEY (IdClef),
  UNIQUE KEY (Clef)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
#------------------------------------------------------------------


# ---------------------------------------------------------------------------------
# Les procedures ci-dessous sont utilisées dans les fichier d initialisation .sql
# Elles ne créent pas de doublons. 
# Les fichiers moduleXxxx.sql peuvent être executés plusieurs fois sans risque (normalement...)

delimiter $$
#
# procédure pour récupérer la nouvelle Id

CREATE PROCEDURE Beurk_creeId (IN compteur INT unsigned, OUT nouvelleId INT unsigned)
    NOT DETERMINISTIC MODIFIES SQL DATA 
BEGIN
    UPDATE Beurk_Compteur SET IdCourante = @tmpId := IdCourante + 1 WHERE IdCompteur = compteur;
    SET nouvelleId := @tmpId;
END$$

#
# procédure pour récupérer la nouvelle Id si la requête fournie en paramètre ne ramène pas d''id

CREATE PROCEDURE Beurk_creeIdSiPasTrouve (IN compteur INT unsigned, OUT nouvelleId INT unsigned, IN requete VARCHAR(1024))
    NOT DETERMINISTIC MODIFIES SQL DATA 
BEGIN
    SET @setReq = CONCAT('SET @tmpId = (', requete, ')');
    PREPARE stmt FROM @setReq;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    IF @tmpId IS NULL THEN
        UPDATE Beurk_Compteur SET IdCourante = @tmpId := IdCourante + 1 WHERE IdCompteur = compteur;
    END IF;
    SET nouvelleId := @tmpId;
END$$

#
# procédure pour insérer un nouveau terminal

CREATE PROCEDURE Beurk_creeTerminal (IN term VARCHAR(32), OUT idTerm INT unsigned)
    NOT DETERMINISTIC MODIFIES SQL DATA 
BEGIN
    SET idTerm := (SELECT IdTerminal FROM Beurk_Terminal WHERE Terminal = term);
    IF idTerm IS NULL THEN
        CALL Beurk_creeId(1, idTerm);
        INSERT IGNORE INTO Beurk_Terminal (IdTerminal, Terminal) VALUES (idTerm, term);
    END IF;
END$$

#
# procédure pour insérer un nouveau terminal relié

CREATE PROCEDURE Beurk_creeTerminalEtLien (IN term VARCHAR(32), IN lien VARCHAR(32), IN termLie VARCHAR(32), OUT idTerm INT unsigned)
    NOT DETERMINISTIC MODIFIES SQL DATA 
BEGIN
    DECLARE idLien, idTermLie INT DEFAULT 0;
    SET idLien := (SELECT IdTerminal FROM Beurk_Terminal WHERE Terminal = lien);
    SET idTermLie := (SELECT IdTerminal FROM Beurk_Terminal WHERE Terminal = termLie);
    SET idTerm := (SELECT IdTerminal FROM Beurk_Terminal WHERE Terminal = term);
    IF idTerm IS NULL THEN
        CALL Beurk_creeTerminal (term, idTerm);
    END IF;
    IF NOT (idLien IS NULL OR idTermLie IS NULL) THEN
        INSERT IGNORE INTO Beurk_Lien (IdNoeud, IdRelation, IdLie) VALUES (idTerm, idLien, idTermLie);
    END IF;
END$$

#
# procédure pour insérer une nouvelle relation sur un terminal existant

CREATE PROCEDURE Beurk_creeLienTerminal (IN idNoe INT unsigned, IN rela VARCHAR(32), IN termLie VARCHAR(32))
    NOT DETERMINISTIC MODIFIES SQL DATA 
BEGIN
    DECLARE idRela, idTermLie INT DEFAULT 0;
    SET idRela := (SELECT IdTerminal FROM Beurk_Terminal WHERE Terminal = rela);
    SET idTermLie := (SELECT IdTerminal FROM Beurk_Terminal WHERE Terminal = termLie);
    IF NOT (idRela IS NULL OR idTermLie IS NULL) THEN
        INSERT IGNORE INTO Beurk_Lien (IdNoeud, IdRelation, IdLie) VALUES (idNoe, idRela, idTermLie);
    END IF;
END$$

#
# procédure pour insérer une nouvelle relation sur un noeud déjà existant

CREATE PROCEDURE Beurk_creeLienNoeud (IN idNoe INT unsigned, IN rela VARCHAR(32), IN idNoeLie INT unsigned)
    NOT DETERMINISTIC MODIFIES SQL DATA 
BEGIN
    DECLARE idRela INT DEFAULT 0;
    SET idRela := (SELECT IdTerminal FROM Beurk_Terminal WHERE Terminal = rela);
    IF NOT (idRela IS NULL) THEN
        INSERT IGNORE INTO Beurk_Lien (IdNoeud, IdRelation, IdLie) VALUES (idNoe, idRela, idNoeLie);
    END IF;
END$$

#
# procédure pour insérer une nouvelle version d historique d un noeud

CREATE PROCEDURE Beurk_creeHistorique (IN pIdNoeud INT unsigned, IN pNoVersion INT unsigned, IN pIdEtat INT unsigned, IN pIdUser INT unsigned, IN pAdresseIp varchar(45),
            IN pIdActMaj INT unsigned, IN pIdMaj INT unsigned, IN pTsAction bigint(20))
    NOT DETERMINISTIC MODIFIES SQL DATA 
BEGIN
    INSERT IGNORE INTO Beurk_Historique (IdNoeud, NoVersion, IdEtat, IdUser, AdresseIp, IdActMaj, IdMaj, TsAction)
        VALUES (pIdNoeud, pNoVersion, pIdEtat, pIdUser, pAdresseIp, pIdActMaj, pIdMaj, pTsAction);
END$$

delimiter ;

# création du compteur pour les graphes
INSERT IGNORE INTO `Beurk_Compteur` (`IdCompteur`, `IdCourante`) VALUES (1, 0);

# création des super-terminaux
CALL Beurk_creeTerminal('Entite', @idTermEntite);
CALL Beurk_creeTerminal('Module', @idTermModule);
CALL Beurk_creeTerminal('Action', @idTermAction);
CALL Beurk_creeTerminal('Relation', @idTermRelation);
CALL Beurk_creeTerminal('Etat', @idTermEtat);

# création de la relation 'estDeType'    qui a pour type elle-même            
CALL Beurk_creeTerminal('estDeType', @idTermEstDeType);
INSERT IGNORE INTO Beurk_Lien (IdNoeud, IdRelation, IdLie) VALUES (@idTermEstDeType, @idTermEstDeType, @idTermRelation);

# création de la relation 'estReciproqueDe'                
CALL Beurk_creeTerminalEtLien ('estReciproqueDe', 'estDeType', 'Relation', @idTermEstReciproqueDe);
CALL Beurk_creeLienTerminal (@idTermEstReciproqueDe, 'estReciproqueDe', 'estReciproqueDe');

# création de la réciproque de 'estDeType' : 'estLeTypeDe'
CALL Beurk_creeTerminalEtLien ('estLeTypeDe', 'estDeType', 'Relation', @idTermEstLeTypeDe);
CALL Beurk_creeLienTerminal (@idTermEstLeTypeDe, 'estReciproqueDe', 'estDeType');


# création de la relation 'estElementDe' et sa réciproque 'contient'                
CALL Beurk_creeTerminalEtLien ('estElementDe', 'estDeType', 'Relation', @idTermEstElementDe);
CALL Beurk_creeTerminalEtLien ('contient', 'estDeType', 'Relation', @idTermContient);
CALL Beurk_creeLienTerminal (@idTermContient, 'estReciproqueDe', 'estElementDe');

# création de la relation 'estAssocieA' et sa réciproque : elle-même                
CALL Beurk_creeTerminalEtLien ('estAssocieA', 'estDeType', 'Relation', @idTermEstAssocieA);
CALL Beurk_creeLienTerminal (@idTermEstAssocieA, 'estReciproqueDe', 'estAssocieA');

# création de la relation 'estDefiniPar' et sa réciproque 'definit'                
CALL Beurk_creeTerminalEtLien ('estDefiniPar', 'estDeType', 'Relation', @idTermEstDefiniPar);
CALL Beurk_creeTerminalEtLien ('definit', 'estDeType', 'Relation', @idTermDefinit);
CALL Beurk_creeLienTerminal (@idTermDefinit, 'estReciproqueDe', 'estDefiniPar');


# déclaration du module 'Beurk'
CALL Beurk_creeTerminalEtLien ('Beurk', 'estDeType', 'Module', @idTermModuleBeurk);

# déclaration des entités du module

CALL Beurk_creeTerminalEtLien ('Clef', 'estDeType', 'Entite', @idTermEntiteClef);
CALL Beurk_creeLienTerminal (@idTermEntiteClef, 'estDefiniPar', 'Beurk');


# Rattachement des relations au module

CALL Beurk_creeLienTerminal (@idTermEstReciproqueDe, 'estDefiniPar', 'Beurk');
CALL Beurk_creeLienTerminal (@idTermEstDeType, 'estDefiniPar', 'Beurk');
CALL Beurk_creeLienTerminal (@idTermEstElementDe, 'estDefiniPar', 'Beurk');
CALL Beurk_creeLienTerminal (@idTermEstAssocieA, 'estDefiniPar', 'Beurk');
CALL Beurk_creeLienTerminal (@idTermEstDefiniPar, 'estDefiniPar', 'Beurk');
CALL Beurk_creeLienTerminal (@idTermContient, 'estDefiniPar', 'Beurk');
CALL Beurk_creeLienTerminal (@idTermDefinit, 'estDefiniPar', 'Beurk');
CALL Beurk_creeLienTerminal (@idTermEstLeTypeDe, 'estDefiniPar', 'Beurk');

# Création des états

CALL Beurk_creeTerminalEtLien ('V', 'estDeType', 'Etat', @idTermEtatV);
CALL Beurk_creeTerminalEtLien ('F', 'estDeType', 'Etat', @idTermEtatF);
CALL Beurk_creeTerminalEtLien ('Z', 'estDeType', 'Etat', @idTermEtatZ);

# Création des codes Action / Vue

CALL Beurk_creeTerminalEtLien ('noeud', 'estDeType', 'Action', @idTermActionNoeud);
CALL Beurk_creeTerminalEtLien ('historique', 'estDeType', 'Action', @idTermActionHistorique);
CALL Beurk_creeTerminalEtLien ('relations', 'estDeType', 'Action', @idTermActionRelations);
CALL Beurk_creeTerminalEtLien ('ascendants', 'estDeType', 'Action', @idTermActionLies);
CALL Beurk_creeTerminalEtLien ('descendants', 'estDeType', 'Action', @idTermActionLies);

CALL Beurk_creeTerminalEtLien ('cree', 'estDeType', 'Action', @idTermActionUtilise);
CALL Beurk_creeTerminalEtLien ('ferme', 'estDeType', 'Action', @idTermActionUtilise);
CALL Beurk_creeTerminalEtLien ('utilise', 'estDeType', 'Action', @idTermActionUtilise);


© 2010-2015 by ToolOscope SASU. © 2016-2018 by Arnaud De Rette. All rights reserved