Rev 730 | Rev 830 | 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 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 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 | |||
511 | daniel-mar | 20 | if (!defined('INSIDE_OIDPLUS')) die(); |
21 | |||
730 | daniel-mar | 22 | abstract class OIDplusDatabaseConnection extends OIDplusBaseClass { |
295 | daniel-mar | 23 | protected /*bool*/ $connected = false; |
24 | protected /*?bool*/ $html = null; |
||
25 | protected /*?string*/ $last_query = null; |
||
502 | daniel-mar | 26 | protected /*bool*/ $slangDetectionDone = false; |
295 | daniel-mar | 27 | |
28 | protected abstract function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult; |
||
29 | public abstract function error(): string; |
||
30 | public abstract function transaction_begin()/*: void*/; |
||
31 | public abstract function transaction_commit()/*: void*/; |
||
32 | public abstract function transaction_rollback()/*: void*/; |
||
33 | public abstract function transaction_supported(): bool; |
||
34 | public abstract function transaction_level(): int; |
||
35 | protected abstract function doConnect()/*: void*/; |
||
36 | protected abstract function doDisconnect()/*: void*/; |
||
37 | |||
817 | daniel-mar | 38 | public function getPlugin()/*: ?OIDplusDatabasePlugin*/ { |
39 | $res = null; |
||
40 | $plugins = OIDplus::getDatabasePlugins(); |
||
41 | foreach ($plugins as $plugin) { |
||
42 | if (get_class($this) == get_class($plugin::newConnection($this))) { |
||
43 | return $plugin; |
||
44 | } |
||
45 | } |
||
46 | return $res; |
||
47 | } |
||
48 | |||
295 | daniel-mar | 49 | public function insert_id(): int { |
50 | // This is the "fallback" variant. If your database provider (e.g. PDO) supports |
||
51 | // a function to detect the last inserted id, please override this |
||
52 | // function in order to use that specialized function (since it is usually |
||
53 | // more reliable). |
||
316 | daniel-mar | 54 | return $this->getSlang()->insert_id($this); |
295 | daniel-mar | 55 | } |
56 | |||
57 | public final function query(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
||
58 | |||
59 | $query_logfile = OIDplus::baseConfig()->getValue('QUERY_LOGFILE', ''); |
||
60 | if (!empty($query_logfile)) { |
||
61 | $ts = explode(" ",microtime()); |
||
592 | daniel-mar | 62 | $ts = date("Y-m-d H:i:s",intval($ts[1])).substr((string)$ts[0],1,4); |
295 | daniel-mar | 63 | static $log_session_id = ""; |
64 | if (empty($log_session_id)) { |
||
65 | $log_session_id = rand(10000,99999); |
||
66 | } |
||
67 | $file = isset($_SERVER['REQUEST_URI']) ? ' | '.$_SERVER['REQUEST_URI'] : ''; |
||
68 | file_put_contents($query_logfile, "$ts <$log_session_id$file> $sql\n", FILE_APPEND); |
||
69 | } |
||
70 | |||
71 | $this->last_query = $sql; |
||
72 | $sql = str_replace('###', OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''), $sql); |
||
502 | daniel-mar | 73 | |
74 | if ($this->slangDetectionDone) { |
||
75 | $slang = $this->getSlang(); |
||
76 | if ($slang) { |
||
77 | $sql = $slang->filterQuery($sql); |
||
78 | } |
||
79 | } |
||
80 | |||
295 | daniel-mar | 81 | return $this->doQuery($sql, $prepared_args); |
82 | } |
||
83 | |||
84 | public final function connect()/*: void*/ { |
||
85 | if ($this->connected) return; |
||
86 | $this->beforeConnect(); |
||
87 | $this->doConnect(); |
||
88 | $this->connected = true; |
||
639 | daniel-mar | 89 | OIDplus::register_shutdown_function(array($this, 'disconnect')); |
318 | daniel-mar | 90 | $this->afterConnectMandatory(); |
295 | daniel-mar | 91 | $this->afterConnect(); |
92 | } |
||
93 | |||
94 | public final function disconnect()/*: void*/ { |
||
95 | if (!$this->connected) return; |
||
96 | $this->beforeDisconnect(); |
||
97 | $this->doDisconnect(); |
||
98 | $this->connected = false; |
||
99 | $this->afterDisconnect(); |
||
100 | } |
||
101 | |||
102 | public function natOrder($fieldname, $order='asc'): string { |
||
325 | daniel-mar | 103 | $slang = $this->getSlang(); |
104 | if (!is_null($slang)) { |
||
105 | return $slang->natOrder($fieldname, $order); |
||
295 | daniel-mar | 106 | } else { |
107 | $order = strtolower($order); |
||
108 | if (($order != 'asc') && ($order != 'desc')) { |
||
360 | daniel-mar | 109 | throw new OIDplusException(_L('Invalid order "%1" (needs to be "asc" or "desc")',$order)); |
295 | daniel-mar | 110 | } |
111 | |||
112 | // For (yet) unsupported DBMS, we do not offer natural sort |
||
113 | return "$fieldname $order"; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | protected function beforeDisconnect()/*: void*/ {} |
||
118 | |||
119 | protected function afterDisconnect()/*: void*/ {} |
||
120 | |||
121 | protected function beforeConnect()/*: void*/ {} |
||
122 | |||
318 | daniel-mar | 123 | protected function afterConnect()/*: void*/ {} |
124 | |||
125 | private function afterConnectMandatory()/*: void*/ { |
||
295 | daniel-mar | 126 | // Check if the config table exists. This is important because the database version is stored in it |
127 | $this->initRequireTables(array('config')); |
||
128 | |||
129 | // Do the database tables need an update? |
||
130 | // It is important that we do it immediately after connecting, |
||
131 | // because the database structure might change and therefore various things might fail. |
||
132 | // Note: The config setting "database_version" is inserted in setup/sql/...sql, not in the OIDplus core init |
||
133 | |||
134 | $res = $this->query("SELECT value FROM ###config WHERE name = 'database_version'"); |
||
135 | $row = $res->fetch_array(); |
||
136 | if ($row == null) { |
||
360 | daniel-mar | 137 | throw new OIDplusConfigInitializationException(_L('Cannot determine database version (the entry "database_version" inside the table "###config" is probably missing)')); |
295 | daniel-mar | 138 | } |
139 | $version = $row['value']; |
||
140 | if (!is_numeric($version) || ($version < 200) || ($version > 999)) { |
||
360 | daniel-mar | 141 | throw new OIDplusConfigInitializationException(_L('Entry "database_version" inside the table "###config" seems to be wrong (expect number between 200 and 999)')); |
295 | daniel-mar | 142 | } |
502 | daniel-mar | 143 | |
496 | daniel-mar | 144 | $update_files = glob(OIDplus::localpath().'includes/db_updates/update*.inc.php'); |
386 | daniel-mar | 145 | foreach ($update_files as $update_file) { |
146 | include_once $update_file; |
||
147 | } |
||
148 | while (function_exists($function_name = "oidplus_dbupdate_".$version."_".($version+1))) { |
||
295 | daniel-mar | 149 | $prev_version = $version; |
386 | daniel-mar | 150 | $function_name($this, $version); |
295 | daniel-mar | 151 | if ($version != $prev_version+1) { |
152 | // This should usually not happen, since the update-file should increase the version |
||
153 | // or throw an Exception by itself |
||
360 | daniel-mar | 154 | throw new OIDplusException(_L('Database update %1 -> %2 failed (script reports new version to be %3)',$prev_version,$prev_version+1,$version)); |
295 | daniel-mar | 155 | } |
156 | } |
||
157 | |||
158 | // Now that our database is up-to-date, we check if database tables are existing |
||
159 | // without config table, because it was checked above |
||
160 | $this->initRequireTables(array('objects', 'asn1id', 'iri', 'ra'/*, 'config'*/)); |
||
316 | daniel-mar | 161 | |
162 | // In case an auto-detection of the slang is required (for generic providers like PDO or ODBC), |
||
163 | // we must not be inside a transaction, because the detection requires intentionally submitting |
||
164 | // invalid queries to detect the correct DBMS. If we would be inside a transaction, providers like |
||
165 | // PDO would automatically roll-back. Therefore, we detect the slang right at the beginning, |
||
166 | // before any transaction is used. |
||
167 | $this->getSlang(); |
||
295 | daniel-mar | 168 | } |
169 | |||
170 | private function initRequireTables($tableNames)/*: void*/ { |
||
171 | $msgs = array(); |
||
172 | foreach ($tableNames as $tableName) { |
||
173 | $prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''); |
||
174 | if (!$this->tableExists($prefix.$tableName)) { |
||
360 | daniel-mar | 175 | $msgs[] = _L('Table %1 is missing!',$prefix.$tableName); |
295 | daniel-mar | 176 | } |
177 | } |
||
178 | if (count($msgs) > 0) { |
||
179 | throw new OIDplusConfigInitializationException(implode("\n\n",$msgs)); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | public function tableExists($tableName): bool { |
||
184 | try { |
||
318 | daniel-mar | 185 | // Attention: This query could interrupt transactions if Rollback-On-Error is enabled |
295 | daniel-mar | 186 | $this->query("select 0 from ".$tableName." where 1=0"); |
187 | return true; |
||
188 | } catch (Exception $e) { |
||
189 | return false; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | public function isConnected(): bool { |
||
194 | return $this->connected; |
||
195 | } |
||
196 | |||
197 | public function init($html = true)/*: void*/ { |
||
198 | $this->html = $html; |
||
199 | } |
||
200 | |||
201 | public function sqlDate(): string { |
||
325 | daniel-mar | 202 | $slang = $this->getSlang(); |
203 | if (!is_null($slang)) { |
||
204 | return $slang->sqlDate(); |
||
295 | daniel-mar | 205 | } else { |
489 | daniel-mar | 206 | return "'" . date('Y-m-d H:i:s') . "'"; |
295 | daniel-mar | 207 | } |
208 | } |
||
209 | |||
502 | daniel-mar | 210 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
211 | $res = null; |
||
326 | daniel-mar | 212 | |
502 | daniel-mar | 213 | if (OIDplus::baseConfig()->exists('FORCE_DBMS_SLANG')) { |
214 | $name = OIDplus::baseConfig()->getValue('FORCE_DBMS_SLANG', ''); |
||
215 | $res = OIDplus::getSqlSlangPlugin($name); |
||
216 | if ($mustExist && is_null($res)) { |
||
217 | throw new OIDplusConfigInitializationException(_L('Enforced SQL slang (via setting FORCE_DBMS_SLANG) "%1" does not exist.',$name)); |
||
218 | } |
||
219 | } else { |
||
220 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
221 | if ($plugin->detect($this)) { |
||
222 | if (OIDplus::baseConfig()->getValue('DEBUG') && !is_null($res)) { |
||
223 | throw new OIDplusException(_L('DB-Slang detection failed: Multiple slangs were detected. Use base config setting FORCE_DBMS_SLANG to define one.')); |
||
224 | } |
||
225 | |||
226 | $res = $plugin; |
||
227 | |||
228 | if (!OIDplus::baseConfig()->getValue('DEBUG')) { |
||
295 | daniel-mar | 229 | break; |
230 | } |
||
231 | } |
||
232 | } |
||
502 | daniel-mar | 233 | if ($mustExist && is_null($res)) { |
234 | throw new OIDplusException(_L('Cannot determine the SQL slang of your DBMS. Your DBMS is probably not supported.')); |
||
235 | } |
||
295 | daniel-mar | 236 | } |
237 | |||
502 | daniel-mar | 238 | return $res; |
239 | } |
||
240 | |||
241 | public final function getSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
||
242 | static /*?OIDplusSqlSlangPlugin*/ $slangCache = null; |
||
243 | |||
244 | if ($this->slangDetectionDone) { |
||
245 | return $slangCache; |
||
246 | } |
||
247 | |||
248 | $slangCache = $this->doGetSlang(); |
||
249 | $this->slangDetectionDone = true; |
||
326 | daniel-mar | 250 | return $slangCache; |
295 | daniel-mar | 251 | } |
489 | daniel-mar | 252 | } |