Rev 502 | 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 | |||
295 | daniel-mar | 22 | class OIDplusDatabaseConnectionMySQLi extends OIDplusDatabaseConnection { |
23 | private $conn = null; // only with MySQLnd |
||
24 | private $prepare_cache = array(); |
||
25 | private $last_error = null; // we need that because MySQL divides prepared statement errors and normal query errors, but we have only one "error()" method |
||
26 | |||
348 | daniel-mar | 27 | public static function getPlugin(): OIDplusDatabasePlugin { |
28 | return new OIDplusDatabasePluginMySQLi(); |
||
29 | } |
||
30 | |||
295 | daniel-mar | 31 | public function doQuery(string $sql, /*?array*/ $prepared_args=null): OIDplusQueryResult { |
32 | $this->last_error = null; |
||
33 | if (is_null($prepared_args)) { |
||
34 | $res = $this->conn->query($sql, MYSQLI_STORE_RESULT); |
||
35 | |||
36 | if ($res === false) { |
||
37 | $this->last_error = $this->conn->error; |
||
38 | throw new OIDplusSQLException($sql, $this->error()); |
||
39 | } else { |
||
40 | return new OIDplusQueryResultMySQL($res); |
||
41 | } |
||
42 | } else { |
||
43 | if (!is_array($prepared_args)) { |
||
360 | daniel-mar | 44 | throw new OIDplusException(_L('"prepared_args" must be either NULL or an ARRAY.')); |
295 | daniel-mar | 45 | } |
46 | |||
47 | foreach ($prepared_args as &$value) { |
||
48 | // MySQLi has problems converting "true/false" to the data type "tinyint(1)" |
||
49 | // It seems to be the same issue like in PDO reported 14 years ago at https://bugs.php.net/bug.php?id=57157 |
||
50 | if (is_bool($value)) $value = $value ? '1' : '0'; |
||
51 | } |
||
52 | |||
53 | if (isset($this->prepare_cache[$sql])) { |
||
54 | $ps = $this->prepare_cache[$sql]; |
||
55 | } else { |
||
56 | $ps = $this->conn->prepare($sql); |
||
57 | if (!$ps) { |
||
58 | $this->last_error = $this->conn->error; |
||
360 | daniel-mar | 59 | throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error()); |
295 | daniel-mar | 60 | } |
61 | |||
62 | // Caching the prepared is very risky |
||
63 | // In PDO and ODBC we may not do it, because execute() will |
||
64 | // destroy the existing cursors. |
||
65 | // (test this with ./?goto=oid%3A1.3.6.1.4.1.37553.8.32488192274 |
||
66 | // you will see that 2.999 is missing in the tree) |
||
67 | // But $ps->get_result() seems to "clone" the cursor, |
||
68 | // so that $ps->execute may be called a second time?! |
||
69 | // However, it only works with mysqlnd's get_result, |
||
70 | // not with OIDplusQueryResultMySQLNoNativeDriver |
||
71 | if (self::nativeDriverAvailable()) { |
||
72 | $this->prepare_cache[$sql] = $ps; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | self::bind_placeholder_vars($ps,$prepared_args); |
||
77 | if (!$ps->execute()) { |
||
78 | $this->last_error = mysqli_stmt_error($ps); |
||
79 | throw new OIDplusSQLException($sql, $this->error()); |
||
80 | } |
||
81 | |||
82 | if (self::nativeDriverAvailable()) { |
||
83 | return new OIDplusQueryResultMySQL($ps->get_result()); |
||
84 | } else { |
||
85 | return new OIDplusQueryResultMySQLNoNativeDriver($ps); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | |||
90 | public function insert_id(): int { |
||
91 | return $this->conn->insert_id; |
||
92 | } |
||
93 | |||
94 | public function error(): string { |
||
95 | $err = $this->last_error; |
||
96 | if ($err == null) $err = ''; |
||
97 | return $err; |
||
98 | } |
||
99 | |||
100 | protected function doConnect()/*: void*/ { |
||
360 | daniel-mar | 101 | if (!function_exists('mysqli_connect')) throw new OIDplusException(_L('PHP extension "%1" not installed','MySQLi')); |
295 | daniel-mar | 102 | |
103 | // Try connecting to the database |
||
104 | $host = OIDplus::baseConfig()->getValue('MYSQL_HOST', 'localhost'); |
||
105 | $username = OIDplus::baseConfig()->getValue('MYSQL_USERNAME', 'root'); |
||
106 | $password = OIDplus::baseConfig()->getValue('MYSQL_PASSWORD', ''); |
||
107 | $database = OIDplus::baseConfig()->getValue('MYSQL_DATABASE', 'oidplus'); |
||
108 | list($hostname,$port) = explode(':', $host.':'.ini_get("mysqli.default_port")); |
||
109 | $this->conn = @new mysqli($hostname, $username, $password, $database, $port); |
||
110 | if (!empty($this->conn->connect_error) || ($this->conn->connect_errno != 0)) { |
||
111 | $message = $this->conn->connect_error; |
||
360 | daniel-mar | 112 | throw new OIDplusConfigInitializationException(_L('Connection to the database failed!').' '.$message); |
295 | daniel-mar | 113 | } |
114 | |||
115 | $this->prepare_cache = array(); |
||
116 | $this->last_error = null; |
||
117 | |||
118 | $this->query("SET NAMES 'utf8'"); |
||
119 | } |
||
120 | |||
121 | protected function doDisconnect()/*: void*/ { |
||
122 | $this->prepare_cache = array(); |
||
123 | if (!is_null($this->conn)) { |
||
124 | $this->conn->close(); |
||
125 | $this->conn = null; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | private $intransaction = false; |
||
130 | |||
131 | public function transaction_supported(): bool { |
||
132 | return true; |
||
133 | } |
||
134 | |||
135 | public function transaction_level(): int { |
||
136 | return $this->intransaction ? 1 : 0; |
||
137 | } |
||
138 | |||
139 | public function transaction_begin()/*: void*/ { |
||
360 | daniel-mar | 140 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
295 | daniel-mar | 141 | $this->conn->autocommit(false); |
142 | $this->conn->begin_transaction(); |
||
143 | $this->intransaction = true; |
||
144 | } |
||
145 | |||
146 | public function transaction_commit()/*: void*/ { |
||
147 | $this->conn->commit(); |
||
148 | $this->conn->autocommit(true); |
||
149 | $this->intransaction = false; |
||
150 | } |
||
151 | |||
152 | public function transaction_rollback()/*: void*/ { |
||
153 | $this->conn->rollback(); |
||
154 | $this->conn->autocommit(true); |
||
155 | $this->intransaction = false; |
||
156 | } |
||
157 | |||
158 | public function sqlDate(): string { |
||
159 | return 'now()'; |
||
160 | } |
||
161 | |||
162 | public static function nativeDriverAvailable(): bool { |
||
163 | return function_exists('mysqli_fetch_all') && (OIDplus::baseConfig()->getValue('MYSQL_FORCE_MYSQLND_SUPPLEMENT', false) === false); |
||
164 | } |
||
165 | |||
166 | private static function bind_placeholder_vars(&$stmt,$params): bool { |
||
167 | // Credit to: Dave Morgan |
||
168 | // Code taken from: http://www.devmorgan.com/blog/2009/03/27/dydl-part-3-dynamic-binding-with-mysqli-php/ |
||
169 | // https://stackoverflow.com/questions/17219214/how-to-bind-in-mysqli-dynamically |
||
170 | if ($params != null) { |
||
171 | $types = ''; //initial sting with types |
||
172 | foreach ($params as $param) { //for each element, determine type and add |
||
173 | if (is_int($param)) { |
||
174 | $types .= 'i'; //integer |
||
175 | } elseif (is_float($param)) { |
||
176 | $types .= 'd'; //double |
||
177 | } elseif (is_string($param)) { |
||
178 | $types .= 's'; //string |
||
179 | } else { |
||
180 | $types .= 'b'; //blob and unknown |
||
181 | } |
||
182 | } |
||
183 | |||
184 | $bind_names = array(); |
||
185 | $bind_names[] = $types; //first param needed is the type string, e.g.: 'issss' |
||
186 | |||
187 | for ($i=0; $i<count($params);$i++) { //go through incoming params and added em to array |
||
188 | $bind_name = 'bind' . $i; //give them an arbitrary name |
||
189 | $$bind_name = $params[$i]; //add the parameter to the variable variable |
||
190 | $bind_names[] = &$$bind_name; //now associate the variable as an element in an array |
||
191 | } |
||
192 | |||
193 | //error_log("better_mysqli has params ".print_r($bind_names, 1)); |
||
194 | //call the function bind_param with dynamic params |
||
195 | call_user_func_array(array($stmt,'bind_param'),$bind_names); |
||
196 | return true; |
||
197 | } else { |
||
198 | return false; |
||
199 | } |
||
200 | } |
||
316 | daniel-mar | 201 | |
502 | daniel-mar | 202 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
318 | daniel-mar | 203 | $slang = OIDplus::getSqlSlangPlugin('mysql'); |
316 | daniel-mar | 204 | if (is_null($slang)) { |
360 | daniel-mar | 205 | 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 SVN snapshot or OIDplus ZIP file.','mysql')); |
316 | daniel-mar | 206 | } |
207 | return $slang; |
||
208 | } |
||
360 | daniel-mar | 209 | } |