Skip to content

Instantly share code, notes, and snippets.

@ckob
Created May 19, 2016 15:10
Show Gist options
  • Save ckob/0862ea246402ed0e21086c6fcd0be431 to your computer and use it in GitHub Desktop.
Save ckob/0862ea246402ed0e21086c6fcd0be431 to your computer and use it in GitHub Desktop.
/*
Examen PLpgSQL
Carles Koch Busquets
*/
drop function afegirservei ( integer, integer, integer) ;
drop function checkin ( character varying) ;
drop function checkout ( integer) ;
drop trigger tcheckIn on reserva ;
drop function fcheckIn() ;
drop function altareserva ( character varying, date, date, integer, character varying) ;
/*
Funció afegirServei
Arguments: codi del servei consumit, quantitat i habitació.
Dóna d'alta un servei donat un moment en el temps.
Retorna: Un missatge amb el nom del client, habitació, servei (descripció) i preu.
*/
create or replace function afegirServei(p_idservei int, p_quantitat int, p_numhab int)
returns text
as
$$
declare
v_cod_client smallint;
v_nom_client text;
v_descripcio_servei text;
v_preu numeric;
v_basura char(1);
begin
select codclient
into v_cod_client
from reserva
where numhab = p_numhab and facturada = 'N' and datasortida >= current_date;
if not found then
raise exception 'Aquesta habitació no está reservada actualment';
end if;
select nom||' '||cognom1||' '||cognom2
into v_nom_client
from client
where codclient = v_cod_client;
if not found then
raise exception 'Habitació amb client incorrecte.';
end if;
select descripcio, preu*p_quantitat
into v_descripcio_servei, v_preu
from servei
where p_idservei = idservei;
if not found then
raise exception 'Codi de servei incorrecte.';
end if;
select 'a'
into v_basura
from serveihabitacio
where numhab = p_numhab and p_idservei = idservei and data = current_date;
if found then -- Si troba que ja hi ha un servei d'aquest tipus el mateix dia i habitació...
update serveihabitacio
set quantitat = quantitat + p_quantitat
where numhab = p_numhab and p_idservei = idservei and data = current_date;
else
insert into serveihabitacio
values (p_numhab, p_idservei, current_date, p_quantitat);
end if;
return 'S''ha introduït un servei de '|| v_descripcio_servei ||' per al client '||v_nom_client||' de l''habitació '||p_numhab||' per un total de '||v_preu||'';
end;
$$
language plpgsql;
/*
Funció checkIn.
Paràmeters: dni
Retorna: cadena de text informant del nom del soci i habitació que li pertoca.
Tasca: canviarà el camp ocupada a S
*/
create or replace function checkIn (p_dni varchar(9))
returns text
as
$$
declare
v_cod_client smallint;
v_nom_client text;
v_num_habitacio smallint;
v_num_reserva smallint;
begin
select codclient, nom||' '||cognom1||' '||cognom2
into v_cod_client, v_nom_client
from client
where upper(dni) = upper(p_dni);
if not found then
raise exception 'No existeix cap client amb aquest DNI';
end if;
select numhab, numreserva
into v_num_habitacio, v_num_reserva
from reserva
where codclient = v_cod_client and dataarribada = current_date and upper(ocupada) = 'N';
if not found then
raise exception 'Aquest client no te cap reserva per avui';
end if;
update reserva
set ocupada = 'S'
where numreserva = v_num_reserva;
return 'El client '||v_nom_client||' ja pot anar a l''habitació '||v_num_habitacio;
end;
$$
language plpgsql;
/*
Funció : checkOut
paràmetres: habitació
cal modificar el camp facturada a S.
Quan marxi el client, a l'import de l'estada se li ha d’afegir els consums que hagi realitzat durant el temps que hi ha estat.
*/
create or replace function checkOut (p_numhab int)
returns text
as
$$
declare
v_import_serveis smallint;
v_num_reserva smallint;
v_dataarribada date;
v_datasortida date;
begin
select numreserva, dataarribada, datasortida
into v_num_reserva, v_dataarribada, v_datasortida
from reserva
where numhab = p_numhab and datasortida = current_date and facturada = 'N';
if not found then
raise exception 'Aquesta habitació no te cap checkout per avui o ja s''ha realitzat';
end if;
select (sum(preu*quantitat)::smallint)
into v_import_serveis
from serveihabitacio sh, servei s
where numhab = 102 and data>=v_dataarribada and data<=v_datasortida and sh.idservei = s.idservei;
update factura
set import = import + v_import_serveis;
update reserva
set facturada = 'S'
where numreserva = v_num_reserva;
return 'Checkout realitzat correctament. La factura ja está disponible al apartat corresponent.';
end;
$$
language plpgsql;
/*
trigger: tcheckIn
S'executa quan es modifica el camp ocupada passa de N a S.
Generarà una nova factura on import es el preu total de l'estada basat en funció del nombre de dies, tipus d'estada i tipus d'habitació.
*/
create or replace function fcheckIn()
returns trigger
as
$$
begin
declare
v_dies smallint;
v_preu_hab smallint;
v_preu_tipus smallint;
begin
v_dies:=new.datasortida-new.dataarribada;
select (preu*v_dies)::smallint
into v_preu_tipus
from tipusestada
where codestada = new.codestada;
select (preu*v_dies)::smallint
into v_preu_hab
from reserva r, habitacio h, tipushab t
where r.numreserva = new.numreserva and r.numhab = h.numhab and h.codtipus = t.codtipus;
insert into factura
values (nextval('seq_numfra'), new.numreserva, current_date, v_preu_tipus+v_preu_hab);
return new;
end;
end;
$$
language plpgsql;
create trigger tcheckIn
after update of ocupada on reserva
for each row
when (old.ocupada ='N' and new.ocupada = 'S')
execute procedure fcheckIn();
/*
funció altaReserva
paràmetres: dni client, data arribada, data_sortida, tipus d’habitacio (simple, doble, …) i tipus d’estada (SA, AD, MP, PC)
Tasca: Per simplificar codi suposem que el client ja existeix.
Crea una nova reserva en base als criteris i assigna la primera habitació disponible que trobi.
Retorna: Segons si troba habitació disponible o no seguint els criteris, els missatge serà ”Habitació XXX assignada al client NomClient” o ”Habitació no disponible segons dates/modalitat. Canvia els criteris, si us plau”.
*/
create or replace function altaReserva (p_dni varchar(9), p_data_arribada date, p_data_sortida date, p_tipus_habitacio int, p_tipus_estada varchar(2))
returns text
as
$$
declare
v_num_habitacio smallint;
v_cod_client smallint;
v_basura char(1);
v_nom_client text;
begin
select max(h.numhab)
into v_num_habitacio
from habitacio h
where codtipus = p_tipus_habitacio and h.numhab not in(select r.numhab
from reserva r
where r.numhab = h.numhab and p_data_arribada between dataarribada and datasortida or p_data_sortida between dataarribada and datasortida);
if not found or v_num_habitacio is null then
raise exception 'Habitació no disponible segons dates/modalitat. Canvia els criteris, si us plau';
end if;
select codclient, nom||' '||cognom1||' '||cognom2
into v_cod_client, v_nom_client
from client
where dni = p_dni;
if not found then
raise exception 'No existeix cap client amb aquest dni.';
end if;
select 'a'
into v_basura
from tipusestada
where codestada = p_tipus_estada;
if not found then
raise exception 'Aquest tipus d''estada no existeix';
end if;
insert into reserva
values (nextval('seq_numreserva'), v_cod_client, current_date, p_data_arribada, p_data_sortida, v_num_habitacio, p_tipus_estada, 'N', 'N');
return 'Habitació '|| v_num_habitacio ||' assignada al client '||v_nom_client;
end;
$$
language plpgsql;
/*
\i '/run/media/iam47912794/LinuxExt4/M02-Bases de dades/UF4 (funcions)/Examen/KochCarles.sql'
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment