|
7 | 7 | * @author Muhammet ŞAFAK <[email protected]> |
8 | 8 | * @copyright Copyright © 2022 Muhammet ŞAFAK |
9 | 9 | * @license ./LICENSE MIT |
10 | | - * @version 2.0 |
| 10 | + * @version 2.0.5 |
11 | 11 | * @link https://www.muhammetsafak.com.tr |
12 | 12 | */ |
13 | 13 |
|
@@ -59,6 +59,8 @@ class Database extends QueryBuilder |
59 | 59 | 'deletable' => true, |
60 | 60 | 'updatable' => true, |
61 | 61 | 'return' => null, |
| 62 | + 'debug' => false, |
| 63 | + 'log' => null, |
62 | 64 | ]; |
63 | 65 |
|
64 | 66 | private Result $_last; |
@@ -354,9 +356,13 @@ final public function query(string $sqlQuery, array $parameters = []): Result |
354 | 356 | } |
355 | 357 | } catch (\Exception $e) { |
356 | 358 | $message = $e->getMessage(); |
357 | | - if(($this->_credentials['debug'] ?? false) === TRUE){ |
358 | | - $message .= ' SQL : ' |
359 | | - . (empty($parameters) ? $sqlQuery : \strtr($sqlQuery, $parameters)); |
| 359 | + $sqlMessage = 'SQL : ' |
| 360 | + . (empty($parameters) ? $sqlQuery : strtr($sqlQuery, $parameters)); |
| 361 | + if(!empty($this->_credentials['log'])){ |
| 362 | + $this->_logCreate($message . ' ' . $sqlMessage); |
| 363 | + } |
| 364 | + if($this->_credentials['debug'] === TRUE){ |
| 365 | + $message .= $message . ' ' . $sqlMessage; |
360 | 366 | } |
361 | 367 | throw new SQLQueryExecuteException($e->getMessage(), (int)$e->getCode()); |
362 | 368 | } |
@@ -801,6 +807,34 @@ public function _deleteQuery(): string |
801 | 807 | . $this->_limitQuery(); |
802 | 808 | } |
803 | 809 |
|
| 810 | + protected function _logCreate(string $message): void |
| 811 | + { |
| 812 | + if(empty($this->_credentials['log'])){ |
| 813 | + return; |
| 814 | + } |
| 815 | + if(\is_callable($this->_credentials['log'])){ |
| 816 | + \call_user_func_array($this->_credentials['log'], [$message]); |
| 817 | + return; |
| 818 | + } |
| 819 | + if(\is_string($this->_credentials['log'])){ |
| 820 | + $path = \strtr($this->_credentials['log'], [ |
| 821 | + '{timestamp}' => \time(), |
| 822 | + '{date}' => \date("Y-m-d"), |
| 823 | + '{year}' => \date("Y"), |
| 824 | + '{month}' => \date("m"), |
| 825 | + '{day}' => \date("d"), |
| 826 | + '{hour}' => \date("H"), |
| 827 | + '{minute}' => \date("i"), |
| 828 | + '{second}' => \date("s"), |
| 829 | + ]); |
| 830 | + @\file_put_contents($path, $message, \FILE_APPEND); |
| 831 | + return; |
| 832 | + } |
| 833 | + if(\is_object($this->_credentials['log']) && \method_exists($this->_credentials['log'], 'critical')){ |
| 834 | + $this->_credentials['log']->critical($message); |
| 835 | + } |
| 836 | + } |
| 837 | + |
804 | 838 | private function _whereQuery(): string |
805 | 839 | { |
806 | 840 | $isAndEmpty = empty($this->_STRUCTURE['where']['AND']); |
|
0 commit comments