Commit 8ef7ac26 authored by Dimitri Nicolas's avatar Dimitri Nicolas
Browse files

Merge branch 'menu_python' of https://gitlab.utc.fr/dnicolas/ai23-projet into menu_python

Branch merged
parents 9abf7bc6 8d5a4f8f
import psycopg2
import string
from DataMapper import *
class PretDataMapper(DataMapper):
def creerPret(self, login, ressources : list, duree, date) :
# vérifier que l'adhérent ne soit pas suspendu
# l'adhérent n'a pas accès au système s'il est blacklisté
connection = self.connect()
try:
cursor : psycopg2.extensions.cursor = connection.cursor()
cursor.execute("SELECT * FROM sanction s WHERE s.adherent = '{0}'").format(login)
row = cursor.fetchone()
if row != None:
print('L\'adhérent est suspendu')
sql1 = "INSERT INTO Pret(date, duree, adherent) VALUES ({0}, '{1}', '{2}') RETURNING id".format(date, duree, codeUnique)
cursor.execute(sql1)
row = cursor.fetchone()
pretID = row[0]
for codeUnique in ressources :
cursor.execute("SELECT e.ref FROM Exemplaire e WHERE e.ressource = '{0}' and e.disponible = true and (e.etat = 'Neuf' OR e.etat = 'Bon')").format(codeUnique)
row = cursor.fetchone()
if row != None:
print('La ressource ne peut pas être empruntée')
else :
ref = row[0]
sql2 = "INSERT INTO exemplairepret(pret, ref) VALUES ({0}, '{1}')".format(pretID, ref)
cursor.execute(sql2)
cursor.execute("UPDATE Exemplaire SET disponible = false WHERE ref = '{0}'".format(ref))
connection.commit()
except Exception as err:
print(err)
finally:
connection.close()
def retourPret(self, pretID) :
connection = self.connect()
try:
cursor : psycopg2.extensions.cursor = connection.cursor()
cursor.execute("SELECT ep.ref FROM ExemplairePret ep WHERE ep.pret = {0}".format(pretID))
rows = cursor.fetchall()
for row in rows :
ref = row[0]
cursor.execute("UPDATE Exemplaire SET disponible = true WHERE ref = '{0}'".format(ref))
connection.commit()
except Exception as err:
print(err)
finally:
connection.close()
def getExemplairesPret(self, pretID) :
connection = self.connect()
try:
cursor : psycopg2.extensions.cursor = connection.cursor()
cursor.execute("SELECT * FROM ExemplairePret ep WHERE ep.pret = {0}".format(pretID))
rows = cursor.fetchall()
return rows
except Exception as err:
print(err)
finally:
connection.close()
def miseAJourEtatExemplaire(self, ref, etat) :
connection = self.connect()
try:
cursor : psycopg2.extensions.cursor = connection.cursor()
cursor.execute("UPDATE Exemplaire SET etat = '{0}' WHERE ref = '{1}'".format(etat, ref))
connection.commit()
except Exception as err:
print(err)
finally:
connection.close()
......@@ -8,7 +8,7 @@ class StatistiquesDataMapper(DataMapper):
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT r.titre, COUNT(p.ref) nb_emprunt FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref GROUP BY r.titre HAVING COUNT(ep.ref) > 1 ORDER BY COUNT(ep.ref) DESC"
sql1 = "SELECT rp.titre, rp.nb_emprunt FROM RessourcesPopulaires rp"
cursor.execute(sql1)
rows = cursor.fetchall()
return rows
......@@ -24,7 +24,7 @@ class StatistiquesDataMapper(DataMapper):
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT COUNT(ep.ref) nb_emprunt FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref WHERE r.codeUnique = '{0}' HAVING COUNT(ep.ref) > 1 ORDER BY COUNT(ep.ref) DESC".format(codeUnique)
sql1 = "SELECT rp.titre, rp.nb_emprunt FROM RessourcesPopulaires rp WHERE rp.codeUnique = '{0}'".format(codeUnique)
cursor.execute(sql1)
ressource = cursor.fetchone()
return ressource[0]
......@@ -39,7 +39,7 @@ class StatistiquesDataMapper(DataMapper):
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT r.genre, COUNT(ep.ref) nb_emprunt FROM Ressource r JOIN Exemplaire ep ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref GROUP BY r.genre HAVING COUNT(ep.ref) > 1 ORDER BY COUNT(ep.ref) DESC"
sql1 = "SELECT gp.titre, gp.nb_emprunt FROM GenresPopulaires gp"
cursor.execute(sql1)
rows = cursor.fetchall()
return rows
......@@ -54,7 +54,7 @@ class StatistiquesDataMapper(DataMapper):
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT COUNT(ep.ref) nb_emprunt FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref WHERE r.genre = '{0}' GROUP BY r.genre HAVING COUNT(ep.ref) > 1 ORDER BY COUNT(ep.ref) DESC".format(genre)
sql1 = "SELECT gp.titre, gp.nb_emprunt FROM GenresPopulaires gp WHERE gp.genre = '{0}' GROUP BY gp.genre".format(genre)
cursor.execute(sql1)
ressource = cursor.fetchone()
return ressource[1]
......@@ -69,10 +69,10 @@ class StatistiquesDataMapper(DataMapper):
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT r.genre, nb_emprunt COUNT(ep.ref) FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref JOIN Pret p ON p.pretID = ep.pretID WHERE p.adherent = '{0}' GROUP BY r.genre HAVING COUNT(ep.ref) > 1 ORDER BY COUNT(ep.ref) DESC".format(login)
sql1 = "SELECT gppa.genre, gppa.nb_emprunt FROM GenrePrefereParAdherent gppa WHERE gppa.login = '{0}'".format(login)
cursor.execute(sql1)
genre = cursor.fetchall()
return genre[0][0] # retourne le premier genre
return genre
except Exception as err:
print(err)
finally:
......@@ -100,13 +100,13 @@ class StatistiquesDataMapper(DataMapper):
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT COUNT(ep.ref) FROM Livre l JOIN Ressource r ON l.codeUnique = r.codeUnique JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.refExemplaire = e.ref"
sql1 = "SELECT lp.nb_emprunt FROM LivresPopulaire lp"
cursor.execute(sql1)
nbLivres = cursor.fetchone()
sql2 = "SELECT COUNT(ep.ref) FROM OeuvreMusicale om JOIN Ressource r ON om.codeUnique = r.codeUnique JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.refExemplaire = e.ref"
sql2 = "SELECT omp.nb_emprunt FROM OeuvresMusicalesPopulaire omp"
cursor.execute(sql2)
nbOeuvres = cursor.fetchone()
sql3 = "SELECT COUNT(ep.ref) FROM Film f JOIN Ressource r ON f.codeUnique = r.codeUnique JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.refExemplaire = e.ref"
sql3 = "SELECT fp.nb_emprunt FROM FilmsPopulaires fp"
cursor.execute(sql3)
nbFilms = cursor.fetchone()
if(nbLivres[0] > nbFilms[0] and nbLivres[0] > nbOeuvres[0]) :
......@@ -119,5 +119,83 @@ class StatistiquesDataMapper(DataMapper):
print(err)
finally:
conn.close()
# pour connaître le nom de la table d'où proviennent les données :
# select v.tableoid::regclass from ville v
def dureeMoyenneEmpruntParAdherent(self, login) :
#DureeMaxEmpruntParAdherent
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT dmepa.dureeMoyenneEmprunt FROM DureeMoyenneEmpruntParAdherent dmepa WHERE dmepa.login = '{0}'".format(login)
cursor.execute(sql1)
duree = cursor.fetchone()
return duree[0]
except Exception as err:
print(err)
finally:
conn.close()
def dureeMoyenneEmprunt(self) :
#DureeMaxEmpruntParAdherent
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT AVG(duree) FROM Pret ORDER BY AVG(duree) DESC"
cursor.execute(sql1)
duree = cursor.fetchone()
return duree[0]
except Exception as err:
print(err)
finally:
conn.close()
def dureeMaxEmpruntParAdherent(self, login) :
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT dmepa.dureeEmpruntMax FROM DureeMaxEmpruntParAdherent dmepa WHERE dmepa.login = '{0}'".format(login)
cursor.execute(sql1)
duree = cursor.fetchone()
return duree[0]
except Exception as err:
print(err)
finally:
conn.close()
def dureeMaxPret(self) :
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT MAX(duree) FROM Pret ORDER BY MAX(duree) DESC"
cursor.execute(sql1)
durees = cursor.fetchall()
return durees
except Exception as err:
print(err)
finally:
conn.close()
def dureeMoyenneEmpruntParRessource(self, codeUnique) :
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT dmepr.dureeMoyenneEmprunt FROM DureeMoyenneEmpruntParRessource dmepr WHERE dmepr.codeUnique = '{0}'".format(codeUnique)
cursor.execute(sql1)
duree = cursor.fetchone()
return duree[0]
except Exception as err:
print(err)
finally:
conn.close()
def dureeMaxEmpruntParRessource(self, codeUnique) :
conn = self.connect()
try:
cursor : psycopg2.extensions.cursor = conn.cursor()
sql1 = "SELECT dmepr.dureeEmpruntMax FROM DureeMaxEmpruntParRessource dmepr WHERE dmepr.codeUnique = '{0}'".format(codeUnique)
cursor.execute(sql1)
duree = cursor.fetchone()
return duree[0]
except Exception as err:
print(err)
finally:
conn.close()
......@@ -18,4 +18,20 @@ DROP TABLE IF EXISTS sanction CASCADE;
DROP TABLE IF EXISTS suspension CASCADE;
DROP TABLE IF EXISTS utilisateur CASCADE;
DROP VIEW IF EXISTS Exemplaires_Disponibles;
DROP VIEW IF EXISTS Repartition_Emprunts;
\ No newline at end of file
DROP VIEW IF EXISTS Repartition_Emprunts;
DROP VIEW IF EXISTS VLivre;
DROP VIEW IF EXISTS VOeuvreMusicale;
DROP VIEW IF EXISTS VFilm;
DROP VIEW IF EXISTS VAdherent;
DROP VIEW IF EXISTS VPersonnel;
DROP VIEW IF EXISTS RessourcesPopulaires;
DROP VIEW IF EXISTS GenresPopulaires;
DROP VIEW IF EXISTS LivresPopulaire;
DROP VIEW IF EXISTS OeuvresMusicalesPopulaire
DROP VIEW IF EXISTS FilmsPopulaires
DROP VIEW IF EXISTS DureeMaxEmpruntParAdherent
DROP VIEW IF EXISTS DureeMoyenneEmpruntParAdherent
DROP VIEW IF EXISTS DureeMaxEmpruntParRessource
DROP VIEW IF EXISTS DureeMoyenneEmpruntParRessource
DROP VIEW IF EXISTS GenrePrefereParAdherent
......@@ -13,61 +13,84 @@ CREATE VIEW Repartition_Emprunts AS
FROM exemplaire
JOIN exemplairepret ON exemplaire.ref = exemplairepret.ref
GROUP BY "category";
CREATE OR REPLACE VIEW VLivre AS
SELECT r.codeunique codeunique, r.titre titre, r.dateapparition dateapparition, r.editeur editeur, r.genre genre, r.codeclassification codeclassification, l.isbn isbn, l.resume resume, l.langue langue
FROM Ressource r JOIN Livre l ON l.codeUnique = r.codeUnique;
CREATE OR REPLACE VIEW VOeuvreMusicale AS
SELECT r.codeunique codeunique, r.titre titre, r.dateapparition dateapparition, r.editeur editeur, r.genre genre, r.codeclassification codeclassification, om.longueur longueur
FROM OeuvreMusicale om JOIN Ressource r ON om.codeUnique = r.codeUnique;
CREATE OR REPLACE VIEW VFilm AS
SELECT r.codeunique codeunique, r.titre titre, r.dateapparition dateapparition, r.editeur editeur, r.genre genre, r.codeclassification codeclassification, f.longueur longueur, f.synopsis synopsis
FROM Film f JOIN Ressource r ON f.codeUnique = r.codeUnique;
CREATE OR REPLACE VIEW VAdherent AS
SELECT u.login login, u.motdepasse motdepasse, u.nom nom, u.prenom prenom, u.adresse adresse, u.mail mail, a.numcarte numcarte, a.datenaissance datenaissance, a.numtel numtel, a.dateadhesion dateadhesion, a.blacklisted blacklisted
FROM Utilisateur u JOIN Adherent a ON u.login = a.login;
CREATE OR REPLACE VIEW VPersonnel AS
SELECT u.login login, u.motdepasse motdepasse, u.nom nom, u.prenom prenom, u.adresse adresse, u.mail mail
FROM Utilisateur u JOIN Personnel p ON u.login = p.login;
CREATE OR REPLACE VIEW RessourcesPopulaires AS
SELECT r.titre, COUNT(ep.ref) nb_emprunt
SELECT r.titre titre, r.codeUnique codeUnique, COUNT(ep.ref) nb_emprunt
FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY r.titre
GROUP BY r.titre, r.codeUnique
HAVING COUNT(ep.ref) > 2
ORDER BY COUNT(ep.ref) DESC;
CREATE OR REPLACE VIEW GenresPopulaires AS
SELECT r.genre, COUNT(ep.ref) nb_emprunt
SELECT r.genre genre, COUNT(ep.ref) nb_emprunt
FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY r.genre
HAVING COUNT(ep.ref) > 2
ORDER BY COUNT(ep.ref) DESC;
CREATE OR REPLACE VIEW LivresPopulaire AS
SELECT r.titre, COUNT(ep.ref) nb_emprunt
FROM Livre l JOIN Ressource r ON l.codeUnique = r.codeUnique JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY r.titre
SELECT vl.titre titre, vl.codeUnique codeUnique, COUNT(ep.ref) nb_emprunt
FROM VLivre vl JOIN Exemplaire e ON e.ressource = vl.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY vl.titre, vl.codeUnique
HAVING COUNT(ep.ref) > 2
ORDER BY COUNT(ep.ref) DESC;
CREATE OR REPLACE VIEW OeuvresMusicalesPopulaire AS
SELECT r.titre, COUNT(ep.ref) nb_emprunt
FROM OeuvreMusicale om JOIN Ressource r ON om.codeUnique = r.codeUnique JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY r.titre
SELECT vom.titre titre, vom.codeUnique codeUnique, COUNT(ep.ref) nb_emprunt
FROM VOeuvreMusicale vom JOIN Exemplaire e ON e.ressource = vom.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY vom.titre, vom.codeUnique
HAVING COUNT(ep.ref) > 2
ORDER BY COUNT(ep.ref) DESC;
CREATE OR REPLACE VIEW FilmsPopulaires AS
SELECT r.titre, COUNT(ep.ref) nb_emprunt
FROM Film f JOIN Ressource r ON f.codeUnique = r.codeUnique JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY r.titre
SELECT vf.titre titre, vf.codeUnique codeUnique, COUNT(ep.ref) nb_emprunt
FROM VFilm vf JOIN Exemplaire e ON e.ressource = vf.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref
GROUP BY vf.titre, vf.codeUnique
HAVING COUNT(ep.ref) > 2
ORDER BY COUNT(ep.ref) DESC;
CREATE OR REPLACE VIEW DureeMaxEmpruntParAdherent AS
SELECT u.nom, MAX(duree) dureeEmpruntMax
FROM Adherent a JOIN Pret p ON p.adherent = a.login JOIN Utilisateur u ON u.login = a.login
GROUP BY u.nom
SELECT va.nom, va.login login, MAX(duree) dureeEmpruntMax
FROM VAdherent va JOIN Pret p ON p.adherent = va.login
GROUP BY va.nom, va.login
ORDER BY MAX(duree) DESC;
CREATE OR REPLACE VIEW DureeMoyenneEmpruntParAdherent AS
SELECT u.nom, AVG(duree) dureeMoyenneEmprunt
FROM Adherent a JOIN Pret p ON p.adherent = a.login JOIN Utilisateur u ON u.login = a.login
GROUP BY u.nom
SELECT va.nom nom, va.login login, AVG(duree) dureeMoyenneEmprunt
FROM VAdherent va JOIN Pret p ON p.adherent = va.login
GROUP BY va.nom, va.login
ORDER BY AVG(duree) DESC;
CREATE OR REPLACE VIEW DureeMaxEmpruntParRessource AS
SELECT r.titre, MAX(duree) dureeEmpruntMax
SELECT r.titre, r.codeUnique codeUnique, MAX(duree) dureeEmpruntMax
FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref JOIN Pret p ON p.pretID = ep.pret
GROUP BY r.titre
GROUP BY r.titre, r.codeUnique
ORDER BY MAX(duree) DESC;
CREATE OR REPLACE VIEW DureeMoyenneEmpruntParRessource AS
SELECT r.titre, AVG(duree) dureeMoyenneEmprunt
SELECT r.titre, r.codeUnique codeUnique, AVG(duree) dureeMoyenneEmprunt
FROM Ressource r JOIN Exemplaire e ON e.ressource = r.codeUnique JOIN ExemplairePret ep ON ep.ref = e.ref JOIN Pret p ON p.pretID = ep.pret
GROUP BY r.titre
GROUP BY r.titre, r.codeUnique
ORDER BY AVG(duree) DESC;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment