Rev 1086 | Rev 1130 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | 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; |
635 | 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 | |||
635 | daniel-mar | 26 | class OIDplusDatabaseConnectionPDO extends OIDplusDatabaseConnection { |
27 | private $conn = null; |
||
28 | private $last_error = null; // we need that because PDO divides prepared statement errors and normal query errors, but we have only one "error()" method |
||
29 | private $transactions_supported = false; |
||
30 | |||
1116 | daniel-mar | 31 | /** |
32 | * @param string $sql |
||
33 | * @param array|null $prepared_args |
||
34 | * @return OIDplusQueryResultPDO |
||
35 | * @throws OIDplusException |
||
36 | */ |
||
37 | public function doQuery(string $sql, array $prepared_args=null): OIDplusQueryResult { |
||
635 | daniel-mar | 38 | $this->last_error = null; |
39 | if (is_null($prepared_args)) { |
||
40 | $res = $this->conn->query($sql); |
||
41 | |||
42 | if ($res === false) { |
||
43 | $this->last_error = $this->conn->errorInfo()[2]; |
||
44 | throw new OIDplusSQLException($sql, $this->error()); |
||
45 | } else { |
||
46 | return new OIDplusQueryResultPDO($res); |
||
47 | } |
||
48 | } else { |
||
49 | if (!is_array($prepared_args)) { |
||
50 | throw new OIDplusException(_L('"prepared_args" must be either NULL or an ARRAY.')); |
||
51 | } |
||
52 | |||
53 | foreach ($prepared_args as &$value) { |
||
54 | // We need to manually convert booleans into strings, because there is a |
||
55 | // 14 year old bug that hasn't been adressed by the PDO developers: |
||
56 | // https://bugs.php.net/bug.php?id=57157 |
||
57 | if (is_bool($value)) { |
||
58 | if ($this->slangDetectionDone) { |
||
59 | $value = $this->getSlang()->getSQLBool($value); |
||
60 | } else { |
||
61 | // This works for everything except Microsoft Access (which needs -1 and 0) |
||
62 | // Note: We are using '1' and '0' instead of 'true' and 'false' because MySQL converts boolean to tinyint(1) |
||
63 | $value = $value ? '1' : '0'; |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | |||
68 | $ps = $this->conn->prepare($sql); |
||
69 | if (!$ps) { |
||
70 | $this->last_error = $this->conn->errorInfo()[2]; |
||
71 | throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error()); |
||
72 | } |
||
73 | |||
74 | if (!$ps->execute($prepared_args)) { |
||
75 | $this->last_error = $ps->errorInfo()[2]; |
||
76 | throw new OIDplusSQLException($sql, $this->error()); |
||
77 | } |
||
78 | return new OIDplusQueryResultPDO($ps); |
||
79 | } |
||
80 | } |
||
81 | |||
1116 | daniel-mar | 82 | /** |
83 | * @return int |
||
84 | * @throws OIDplusException |
||
85 | */ |
||
635 | daniel-mar | 86 | public function insert_id(): int { |
87 | try { |
||
88 | $out = @($this->conn->lastInsertId()); |
||
89 | if ($out === false) return parent::insert_id(); // fallback method that uses the SQL slang |
||
90 | return $out; |
||
1050 | daniel-mar | 91 | } catch (\Exception $e) { |
635 | daniel-mar | 92 | return parent::insert_id(); // fallback method that uses the SQL slang |
93 | } |
||
94 | } |
||
95 | |||
1116 | daniel-mar | 96 | /** |
97 | * @return string |
||
98 | */ |
||
635 | daniel-mar | 99 | public function error(): string { |
100 | $err = $this->last_error; |
||
101 | if ($err == null) $err = ''; |
||
102 | return $err; |
||
103 | } |
||
104 | |||
1116 | daniel-mar | 105 | /** |
106 | * @return void |
||
107 | * @throws OIDplusConfigInitializationException |
||
108 | * @throws OIDplusException |
||
109 | */ |
||
635 | daniel-mar | 110 | protected function doConnect()/*: void*/ { |
111 | if (!class_exists('PDO')) throw new OIDplusConfigInitializationException(_L('PHP extension "%1" not installed','PDO')); |
||
112 | |||
113 | try { |
||
114 | $options = [ |
||
1050 | daniel-mar | 115 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_SILENT, |
116 | \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, |
||
117 | \PDO::ATTR_EMULATE_PREPARES => true, |
||
635 | daniel-mar | 118 | ]; |
119 | |||
120 | // Try connecting to the database |
||
787 | daniel-mar | 121 | $dsn = OIDplus::baseConfig()->getValue('PDO_DSN', 'mysql:host=localhost;dbname=oidplus;charset=UTF8'); |
635 | daniel-mar | 122 | $username = OIDplus::baseConfig()->getValue('PDO_USERNAME', 'root'); |
123 | $password = OIDplus::baseConfig()->getValue('PDO_PASSWORD', ''); |
||
787 | daniel-mar | 124 | |
125 | if (stripos($dsn,"charset=") === false) $dsn = "$dsn;charset=UTF8"; |
||
126 | |||
1050 | daniel-mar | 127 | $this->conn = new \PDO($dsn, $username, $password, $options); |
128 | } catch (\PDOException $e) { |
||
635 | daniel-mar | 129 | $message = $e->getMessage(); |
863 | daniel-mar | 130 | throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' '.$message)); |
635 | daniel-mar | 131 | } |
132 | |||
133 | $this->last_error = null; |
||
134 | |||
135 | try { |
||
136 | @$this->conn->exec("SET NAMES 'utf8'"); |
||
1050 | daniel-mar | 137 | } catch (\Exception $e) { |
635 | daniel-mar | 138 | } |
139 | |||
140 | // We check if the DBMS supports autocommit. |
||
141 | // Attention: Check it after you have sent a query already, because Microsoft Access doesn't seem to allow |
||
142 | // changing auto commit once a query was executed ("Attribute cannot be set now SQLState: S1011") |
||
143 | // Note: For some weird reason we *DO* need to redirect the output to "$dummy", otherwise it won't work! |
||
144 | $sql = "select name from ###config where 1=0"; |
||
145 | $sql = str_replace('###', OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''), $sql); |
||
146 | $dummy = $this->conn->query($sql); |
||
147 | try { |
||
148 | $this->conn->beginTransaction(); |
||
149 | $this->conn->rollBack(); |
||
150 | $this->transactions_supported = true; |
||
1050 | daniel-mar | 151 | } catch (\Exception $e) { |
635 | daniel-mar | 152 | $this->transactions_supported = false; |
153 | } |
||
154 | } |
||
155 | |||
1116 | daniel-mar | 156 | /** |
157 | * @return void |
||
158 | */ |
||
635 | daniel-mar | 159 | protected function doDisconnect()/*: void*/ { |
160 | $this->conn = null; // the connection will be closed by removing the reference |
||
161 | } |
||
162 | |||
1116 | daniel-mar | 163 | /** |
164 | * @var bool |
||
165 | */ |
||
635 | daniel-mar | 166 | private $intransaction = false; |
167 | |||
1116 | daniel-mar | 168 | /** |
169 | * @return bool |
||
170 | */ |
||
635 | daniel-mar | 171 | public function transaction_supported(): bool { |
172 | return $this->transactions_supported; |
||
173 | } |
||
174 | |||
1116 | daniel-mar | 175 | /** |
176 | * @return int |
||
177 | */ |
||
635 | daniel-mar | 178 | public function transaction_level(): int { |
179 | if (!$this->transaction_supported()) { |
||
180 | // TODO? |
||
181 | return 0; |
||
182 | } |
||
183 | return $this->intransaction ? 1 : 0; |
||
184 | } |
||
185 | |||
1116 | daniel-mar | 186 | /** |
187 | * @return void |
||
188 | * @throws OIDplusException |
||
189 | */ |
||
635 | daniel-mar | 190 | public function transaction_begin()/*: void*/ { |
191 | if (!$this->transaction_supported()) { |
||
192 | // TODO? |
||
193 | return; |
||
194 | } |
||
195 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
||
196 | $this->conn->beginTransaction(); |
||
197 | $this->intransaction = true; |
||
198 | } |
||
199 | |||
1116 | daniel-mar | 200 | /** |
201 | * @return void |
||
202 | */ |
||
635 | daniel-mar | 203 | public function transaction_commit()/*: void*/ { |
204 | if (!$this->transaction_supported()) { |
||
205 | // TODO? |
||
206 | return; |
||
207 | } |
||
208 | $this->conn->commit(); |
||
209 | $this->intransaction = false; |
||
210 | } |
||
211 | |||
1116 | daniel-mar | 212 | /** |
213 | * @return void |
||
214 | */ |
||
635 | daniel-mar | 215 | public function transaction_rollback()/*: void*/ { |
216 | if (!$this->transaction_supported()) { |
||
217 | // TODO? |
||
218 | return; |
||
219 | } |
||
220 | $this->conn->rollBack(); |
||
221 | $this->intransaction = false; |
||
222 | } |
||
223 | |||
224 | } |