Rev 1220 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
786 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
786 | 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; |
786 | 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 | |||
786 | daniel-mar | 26 | class OIDplusDatabaseConnectionOci extends OIDplusDatabaseConnection { |
1130 | daniel-mar | 27 | /** |
28 | * @var mixed|null |
||
29 | */ |
||
786 | daniel-mar | 30 | private $conn = null; |
1130 | daniel-mar | 31 | |
32 | /** |
||
33 | * @var array|null |
||
34 | */ |
||
786 | daniel-mar | 35 | private $last_error = null; // do the same like MySQL+PDO, just to be equal in the behavior |
36 | |||
1116 | daniel-mar | 37 | /** |
38 | * @param string $sql |
||
39 | * @param array|null $prepared_args |
||
40 | * @return OIDplusQueryResultOci |
||
41 | * @throws OIDplusException |
||
42 | */ |
||
43 | public function doQuery(string $sql, array $prepared_args=null): OIDplusQueryResult { |
||
786 | daniel-mar | 44 | $this->last_error = null; |
45 | |||
46 | $mode = $this->intransaction ? OCI_NO_AUTO_COMMIT : OCI_COMMIT_ON_SUCCESS; |
||
47 | |||
48 | if (is_null($prepared_args)) { |
||
787 | daniel-mar | 49 | $res = @oci_parse($this->conn, $sql); |
786 | daniel-mar | 50 | if ($res === false) { |
51 | $this->last_error = oci_error($this->conn); |
||
52 | throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error()); |
||
53 | } else if (!@oci_execute($res, $mode)) { |
||
54 | $this->last_error = oci_error($res); |
||
55 | throw new OIDplusSQLException($sql, $this->error()); |
||
56 | } else { |
||
57 | return new OIDplusQueryResultOci($res); |
||
58 | } |
||
59 | //oci_free_statement($res); // will be done in OIDplusQueryResultOci::__destruct() |
||
60 | } else { |
||
61 | // convert ? ? ? to :param1 :param2 :param3 |
||
62 | $count = 0; |
||
63 | $sql = preg_replace_callback('@\\?@', function($found) { |
||
64 | static $i = 0; |
||
65 | $i++; |
||
66 | return ":param$i"; |
||
67 | }, $sql, count($prepared_args), $count); |
||
68 | |||
69 | $res = @oci_parse($this->conn, $sql); |
||
70 | if ($res === false) { |
||
71 | $this->last_error = oci_error($this->conn); |
||
72 | throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error()); |
||
73 | } |
||
74 | |||
75 | $i = 0; |
||
854 | daniel-mar | 76 | foreach ($prepared_args as $value) { |
786 | daniel-mar | 77 | $i++; |
78 | if ($i > $count) break; |
||
854 | daniel-mar | 79 | if (is_bool($value)) $value = $value ? 1 : 0; |
786 | daniel-mar | 80 | $paramname = ":param$i"; |
854 | daniel-mar | 81 | $$paramname = $value; // It is VERY important to clone the variable in this stupid way, because the binding will be done to the memory address of $value ! |
786 | daniel-mar | 82 | if (@oci_bind_by_name($res, $paramname, $$paramname) === false) { |
789 | daniel-mar | 83 | $this->last_error = oci_error($res); |
84 | throw new OIDplusSQLException($sql, _L('Cannot bind parameter %1 to value %2',$paramname,$$paramname).': '.$this->error()); |
||
786 | daniel-mar | 85 | } |
86 | } |
||
87 | |||
88 | if (!@oci_execute($res, $mode)) { |
||
89 | $this->last_error = oci_error($res); |
||
90 | throw new OIDplusSQLException($sql, $this->error()); |
||
91 | } else { |
||
92 | return new OIDplusQueryResultOci($res); |
||
93 | } |
||
94 | //oci_free_statement($res); // will be done in OIDplusQueryResultOci::__destruct() |
||
95 | |||
96 | } |
||
97 | } |
||
98 | |||
1116 | daniel-mar | 99 | /** |
100 | * @return string |
||
101 | */ |
||
786 | daniel-mar | 102 | public function error(): string { |
103 | $err = $this->last_error; |
||
1130 | daniel-mar | 104 | if (!$err) $err = ''; |
786 | daniel-mar | 105 | /* |
106 | array(4) { |
||
107 | ["code"]=> |
||
108 | int(1) |
||
109 | ["message"]=> |
||
110 | string(55) "ORA-00001: unique constraint (HR.SYS_C0012493) violated" |
||
111 | ["offset"]=> |
||
112 | int(0) |
||
113 | ["sqltext"]=> |
||
114 | string(118) "insert into config (name, description, value, protected, visible) values (:param1, :param2, :param3, :param4, :param5)" |
||
115 | } |
||
116 | */ |
||
117 | if (isset($err['message'])) return $err['message']; |
||
118 | return $err; |
||
119 | } |
||
120 | |||
1116 | daniel-mar | 121 | /** |
122 | * @return void |
||
123 | * @throws OIDplusConfigInitializationException |
||
124 | * @throws OIDplusException |
||
125 | */ |
||
786 | daniel-mar | 126 | protected function doConnect()/*: void*/ { |
127 | if (!function_exists('oci_connect')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','OCI8')); |
||
128 | |||
129 | // Try connecting to the database |
||
130 | ob_start(); |
||
131 | $err = ''; |
||
132 | try { |
||
133 | $conn_str = OIDplus::baseConfig()->getValue('OCI_CONN_STR', 'localhost/XE'); |
||
134 | $username = OIDplus::baseConfig()->getValue('OCI_USERNAME', 'hr'); |
||
135 | $password = OIDplus::baseConfig()->getValue('OCI_PASSWORD', 'oracle'); |
||
136 | $this->conn = oci_connect($username, $password, $conn_str, "AL32UTF8" /*, $session_mode*/); |
||
137 | } finally { |
||
138 | $err = ob_get_contents(); |
||
139 | ob_end_clean(); |
||
862 | daniel-mar | 140 | |
141 | $tmp = oci_error(); |
||
142 | if ($tmp !== false) { |
||
143 | $err .= $tmp['message']; |
||
144 | } |
||
786 | daniel-mar | 145 | } |
1050 | daniel-mar | 146 | |
786 | daniel-mar | 147 | if (!$this->conn) { |
862 | daniel-mar | 148 | throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' ' . strip_tags($err))); |
786 | daniel-mar | 149 | } |
150 | |||
151 | $this->last_error = null; |
||
152 | } |
||
153 | |||
1116 | daniel-mar | 154 | /** |
155 | * @return void |
||
156 | */ |
||
786 | daniel-mar | 157 | protected function doDisconnect()/*: void*/ { |
158 | if (!is_null($this->conn)) { |
||
159 | oci_close($this->conn); |
||
160 | $this->conn = null; |
||
161 | } |
||
162 | } |
||
163 | |||
1116 | daniel-mar | 164 | /** |
165 | * @var bool |
||
166 | */ |
||
786 | daniel-mar | 167 | private $intransaction = false; |
168 | |||
1116 | daniel-mar | 169 | /** |
170 | * @return bool |
||
171 | */ |
||
786 | daniel-mar | 172 | public function transaction_supported(): bool { |
173 | return true; |
||
174 | } |
||
175 | |||
1116 | daniel-mar | 176 | /** |
177 | * @return int |
||
178 | */ |
||
786 | daniel-mar | 179 | public function transaction_level(): int { |
180 | return $this->intransaction ? 1 : 0; |
||
181 | } |
||
182 | |||
1116 | daniel-mar | 183 | /** |
184 | * @return void |
||
185 | * @throws OIDplusException |
||
186 | */ |
||
786 | daniel-mar | 187 | public function transaction_begin()/*: void*/ { |
188 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
||
189 | // Later, in oci_execute() we will include OCI_NO_AUTO_COMMIT |
||
190 | $this->intransaction = true; |
||
191 | } |
||
192 | |||
1116 | daniel-mar | 193 | /** |
194 | * @return void |
||
195 | */ |
||
786 | daniel-mar | 196 | public function transaction_commit()/*: void*/ { |
197 | oci_commit($this->conn); |
||
198 | $this->intransaction = false; |
||
199 | } |
||
200 | |||
1116 | daniel-mar | 201 | /** |
202 | * @return void |
||
203 | */ |
||
786 | daniel-mar | 204 | public function transaction_rollback()/*: void*/ { |
205 | oci_rollback($this->conn); |
||
206 | $this->intransaction = false; |
||
207 | } |
||
208 | |||
1116 | daniel-mar | 209 | /** |
210 | * @param bool $mustExist |
||
211 | * @return OIDplusSqlSlangPlugin|null |
||
212 | * @throws OIDplusConfigInitializationException |
||
213 | */ |
||
786 | daniel-mar | 214 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
215 | $slang = OIDplus::getSqlSlangPlugin('oracle'); |
||
216 | if (is_null($slang)) { |
||
1430 | daniel-mar | 217 | throw new OIDplusConfigInitializationException(_L('SQL-Slang plugin "%1" is missing. Please check if it exists in the directory "plugin/sqlSlang". If it is not existing, please recover it from an GIT/SVN snapshot or OIDplus archive file.','oracle')); |
786 | daniel-mar | 218 | } |
219 | return $slang; |
||
220 | } |
||
1220 | daniel-mar | 221 | |
222 | /** |
||
223 | * @return array |
||
224 | */ |
||
225 | public function getExtendedInfo(): array { |
||
226 | $conn_str = OIDplus::baseConfig()->getValue('OCI_CONN_STR', 'localhost/XE'); |
||
227 | return array( |
||
228 | _L('Connection String') => $conn_str |
||
229 | ); |
||
230 | } |
||
786 | daniel-mar | 231 | } |