2 --TRUNCATE TABLE "Vente";
3 --TRUNCATE TABLE "Panier";
4 DROP PROCEDURE insertVente
IF EXISTS CASCADE;
5 CREATE PROCEDURE insertVente
7 , IN venteClient
INTEGER
8 , IN venteProduit
INTEGER
9 , IN venteQuantité
DECIMAL
13 DECLARE panierID
INTEGER DEFAULT NULL;
14 DECLARE venteID
INTEGER DEFAULT NULL;
16 -- CorrectnessNote: seule la date et le client
17 -- font partie des critères pour décider
18 -- si le panier existe déjà où non.
19 SET panierID
= SELECT ID FROM "Panier"
20 WHERE "Panier".
date = venteDate
21 AND "Panier".client
= venteClient
;
24 INSERT INTO "Panier"(date, client
)
25 VALUES (venteDate
, venteClient
);
26 SET panierID
= IDENTITY();
29 -- CorrectnessNote: la quantité ne fait pas partie des critères
30 -- pour décider si la vente existe déjà ou non.
31 SET venteID
= SELECT ID FROM "Vente"
32 WHERE "Vente".panier
= panierID
33 AND "Vente".produit
= venteProduit
;
36 INSERT INTO "Vente"(panier
, produit
, quantité
)
37 VALUES (panierID
, venteProduit
, venteQuantité
);
42 DROP PROCEDURE insertVentesParAbonnement
IF EXISTS;
43 CREATE PROCEDURE insertVentesParAbonnement(IN finalDate
DATE)
46 -- ExplanationNote: gère chaque jour l'un après l'autre.
49 FROM UNNEST (SEQUENCE_ARRAY(DATE '2025-04-07', finalDate
, 1 DAY))
50 WITH ORDINALITY as T(day, I
)
52 -- ExplanationNote: pour un jour donné,
53 -- gère chaque abonnement l'un après l'autre.
55 FOR SELECT ID as aboID
57 , produit
as aboProduit
58 , quantité
as aboQuantité
61 JOIN "Période" ON "Période".
ID = "Abonnement".période
62 AND "Période".nom
= DAYNAME(day)
64 CALL insertVente(day, aboClient
, aboProduit
, aboQuantité
);
70 CALL insertVentesParAbonnement(DATE '2025-04-07' + 7 DAY);
74 --call prix_à_date(CURRENT_DATE);
75 select "Prod".nom_complet
, date, euros
76 from table (prix_à
_date(CURRENT_DATE)) as PD
77 join table (choisirUnProduit()) as "Prod" ON "Prod".
ID = produit