1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Provides database connectivity |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Core |
9
|
|
|
* @package Database |
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\database; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Provides database connectivity |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Database |
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
abstract class Base extends \csphere\core\service\Drivers |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Stores the logger object |
33
|
|
|
**/ |
34
|
|
|
protected $logger = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Stores the database prefix |
38
|
|
|
**/ |
39
|
|
|
protected $prefix = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates the database handler object |
43
|
|
|
* |
44
|
|
|
* @param array $config Configuration details as an array |
45
|
|
|
* |
46
|
|
|
* @return \csphere\core\database\Base |
47
|
|
|
**/ |
|
|
|
|
48
|
|
|
|
49
|
|
|
public function __construct(array $config) |
50
|
|
|
{ |
51
|
|
|
parent::__construct($config); |
52
|
|
|
|
53
|
|
|
// Set prefix for tables |
54
|
|
|
$this->prefix = $this->config['prefix']; |
55
|
|
|
|
56
|
|
|
// Set logger object |
57
|
|
|
$this->logger = $this->loader->load('logs'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Logs database queries |
62
|
|
|
* |
63
|
|
|
* @param string $query The database query for this case |
64
|
|
|
* @param array $assoc Array with columns and values |
65
|
|
|
* @param boolean $log Defaults to true which enables log files if used |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
**/ |
|
|
|
|
69
|
|
|
|
70
|
|
|
protected function log($query, array $assoc, $log = true) |
71
|
|
|
{ |
72
|
|
|
// Replace assoc data to make queries readable |
73
|
|
|
if ($assoc != []) { |
74
|
|
|
|
75
|
|
|
foreach ($assoc AS $key => $value) { |
76
|
|
|
|
77
|
|
|
$query = str_replace($key, '\'' . $value . '\'', $query); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->logger->log('database', $query, $log); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Handles errors for the database connection |
86
|
|
|
* |
87
|
|
|
* @param string $query The database query for this case |
88
|
|
|
* @param array $assoc Array with columns and values |
89
|
|
|
* @param string $msg The error message if already known |
90
|
|
|
* @param boolean $more Append query and and data to message |
91
|
|
|
* |
92
|
|
|
* @throws \Exception |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
**/ |
|
|
|
|
96
|
|
|
|
97
|
|
|
protected function error($query, array $assoc, $msg = '', $more = true) |
98
|
|
|
{ |
99
|
|
|
// Check for error message if not provided |
100
|
|
|
$error = empty($msg) ? 'Unknown' : $msg; |
101
|
|
|
|
102
|
|
|
if ($more === true) { |
103
|
|
|
|
104
|
|
|
// Append query string and data keys |
105
|
|
|
$error .= "\n" . 'Query: ' . $query . "\n" . 'Data: Array('; |
106
|
|
|
|
107
|
|
|
$data = ''; |
108
|
|
|
|
109
|
|
|
foreach ($assoc AS $key => $value) { |
110
|
|
|
|
111
|
|
|
$data .= '\'' . $key . '\', '; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Clear unused var |
115
|
|
|
unset($value); |
116
|
|
|
|
117
|
|
|
$error .= substr($data, 0, -2) . ')'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Throw exception |
121
|
|
|
throw new \Exception($error); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a formatted array with statistics |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
**/ |
|
|
|
|
129
|
|
|
|
130
|
|
|
public function info() |
131
|
|
|
{ |
132
|
|
|
// Build array with information to return |
133
|
|
|
$info = $this->config; |
134
|
|
|
|
135
|
|
|
unset($info['password'], $info['file']); |
136
|
|
|
|
137
|
|
|
$more = ['version' => '', |
138
|
|
|
'client' => '', |
139
|
|
|
'server' => '', |
140
|
|
|
'size' => '', |
141
|
|
|
'encoding' => '', |
142
|
|
|
'tables' => '']; |
143
|
|
|
|
144
|
|
|
$info = array_merge($info, $more); |
145
|
|
|
|
146
|
|
|
return $info; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Handle database driver specific transactions |
151
|
|
|
* |
152
|
|
|
* @param string $command One of these strings: begin, commit, rollback |
153
|
|
|
* |
154
|
|
|
* @return boolean |
155
|
|
|
**/ |
|
|
|
|
156
|
|
|
|
157
|
|
|
abstract public function transaction($command); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sends a command to the database and gets the affected rows |
161
|
|
|
* |
162
|
|
|
* @param string $prepare Prepared query string with placeholders |
163
|
|
|
* @param array $assoc Array with columns and values |
164
|
|
|
* @param boolean $replace If more than {pre} needs to be replaced |
165
|
|
|
* @param boolean $insertid Return the last insert id instead of a rowcount |
166
|
|
|
* @param boolean $log Defaults to true which enables log files if used |
167
|
|
|
* |
168
|
|
|
* @return integer |
169
|
|
|
**/ |
|
|
|
|
170
|
|
|
|
171
|
|
|
abstract public function exec( |
172
|
|
|
$prepare, array $assoc, $replace = false, $insertid = false, $log = true |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sends a query to the database and fetches the result |
177
|
|
|
* |
178
|
|
|
* @param string $prepare Prepared query string with placeholders |
179
|
|
|
* @param array $assoc Array with columns and values |
180
|
|
|
* @param integer $first Number of the first dataset to show |
181
|
|
|
* @param integer $max Number of datasets to show from first on |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
**/ |
|
|
|
|
185
|
|
|
|
186
|
|
|
abstract public function query($prepare, array $assoc, $first = 0, $max = 1); |
187
|
|
|
} |
188
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.