Sunteți pe pagina 1din 18

PL/SQL Obiectual

Radu Alina Mirela

In prezentul proiect am folosit o baza de date cu 4 tabele ce reprezinta gestiunea unui hotel. 1. Schema conceptuala a bazei de date:

2. Crearea tipurilor de obiecte Pentru implementarea clase Cclient avem urmatoarea implementare:

create or replace TYPE client_o AS OBJECT ( id_client NUMBER, nume VARCHAR2(50), prenume VARCHAR2(50), cnp VARCHAR2(13), adresa VARCHAR(50), oras VARCHAR(50), MEMBER FUNCTION calculeaza_plata RETURN NUMBER, STATIC FUNCTION calculeaza_statistici(oras VARCHAR2) RETURN NUMBER );

Crearea tabelei clienti pe baza definitiei clasei client_o.

Pentru implementarea clasei CSejur avem urmatoarea implementare:

create or replace TYPE sejur_o AS OBJECT ( id_sejur NUMBER, data_inc DATE, nr_nopti NUMBER, id_client NUMBER, id_camera NUMBER, STATIC FUNCTION calculeaza_statistici(c_id NUMBER) RETURN NUMBER );

Crearea tabelei sejur pe baza definitiei clasei sejur_o.

Pentru implementarea clasei CCamera avem urmatoarea implementare:

create or replace TYPE camera_o AS OBJECT ( id_camera NUMBER, etaj NUMBER, tip VARCHAR2(30), pret NUMBER, STATIC FUNCTION nrtotal_camere RETURN NUMBER );

Crearea tabelei camere pe baza definitiei clasei camera_o.

Pentru implementarea clasei CFactura avem urmatoarea implementare

create or replace TYPE factura_o AS OBJECT ( id_factura NUMBER, valoare NUMBER, id_client NUMBER, id_angajat NUMBER );

Crearea tabelei factura pe baza definitiei clasei factura_o

Pentru implementarea clasei CAngajat avem urmatoarea implementare:

create or replace TYPE angajat_o AS OBJECT ( id_angajat NUMBER, nume VARCHAR2(50), prenume VARCHAR2(50), cnp VARCHAR2(13), telefon VARCHAR2(10), salariu NUMBER, vechime NUMBER, data_angajarii DATE, functie VARCHAR2(50), MEMBER FUNCTION calculeaza_bonus RETURN NUMBER, MEMBER FUNCTION calculeaza_taxe RETURN NUMBER, MEMBER FUNCTION calculeaza_venituri_totale RETURN NUMBER );

Crearea tabelei angajati pe baza definitiei clasei angajat_o.

Popularea tabelelor cu date: Camere: INSERT INTO CAMERE(Id_camera,Etaj,Tip,Pret) VALUES('01', '1','Single','70');

INSERT INTO CAMERE(Id_camera, Etaj , Tip , Pret) VALUES('02', '1','Single','75'); INSERT INTO CAMERE(Id_camera, Etaj , Tip , Pret) VALUES('03', '1','Double','95');

INSERT INTO CAMERE(Id_camera, Etaj , Tip , Pret) VALUES('04', '2','Double','90'); INSERT INTO CAMERE(Id_camera, Etaj , Tip , Pret) VALUES('05', '1','Apartment','150'); INSERT INTO CAMERE(Id_camera, Etaj , Tip , Pret) VALUES('06', '1','Double','95');

Clienti

INSERT INTO CLIENTI(id_client, nume, prenume, cnp, adresa, oras) VALUES('01', 'Abalasei','Ioan', '1781216070890','Str. Savenilor 27, bl M1, Botosani', 'Botosani');

INSERT INTO CLIENTI(id_client, nume, prenume, cnp, adresa, oras) VALUES('02', 'Braham','Matei', '1781216070899','Str. Constructorilor 27, bl M1, Slobozia', 'Slobozia'); INSERT INTO CLIENTI(id_client, nume, prenume, cnp, adresa, oras) VALUES('03', 'Condrea','Andreea', '2871216026490','Str. Mihai Bravu, bl A1, Bucuresti', 'Bucuresti'); INSERT INTO CLIENTI(id_client, nume, prenume, cnp, adresa, oras) VALUES('04', 'Cristea','Anca', '2781216070890','Str. Padesu 14, bl B3, Bucuresti', 'Bucuresti');

INSERT INTO CLIENTI(id_client, nume, prenume, cnp, adresa, oras) VALUES('05', 'Coman','Alina', '2781216070119','Str. Padesu 14, bl B3, Bucuresti', 'Bucuresti'); Sejur

INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('001',to_date('24-12-2009', 'dd-mm-yyyy'),'5', '01', '01'); INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('002',to_date('25-12-2009', 'dd-mm-yyyy'),'3','02','02'); INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('003', to_date('29-12-2009', 'dd-mm-yyyy'), '4','03', '01'); INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('004', to_date('01-01-2010', 'dd-mm-yyyy'),'5','04','04'); INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('005', to_date('02-01-2010', 'dd-mm-yyyy'),'7','05','06'); INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('006', to_date('13-06-2008', 'dd-mm-yyyy'),'5','03','06'); INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('007', to_date('15-07-2008', 'dd-mm-yyyy'),'7','05','01'); INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('008', to_date('13-06-2008', 'dd-mm-yyyy'),'5','04','02');

INSERT INTO sejur(id_sejur,data_inc,nr_nopti, id_client, id_camera) VALUES('009', to_date('12-05-2009', 'dd-mm-yyyy'),'4','03','06'); Factura

INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat) VALUES('001','350','01','003'); INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('002','225','02','003')); INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('003','420','03','001')); INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('004','270','04','006'); INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('005','285','05','003'); INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('006','300','03','003'); INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('007','450','05','001');

INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('008','390','04','001'));

INSERT INTO FACTURA(id_factura, valoare, id_client,id_angajat ) VALUES('009','280','03','001'); Angajati

insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('001','Buliga','Ramona','275021207138','0725663322','1500','10',to_date('21-09-1989', 'dd-mm-yyyy'),'Operator receptie'); insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('002','Popescu','George','176030411196','0724259988','2700','12',to_date('13-052000', 'dd-mm-yyyy'),'Director'); insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('003','Popa','Maria','273030512197','0724326596','1500','5',to_date('05-06-1986', 'ddmm-yyyy'), 'Operator receptie');

insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('004','Toma','Ion','187050711294','0726332269','850','7',to_date('10-06-2009', 'ddmm-yyyy'),'Ospatar'); insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('005','Luca','Mircea','185090211234','0726368569','1000','20',to_date('06-06-2002', 'dd-mm-yyyy'),'Ospatar'); insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('006','Stancescu','Luciana','2870302736264','0723942279','1000','15',to_date('01-072000', 'dd-mm-yyyy'), 'Operator receptie'); insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,data_angajarii,functie) values ('007','Ionescu','Oana','286080753194','0723265969','2000','20',to_date('10-06-2006', 'dd-mm-yyyy'),'Manager'); insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('008','Zavoianu','Andrei','2890502161129','0726333217','850','20',to_date('10-062009', 'dd-mm-yyyy'),'Barman'); insert into angajati(id_angajat,nume,prenume,cnp,telefon,salariu,vechime,data_angajarii,functie) values ('009','Matei','Andreea','287050712695','0726335699','1200',to_date('01-07-2006', 'ddmm-yyyy'), 'Camerista'); Implementarea metodelor din clase se realizeaz n corpul tipului de obiecte (object type body) al fiecrui tip n parte : Implementarea metodei nrtotal_camere() din clasa camera_o:

CREATE OR REPLACE TYPE BODY camera_o IS STATIC FUNCTION nrtotal_camere RETURN NUMBER IS v_nr_cam number; BEGIN SELECT count(*) INTO v_nr_cam FROM camere c

RETURN v_nr_cam; END; END;

SELECT camera_o.nrtotal_camere as nrcamere FROM camere;

Implementarea metodelor calculeaza_bonus(),calculeaza_venituri_totale(), calculeaza_taxe() din clasa anagajat_o:

CREATE OR REPLACE TYPE BODY angajat_o IS MEMBER FUNCTION calculeaza_bonus RETURN NUMBER IS BEGIN RETURN SELF.salariu * 0.2 * NVL(SELF.vechime,0); END; MEMBER FUNCTION calculeaza_venituri_totale RETURN NUMBER IS BEGIN RETURN SELF.salariu*(1+NVL(SELF.vechime,0)); END; MEMBER FUNCTION calculeaza_taxe RETURN NUMBER IS BEGIN RETURN calculeaza_venituri_totale*0.16; END; END;

select nume, prenume, angajat_o(ID_ANGAJAT,NUME,PRENUME, CNP, TELEFON, SALARIU, VECHIME, DATA_ANGAJARII, FUNCTIE).calculeaza_bonus() bonus FROM angajati;

select nume, prenume, angajat_o(ID_ANGAJAT,NUME,PRENUME, CNP, TELEFON, SALARIU, VECHIME, DATA_ANGAJARII, FUNCTIE).calculeaza_taxe() taxe FROM angajati;

select nume, prenume, angajat_o(ID_ANGAJAT,NUME,PRENUME, CNP, TELEFON, SALARIU, VECHIME, DATA_ANGAJARII, FUNCTIE).calculeaza_venituri_totale() venituri_totale FROM angajati;

Implementarea clasei calculeaza_statistici() din clasa sejur_o:

create or replace TYPE BODY sejur_o IS STATIC FUNCTION calculeaza_statistici (c_id NUMBER) RETURN NUMBER IS v_nr_cam number; BEGIN SELECT count(*) INTO v_nr_cam FROM camere c WHERE c.id_camera=c_id; RETURN v_nr_cam; END; END;

SELECT id_sejur, data_inc, nr_nopti, sejur_o.calculeaza_statistici(id_sejur) FROM sejur;