1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Sunrise CMS - Open source CMS for widespread use. |
4
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
5
|
|
|
|
6
|
|
|
See SOURCE.txt for other and additional information. |
7
|
|
|
|
8
|
|
|
This file is part of Sunrise CMS. |
9
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
(at your option) any later version. |
14
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
22
|
|
|
|
23
|
|
|
namespace Sunrise\Engine\Library; |
24
|
|
|
|
25
|
|
|
class Db |
26
|
|
|
{ |
27
|
|
|
private $adaptor; |
28
|
|
|
// tracy |
29
|
|
|
private $log; |
30
|
|
|
|
31
|
|
|
public function __construct($adaptor, $hostname, $username, $password, $database, $port = null) |
32
|
|
|
{ |
33
|
|
|
$class = 'Sunrise\\Engine\\Adaptor\\Db\\' . $adaptor; |
34
|
|
|
|
35
|
|
|
if (class_exists($class)) { |
36
|
|
|
$this->adaptor = new $class($hostname, $username, $password, $database, $port); |
37
|
|
|
|
38
|
|
|
// Tracy Debugger |
39
|
|
|
if (isset($_SESSION['_tracy']['sql_log'])) { |
40
|
|
|
unset($_SESSION['_tracy']['sql_log']); |
41
|
|
|
} |
42
|
|
|
$this->log = array(); |
43
|
|
|
$this->log['page_url'] = (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : 'index.php'; |
44
|
|
|
$this->log['query_total_time'] = 0; |
45
|
|
|
// |
46
|
|
|
} else { |
47
|
|
|
throw new \Exception('Error: Could not load database adaptor ' . $adaptor . '!'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
\Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::__construct'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function query($sql) |
54
|
|
|
{ |
55
|
|
|
// Tracy Debugger |
56
|
|
|
$this->log['query_total_time'] = 0; |
57
|
|
|
|
58
|
|
|
$trace = debug_backtrace(); |
59
|
|
|
$filename = (isset($trace[0]['file'])) ? $trace[0]['file'] : '---'; |
60
|
|
|
$cmsPath = str_replace('upload/engine/', '', $_SERVER['DOCUMENT_ROOT'] . '/engine/'); |
61
|
|
|
$cmsPath = str_replace('public/engine/', '', $cmsPath); |
62
|
|
|
$pureFile = str_replace($cmsPath, '', $filename); |
63
|
|
|
|
64
|
|
|
$bench = new \Ubench; |
65
|
|
|
$bench->start(); |
66
|
|
|
// default |
67
|
|
|
$result = $this->adaptor->query($sql); |
68
|
|
|
// |
69
|
|
|
$bench->end(); |
70
|
|
|
$exec_time = $bench->getTime(); |
71
|
|
|
|
72
|
|
|
if (!isset($this->log['query_total_time'])) { |
73
|
|
|
$this->log['query_total_time'] = 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->log['query_total_time'] = (float) $this->log['query_total_time'] + (float) $exec_time; |
77
|
|
|
$this->log['file'] = $pureFile; |
78
|
|
|
$this->log['time'] = $exec_time; |
79
|
|
|
$this->log['query'] = \SqlFormatter::format($sql); |
80
|
|
|
$_SESSION['_tracy']['sql_log'][] = $this->log; |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
// |
84
|
|
|
\Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::query'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function escape($value) |
88
|
|
|
{ |
89
|
|
|
return $this->adaptor->escape($value); |
90
|
|
|
|
91
|
|
|
\Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::escape'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function countAffected() |
95
|
|
|
{ |
96
|
|
|
return $this->adaptor->countAffected(); |
97
|
|
|
|
98
|
|
|
\Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::countAffected'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getLastId() |
102
|
|
|
{ |
103
|
|
|
return $this->adaptor->getLastId(); |
104
|
|
|
|
105
|
|
|
\Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::getLastId'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|