|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vundi\Potato; |
|
4
|
|
|
|
|
5
|
|
|
use PDO; |
|
6
|
|
|
use PDOException; |
|
7
|
|
|
use Vundi\Potato\Exceptions\NonExistentID; |
|
8
|
|
|
|
|
9
|
|
|
class Database |
|
10
|
|
|
{ |
|
11
|
|
|
//Instance variables for db connection |
|
12
|
|
|
private $host; |
|
13
|
|
|
private $user; |
|
14
|
|
|
private $pass; |
|
15
|
|
|
private $dbname; |
|
16
|
|
|
private $dbtype; |
|
17
|
|
|
|
|
18
|
|
|
public static $db_handler; |
|
19
|
|
|
private static $statement; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->host = getenv('DB_HOST'); |
|
24
|
|
|
$this->user = getenv('DB_USER'); |
|
25
|
|
|
$this->pass = getenv('DB_PASS'); |
|
26
|
|
|
$this->dbname = getenv('DB_NAME'); |
|
27
|
|
|
$this->dbtype = getenv('DB_TYPE'); |
|
28
|
|
|
|
|
29
|
|
|
try { |
|
30
|
|
|
if ($this->dbtype == 'sqlite') { |
|
31
|
|
|
$dsn = $this->dbtype.':'.$this->dbname; |
|
32
|
|
|
self::$db_handler = new PDO($dsn); |
|
33
|
|
|
} else { |
|
34
|
|
|
$dsn = $this->dbtype.':host='.$this->host.';dbname='.$this->dbname; |
|
35
|
|
|
self::$db_handler = new PDO($dsn, $this->user, $this->pass); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
self::$db_handler->setAttribute(PDO::ATTR_PERSISTENT, true); |
|
39
|
|
|
self::$db_handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
40
|
|
|
} catch (PDOException $e) { |
|
41
|
|
|
echo $e->getmessage(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function prepare($query) |
|
46
|
|
|
{ |
|
47
|
|
|
self::$statement = self::$db_handler->prepare($query); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function bind($param, $value, $type = null) |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_null($type)) { |
|
53
|
|
|
switch (true) { |
|
54
|
|
|
case is_int($value): |
|
55
|
|
|
$type = PDO::PARAM_INT; |
|
56
|
|
|
break; |
|
57
|
|
|
case is_bool($value): |
|
58
|
|
|
$type = PDO::PARAM_BOOL; |
|
59
|
|
|
break; |
|
60
|
|
|
case is_null($value): |
|
61
|
|
|
$type = PDO::PARAM_NULL; |
|
62
|
|
|
break; |
|
63
|
|
|
default: |
|
64
|
|
|
$type = PDO::PARAM_STR; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
self::$statement->bindValue($param, $value, $type); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* ` |
|
72
|
|
|
* Execute statements passed in as queries. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function execute() |
|
75
|
|
|
{ |
|
76
|
|
|
self::$statement->execute(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function select($table, $where = '', $fields = '*', $order = '', $limit = null, $offset = '') |
|
80
|
|
|
{ |
|
81
|
|
|
$query = "SELECT $fields FROM $table " |
|
82
|
|
|
.($where ? " WHERE $where " : '') |
|
83
|
|
|
.($limit ? " LIMIT $limit " : '') |
|
84
|
|
|
.(($offset && $limit ? " OFFSET $offset " : '')) |
|
85
|
|
|
.($order ? " ORDER BY $order " : ''); |
|
86
|
|
|
$this->prepare($query); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function insert($table, $data) |
|
90
|
|
|
{ |
|
91
|
|
|
$fieldNames = implode(',', array_keys($data)); |
|
92
|
|
|
$fieldValues = ':'.implode(', :', array_keys($data)); |
|
93
|
|
|
|
|
94
|
|
|
$query = "INSERT INTO $table ($fieldNames) VALUES($fieldValues)"; |
|
95
|
|
|
$this->prepare($query); |
|
96
|
|
|
|
|
97
|
|
|
foreach ($data as $key => $value) { |
|
98
|
|
|
$this->bind(":$key", $value); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
$this->execute(); |
|
103
|
|
|
|
|
104
|
|
|
return true; |
|
105
|
|
|
} catch (PDOException $e) { |
|
106
|
|
|
echo $e->errorInfo[2]; |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Update data in an existing row. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function update($table, $data, $where = '') |
|
116
|
|
|
{ |
|
117
|
|
|
$fieldDetails = null; |
|
118
|
|
|
foreach ($data as $key => $value) { |
|
119
|
|
|
$fieldDetails .= "$key = :$key,"; |
|
120
|
|
|
} |
|
121
|
|
|
$fieldDetails = rtrim($fieldDetails, ','); |
|
122
|
|
|
$query = "UPDATE $table SET $fieldDetails ".($where ? 'WHERE '.$where : ''); |
|
123
|
|
|
$this->prepare($query); |
|
124
|
|
|
|
|
125
|
|
|
foreach ($data as $key => $value) { |
|
126
|
|
|
$this->bind(":$key", $value); |
|
127
|
|
|
} |
|
128
|
|
|
try { |
|
129
|
|
|
$this->execute(); |
|
130
|
|
|
|
|
131
|
|
|
return true; |
|
132
|
|
|
} catch (PDOException $e) { |
|
133
|
|
|
echo $e->errorInfo[2]; |
|
134
|
|
|
|
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Delete row from database. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function delete($table, $where) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->prepare("DELETE FROM $table WHERE $where"); |
|
145
|
|
|
|
|
146
|
|
|
$this->execute(); |
|
147
|
|
|
$num = $this->rowCount(); |
|
148
|
|
|
|
|
149
|
|
|
if ($num < 1) { |
|
150
|
|
|
throw new NonExistentID('Cannot delete the record with that ID since it is non existent'); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Return Objectset. |
|
156
|
|
|
*/ |
|
157
|
|
|
public function objectSet() |
|
158
|
|
|
{ |
|
159
|
|
|
$this->execute(); |
|
160
|
|
|
self::$statement->setFetchMode(PDO::FETCH_ASSOC); |
|
161
|
|
|
|
|
162
|
|
|
return self::$statement->fetchAll(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Return single object. |
|
167
|
|
|
*/ |
|
168
|
|
|
public function singleObject($entityClass) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->execute(); |
|
171
|
|
|
self::$statement->setFetchMode(PDO::FETCH_CLASS, $entityClass); |
|
172
|
|
|
$results = self::$statement->fetch(); |
|
173
|
|
|
|
|
174
|
|
|
if (empty($results)) { |
|
175
|
|
|
throw new NonExistentID('Could not find that record, pass a record ID that exists', 1); |
|
176
|
|
|
} else { |
|
177
|
|
|
return $results; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Return number of rows in the table. |
|
183
|
|
|
* |
|
184
|
|
|
* @return int |
|
185
|
|
|
*/ |
|
186
|
|
|
public function rowCount() |
|
187
|
|
|
{ |
|
188
|
|
|
return self::$statement->rowCount(); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|