From 0b7981be4a46172909e59b1dfa0f8e38d878f220 Mon Sep 17 00:00:00 2001
From: "Jangberry (Nomad-Debian)" <matt2001@hotmail.fr>
Date: Wed, 13 Dec 2023 17:03:28 +0100
Subject: [PATCH] Auto-increment + fill + utils

---
 .gitignore          |  1 +
 BE_SGBD_LIBRARY.ddl | 11 ++++----
 fill.ipynb          | 62 +++++++++++++++++++++++++++++++++++++++++----
 util.py             | 47 ++++++++++++++++++++++++++++++++++
 4 files changed, 111 insertions(+), 10 deletions(-)
 create mode 100644 util.py

diff --git a/.gitignore b/.gitignore
index 2d1ab1c..f69ab4e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 .idea
 .venv
 node_modules/
+__pycache__
\ No newline at end of file
diff --git a/BE_SGBD_LIBRARY.ddl b/BE_SGBD_LIBRARY.ddl
index 777ff56..adbbf64 100644
--- a/BE_SGBD_LIBRARY.ddl
+++ b/BE_SGBD_LIBRARY.ddl
@@ -53,7 +53,7 @@ create table DEVISE (
      constraint ID_DEVISE_ID primary key (code));
 
 create table EXEMPLAIRE (
-     ID_EXE bigint not null,
+     ID_EXE bigint not null auto_increment,
      statut smallint not null,
      nom varchar(300) not null,
      email varchar(300),
@@ -70,6 +70,7 @@ create table LABORATOIRE (
      constraint ID_LABORATOIRE_ID primary key (nom));
 
 create table LIVRE (
+     titre varchar(100) not null,
      ISBN char(13) not null,
      editeur varchar(300) not null,
      edition varchar(100) not null,
@@ -85,7 +86,7 @@ create table MOT_CLE (
      constraint ID_MOT_CLE_ID primary key (mot));
 
 create table PERIODIQUE (
-     ID_PER bigint not null,
+     ID_PER bigint not null auto_increment,
      numero char(5) not null,
      editeur varchar(300) not null,
      edition varchar(100) not null,
@@ -99,7 +100,7 @@ create table propose (
      constraint FKpro_PUB_ID primary key (ID_PUB));
 
 create table PUBLICATION (
-     ID_PUB bigint not null,
+     ID_PUB bigint not null auto_increment,
      ID_PER bigint,
      ID_RAP bigint,
      ISBN char(13),
@@ -111,8 +112,8 @@ create table PUBLICATION (
 
 create table RAPPORT (
      titre varchar(300) not null,
-     ID_RAP bigint not null,
-     annee_de_publication char(1) not null,
+     ID_RAP bigint not null auto_increment,
+     annee_de_publication char(4) not null,
      constraint ID_RAPPORT_ID primary key (ID_RAP));
 
 create table redige_par (
diff --git a/fill.ipynb b/fill.ipynb
index 7cb2339..404b799 100644
--- a/fill.ipynb
+++ b/fill.ipynb
@@ -17,7 +17,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": 13,
    "metadata": {},
    "outputs": [
     {
@@ -32,12 +32,13 @@
    ],
    "source": [
     "%pip install mariadb\n",
-    "import mariadb"
+    "import mariadb\n",
+    "from util import *"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": 7,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -60,7 +61,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": 8,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -87,7 +88,58 @@
   {
    "cell_type": "markdown",
    "metadata": {},
-   "source": []
+   "source": [
+    "And this code will fill the database with some random data:"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import random\n",
+    "\n",
+    "def nom_livre_random():\n",
+    "    pre = [\"L'histroire des\", \"Tout savoir sur les\", \"Encyclopédie des\", \"Le guide des\", \"Les secrets des\", \"Le manuel des\", \"Le livre des\", \"Le dictionnaire des\", \"Le guide pratique des\"]\n",
+    "    sujet = [\"animaux\", \"plantes\", \"champignons\", \"insectes\", \"oiseaux\", \"poissons\", \"mammifères\", \"reptiles\", \"amphibiens\", \"ordinateurs\", \"micro-controleurs\", \"micro-ordinateurs\", \"micro-ondes\", \"microscopes\", \"micro-organismes\", \"micro-organisations\", \"macro-états\", \"dictatures\", \"démocraties\", \"monarchies\", \"républiques\", \"oligarchies\", \"aristocraties\", \"autocraties\", \"théocraties\", \"anarchies\", \"communautés\", \"communistes\", \"capitalistes\", \"socialistes\", \"libéraux\"]\n",
+    "    post = [\"pour les nuls\", \"\", \"pour les enfants\", \"pour les adultes\", \"pour les vieux\", \"pour les jeunes\", \"pour les riches\", \"pour les pauvres\", \"pour les moyens\", \"pour les grands\", \"pour les petits\", \"pour les grands et les petits\"]\n",
+    "    return pre[random.randint(0, len(pre)-1)] + \" \" + sujet[random.randint(0, len(sujet)-1)] + \" \" + post[random.randint(0, len(post)- 1)]\n",
+    "\n",
+    "def nom_auteur_random():\n",
+    "    prenom = [\"Patrick\", \"Jean\", \"Alexandre\", \"Marie\", \"Jeanne\", \"Eudes\", \"Eudette\", \"Valentine\", \"Valentin\", \"Albert\", \"Alfred\", \"Marcel\", \"Claude\", \"Claudette\", \"Gérard\"\"Martin\", \"Bernard\", \"Thomas\", \"Robert\", \"Richard\", \"Mathieu\", \"Clement\", \"Lucas\", \"Jean\", \"Marie\", \"Pierre\", \"Arnaud\", \"Rolland\", \"Philippe\", \"Olivier\", \"Hubert\", \"Louis\", \"Charles\", \"Guillaume\", \"Benoit\", \"Antoine\", \"Géraldine\"]\n",
+    "    nom = [\"Dupont\", \"Dupond\", \"Durand\", \"Duchemin\", \"Duchesse\", \"Duchesne\", \"Petit\", \"Leroy\", \"Moreau\", \"Lefebvre\", \"Garcia\", \"David\", \"Bertrand\", \"Roux\", \"Balkani\", \"Fournier\", \"Lefevre\", \"Mercier\", \"Dupuy\", \"Lambert\", \"Bonnet\", \"Martinez\", \"Legrand\", \"Garnier\", \"Faure\", \"Rousseau\", \"Blanc\", \"Guerin\", \"Muller\", \"Roussel\", \"Perrin\", \"Morin\", \"Gauthier\", \"Dumont\", \"Lopez\", \"Fontaine\", \"Chevalier\", \"Robin\", \"Masson\", \"Sanchez\", \"Nguyen\", \"Boyer\", \"Denis\", \"Lemaire\", \"Duval\", \"Joly\", \"Roger\", \"Roche\", \"Roy\", \"Meyer\", \"Meunier\", \"Perez\", \"Marchand\", \"Dufour\", \"Blanchard\", \"Barbier\", \"Brun\", \"Dumas\", \"Brunet\", \"Schmitt\", \"Leroux\", \"Colin\", \"Fernandez\", \"Renard\", \"Caron\", \"Aubert\", \"Giraud\", \"Leclerc\", \"Vidal\", \"Bourgeois\", \"Renaud\", \"Lemoine\", \"Picard\", \"Gaillard\", \"Leclercq\", \"Lacroix\", \"Fabre\", \"Dupuis\", \"Rodriguez\", \"Da silva\", \"Riviere\", \"Le gall\", \"Guillot\", \"Royer\", \"Huet\", \"Dupre\" \"Maillard\", \"Bailly\", \"Janvier\", \"Renault\", \"Charpentier\"]\n",
+    "    return prenom[random.randint(0, len(prenom)-1)] + \" \" + nom[random.randint(0, len(nom)-1)]\n",
+    "\n",
+    "def nom_periodique_random():\n",
+    "    nom = [\"Nature\", \"Science\", \"Science et vie\", \"Science et avenir\", \"Science et fiction\", \"Science et technologie\", \"Science et nature\", \"Science et société\", \"Science et politique\", \"Science et religion\", \"Science et philosophie\", \"Science et histoire\", \"Science et géographie\", \"Science et économie\", \"Science et finance\", \"Science et culture\", \"Science et éducation\", \"Science et éthique\", \"Science et morale\", \"Science et art\", \"Science et littérature\", \"Science et poésie\", \"Science et musique\", \"Science et cinéma\", \"Science et théatre\", \"Science et architecture\", \"Science et peinture\", \"Science et sculpture\", \"Science et photographie\", \"Science et dessin\", \"Science et design\", \"Science et mode\", \"Science et sport\", \"Science et santé\", \"Science et médecine\", \"Science et psychologie\", \"Science et psychiatrie\", \"Science et psychanalyse\", \"Scie\"]\n",
+    "    return nom[random.randint(0, len(nom)-1)]\n",
+    "\n",
+    "def nom_rapport_random():\n",
+    "    rtype = [\"Rapport de PE\", \"Rapport de PAi\", \"Rapport de PAr\", \"These\"]\n",
+    "    about = [\"sur\", \"concernant\", \"à propos de\", \"relatif à\", \"portant sur\", \"traitant de\", \"au sujet de\"]\n",
+    "    sub = [\"l'impact\", \"l'effet\", \"l'influence\", \"l'incidence\", \"l'effet\", \"la reproduction\", \"la propagation\"]\n",
+    "    sub2 = [\"des escargots\", \"des limaces\", \"des fourmis\", \"de l'intelligence\", \"de la bêtise\", \"des aérosols\", \"des gaz\", \"des liquides\", \"du soleil\", \"de la lune\"]\n",
+    "    about2 = [\"sur\", \"dans\", \"dans le\", \"dans la\", \"dans les\", \"dans l'\"]\n",
+    "    end = [\"espace\", \"univers\", \"système solaire\", \"galaxie\", \"voie lactée\", \"planète\", \"terre\", \"atmosphère\", \"air\", \"eau\", \"mer\", \"océan\", \"continent\", \"pays\", \"région\", \"département\", \"ville\", \"village\", \"commune\", \"rue\", \"maison\", \"chambre\", \"salon\", \"cuisine\", \"salle de bain\", \"toilettes\", \"chiotte\", \"cave\", \"grenier\", \"garage\", \"jardin\", \"parc\", \"forêt\", \"montagne\", \"colline\", \"plaine\", \"rivière\", \"lac\", \"étang\", \"mer\", \"océan\", \"continent\", \"pays\", \"région\", \"département\", \"ville\", \"village\", \"commune\", \"rue\", \"maison\", \"chambre\", \"salon\", \"cuisine\", \"salle de bain\", \"toilettes\", \"chiotte\", \"cave\", \"grenier\", \"garage\", \"jardin\", \"parc\", \"forêt\", \"montagne\", \"colline\", \"plaine\", \"rivière\", \"lac\", \"étang\"]\n",
+    "    return rtype[random.randint(0, len(rtype)-1)] + \" \" + about[random.randint(0, len(about)-1)] + \" \" + sub[random.randint(0, len(sub)-1)] + \" \" + sub2[random.randint(0, len(sub2)-1)] + \" \" + about2[random.randint(0, len(about2)-1)] + \" \" + end[random.randint(0, len(end)-1)]\n",
+    "\n",
+    "def ISBN_random():\n",
+    "    return str(random.randint(1000000000000, 9999999999999))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "for _ in range(500):\n",
+    "    add_livre(mariaDB, nom_livre_random(), \"Hachette\", random.randint(1,5), random.randint(1900, 2020), [\"Science\", \"Nature\"], ISBN_random(), nom_auteur_random())\n",
+    "    n = random.randint(1, 100)\n",
+    "    add_periodique(mariaDB, n, nom_periodique_random(), random.randint(1,5), random.randint(1900 + n//10, 2000+n//10))\n",
+    "    add_rapport(mariaDB, nom_rapport_random(), random.randint(1900, 2020), nom_auteur_random())"
+   ]
   }
  ],
  "metadata": {
diff --git a/util.py b/util.py
new file mode 100644
index 0000000..757e959
--- /dev/null
+++ b/util.py
@@ -0,0 +1,47 @@
+def add_livre(mariaDB, titre, editeur, edition, annee, categories, ISBN, auteur):
+    assert len(categories) <= 4
+    if (len(categories) < 4):
+        categories += [None] * (4 - len(categories))
+        # some padding
+    mariaDB.begin()
+    cur = mariaDB.cursor()
+    cur.execute("INSERT INTO LIVRE (titre, ISBN, editeur, edition, annee_de_publication, categorie1, categorie2, categorie3, categorie4) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", (titre, ISBN, editeur, edition, annee, categories[0], categories[1], categories[2], categories[3]))
+    cur.execute("INSERT INTO PUBLICATION (ISBN, type) VALUES (?, 0)", (ISBN,))
+    cur.execute("INSERT INTO Auteur (nom) VALUES (?) ON DUPLICATE KEY UPDATE nom = ?", (auteur, auteur))
+    cur.execute("INSERT INTO ecrit_par (ISBN, nom) VALUES (?, ?)", (ISBN, auteur))
+    mariaDB.commit()
+    return cur.lastrowid
+
+def add_rapport(mariaDB, titre, annee, auteur):
+    mariaDB.begin()
+    cur = mariaDB.cursor()
+    cur.execute("INSERT INTO RAPPORT (titre, annee_de_publication) VALUES (?, ?)", (titre, annee))
+    id_rap = cur.lastrowid
+    cur.execute("INSERT INTO PUBLICATION (ID_RAP, type) VALUES (?, 1)", (id_rap,))
+    cur.execute("INSERT INTO Auteur (nom) VALUES (?) ON DUPLICATE KEY UPDATE nom = ?", (auteur, auteur))
+    cur.execute("INSERT INTO redige_par (ID_RAP, nom) VALUES (?, ?)", (id_rap, auteur))
+    mariaDB.commit()
+    return cur.lastrowid
+
+def add_periodique(mariaDB, numero, editeur, edition, annee):
+    mariaDB.begin()
+    cur = mariaDB.cursor()
+    cur.execute("INSERT INTO PERIODIQUE (numero, editeur, edition, annee_de_publication) VALUES (?, ?, ?, ?)", (numero, editeur, edition, annee))
+    cur.execute("INSERT INTO PUBLICATION (ID_PER, type) VALUES (?, 2)", (cur.lastrowid,))
+    mariaDB.commit()
+    return cur.lastrowid
+
+def get_livre(mariaDB, ISBN):
+    cur = mariaDB.cursor()
+    cur.execute("SELECT titre, LIVRE.ISBN, editeur, edition, annee_de_publication, categorie1, categorie2, categorie3, categorie4, A.nom FROM LIVRE JOIN MAIN.ecrit_par ep on LIVRE.ISBN = ep.ISBN JOIN MAIN.Auteur A on A.nom = ep.nom JOIN MAIN.PUBLICATION P on LIVRE.ISBN = P.ISBN WHERE ISBN = ?", (ISBN,))
+    return cur.fetchone()
+
+def get_rapport(mariaDB, ID_RAP):
+    cur = mariaDB.cursor()
+    cur.execute("SELECT titre, annee_de_publication, A.nom FROM RAPPORT JOIN MAIN.redige_par rp on RAPPORT.ID_RAP = rp.ID_RAP JOIN MAIN.Auteur A on A.nom = rp.nom JOIN MAIN.PUBLICATION P on RAPPORT.ID_RAP = P.ID_RAP WHERE RAPPORT.ID_RAP = ?", (ID_RAP,))
+    return cur.fetchone()
+
+def get_periodique(mariaDB, ID_PER):
+    cur = mariaDB.cursor()
+    cur.execute("SELECT numero, editeur, edition, annee_de_publication FROM PERIODIQUE JOIN MAIN.PUBLICATION P on PERIODIQUE.ID_PER = P.ID_PER WHERE PERIODIQUE.ID_PER = ?", (ID_PER,))
+    return cur.fetchone()
\ No newline at end of file
-- 
GitLab