-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtables.php
360 lines (315 loc) · 11.4 KB
/
tables.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
require "database_connector.php";
class tables{
public $db;
public function __construct(){
$this->db = database_connector::new_database_connection();
}
//CREATE
public function create_player_table(){
$db = $this->db;
$query = '
DROP TABLE IF EXISTS "player";
CREATE TABLE "player"(
"id" INT NOT NULL,
"first-name" VARCHAR(64),
"last-name" VARCHAR(64),
"age" INT,
"price" INT,
PRIMARY KEY ( "id" )
);
';
$db->exec($query);
}
public function create_stadium_table(){
$query = '
DROP TABLE IF EXISTS "stadium";
CREATE TABLE "stadium"(
"id" INTEGER NOT NULL,
"name" VARCHAR(128),
"capacity" INTEGER,
"age" INTEGER,
PRIMARY KEY ( "id" )
);
';
$this->db->exec($query);
}
public function create_team_table(){
$query = '
DROP TABLE IF EXISTS "team";
CREATE TABLE "team"(
"id" INTEGER NOT NULL,
"name" VARCHAR(256),
"coach" VARCHAR(256),
"captain" VARCHAR(256),
"main-stadium" INTEGER,
"budget" INTEGER,
PRIMARY KEY ( "id" ),
FOREIGN KEY ( "main-stadium" ) REFERENCES "stadium" ( "id" )
);
';
$this->db->exec($query);
}
public function create_contract_table(){
$query = '
DROP TABLE IF EXISTS "contract";
CREATE TABLE "contract"(
"id" INTEGER NOT NULL,
"player-id" INTEGER NOT NULL,
"team-id" INTEGER NOT NULL,
"expiration-date" INTEGER,
UNIQUE ( "player-id" ),
PRIMARY KEY ( "id" ),
FOREIGN KEY ( "player-id" ) REFERENCES "player" ( "id" ),
FOREIGN KEY ( "team-id" ) REFERENCES "team" ( "id" )
);
';
$this->db->exec($query);
}
public function create_referee_table(){
$query = '
DROP TABLE IF EXISTS "referee";
CREATE TABLE "referee"(
"id" INTEGER NOT NULL,
"first-name" VARCHAR(64),
"last-name" VARCHAR(64),
"age" INTEGER,
PRIMARY KEY ( "id" )
);
';
$this->db->exec($query);
}
public function create_match_table(){
$query = '
DROP TABLE IF EXISTS "match";
CREATE TABLE "match"(
"id" INTEGER NOT NULL,
"home-id" INTEGER NOT NULL,
"away-id" INTEGER NOT NULL,
"home-goals" INTEGER NOT NULL,
"away-goals" INTEGER NOT NULL,
"referee-id" INTEGER,
"stadium-id" INTEGER,
"total-attendance" INTEGER,
PRIMARY KEY ( "id" ),
FOREIGN KEY ( "home-id" ) REFERENCES "team" ( "id" ),
FOREIGN KEY ( "away-id" ) REFERENCES "team" ( "id" ),
FOREIGN KEY ( "referee-id" ) REFERENCES "referee" ( "id" ),
FOREIGN KEY ( "stadium-id" ) REFERENCES "stadium" ( "id" )
);
';
$this->db->exec($query);
}
public function create_standing_table(){
$query = '
DROP TABLE IF EXISTS "standing";
CREATE TABLE "standing"(
"team-id" INTEGER NOT NULL,
"match-played" INTEGER,
"match-won" INTEGER,
"match-draw" INTEGER,
"match-lost" INTEGER,
"goal-for" INTEGER,
"goal-against" INTEGER,
"goal-difference" INTEGER,
"total-points" INTEGER,
PRIMARY KEY ( "team-id" ),
FOREIGN KEY ( "team-id" ) REFERENCES "team" ( "id" )
);
';
$this->db->exec($query);
}
//DROP
public function drop_player_table(){
$query = '
DROP TABLE IF EXISTS "player";
';
$this->db->exec($query);
}
public function drop_stadium_table(){
$query = '
DROP TABLE IF EXISTS "stadium";
';
$this->db->exec($query);
}
public function drop_team_table(){
$query = '
DROP TABLE IF EXISTS "team";
';
$this->db->exec($query);
}
public function drop_contract_table(){
$query = '
DROP TABLE IF EXISTS "contract";
';
$this->db->exec($query);
}
public function drop_referee_table(){
$query = '
DROP TABLE IF EXISTS "referee";
';
$this->db->exec($query);
}
public function drop_match_table(){
$query = '
DROP TABLE IF EXISTS "match";
';
$this->db->exec($query);
}
public function drop_standing_table(){
$query = '
DROP TABLE IF EXISTS "standing";
';
$this->db->exec($query);
}
//INDEX
public function create_index_on_player(){
$query = '
CREATE INDEX "player_age" ON "player" ("age");
';
$this->db->exec($query);
}
public function create_index_on_team(){
$query = '
CREATE INDEX "team_budget" ON "team" ("budget");
';
$this->db->exec($query);
}
public function create_index_on_contract(){
$query = '
CREATE INDEX "contract_expire" ON "contract" ("expiration-date");
';
$this->db->exec($query);
}
public function create_index_on_referee(){
$query = '
CREATE INDEX "referee_age" ON "referee" ("age");
';
$this->db->exec($query);
}
public function create_index_on_stadium(){
$query = '
CREATE INDEX "stadium_capacity" ON "stadium" ("capacity");
';
$this->db->exec($query);
}
public function create_index_on_standing(){
$query = '
CREATE INDEX "standing_point" ON "standing" ("total-points");
';
$this->db->exec($query);
}
//TRIGGER
public function create_trigger_inserting_into_standing_table(){
$query = '
CREATE OR REPLACE FUNCTION insert_into_standing ()
RETURNS trigger AS
$$
BEGIN
INSERT INTO standing ("team-id" , "goal-for") VALUES ( NEW."home-id" , NEW."home-goals" )
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER insert_into_standing_trigger
AFTER INSERT ON "match"
FOR EACH ROW
EXECUTE PROCEDURE insert_into_standing();
';
$this->db->exec($query);
}
public function create_trigger_dont_insert_or_delete_into_standing(){
$query = '
CREATE OR REPLACE FUNCTION dont_insert_or_delete_into_standing ()
RETURNS trigger AS
$$
BEGIN
RAISE NOTICE '."'cant insert or delete from this table'".';
RETURN NULL;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER dont_insert_or_delete_into_standing
BEFORE INSERT ON "standing"
FOR EACH ROW
EXECUTE PROCEDURE dont_insert_or_delete_into_standing();
';
$this->db->exec($query);
}
public function create_trigger_dont_delete_match(){
$query = '
CREATE OR REPLACE FUNCTION dont_delete_match()
RETURNS trigger AS
$$
BEGIN
RAISE NOTICE '."'Cant remove any match'".';
RETURN NULL;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER dont_delete_match
BEFORE DELETE ON "match"
FOR EACH ROW
EXECUTE PROCEDURE dont_delete_match();
';
$this->db->exec($query);
}
//DROP TRIGGERS
public function drop_trigger_inserting_into_standing_table(){
$query = '
DROP TRIGGER insert_into_standing ON "match";
DROP FUNCTION insert_into_standing_trigger ();
';
$this->db->exec($query);
}
public function drop_trigger_dont_insert_or_delete_into_standing(){
$query = '
DROP TRIGGER dont_insert_or_delete_into_standing ON "standing";
DROP FUNCTION dont_insert_or_delete_into_standing();
';
$this->db->exec($query);
}
public function drop_trigger_dont_delete_match(){
$query = '
DROP TRIGGER dont_delete_match ON "match";
DROP FUNCTION dont_delete_match();
';
$this->db->exec($query);
}
//SUM
public function create_all_tables(){
$this->create_player_table();
$this->create_stadium_table();
$this->create_team_table();
$this->create_contract_table();
$this->create_referee_table();
$this->create_match_table();
$this->create_standing_table();
}
public function drop_all_tables(){
$this->drop_standing_table();
$this->drop_match_table();
$this->drop_referee_table();
$this->drop_contract_table();
$this->drop_team_table();
$this->drop_stadium_table();
$this->drop_player_table();
}
public function create_all_indexes(){
$this->create_index_on_player();
$this->create_index_on_team();
$this->create_index_on_contract();
$this->create_index_on_referee();
$this->create_index_on_stadium();
$this->create_index_on_standing();
}
public function create_all_triggers(){
$this->create_trigger_inserting_into_standing_table();
$this->create_trigger_dont_insert_or_delete_into_standing();
$this->create_trigger_dont_delete_match();
}
public function drop_all_triggers(){
$this->drop_trigger_inserting_into_standing_table();
$this->drop_trigger_dont_insert_or_delete_into_standing();
$this->drop_trigger_dont_delete_match();
}
}