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