-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
74 lines (64 loc) · 2.49 KB
/
database.php
File metadata and controls
74 lines (64 loc) · 2.49 KB
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
<?php
if(!defined('ENGINE')) die('Denied!');
class Database_Wrapper {
private $connection;
function __construct($arr)
{
$con['connection'] = isset($arr['connection'])? $arr['connection']: '';
$con['server'] = isset($arr['server']) ? $arr['server']: '';
switch($con['connection']) {
case 'sqlite':
if(!file_exists($con['server'])) die('Database file not found.');
if(!is_writable($con['server'])) die('Database file not writable.');
$this->connection = new PDO('sqlite:'. $con['server']);
break;
default:
die('Invalid database connection type');
break;
}
}
function query($sql) {
return $this->connection->query($sql);
}
function execute($type, $sql, $params) {
if(!$run = $this->connection->prepare($sql)) {
return array('success' => false, 'message' => 'SQL statement prepare failed.');
}
if(!$run->execute($params)) {
return array('success' => false, 'message' => 'SQL statement execute failed.');
}
switch($type){
case 'select':
$fetch = $run->fetchAll(PDO::FETCH_ASSOC);
return [
'success' => true,
'affected' => count($fetch),
'rows' => $fetch
];
break;
case 'update':
case 'delete':
return [
'success' => true,
'affected' => $run->rowCount()
];
break;
case 'insert':
return ['success' => true, 'affected' => $run->rowCount(), 'last_insert_id' => $this->connection->lastInsertId()];
break;
default:
return ['success' => false, 'affected' => 0, 'message' => 'Invalid switch case. Default thrown'];
}
}
function insert($table, $arrParams){
$indexField = array_keys($arrParams);
$sql = "INSERT INTO ". $table."(". implode(',', $indexField).")VALUES(";
$paramsField = array_values($arrParams);
if(count($arrParams) > 1) {
$sql .= str_repeat('?,', count($arrParams) - 1). '?);';
} else {
$sql.= "?);";
}
return $this->execute('insert', $sql, $paramsField);
}
}