@@ -8,92 +8,92 @@ discard block |
||
8 | 8 | |
9 | 9 | class Database |
10 | 10 | { |
11 | - private $app; |
|
11 | + private $app; |
|
12 | 12 | |
13 | - private static $connection; |
|
13 | + private static $connection; |
|
14 | 14 | |
15 | - private $table; |
|
15 | + private $table; |
|
16 | 16 | |
17 | - private $rows; |
|
17 | + private $rows; |
|
18 | 18 | |
19 | - private $lastId; |
|
19 | + private $lastId; |
|
20 | 20 | |
21 | - private $data = []; |
|
21 | + private $data = []; |
|
22 | 22 | |
23 | - private $bindings = []; |
|
23 | + private $bindings = []; |
|
24 | 24 | |
25 | - private $selects = []; |
|
25 | + private $selects = []; |
|
26 | 26 | |
27 | - private $joins = []; |
|
27 | + private $joins = []; |
|
28 | 28 | |
29 | - private $wheres = []; |
|
29 | + private $wheres = []; |
|
30 | 30 | |
31 | - private $havings = []; |
|
31 | + private $havings = []; |
|
32 | 32 | |
33 | - private $orderBy = []; |
|
33 | + private $orderBy = []; |
|
34 | 34 | |
35 | - private $limit; |
|
35 | + private $limit; |
|
36 | 36 | |
37 | - private $offset; |
|
37 | + private $offset; |
|
38 | 38 | |
39 | - private $groupBy = []; |
|
39 | + private $groupBy = []; |
|
40 | 40 | |
41 | - public function __construct(Application $app) |
|
42 | - { |
|
41 | + public function __construct(Application $app) |
|
42 | + { |
|
43 | 43 | $this->app = $app; |
44 | 44 | |
45 | 45 | if (! $this->isConnected()) { |
46 | - $this->connect(); |
|
46 | + $this->connect(); |
|
47 | + } |
|
47 | 48 | } |
48 | - } |
|
49 | 49 | |
50 | - private function isConnected() |
|
51 | - { |
|
50 | + private function isConnected() |
|
51 | + { |
|
52 | 52 | return self::$connection instanceof PDO; |
53 | - } |
|
53 | + } |
|
54 | 54 | |
55 | - private function connect() |
|
56 | - { |
|
55 | + private function connect() |
|
56 | + { |
|
57 | 57 | $data = $this->app->config['db']; |
58 | 58 | |
59 | 59 | extract($data); |
60 | 60 | |
61 | 61 | try { |
62 | - self::$connection = new PDO('mysql:host=' . $server . ';dbname=' . $dbname, $dbuser, $dbpass); |
|
62 | + self::$connection = new PDO('mysql:host=' . $server . ';dbname=' . $dbname, $dbuser, $dbpass); |
|
63 | 63 | |
64 | - self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
64 | + self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
65 | 65 | |
66 | - self::$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
66 | + self::$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
67 | 67 | |
68 | - self::$connection->exec('SET NAMES utf8'); |
|
68 | + self::$connection->exec('SET NAMES utf8'); |
|
69 | 69 | |
70 | 70 | } catch (PDOException $e) { |
71 | 71 | |
72 | - throw new Exception($e->getMessage()); |
|
72 | + throw new Exception($e->getMessage()); |
|
73 | + } |
|
73 | 74 | } |
74 | - } |
|
75 | 75 | |
76 | - public function connection() |
|
77 | - { |
|
76 | + public function connection() |
|
77 | + { |
|
78 | 78 | return self::$connection; |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | - public function table($table) |
|
82 | - { |
|
81 | + public function table($table) |
|
82 | + { |
|
83 | 83 | $this->table = $table; |
84 | 84 | |
85 | 85 | return $this; |
86 | - } |
|
86 | + } |
|
87 | 87 | |
88 | - public function select(...$select) |
|
89 | - { |
|
88 | + public function select(...$select) |
|
89 | + { |
|
90 | 90 | $this->selects = array_merge($this->selects, $select); |
91 | 91 | |
92 | 92 | return $this; |
93 | - } |
|
93 | + } |
|
94 | 94 | |
95 | - public function join($join, $localId = null, $forginId = null) |
|
96 | - { |
|
95 | + public function join($join, $localId = null, $forginId = null) |
|
96 | + { |
|
97 | 97 | if (! $localId) $localId = trim($join, 's' ). '_id'; |
98 | 98 | |
99 | 99 | if (! $forginId) $forginId = 'id'; |
@@ -103,10 +103,10 @@ discard block |
||
103 | 103 | $this->joins[] = $sql; |
104 | 104 | |
105 | 105 | return $this; |
106 | - } |
|
106 | + } |
|
107 | 107 | |
108 | - public function where(...$bindings) |
|
109 | - { |
|
108 | + public function where(...$bindings) |
|
109 | + { |
|
110 | 110 | $sql = array_shift($bindings); |
111 | 111 | |
112 | 112 | if (is_array($bindings[0])) $bindings = $bindings[0]; |
@@ -116,10 +116,10 @@ discard block |
||
116 | 116 | $this->wheres[] = $sql; |
117 | 117 | |
118 | 118 | return $this; |
119 | - } |
|
119 | + } |
|
120 | 120 | |
121 | - public function having() |
|
122 | - { |
|
121 | + public function having() |
|
122 | + { |
|
123 | 123 | $bindings = func_get_args(); |
124 | 124 | |
125 | 125 | $sql = array_shift($bindings); |
@@ -129,40 +129,40 @@ discard block |
||
129 | 129 | $this->havings[] = $sql; |
130 | 130 | |
131 | 131 | return $this; |
132 | - } |
|
132 | + } |
|
133 | 133 | |
134 | - public function groupBy(...$arguments) |
|
135 | - { |
|
134 | + public function groupBy(...$arguments) |
|
135 | + { |
|
136 | 136 | $this->groupBy = $arguments; |
137 | 137 | |
138 | 138 | return $this; |
139 | - } |
|
139 | + } |
|
140 | 140 | |
141 | - public function limit($limit, $offset = 0) |
|
142 | - { |
|
141 | + public function limit($limit, $offset = 0) |
|
142 | + { |
|
143 | 143 | $this->limit = $limit; |
144 | 144 | |
145 | 145 | $this->offset = $offset; |
146 | 146 | |
147 | 147 | return $this; |
148 | - } |
|
148 | + } |
|
149 | 149 | |
150 | - public function rows() |
|
151 | - { |
|
150 | + public function rows() |
|
151 | + { |
|
152 | 152 | return $this->rows; |
153 | - } |
|
153 | + } |
|
154 | 154 | |
155 | - public function orderBy($orderBy, $sort = 'ASC') |
|
156 | - { |
|
155 | + public function orderBy($orderBy, $sort = 'ASC') |
|
156 | + { |
|
157 | 157 | $this->orderBy = [$orderBy, $sort]; |
158 | 158 | |
159 | 159 | return $this; |
160 | - } |
|
160 | + } |
|
161 | 161 | |
162 | - public function fetch($table = null) |
|
163 | - { |
|
162 | + public function fetch($table = null) |
|
163 | + { |
|
164 | 164 | if ($table) { |
165 | - $this->table($table); |
|
165 | + $this->table($table); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | $sql = $this->fetchStatment(); |
@@ -174,12 +174,12 @@ discard block |
||
174 | 174 | $this->rows = $query->rowCount(); |
175 | 175 | |
176 | 176 | return $result; |
177 | - } |
|
177 | + } |
|
178 | 178 | |
179 | - public function fetchAll($table = null) |
|
180 | - { |
|
179 | + public function fetchAll($table = null) |
|
180 | + { |
|
181 | 181 | if ($table) { |
182 | - $this->table($table); |
|
182 | + $this->table($table); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | $sql = $this->fetchStatment(); |
@@ -191,10 +191,10 @@ discard block |
||
191 | 191 | $this->rows = $query->rowCount(); |
192 | 192 | |
193 | 193 | return $results; |
194 | - } |
|
194 | + } |
|
195 | 195 | |
196 | - private function fetchStatment() |
|
197 | - { |
|
196 | + private function fetchStatment() |
|
197 | + { |
|
198 | 198 | $sql = 'SELECT '; |
199 | 199 | |
200 | 200 | $sql .= $this->selects ? implode(', ', $this->selects) : '*'; |
@@ -203,74 +203,74 @@ discard block |
||
203 | 203 | |
204 | 204 | if (!empty($this->joins)) { |
205 | 205 | |
206 | - $sql .= 'LEFT JOIN ' . implode(' ', $this->joins); |
|
206 | + $sql .= 'LEFT JOIN ' . implode(' ', $this->joins); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | if (!empty($this->wheres)) { |
210 | 210 | |
211 | - $sql .= ' WHERE ' . implode(' ', $this->wheres); |
|
211 | + $sql .= ' WHERE ' . implode(' ', $this->wheres); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | if (!empty($this->havings)) { |
215 | 215 | |
216 | - $sql .= ' HAVING ' . implode(' ', $this->havings) . ' '; |
|
216 | + $sql .= ' HAVING ' . implode(' ', $this->havings) . ' '; |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | if (!empty($this->orderBy)) { |
220 | 220 | |
221 | - $sql .= ' ORDER BY ' . implode (' ', $this->orderBy); |
|
221 | + $sql .= ' ORDER BY ' . implode (' ', $this->orderBy); |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | if ($this->limit) { |
225 | 225 | |
226 | - $sql .= ' LIMIT ' . $this->limit; |
|
226 | + $sql .= ' LIMIT ' . $this->limit; |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | if ($this->offset) { |
230 | 230 | |
231 | - $sql .= ' OFFSET ' . $this->offset; |
|
231 | + $sql .= ' OFFSET ' . $this->offset; |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | if (!empty($this->groupBy)) { |
235 | - $sql .= ' GROUP BY ' . implode(' ' , $this->groupBy); |
|
235 | + $sql .= ' GROUP BY ' . implode(' ' , $this->groupBy); |
|
236 | 236 | } |
237 | 237 | |
238 | 238 | return $sql; |
239 | - } |
|
239 | + } |
|
240 | 240 | |
241 | - public function lastId() |
|
242 | - { |
|
241 | + public function lastId() |
|
242 | + { |
|
243 | 243 | return $this->lastId; |
244 | - } |
|
244 | + } |
|
245 | 245 | |
246 | - public function from($table) |
|
247 | - { |
|
246 | + public function from($table) |
|
247 | + { |
|
248 | 248 | return $this->table($table); |
249 | - } |
|
249 | + } |
|
250 | 250 | |
251 | - public function data($key, $value = null) |
|
252 | - { |
|
251 | + public function data($key, $value = null) |
|
252 | + { |
|
253 | 253 | if (is_array($key)) { |
254 | 254 | |
255 | - $this->data = array_merge($this->data, $key); |
|
255 | + $this->data = array_merge($this->data, $key); |
|
256 | 256 | |
257 | - $this->addToBindings($key); |
|
257 | + $this->addToBindings($key); |
|
258 | 258 | |
259 | 259 | } else { |
260 | 260 | |
261 | - $this->data[$key] = $value; |
|
261 | + $this->data[$key] = $value; |
|
262 | 262 | |
263 | - $this->addToBindings($value); |
|
263 | + $this->addToBindings($value); |
|
264 | 264 | |
265 | 265 | } |
266 | 266 | |
267 | 267 | return $this; |
268 | - } |
|
268 | + } |
|
269 | 269 | |
270 | - public function insert($table = null) |
|
271 | - { |
|
270 | + public function insert($table = null) |
|
271 | + { |
|
272 | 272 | if ($table) { |
273 | - $this->table($table); |
|
273 | + $this->table($table); |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | $sql = 'INSERT INTO ' . $this->table . ' SET '; |
@@ -282,12 +282,12 @@ discard block |
||
282 | 282 | $this->lastId = $this->connection()->lastInsertId(); |
283 | 283 | |
284 | 284 | return $this; |
285 | - } |
|
285 | + } |
|
286 | 286 | |
287 | - public function update($table = null) |
|
288 | - { |
|
287 | + public function update($table = null) |
|
288 | + { |
|
289 | 289 | if ($table) { |
290 | - $this->table($table); |
|
290 | + $this->table($table); |
|
291 | 291 | } |
292 | 292 | |
293 | 293 | $sql = 'UPDATE ' . $this->table . ' SET '; |
@@ -295,69 +295,69 @@ discard block |
||
295 | 295 | $sql .= $this->setField(); |
296 | 296 | |
297 | 297 | if ($this->wheres) { |
298 | - $sql .= ' WHERE ' . implode('', $this->wheres); |
|
298 | + $sql .= ' WHERE ' . implode('', $this->wheres); |
|
299 | 299 | } |
300 | 300 | |
301 | 301 | $this->query($sql, $this->bindings); |
302 | 302 | |
303 | 303 | return $this; |
304 | - } |
|
304 | + } |
|
305 | 305 | |
306 | - public function delete($table = null) |
|
307 | - { |
|
306 | + public function delete($table = null) |
|
307 | + { |
|
308 | 308 | if ($table) { |
309 | - $this->table($table); |
|
309 | + $this->table($table); |
|
310 | 310 | } |
311 | 311 | |
312 | 312 | $sql = 'DELETE FROM ' . $this->table . ' '; |
313 | 313 | |
314 | 314 | if ($this->wheres) { |
315 | - $sql .= ' WHERE ' . implode('', $this->wheres); |
|
315 | + $sql .= ' WHERE ' . implode('', $this->wheres); |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | $this->query($sql, $this->bindings); |
319 | 319 | |
320 | 320 | return $this; |
321 | - } |
|
321 | + } |
|
322 | 322 | |
323 | - private function setField() |
|
324 | - { |
|
323 | + private function setField() |
|
324 | + { |
|
325 | 325 | $sql = ''; |
326 | 326 | |
327 | 327 | foreach($this->data as $key => $value) { |
328 | 328 | |
329 | - $sql .= '`' . $key . '` = ? ,'; |
|
329 | + $sql .= '`' . $key . '` = ? ,'; |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | $sql = rtrim($sql, ' ,'); |
333 | 333 | |
334 | 334 | return $sql; |
335 | - } |
|
335 | + } |
|
336 | 336 | |
337 | - private function addToBindings($value) |
|
338 | - { |
|
337 | + private function addToBindings($value) |
|
338 | + { |
|
339 | 339 | if (is_array($value)) { |
340 | 340 | |
341 | - $this->bindings = array_merge($this->bindings, array_values($value)); |
|
341 | + $this->bindings = array_merge($this->bindings, array_values($value)); |
|
342 | 342 | } else { |
343 | 343 | |
344 | - $this->bindings[] = $value; |
|
344 | + $this->bindings[] = $value; |
|
345 | + } |
|
345 | 346 | } |
346 | - } |
|
347 | 347 | |
348 | - public function query(...$bindings) |
|
349 | - { |
|
348 | + public function query(...$bindings) |
|
349 | + { |
|
350 | 350 | $sql = array_shift($bindings); |
351 | 351 | |
352 | 352 | if (count($bindings) == 1 AND is_array($bindings[0])) { |
353 | - $bindings = $bindings[0]; |
|
353 | + $bindings = $bindings[0]; |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | try { |
357 | 357 | $query = $this->connection()->prepare($sql); |
358 | 358 | |
359 | 359 | foreach ($bindings as $key => $value) { |
360 | - $query->bindValue($key + 1, _e($value)); |
|
360 | + $query->bindValue($key + 1, _e($value)); |
|
361 | 361 | } |
362 | 362 | |
363 | 363 | $query->execute(); |
@@ -368,12 +368,12 @@ discard block |
||
368 | 368 | |
369 | 369 | } catch (PDOException $e) { |
370 | 370 | |
371 | - throw new Exception($e->getMessage()); |
|
371 | + throw new Exception($e->getMessage()); |
|
372 | + } |
|
372 | 373 | } |
373 | - } |
|
374 | 374 | |
375 | - private function reset() |
|
376 | - { |
|
375 | + private function reset() |
|
376 | + { |
|
377 | 377 | $this->table = null; |
378 | 378 | |
379 | 379 | $this->rows = 0; |
@@ -397,5 +397,5 @@ discard block |
||
397 | 397 | $this->offset = 0; |
398 | 398 | |
399 | 399 | $this->groupBy = []; |
400 | - } |
|
400 | + } |
|
401 | 401 | } |
402 | 402 | \ No newline at end of file |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | { |
43 | 43 | $this->app = $app; |
44 | 44 | |
45 | - if (! $this->isConnected()) { |
|
45 | + if (!$this->isConnected()) { |
|
46 | 46 | $this->connect(); |
47 | 47 | } |
48 | 48 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | extract($data); |
60 | 60 | |
61 | 61 | try { |
62 | - self::$connection = new PDO('mysql:host=' . $server . ';dbname=' . $dbname, $dbuser, $dbpass); |
|
62 | + self::$connection = new PDO('mysql:host=' . $server . ';dbname=' . $dbname, $dbuser, $dbpass); |
|
63 | 63 | |
64 | 64 | self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
65 | 65 | |
@@ -94,9 +94,9 @@ discard block |
||
94 | 94 | |
95 | 95 | public function join($join, $localId = null, $forginId = null) |
96 | 96 | { |
97 | - if (! $localId) $localId = trim($join, 's' ). '_id'; |
|
97 | + if (!$localId) $localId = trim($join, 's') . '_id'; |
|
98 | 98 | |
99 | - if (! $forginId) $forginId = 'id'; |
|
99 | + if (!$forginId) $forginId = 'id'; |
|
100 | 100 | |
101 | 101 | $sql = $join . ' ON ' . $this->table . '.' . $localId . ' = ' . $join . '.' . $forginId; |
102 | 102 | |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | |
219 | 219 | if (!empty($this->orderBy)) { |
220 | 220 | |
221 | - $sql .= ' ORDER BY ' . implode (' ', $this->orderBy); |
|
221 | + $sql .= ' ORDER BY ' . implode(' ', $this->orderBy); |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | if ($this->limit) { |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | } |
233 | 233 | |
234 | 234 | if (!empty($this->groupBy)) { |
235 | - $sql .= ' GROUP BY ' . implode(' ' , $this->groupBy); |
|
235 | + $sql .= ' GROUP BY ' . implode(' ', $this->groupBy); |
|
236 | 236 | } |
237 | 237 | |
238 | 238 | return $sql; |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | { |
325 | 325 | $sql = ''; |
326 | 326 | |
327 | - foreach($this->data as $key => $value) { |
|
327 | + foreach ($this->data as $key => $value) { |
|
328 | 328 | |
329 | 329 | $sql .= '`' . $key . '` = ? ,'; |
330 | 330 | } |