Advertisement
Guest User

nouvel emprunt

a guest
Jan 9th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION vallee.newemprunt(integer, integer, integer)
  2.  RETURNS integer
  3.  LANGUAGE plpgsql
  4. AS $function$
  5.     declare
  6.         res integer;
  7.         nbExemplaire integer;
  8.         nbLecteur integer;
  9.         nbEmprunteur integer;
  10.         nbLecteurDejaEmprunte integer;
  11.     begin
  12.         res = 0;
  13.         select into nbExemplaire count(numero) from exemplaire where idlivre = $3;
  14.         if nbExemplaire = 0 then
  15.         res = 1;
  16.    
  17.             select into nbLecteur count(idelecteur)  from lecteur where idelecteur = $2;
  18.             if nbLecteur = 0 then
  19.             res = 2;
  20.             end if;
  21.         end if;
  22.        
  23.         select into nbEmprunteur count(numero) from exemplaire where numero = $1 and idlivre = $3 and dateretour is null;
  24.         if nbEmprunteur = 0 then
  25.         res = 3;
  26.             select into nbEmprunteur count(numero) from exemplaire where emprunteur = $2;
  27.             if nbEmprunteur >= 1 then
  28.             res = 4;
  29.             end if;
  30.         end if;
  31.                    
  32.         if res = 0 then
  33.         update exemplaire set dateretour = date(now())+15, emprunteur = $2
  34.         where numero = $1
  35.         and idlivre = $3;
  36.         end if;
  37.         return res;
  38.     end;
  39. $function$
  40. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement