|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of product_management |
|
4
|
|
|
* User: Sinan TURGUT <[email protected]> |
|
5
|
|
|
* Date: 24.06.2019 |
|
6
|
|
|
* php version 7.2 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Assessment |
|
9
|
|
|
* @package ProductManagement |
|
10
|
|
|
* @author Sinan TURGUT <[email protected]> |
|
11
|
|
|
* @license See LICENSE file |
|
12
|
|
|
* @link https://dev.sinanturgut.com.tr |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace App\Utility; |
|
16
|
|
|
|
|
17
|
|
|
use PDO; |
|
18
|
|
|
use PDOException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Database |
|
22
|
|
|
* @package App\Utility |
|
23
|
|
|
*/ |
|
24
|
|
|
class Database |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var null |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $_Database = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var PDO|null |
|
33
|
|
|
*/ |
|
34
|
|
|
private $_PDO = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var null |
|
38
|
|
|
*/ |
|
39
|
|
|
private $_query = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
private $_error = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $_results = []; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var int |
|
54
|
|
|
*/ |
|
55
|
|
|
private $_count = 0; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Database constructor. |
|
59
|
|
|
*/ |
|
60
|
|
|
private function __construct() |
|
61
|
|
|
{ |
|
62
|
|
|
try { |
|
63
|
|
|
$host = DATABASE_HOST; |
|
64
|
|
|
$name = DATABASE_NAME; |
|
65
|
|
|
$username = DATABASE_USERNAME; |
|
66
|
|
|
$password = DATABASE_PASSWORD; |
|
67
|
|
|
$this->_PDO = new PDO("mysql:host={$host};dbname={$name};charset=utf8", "{$username}", "{$password}"); |
|
68
|
|
|
|
|
69
|
|
|
} catch (PDOException $e) { |
|
70
|
|
|
die($e->getMessage()); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $action |
|
76
|
|
|
* @param $table |
|
77
|
|
|
* @param array $where |
|
78
|
|
|
* @return $this|bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public function action($action, $table, array $where = []) |
|
81
|
|
|
{ |
|
82
|
|
|
if (count($where) === 3) { |
|
83
|
|
|
$operator = $where[1]; |
|
84
|
|
|
$operators = ["=", ">", "<", ">=", "<="]; |
|
85
|
|
|
if (in_array($operator, $operators)) { |
|
86
|
|
|
$field = $where[0]; |
|
87
|
|
|
$value = $where[2]; |
|
88
|
|
|
$params = [":value" => $value]; |
|
89
|
|
|
if (!$this->query("{$action} FROM {$table} WHERE {$field} {$operator} :value", $params)->error()) { |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
if (!$this->query("{$action} FROM {$table}")->error()) { |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return int |
|
103
|
|
|
*/ |
|
104
|
|
|
public function count() |
|
105
|
|
|
{ |
|
106
|
|
|
return($this->_count); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $table |
|
111
|
|
|
* @param array $where |
|
112
|
|
|
* @return Database|bool |
|
113
|
|
|
*/ |
|
114
|
|
|
public function delete($table, array $where = []) |
|
115
|
|
|
{ |
|
116
|
|
|
return($this->action('DELETE', $table, $where)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
public function error() |
|
123
|
|
|
{ |
|
124
|
|
|
return($this->_error); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array|mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function first() |
|
132
|
|
|
{ |
|
133
|
|
|
return($this->results(0)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return Database|null |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function getInstance() |
|
140
|
|
|
{ |
|
141
|
|
|
if (!isset(self::$_Database)) { |
|
142
|
|
|
self::$_Database = new Database(); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
return(self::$_Database); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param $table |
|
149
|
|
|
* @param array $fields |
|
150
|
|
|
* @return bool|string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function insert($table, array $fields) |
|
153
|
|
|
{ |
|
154
|
|
|
if (count($fields)) { |
|
155
|
|
|
$params = []; |
|
156
|
|
|
foreach ($fields as $key => $value) { |
|
157
|
|
|
$params[":{$key}"] = $value; |
|
158
|
|
|
} |
|
159
|
|
|
$columns = implode(",", array_keys($fields)); |
|
160
|
|
|
$values = implode(",", array_keys($params)); |
|
161
|
|
|
|
|
162
|
|
|
if (!$this->query("INSERT INTO {$table} ({$columns}) VALUES ({$values})", $params)->error()) { |
|
163
|
|
|
return($this->_PDO->lastInsertId()); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param $sql |
|
171
|
|
|
* @param array $params |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
|
|
public function query($sql, array $params = []) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->_count = 0; |
|
177
|
|
|
$this->_error = false; |
|
178
|
|
|
$this->_results = []; |
|
179
|
|
|
if ($this->_query = $this->_PDO->prepare($sql)) { |
|
|
|
|
|
|
180
|
|
|
foreach ($params as $key => $value) { |
|
181
|
|
|
$this->_query->bindValue($key, $value); |
|
182
|
|
|
} |
|
183
|
|
|
if ($this->_query->execute()) { |
|
184
|
|
|
|
|
185
|
|
|
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); |
|
186
|
|
|
|
|
187
|
|
|
$this->_count = $this->_query->rowCount(); |
|
188
|
|
|
} else { |
|
189
|
|
|
$this->_error = true; |
|
190
|
|
|
die(print_r($this->_query->errorInfo())); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param null $key |
|
|
|
|
|
|
198
|
|
|
* @return array|mixed |
|
199
|
|
|
*/ |
|
200
|
|
|
public function results($key = null) |
|
201
|
|
|
{ |
|
202
|
|
|
return($key !== null ? $this->_results[$key] : $this->_results); |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
public function lastInsertId() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->_PDO->lastInsertId(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param $table |
|
215
|
|
|
* @param array $where |
|
216
|
|
|
* @return Database|bool |
|
217
|
|
|
*/ |
|
218
|
|
|
public function select($table, array $where = []) |
|
219
|
|
|
{ |
|
220
|
|
|
return($this->action('SELECT *', $table, $where)); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param $table |
|
225
|
|
|
* @param $recordID |
|
226
|
|
|
* @param array $fields |
|
227
|
|
|
* @param $where |
|
228
|
|
|
* @return bool |
|
229
|
|
|
*/ |
|
230
|
|
|
public function update($table, $recordID, array $fields, $where) |
|
231
|
|
|
{ |
|
232
|
|
|
if (count($fields)) { |
|
233
|
|
|
$x = 1; |
|
234
|
|
|
$set = ""; |
|
235
|
|
|
$params = []; |
|
236
|
|
|
foreach ($fields as $key => $value) { |
|
237
|
|
|
$params[":{$key}"] = $value; |
|
238
|
|
|
$set .= "{$key} = :$key"; |
|
239
|
|
|
if ($x < count($fields)) { |
|
240
|
|
|
$set .= ", "; |
|
241
|
|
|
} |
|
242
|
|
|
$x ++; |
|
243
|
|
|
} |
|
244
|
|
|
if (!$this->query("UPDATE {$table} SET {$set} WHERE $where = {$recordID}", $params)->error()) { |
|
245
|
|
|
return true; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
return false; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.