Rev 1233 | Rev 1238 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
295 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
295 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
511 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
730 | daniel-mar | 26 | abstract class OIDplusDatabaseConnection extends OIDplusBaseClass { |
1130 | daniel-mar | 27 | /** |
28 | * @var bool |
||
29 | */ |
||
295 | daniel-mar | 30 | protected /*bool*/ $connected = false; |
1130 | daniel-mar | 31 | |
32 | /** |
||
33 | * @var bool|null |
||
34 | */ |
||
295 | daniel-mar | 35 | protected /*?bool*/ $html = null; |
1130 | daniel-mar | 36 | |
37 | /** |
||
38 | * @var string|null |
||
39 | */ |
||
295 | daniel-mar | 40 | protected /*?string*/ $last_query = null; |
1130 | daniel-mar | 41 | |
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
502 | daniel-mar | 45 | protected /*bool*/ $slangDetectionDone = false; |
295 | daniel-mar | 46 | |
1116 | daniel-mar | 47 | /** |
48 | * @param string $sql |
||
49 | * @param array|null $prepared_args |
||
50 | * @return OIDplusQueryResult |
||
51 | * @throws OIDplusException |
||
52 | */ |
||
53 | protected abstract function doQuery(string $sql, array $prepared_args=null): OIDplusQueryResult; |
||
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
295 | daniel-mar | 58 | public abstract function error(): string; |
1116 | daniel-mar | 59 | |
60 | /** |
||
61 | * @return void |
||
62 | */ |
||
295 | daniel-mar | 63 | public abstract function transaction_begin()/*: void*/; |
1116 | daniel-mar | 64 | |
65 | /** |
||
66 | * @return void |
||
67 | */ |
||
295 | daniel-mar | 68 | public abstract function transaction_commit()/*: void*/; |
1116 | daniel-mar | 69 | |
70 | /** |
||
71 | * @return void |
||
72 | */ |
||
295 | daniel-mar | 73 | public abstract function transaction_rollback()/*: void*/; |
1116 | daniel-mar | 74 | |
75 | /** |
||
76 | * @return bool |
||
77 | */ |
||
295 | daniel-mar | 78 | public abstract function transaction_supported(): bool; |
1116 | daniel-mar | 79 | |
80 | /** |
||
81 | * @return int |
||
82 | */ |
||
295 | daniel-mar | 83 | public abstract function transaction_level(): int; |
1116 | daniel-mar | 84 | |
85 | /** |
||
86 | * @return void |
||
87 | */ |
||
295 | daniel-mar | 88 | protected abstract function doConnect()/*: void*/; |
1116 | daniel-mar | 89 | |
90 | /** |
||
91 | * @return void |
||
92 | */ |
||
295 | daniel-mar | 93 | protected abstract function doDisconnect()/*: void*/; |
94 | |||
1116 | daniel-mar | 95 | /** |
96 | * @return OIDplusDatabasePlugin|null |
||
97 | */ |
||
817 | daniel-mar | 98 | public function getPlugin()/*: ?OIDplusDatabasePlugin*/ { |
99 | $plugins = OIDplus::getDatabasePlugins(); |
||
100 | foreach ($plugins as $plugin) { |
||
1116 | daniel-mar | 101 | if (get_class($this) == get_class($plugin::newConnection())) { |
817 | daniel-mar | 102 | return $plugin; |
103 | } |
||
104 | } |
||
1130 | daniel-mar | 105 | return null; |
817 | daniel-mar | 106 | } |
107 | |||
1116 | daniel-mar | 108 | /** |
109 | * @return int |
||
1160 | daniel-mar | 110 | * @throws OIDplusConfigInitializationException |
1116 | daniel-mar | 111 | * @throws OIDplusException |
112 | */ |
||
1160 | daniel-mar | 113 | protected function doInsertId(): int { |
295 | daniel-mar | 114 | // This is the "fallback" variant. If your database provider (e.g. PDO) supports |
115 | // a function to detect the last inserted id, please override this |
||
116 | // function in order to use that specialized function (since it is usually |
||
117 | // more reliable). |
||
316 | daniel-mar | 118 | return $this->getSlang()->insert_id($this); |
295 | daniel-mar | 119 | } |
120 | |||
1116 | daniel-mar | 121 | /** |
1160 | daniel-mar | 122 | * @return int |
123 | * @throws OIDplusException |
||
124 | */ |
||
125 | public final function insert_id(): int { |
||
126 | // DM 04 Apr 2023: Added, because MSSQL's @@IDENTITY, PgSQL, and SQLite3 does not reset after |
||
127 | // a Non-Insert query (this is a test case in dev/test_database_plugins). |
||
128 | // Note that the INSERT could be hidden inside a Stored Procedure; we don't support (or need) that yet. |
||
129 | if (!str_starts_with(trim(strtolower($this->last_query)),'insert')) return 0; |
||
130 | |||
131 | return $this->doInsertId(); |
||
132 | } |
||
133 | |||
134 | /** |
||
1226 | daniel-mar | 135 | * Get the rows affected, for either SELECT, INSERT, DELETE, UPDATE |
136 | * @return int |
||
137 | */ |
||
138 | public function rowsAffected(): int { |
||
139 | return -1; // -1 means not implemented |
||
140 | } |
||
141 | |||
142 | /** |
||
1116 | daniel-mar | 143 | * @param string $sql |
144 | * @return array[] |
||
145 | * @throws OIDplusException |
||
146 | */ |
||
147 | public final function getTable(string $sql): array { |
||
990 | daniel-mar | 148 | $out = array(); |
989 | daniel-mar | 149 | $res = $this->query($sql); |
990 | daniel-mar | 150 | while ($row = $res->fetch_array()) { |
1226 | daniel-mar | 151 | $out[] = /*yield*/ $row; |
990 | daniel-mar | 152 | } |
153 | return $out; |
||
154 | } |
||
155 | |||
1116 | daniel-mar | 156 | /** |
157 | * @param string $sql |
||
158 | * @return mixed|null |
||
159 | * @throws OIDplusException |
||
160 | */ |
||
990 | daniel-mar | 161 | public final function getScalar(string $sql) { |
162 | $res = $this->query($sql); |
||
989 | daniel-mar | 163 | $row = $res->fetch_array(); |
990 | daniel-mar | 164 | return $row ? reset($row) : null; |
989 | daniel-mar | 165 | } |
166 | |||
1116 | daniel-mar | 167 | /** |
168 | * @param string $sql |
||
169 | * @param array|null $prepared_args |
||
170 | * @return OIDplusQueryResult |
||
171 | * @throws OIDplusException |
||
172 | */ |
||
1130 | daniel-mar | 173 | public final function query(string $sql, array $prepared_args=null): OIDplusQueryResult { |
295 | daniel-mar | 174 | |
175 | $query_logfile = OIDplus::baseConfig()->getValue('QUERY_LOGFILE', ''); |
||
176 | if (!empty($query_logfile)) { |
||
177 | $ts = explode(" ",microtime()); |
||
592 | daniel-mar | 178 | $ts = date("Y-m-d H:i:s",intval($ts[1])).substr((string)$ts[0],1,4); |
295 | daniel-mar | 179 | static $log_session_id = ""; |
180 | if (empty($log_session_id)) { |
||
181 | $log_session_id = rand(10000,99999); |
||
182 | } |
||
183 | $file = isset($_SERVER['REQUEST_URI']) ? ' | '.$_SERVER['REQUEST_URI'] : ''; |
||
184 | file_put_contents($query_logfile, "$ts <$log_session_id$file> $sql\n", FILE_APPEND); |
||
185 | } |
||
186 | |||
187 | $this->last_query = $sql; |
||
188 | $sql = str_replace('###', OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''), $sql); |
||
502 | daniel-mar | 189 | |
1235 | daniel-mar | 190 | if ($this->slangDetectionDone || OIDplus::baseConfig()->exists('FORCE_DBMS_SLANG')) { |
502 | daniel-mar | 191 | $slang = $this->getSlang(); |
192 | if ($slang) { |
||
193 | $sql = $slang->filterQuery($sql); |
||
194 | } |
||
195 | } |
||
196 | |||
295 | daniel-mar | 197 | return $this->doQuery($sql, $prepared_args); |
198 | } |
||
199 | |||
1116 | daniel-mar | 200 | /** |
201 | * @return void |
||
202 | * @throws OIDplusException |
||
203 | */ |
||
295 | daniel-mar | 204 | public final function connect()/*: void*/ { |
205 | if ($this->connected) return; |
||
1177 | daniel-mar | 206 | |
207 | $bcKeys = OIDplus::baseConfig()->getAllKeys(); |
||
208 | foreach ($bcKeys as $bkKey) { |
||
209 | $val = OIDplus::baseConfig()->getValue($bkKey, ''); |
||
210 | if (is_string($val) && preg_match('@(userdata[/\\\\]database[/\\\\]oidplus_(empty|example)\\.(db|db3|sqlite|sqlite3|mdb|accdb))@i', $val, $m)) { |
||
211 | throw new OIDplusConfigInitializationException(_L('It looks like you are trying to use the template database file %1 in your base configuration. Since this file gets overridden by software updates, you must copy the template file and use this copy instead.', $m[1])); |
||
212 | } |
||
213 | } |
||
214 | |||
295 | daniel-mar | 215 | $this->beforeConnect(); |
216 | $this->doConnect(); |
||
217 | $this->connected = true; |
||
639 | daniel-mar | 218 | OIDplus::register_shutdown_function(array($this, 'disconnect')); |
318 | daniel-mar | 219 | $this->afterConnectMandatory(); |
295 | daniel-mar | 220 | $this->afterConnect(); |
221 | } |
||
222 | |||
1116 | daniel-mar | 223 | /** |
224 | * @return void |
||
225 | */ |
||
295 | daniel-mar | 226 | public final function disconnect()/*: void*/ { |
227 | if (!$this->connected) return; |
||
228 | $this->beforeDisconnect(); |
||
229 | $this->doDisconnect(); |
||
230 | $this->connected = false; |
||
231 | $this->afterDisconnect(); |
||
232 | } |
||
233 | |||
1116 | daniel-mar | 234 | /** |
235 | * @return void |
||
236 | */ |
||
295 | daniel-mar | 237 | protected function beforeDisconnect()/*: void*/ {} |
238 | |||
1116 | daniel-mar | 239 | /** |
240 | * @return void |
||
241 | */ |
||
295 | daniel-mar | 242 | protected function afterDisconnect()/*: void*/ {} |
243 | |||
1116 | daniel-mar | 244 | /** |
245 | * @return void |
||
246 | */ |
||
295 | daniel-mar | 247 | protected function beforeConnect()/*: void*/ {} |
248 | |||
1116 | daniel-mar | 249 | /** |
250 | * @return void |
||
251 | */ |
||
318 | daniel-mar | 252 | protected function afterConnect()/*: void*/ {} |
253 | |||
1116 | daniel-mar | 254 | /** |
255 | * @return void |
||
256 | * @throws OIDplusConfigInitializationException |
||
257 | * @throws OIDplusException |
||
258 | */ |
||
318 | daniel-mar | 259 | private function afterConnectMandatory()/*: void*/ { |
295 | daniel-mar | 260 | // Check if the config table exists. This is important because the database version is stored in it |
261 | $this->initRequireTables(array('config')); |
||
262 | |||
263 | // Do the database tables need an update? |
||
264 | // It is important that we do it immediately after connecting, |
||
265 | // because the database structure might change and therefore various things might fail. |
||
830 | daniel-mar | 266 | require_once __DIR__.'/../db_updates/run.inc.php'; |
267 | oidplus_dbupdate($this); |
||
295 | daniel-mar | 268 | |
269 | // Now that our database is up-to-date, we check if database tables are existing |
||
270 | // without config table, because it was checked above |
||
271 | $this->initRequireTables(array('objects', 'asn1id', 'iri', 'ra'/*, 'config'*/)); |
||
316 | daniel-mar | 272 | |
273 | // In case an auto-detection of the slang is required (for generic providers like PDO or ODBC), |
||
274 | // we must not be inside a transaction, because the detection requires intentionally submitting |
||
275 | // invalid queries to detect the correct DBMS. If we would be inside a transaction, providers like |
||
276 | // PDO would automatically roll-back. Therefore, we detect the slang right at the beginning, |
||
277 | // before any transaction is used. |
||
278 | $this->getSlang(); |
||
295 | daniel-mar | 279 | } |
280 | |||
1116 | daniel-mar | 281 | /** |
282 | * @param string[] $tableNames |
||
283 | * @return void |
||
284 | * @throws OIDplusConfigInitializationException |
||
285 | * @throws OIDplusException |
||
286 | */ |
||
287 | private function initRequireTables(array $tableNames)/*: void*/ { |
||
295 | daniel-mar | 288 | $msgs = array(); |
289 | foreach ($tableNames as $tableName) { |
||
290 | $prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''); |
||
291 | if (!$this->tableExists($prefix.$tableName)) { |
||
360 | daniel-mar | 292 | $msgs[] = _L('Table %1 is missing!',$prefix.$tableName); |
295 | daniel-mar | 293 | } |
294 | } |
||
1226 | daniel-mar | 295 | // TODO: If there is a general database error (e.g. locked database, etc.) then you will receive the false warning "Table oidplus_config missing" |
295 | daniel-mar | 296 | if (count($msgs) > 0) { |
297 | throw new OIDplusConfigInitializationException(implode("\n\n",$msgs)); |
||
298 | } |
||
299 | } |
||
300 | |||
1116 | daniel-mar | 301 | /** |
302 | * @param string $tableName |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function tableExists(string $tableName): bool { |
||
295 | daniel-mar | 306 | try { |
318 | daniel-mar | 307 | // Attention: This query could interrupt transactions if Rollback-On-Error is enabled |
295 | daniel-mar | 308 | $this->query("select 0 from ".$tableName." where 1=0"); |
309 | return true; |
||
1050 | daniel-mar | 310 | } catch (\Exception $e) { |
295 | daniel-mar | 311 | return false; |
312 | } |
||
313 | } |
||
314 | |||
1116 | daniel-mar | 315 | /** |
316 | * @return bool |
||
317 | */ |
||
295 | daniel-mar | 318 | public function isConnected(): bool { |
319 | return $this->connected; |
||
320 | } |
||
321 | |||
1116 | daniel-mar | 322 | /** |
323 | * @param bool $html |
||
324 | * @return void |
||
325 | */ |
||
326 | public function init(bool $html = true)/*: void*/ { |
||
295 | daniel-mar | 327 | $this->html = $html; |
328 | } |
||
329 | |||
1116 | daniel-mar | 330 | /** |
331 | * @return string |
||
332 | * @throws OIDplusException |
||
333 | */ |
||
295 | daniel-mar | 334 | public function sqlDate(): string { |
325 | daniel-mar | 335 | $slang = $this->getSlang(); |
336 | if (!is_null($slang)) { |
||
337 | return $slang->sqlDate(); |
||
295 | daniel-mar | 338 | } else { |
1233 | daniel-mar | 339 | // Fallback: Take the server date |
489 | daniel-mar | 340 | return "'" . date('Y-m-d H:i:s') . "'"; |
295 | daniel-mar | 341 | } |
342 | } |
||
343 | |||
1116 | daniel-mar | 344 | /** |
345 | * @param bool $mustExist |
||
346 | * @return OIDplusSqlSlangPlugin|null |
||
347 | * @throws OIDplusConfigInitializationException |
||
348 | * @throws OIDplusException |
||
349 | */ |
||
502 | daniel-mar | 350 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
351 | $res = null; |
||
326 | daniel-mar | 352 | |
502 | daniel-mar | 353 | if (OIDplus::baseConfig()->exists('FORCE_DBMS_SLANG')) { |
354 | $name = OIDplus::baseConfig()->getValue('FORCE_DBMS_SLANG', ''); |
||
355 | $res = OIDplus::getSqlSlangPlugin($name); |
||
356 | if ($mustExist && is_null($res)) { |
||
357 | throw new OIDplusConfigInitializationException(_L('Enforced SQL slang (via setting FORCE_DBMS_SLANG) "%1" does not exist.',$name)); |
||
358 | } |
||
359 | } else { |
||
360 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
361 | if ($plugin->detect($this)) { |
||
362 | if (OIDplus::baseConfig()->getValue('DEBUG') && !is_null($res)) { |
||
1160 | daniel-mar | 363 | |
364 | $detected = array(); |
||
365 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
366 | if ($plugin->detect($this)) { |
||
367 | $detected[] = get_class($plugin); |
||
368 | } |
||
369 | } |
||
370 | |||
371 | throw new OIDplusException(_L('DB-Slang detection failed: Multiple slangs were detected (%1). Use base config setting FORCE_DBMS_SLANG to define one.', implode(', ',$detected))); |
||
502 | daniel-mar | 372 | } |
373 | |||
374 | $res = $plugin; |
||
375 | |||
376 | if (!OIDplus::baseConfig()->getValue('DEBUG')) { |
||
295 | daniel-mar | 377 | break; |
378 | } |
||
379 | } |
||
380 | } |
||
502 | daniel-mar | 381 | if ($mustExist && is_null($res)) { |
382 | throw new OIDplusException(_L('Cannot determine the SQL slang of your DBMS. Your DBMS is probably not supported.')); |
||
383 | } |
||
295 | daniel-mar | 384 | } |
385 | |||
502 | daniel-mar | 386 | return $res; |
387 | } |
||
388 | |||
1116 | daniel-mar | 389 | /** |
390 | * @param bool $mustExist |
||
391 | * @return OIDplusSqlSlangPlugin|null |
||
392 | * @throws OIDplusConfigInitializationException |
||
393 | * @throws OIDplusException |
||
394 | */ |
||
502 | daniel-mar | 395 | public final function getSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
396 | static /*?OIDplusSqlSlangPlugin*/ $slangCache = null; |
||
397 | |||
398 | if ($this->slangDetectionDone) { |
||
399 | return $slangCache; |
||
400 | } |
||
401 | |||
402 | $slangCache = $this->doGetSlang(); |
||
403 | $this->slangDetectionDone = true; |
||
326 | daniel-mar | 404 | return $slangCache; |
295 | daniel-mar | 405 | } |
1220 | daniel-mar | 406 | |
407 | /** |
||
408 | * @return array |
||
409 | */ |
||
410 | public function getExtendedInfo(): array { |
||
411 | return array(); |
||
412 | } |
||
489 | daniel-mar | 413 | } |