Rev 1240 | Rev 1447 | 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 | |
1238 | daniel-mar | 190 | if ($this->slangDetectionDone) { |
502 | daniel-mar | 191 | $slang = $this->getSlang(); |
192 | if ($slang) { |
||
193 | $sql = $slang->filterQuery($sql); |
||
194 | } |
||
195 | } |
||
196 | |||
1240 | daniel-mar | 197 | $res = $this->doQuery($sql, $prepared_args); |
198 | if ($this->slangDetectionDone) $this->getSlang()->reviewResult($res, $sql, $prepared_args); |
||
199 | return $res; |
||
295 | daniel-mar | 200 | } |
201 | |||
1116 | daniel-mar | 202 | /** |
203 | * @return void |
||
204 | * @throws OIDplusException |
||
205 | */ |
||
295 | daniel-mar | 206 | public final function connect()/*: void*/ { |
207 | if ($this->connected) return; |
||
1177 | daniel-mar | 208 | |
209 | $bcKeys = OIDplus::baseConfig()->getAllKeys(); |
||
210 | foreach ($bcKeys as $bkKey) { |
||
211 | $val = OIDplus::baseConfig()->getValue($bkKey, ''); |
||
212 | if (is_string($val) && preg_match('@(userdata[/\\\\]database[/\\\\]oidplus_(empty|example)\\.(db|db3|sqlite|sqlite3|mdb|accdb))@i', $val, $m)) { |
||
213 | 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])); |
||
214 | } |
||
215 | } |
||
216 | |||
295 | daniel-mar | 217 | $this->beforeConnect(); |
218 | $this->doConnect(); |
||
219 | $this->connected = true; |
||
639 | daniel-mar | 220 | OIDplus::register_shutdown_function(array($this, 'disconnect')); |
318 | daniel-mar | 221 | $this->afterConnectMandatory(); |
295 | daniel-mar | 222 | $this->afterConnect(); |
223 | } |
||
224 | |||
1116 | daniel-mar | 225 | /** |
226 | * @return void |
||
227 | */ |
||
295 | daniel-mar | 228 | public final function disconnect()/*: void*/ { |
229 | if (!$this->connected) return; |
||
230 | $this->beforeDisconnect(); |
||
231 | $this->doDisconnect(); |
||
232 | $this->connected = false; |
||
233 | $this->afterDisconnect(); |
||
234 | } |
||
235 | |||
1116 | daniel-mar | 236 | /** |
237 | * @return void |
||
238 | */ |
||
295 | daniel-mar | 239 | protected function beforeDisconnect()/*: void*/ {} |
240 | |||
1116 | daniel-mar | 241 | /** |
242 | * @return void |
||
243 | */ |
||
295 | daniel-mar | 244 | protected function afterDisconnect()/*: void*/ {} |
245 | |||
1116 | daniel-mar | 246 | /** |
247 | * @return void |
||
248 | */ |
||
295 | daniel-mar | 249 | protected function beforeConnect()/*: void*/ {} |
250 | |||
1116 | daniel-mar | 251 | /** |
252 | * @return void |
||
253 | */ |
||
318 | daniel-mar | 254 | protected function afterConnect()/*: void*/ {} |
255 | |||
1116 | daniel-mar | 256 | /** |
257 | * @return void |
||
258 | * @throws OIDplusConfigInitializationException |
||
259 | * @throws OIDplusException |
||
260 | */ |
||
318 | daniel-mar | 261 | private function afterConnectMandatory()/*: void*/ { |
1238 | daniel-mar | 262 | // In case an auto-detection of the slang is required (for generic providers like PDO or ODBC), |
263 | // we must not be inside a transaction, because the detection requires intentionally submitting |
||
264 | // invalid queries to detect the correct DBMS. If we would be inside a transaction, providers like |
||
265 | // PDO would automatically roll-back. Therefore, we detect the slang right at the beginning, |
||
266 | // before any transaction is used. |
||
267 | $this->getSlang(); |
||
268 | |||
295 | daniel-mar | 269 | // Check if the config table exists. This is important because the database version is stored in it |
270 | $this->initRequireTables(array('config')); |
||
271 | |||
272 | // Do the database tables need an update? |
||
273 | // It is important that we do it immediately after connecting, |
||
274 | // because the database structure might change and therefore various things might fail. |
||
830 | daniel-mar | 275 | require_once __DIR__.'/../db_updates/run.inc.php'; |
276 | oidplus_dbupdate($this); |
||
295 | daniel-mar | 277 | |
278 | // Now that our database is up-to-date, we check if database tables are existing |
||
279 | // without config table, because it was checked above |
||
280 | $this->initRequireTables(array('objects', 'asn1id', 'iri', 'ra'/*, 'config'*/)); |
||
281 | } |
||
282 | |||
1116 | daniel-mar | 283 | /** |
284 | * @param string[] $tableNames |
||
285 | * @return void |
||
286 | * @throws OIDplusConfigInitializationException |
||
287 | * @throws OIDplusException |
||
288 | */ |
||
289 | private function initRequireTables(array $tableNames)/*: void*/ { |
||
295 | daniel-mar | 290 | $msgs = array(); |
1241 | daniel-mar | 291 | |
292 | // Check for a general database error like a locked file DBMS |
||
293 | // which would raise a false warning "Table oidplus_config missing" |
||
294 | $this->query("select 0"); |
||
295 | |||
296 | $prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''); |
||
295 | daniel-mar | 297 | foreach ($tableNames as $tableName) { |
298 | if (!$this->tableExists($prefix.$tableName)) { |
||
360 | daniel-mar | 299 | $msgs[] = _L('Table %1 is missing!',$prefix.$tableName); |
295 | daniel-mar | 300 | } |
301 | } |
||
302 | if (count($msgs) > 0) { |
||
303 | throw new OIDplusConfigInitializationException(implode("\n\n",$msgs)); |
||
304 | } |
||
305 | } |
||
306 | |||
1116 | daniel-mar | 307 | /** |
308 | * @param string $tableName |
||
309 | * @return bool |
||
310 | */ |
||
311 | public function tableExists(string $tableName): bool { |
||
295 | daniel-mar | 312 | try { |
318 | daniel-mar | 313 | // Attention: This query could interrupt transactions if Rollback-On-Error is enabled |
295 | daniel-mar | 314 | $this->query("select 0 from ".$tableName." where 1=0"); |
315 | return true; |
||
1050 | daniel-mar | 316 | } catch (\Exception $e) { |
295 | daniel-mar | 317 | return false; |
318 | } |
||
319 | } |
||
320 | |||
1116 | daniel-mar | 321 | /** |
322 | * @return bool |
||
323 | */ |
||
295 | daniel-mar | 324 | public function isConnected(): bool { |
325 | return $this->connected; |
||
326 | } |
||
327 | |||
1116 | daniel-mar | 328 | /** |
329 | * @param bool $html |
||
330 | * @return void |
||
331 | */ |
||
332 | public function init(bool $html = true)/*: void*/ { |
||
295 | daniel-mar | 333 | $this->html = $html; |
334 | } |
||
335 | |||
1116 | daniel-mar | 336 | /** |
337 | * @return string |
||
338 | * @throws OIDplusException |
||
339 | */ |
||
295 | daniel-mar | 340 | public function sqlDate(): string { |
325 | daniel-mar | 341 | $slang = $this->getSlang(); |
342 | if (!is_null($slang)) { |
||
343 | return $slang->sqlDate(); |
||
295 | daniel-mar | 344 | } else { |
1233 | daniel-mar | 345 | // Fallback: Take the server date |
489 | daniel-mar | 346 | return "'" . date('Y-m-d H:i:s') . "'"; |
295 | daniel-mar | 347 | } |
348 | } |
||
349 | |||
1116 | daniel-mar | 350 | /** |
351 | * @param bool $mustExist |
||
352 | * @return OIDplusSqlSlangPlugin|null |
||
353 | * @throws OIDplusConfigInitializationException |
||
354 | * @throws OIDplusException |
||
355 | */ |
||
502 | daniel-mar | 356 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
357 | $res = null; |
||
326 | daniel-mar | 358 | |
502 | daniel-mar | 359 | if (OIDplus::baseConfig()->exists('FORCE_DBMS_SLANG')) { |
360 | $name = OIDplus::baseConfig()->getValue('FORCE_DBMS_SLANG', ''); |
||
361 | $res = OIDplus::getSqlSlangPlugin($name); |
||
362 | if ($mustExist && is_null($res)) { |
||
363 | throw new OIDplusConfigInitializationException(_L('Enforced SQL slang (via setting FORCE_DBMS_SLANG) "%1" does not exist.',$name)); |
||
364 | } |
||
365 | } else { |
||
366 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
367 | if ($plugin->detect($this)) { |
||
368 | if (OIDplus::baseConfig()->getValue('DEBUG') && !is_null($res)) { |
||
1160 | daniel-mar | 369 | |
370 | $detected = array(); |
||
371 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
372 | if ($plugin->detect($this)) { |
||
373 | $detected[] = get_class($plugin); |
||
374 | } |
||
375 | } |
||
376 | |||
377 | 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 | 378 | } |
379 | |||
380 | $res = $plugin; |
||
381 | |||
382 | if (!OIDplus::baseConfig()->getValue('DEBUG')) { |
||
295 | daniel-mar | 383 | break; |
384 | } |
||
385 | } |
||
386 | } |
||
502 | daniel-mar | 387 | if ($mustExist && is_null($res)) { |
388 | throw new OIDplusException(_L('Cannot determine the SQL slang of your DBMS. Your DBMS is probably not supported.')); |
||
389 | } |
||
295 | daniel-mar | 390 | } |
391 | |||
502 | daniel-mar | 392 | return $res; |
393 | } |
||
394 | |||
1116 | daniel-mar | 395 | /** |
396 | * @param bool $mustExist |
||
397 | * @return OIDplusSqlSlangPlugin|null |
||
398 | * @throws OIDplusConfigInitializationException |
||
399 | * @throws OIDplusException |
||
400 | */ |
||
502 | daniel-mar | 401 | public final function getSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
402 | static /*?OIDplusSqlSlangPlugin*/ $slangCache = null; |
||
403 | |||
404 | if ($this->slangDetectionDone) { |
||
405 | return $slangCache; |
||
406 | } |
||
407 | |||
408 | $slangCache = $this->doGetSlang(); |
||
409 | $this->slangDetectionDone = true; |
||
326 | daniel-mar | 410 | return $slangCache; |
295 | daniel-mar | 411 | } |
1220 | daniel-mar | 412 | |
413 | /** |
||
414 | * @return array |
||
415 | */ |
||
416 | public function getExtendedInfo(): array { |
||
417 | return array(); |
||
418 | } |
||
489 | daniel-mar | 419 | } |