Rev 817 | Rev 989 | 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. |
||
830 | daniel-mar | 132 | require_once __DIR__.'/../db_updates/run.inc.php'; |
133 | oidplus_dbupdate($this); |
||
295 | daniel-mar | 134 | |
135 | // Now that our database is up-to-date, we check if database tables are existing |
||
136 | // without config table, because it was checked above |
||
137 | $this->initRequireTables(array('objects', 'asn1id', 'iri', 'ra'/*, 'config'*/)); |
||
316 | daniel-mar | 138 | |
139 | // In case an auto-detection of the slang is required (for generic providers like PDO or ODBC), |
||
140 | // we must not be inside a transaction, because the detection requires intentionally submitting |
||
141 | // invalid queries to detect the correct DBMS. If we would be inside a transaction, providers like |
||
142 | // PDO would automatically roll-back. Therefore, we detect the slang right at the beginning, |
||
143 | // before any transaction is used. |
||
144 | $this->getSlang(); |
||
295 | daniel-mar | 145 | } |
146 | |||
147 | private function initRequireTables($tableNames)/*: void*/ { |
||
148 | $msgs = array(); |
||
149 | foreach ($tableNames as $tableName) { |
||
150 | $prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''); |
||
151 | if (!$this->tableExists($prefix.$tableName)) { |
||
360 | daniel-mar | 152 | $msgs[] = _L('Table %1 is missing!',$prefix.$tableName); |
295 | daniel-mar | 153 | } |
154 | } |
||
155 | if (count($msgs) > 0) { |
||
156 | throw new OIDplusConfigInitializationException(implode("\n\n",$msgs)); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | public function tableExists($tableName): bool { |
||
161 | try { |
||
318 | daniel-mar | 162 | // Attention: This query could interrupt transactions if Rollback-On-Error is enabled |
295 | daniel-mar | 163 | $this->query("select 0 from ".$tableName." where 1=0"); |
164 | return true; |
||
165 | } catch (Exception $e) { |
||
166 | return false; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | public function isConnected(): bool { |
||
171 | return $this->connected; |
||
172 | } |
||
173 | |||
174 | public function init($html = true)/*: void*/ { |
||
175 | $this->html = $html; |
||
176 | } |
||
177 | |||
178 | public function sqlDate(): string { |
||
325 | daniel-mar | 179 | $slang = $this->getSlang(); |
180 | if (!is_null($slang)) { |
||
181 | return $slang->sqlDate(); |
||
295 | daniel-mar | 182 | } else { |
489 | daniel-mar | 183 | return "'" . date('Y-m-d H:i:s') . "'"; |
295 | daniel-mar | 184 | } |
185 | } |
||
186 | |||
502 | daniel-mar | 187 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
188 | $res = null; |
||
326 | daniel-mar | 189 | |
502 | daniel-mar | 190 | if (OIDplus::baseConfig()->exists('FORCE_DBMS_SLANG')) { |
191 | $name = OIDplus::baseConfig()->getValue('FORCE_DBMS_SLANG', ''); |
||
192 | $res = OIDplus::getSqlSlangPlugin($name); |
||
193 | if ($mustExist && is_null($res)) { |
||
194 | throw new OIDplusConfigInitializationException(_L('Enforced SQL slang (via setting FORCE_DBMS_SLANG) "%1" does not exist.',$name)); |
||
195 | } |
||
196 | } else { |
||
197 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
198 | if ($plugin->detect($this)) { |
||
199 | if (OIDplus::baseConfig()->getValue('DEBUG') && !is_null($res)) { |
||
200 | throw new OIDplusException(_L('DB-Slang detection failed: Multiple slangs were detected. Use base config setting FORCE_DBMS_SLANG to define one.')); |
||
201 | } |
||
202 | |||
203 | $res = $plugin; |
||
204 | |||
205 | if (!OIDplus::baseConfig()->getValue('DEBUG')) { |
||
295 | daniel-mar | 206 | break; |
207 | } |
||
208 | } |
||
209 | } |
||
502 | daniel-mar | 210 | if ($mustExist && is_null($res)) { |
211 | throw new OIDplusException(_L('Cannot determine the SQL slang of your DBMS. Your DBMS is probably not supported.')); |
||
212 | } |
||
295 | daniel-mar | 213 | } |
214 | |||
502 | daniel-mar | 215 | return $res; |
216 | } |
||
217 | |||
218 | public final function getSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
||
219 | static /*?OIDplusSqlSlangPlugin*/ $slangCache = null; |
||
220 | |||
221 | if ($this->slangDetectionDone) { |
||
222 | return $slangCache; |
||
223 | } |
||
224 | |||
225 | $slangCache = $this->doGetSlang(); |
||
226 | $this->slangDetectionDone = true; |
||
326 | daniel-mar | 227 | return $slangCache; |
295 | daniel-mar | 228 | } |
489 | daniel-mar | 229 | } |