-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabasehelper.php
550 lines (482 loc) · 20.9 KB
/
databasehelper.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
class DatabaseHelper
{
const renew_incr = 7;
const return_date = 14;
const renew_allowed = 2;
// Since the connection details are constant, define them as const
// We can refer to constants like e.g. DatabaseHelper::username
const username = ''; //use your oracle database username
const password = ''; // use your oracle db password
const con_string = ''; //use the name of your local oracle instance
// Since we need only one connection object, it can be stored in a member variable.
// $conn is set in the constructor.
protected $conn;
// Create connection in the constructor
public function __construct() {
try {
// Create connection with the command oci_connect(String(username), String(password), String(connection_string))
$this->conn = oci_connect(
DatabaseHelper::username,
DatabaseHelper::password,
DatabaseHelper::con_string
);
//check if the connection object is != null
if (!$this->conn) {
// die(String(message)): stop PHP script and output message:
die("DB error: Connection can't be established!");
}
} catch (Exception $e) {
die("DB error: {$e->getMessage()}");
}
}
public function __destruct() {
// clean up
oci_close($this->conn);
}
public function SearchQuery($author_name, $item_title, $medium_id, $genre_id, $language) {
$sql = "SELECT
*
FROM
searchquery s
WHERE
upper(author_name) LIKE upper('%{$author_name}%')
AND upper(item_title) LIKE upper('%{$item_title}%')";
if ($medium_id != 0)
$sql .= " AND medium_id = {$medium_id}";
if ($genre_id != 0)
$sql .= " AND genre_id = {$genre_id}";
if ($language != 0)
$sql .= " AND (ph_lang_id = {$language} OR aud_lang_id = {$language})";
// oci_parse(...) prepares the Oracle statement for execution
// notice the reference to the class variable $this->conn (set in the constructor)
$statement = oci_parse($this->conn, $sql);
// Executes the statement
oci_execute($statement);
// Fetches multiple rows from a query into a two-dimensional array
// Parameters of oci_fetch_all:
// $statement: must be executed before
// $res: will hold the result after the execution of oci_fetch_all
// $skip: it's null because we don't need to skip rows
// $maxrows: it's null because we want to fetch all rows
// $flag: defines how the result is structured: 'by rows' or 'by columns'
// OCI_FETCHSTATEMENT_BY_ROW (The outer array will contain one sub-array per query row)
// OCI_FETCHSTATEMENT_BY_COLUMN (The outer array will contain one sub-array per query column. This is the default.)
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
//clean up;
oci_free_statement($statement);
return $res;
}
public function MediumDropdown() {
$sql = "SELECT * FROM mediumdropdown";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
public function GenreDropdown() {
$sql = "SELECT * FROM genredropdown";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
public function LanguageDropdown() {
$sql = "SELECT * FROM languagedropdown";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
public function Author($author_id) {
$sql = "SELECT * from selectauthor WHERE author_id = {$author_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*fetch all the authors influenced by chosen author denoted by author_id*/
public function Influencer($author_id) {
$sql = "SELECT
influenced,
author_name
FROM
influenced_by i
INNER JOIN author a ON i.influenced = a.author_id
WHERE
influencer = {$author_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*fetch all the authors who have influenced this author denoted by author_id*/
public function Influenced($author_id) {
$sql = "SELECT
influencer,
author_name
FROM
influenced_by i
INNER JOIN author a ON i.influencer = a.author_id
WHERE
influenced = {$author_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
public function ItemData($item_id) {
$sql = "SELECT
it.item_id,
item_title,
item_edition,
item_publisher,
item_pub_year,
medium_name,
a.author_id,
author_name,
page_count,
dig,
book_qty,
lang_name
FROM
lib_item it
INNER JOIN item_medium m ON it.medium_id = m.medium_id
LEFT JOIN authored_by au ON it.item_id = au.item_id
INNER JOIN author a ON au.author_id = a.author_id
INNER JOIN phys_book b ON it.item_id = b.item_id
INNER JOIN lang l ON b.lang_id = l.lang_id
WHERE
it.item_id = {$item_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*displays the item's genres on the item page*/
public function ItemGenres($item_id) {
$sql = "SELECT
genre_name
FROM
lib_item it
INNER JOIN belongs_to b ON it.item_id = b.item_id
INNER JOIN item_genre g ON b.genre_id = g.genre_id
WHERE
it.item_id = {$item_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*informs the user of the location of their book once they've borrowed it, that is, the location of the library where that book is waiting for them to pick it up*/
public function ItemLocation($item_id, $language) {
$sql = "SELECT
lib_name,
lib_street,
lib_postcode,
lib_city,
lib_country,
s.section_id AS section_id,
s.section_name AS section_name
FROM
phys_book p
INNER JOIN lib l ON p.lib_id = l.lib_id
INNER JOIN lib_section s ON p.section_id = s.section_id AND p.lib_id = s.lib_id
WHERE item_id = {$item_id} AND lang_id = {$language}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
public function ItemPhysDig($item_id) {
$sql = "SELECT
p.item_id,
dig,
book_qty,
la.lang_id as lang_id,
lang_name
FROM
lib_item l
INNER JOIN phys_book p ON l.item_id = p.item_id
INNER JOIN lang la ON p.lang_id = la.lang_id
WHERE
l.item_id = {$item_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*used to notify the user of the existence of the book they're viewing in audio format */
public function ItemAudio($item_id) {
$sql = "SELECT
narrator,
lang_name
FROM
audio_book a
INNER JOIN lang l on a.lang_id = l.lang_id
WHERE
item_id = {$item_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
//this function registers the user with the credentials they inserted into the corresponding html form
/*Register() attempts to register a new user with their inserted credentials*/
//the only unique attribute is the email address which will, if already existent in the db, return an error on attempted duplicate input and that will be passed on to html via the $success variable
public function Register($username, $email, $pass) {
$sql = "INSERT INTO
user_account (username, email, user_password)
VALUES ('{$username}', '{$email}', '{$pass}')";
$statement = oci_parse($this->conn, $sql);
$success = oci_execute($statement) && oci_commit($this->conn);
oci_free_statement($statement);
return $success;
}
/*checks if a user is attempting to register with an existing email address - always called before Register()*/
public function ExistingUser($email) {
$sql = "SELECT account_id FROM user_account WHERE email = '{$email}'";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*checks if a user's already borrowed the selected book in the selected language in which case they're not allowed to borrow the same book twice*/
public function CheckQty($item_id, $language) {
$sql = "SELECT
item_id
FROM
phys_book
WHERE
item_id = {$item_id}
AND lang_id = {$language}
AND book_qty > 0";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
public function AlreadyHasBookCheck($email, $item_id, $language) {
$sql = "SELECT
COUNT(borrow_id) AS count
FROM
user_account ac
INNER JOIN borrowed_by b ON ac.account_id = b.account_id
WHERE
email = '{$email}'
AND lang_id = {$language}
AND item_id = {$item_id}
AND return_date IS NULL";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*checks if the selected user has any overdue books for purposes of preventing them from borrowing new books in case they indeed have overdue books*/
public function OverdueCheck($email) {
$sql = "SELECT
COUNT(borrow_id) AS count
FROM
user_account ac
INNER JOIN borrowed_by b ON ac.account_id = b.account_id
WHERE
email = '{$email}'
AND overdue_date <= current_date
AND return_date IS NULL";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*decrements the selected book's quantity in the phys_book relation as the first step in the borrowing process*/
/*NOTE: QtyDecrement() is only to be executed if all 4 of the previous tests are passed: CheckQty(), Register(), AlreadyHasBookCheck(), OverdueCheck()*/
public function QtyDecrement ($item_id, $language) {
$sql = "UPDATE phys_book
SET
book_qty = book_qty - 1
WHERE
item_id = {$item_id}
AND lang_id = {$language}
AND book_qty > 0";
$statement = oci_parse($this->conn, $sql);
$success = oci_execute($statement) && oci_commit($this->conn);
$affected_rows = oci_num_rows($statement);
oci_free_statement($statement);
return $affected_rows;
}
/*Borrow() finalizes the two-step borrowing process by adding a copy of the book to the user's borrowed books*/
/*NOTE: Borrow() is only to be executed if QtyDecrement successfully decremented the corresponding book's quantity in the phys_book relation*/
public function Borrow($email, $language, $item_id) {
$sql = "INSERT INTO borrowed_by (
account_id,
item_id,
lang_id,
borrow_date,
return_date,
overdue_date
) VALUES (
(
SELECT
account_id
FROM
user_account
WHERE
email = '{$email}'
),
{$item_id},
{$language},
CURRENT_DATE,
NULL,
CURRENT_DATE + " . DatabaseHelper::return_date . ")";
$statement = oci_parse($this->conn, $sql);
$success = oci_execute($statement) && oci_commit($this->conn);
oci_free_statement($statement);
return $success;
}
/*verifies the user's login information upon borrowing or logging in to examine borrowed books*/
public function Login($email, $password) {
$sql = "SELECT
account_id
FROM
user_account ac
WHERE
email = '{$email}'
AND user_password = '{$password}'";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*MyBooks() returns all of the data that is presented to the user in their MyBooks section (apart from authors which is obtained using SelectAuthors()*/
public function MyBooks($email) {
$sql = "SELECT
username,
lang_name,
b.lang_id AS lang_id,
b.item_id AS item_id,
item_title,
borrow_date,
overdue_date,
CASE
WHEN ( overdue_date <= current_date ) THEN
1
ELSE
0
END AS overdue,
CASE
WHEN ( overdue_date <= current_date + " . DatabaseHelper::renew_allowed . " ) THEN
1
ELSE
0
END AS renew
FROM
borrowed_by b
INNER JOIN user_account ac ON b.account_id = ac.account_id
INNER JOIN lib_item l ON b.item_id = l.item_id
INNER JOIN lang la ON b.lang_id = la.lang_id
WHERE
email = '{$email}'
AND return_date IS NULL";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*just returns the names of the authors who authored the selected item*/
public function SelectAuthors ($item_id) {
$sql = "SELECT
author_name,
au.author_id as author_id
FROM
author a
INNER JOIN authored_by au ON a.author_id = au.author_id
WHERE
item_id = {$item_id}";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_fetch_all($statement, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
oci_free_statement($statement);
return $res;
}
/*renews a borrowed book for a selected user and returns the number of affected rows*/
public function Renew ($item_id, $language, $email) {
$sql = "UPDATE borrowed_by
SET
overdue_date = overdue_date + " . DatabaseHelper::renew_incr .
"WHERE
account_id = (
SELECT
account_id
FROM
user_account
WHERE
email = '{$email}'
)
AND item_id = {$item_id}
AND lang_id = {$language}
AND return_date IS NULL";
$statement = oci_parse($this->conn, $sql);
$success = oci_execute($statement) && oci_commit($this->conn);
$affected_rows = oci_num_rows($statement);
oci_free_statement($statement);
return $affected_rows;
}
/*QtyIncrement() and Return() are involved in the two-step process of returning a book. First, the corresponding book's qty in the phys_book relation is incremented (via QtyIncrement() ), and only if that is successful should the user's entry in the borrowed_by relation be timestamped with a valid return date (via Return() )*/
public function QtyIncrement($item_id, $language) {
$sql = "UPDATE phys_book
SET
book_qty = book_qty + 1
WHERE
item_id = {$item_id}
AND lang_id = {$language}";
$statement = oci_parse($this->conn, $sql);
$success = oci_execute($statement) && oci_commit($this->conn);
$affected_rows = oci_num_rows($statement);
oci_free_statement($statement);
return $affected_rows;
}
public function Return ($item_id, $language, $email) {
$sql = "UPDATE borrowed_by
SET
return_date = current_date
WHERE
account_id = (
SELECT
account_id
FROM
user_account
WHERE
email = '{$email}'
)
AND item_id = {$item_id} AND lang_id = {$language} AND return_date IS NULL";
$statement = oci_parse($this->conn, $sql);
$success = oci_execute($statement) && oci_commit($this->conn);
$affected_rows = oci_num_rows($statement);
oci_free_statement($statement);
return $affected_rows;
}
// This function uses a SQL procedure to delete an account and automatically cascades constraints in cases where that row was referenced elsewhere
public function DeleteAccount($email) {
$sql = "BEGIN delete_user('{$email}'); END;";
$statement = oci_parse($this->conn, $sql);
oci_execute($statement);
oci_free_statement($statement);
}
}