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