-
Notifications
You must be signed in to change notification settings - Fork 0
/
zawodnicy_management.ddl
124 lines (105 loc) · 3.47 KB
/
zawodnicy_management.ddl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
--naglówek mecze_management
CREATE OR REPLACE PACKAGE zawodnicy_management IS
--procedura DODAWANIA BRAMEK ZAWODNIKOWI
PROCEDURE dodaj_bramki
(
db_id_zawodnika zawodnicy.id_zawodnika%TYPE,
db_strzelone_bramki zawodnicy.strzelone_bramki%TYPE,
db_id_meczu mecze.id_meczu%TYPE
);
--procedura TRANSFERU ZAWODNIKA DO INNEGO KLUBU
PROCEDURE transfer_zawodnika
(
tz_id_zawodnika zawodnicy.id_zawodnika%TYPE,
tz_dokad kluby.id_klubu%TYPE
);
FUNCTION krol_strzelcow
RETURN zawodnicy.id_zawodnika%TYPE;
END zawodnicy_management;
/
--cialo zawodnicy_management
CREATE OR REPLACE PACKAGE BODY zawodnicy_management IS
--procedura DODAWANIA BRAMEK ZAWODNIKOWI
PROCEDURE dodaj_bramki
(
db_id_zawodnika zawodnicy.id_zawodnika%TYPE,
db_strzelone_bramki zawodnicy.strzelone_bramki%TYPE,
db_id_meczu mecze.id_meczu%TYPE
)
AS
v_informacje_prawidlowe NUMBER := 0;
v_id_klubu_zawodnika kluby.id_klubu%TYPE;
BEGIN
--sprawdzenie, z którego klubu powinien być dany zawodnik
--na podstawie jego identyfikatora
SELECT k.id_klubu
INTO v_id_klubu_zawodnika
FROM kluby k
JOIN zawodnicy z
ON k.id_klubu = z.id_klubu
WHERE z.id_zawodnika = db_id_zawodnika;
--sprawdzenie, czy zawodnik faktycznie gra w danym meczu
SELECT count(*)
INTO v_informacje_prawidlowe
FROM mecze_kluby
WHERE id_meczu = db_id_meczu AND id_klubu = v_id_klubu_zawodnika;
IF v_informacje_prawidlowe = 1
--zaktualizowanie rekordu w tabeli zawodnicy, dodanie do aktualnej liczby
--strzelonych bramek tych, które zostaly wprowadzone przez użytkownika
--poprzez procedurę
THEN
IF db_strzelone_bramki > 0
THEN
UPDATE zawodnicy
SET strzelone_bramki = strzelone_bramki + db_strzelone_bramki
WHERE id_zawodnika = db_id_zawodnika;
ELSE
dbms_output.put_line('Nie można podać ujemnej liczby strzelonych bramek!');
END IF;
ELSE
dbms_output.put_line('Zawodnik nie należy do żadnego z klubów, które graly w podanym meczu.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('Sprawdz ponownie wprowadzone dane.');
END dodaj_bramki;
--procedura TRANSFERU ZAWODNIKA DO INNEGO KLUBU
PROCEDURE transfer_zawodnika
(
tz_id_zawodnika zawodnicy.id_zawodnika%TYPE,
tz_dokad kluby.id_klubu%TYPE
)
AS
v_zawodnik_istnieje NUMBER := 0;
BEGIN
--sprawdzenie czy dany zawodnik jest już w danym klubie
SELECT count (*)
INTO v_zawodnik_istnieje
FROM zawodnicy
WHERE id_klubu = tz_dokad AND id_zawodnika = tz_id_zawodnika;
IF v_zawodnik_istnieje = 1
THEN dbms_output.put_line('Zawodnik już należy do tego klubu.');
ELSE
UPDATE zawodnicy
SET id_klubu = tz_dokad
WHERE id_zawodnika = tz_id_zawodnika;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('Sprawdz ponownie wprowadzone dane.');
END transfer_zawodnika;
--KRÓL STRZELCÓW
FUNCTION krol_strzelcow
RETURN zawodnicy.id_zawodnika%TYPE
AS
v_krol zawodnicy.id_zawodnika%TYPE;
BEGIN
SELECT id_zawodnika
INTO v_krol
FROM (SELECT *
FROM zawodnicy
ORDER BY strzelone_bramki DESC, id_zawodnika)
WHERE ROWNUM <=1;
return(v_krol);
END krol_strzelcow;
END zawodnicy_management;