Rev 1050 | Rev 1116 | 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 OIDplusDatabaseConnectionMySQLi extends OIDplusDatabaseConnection { |
27 | private $conn = null; // only with MySQLnd |
||
28 | private $prepare_cache = array(); |
||
29 | private $last_error = null; // we need that because MySQL divides prepared statement errors and normal query errors, but we have only one "error()" method |
||
30 | |||
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)) { |
||
44 | throw new OIDplusException(_L('"prepared_args" must be either NULL or an ARRAY.')); |
||
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; |
||
59 | throw new OIDplusSQLException($sql, _L('Cannot prepare statement').': '.$this->error()); |
||
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*/ { |
||
101 | if (!function_exists('mysqli_connect')) throw new OIDplusException(_L('PHP extension "%1" not installed','MySQLi')); |
||
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'); |
||
813 | daniel-mar | 108 | $socket = OIDplus::baseConfig()->getValue('MYSQL_SOCKET', ''); |
635 | daniel-mar | 109 | list($hostname,$port) = explode(':', $host.':'.ini_get("mysqli.default_port")); |
110 | $port = intval($port); |
||
1050 | daniel-mar | 111 | $this->conn = @new \mysqli($hostname, $username, $password, $database, $port, $socket); |
635 | daniel-mar | 112 | if (!empty($this->conn->connect_error) || ($this->conn->connect_errno != 0)) { |
113 | $message = $this->conn->connect_error; |
||
863 | daniel-mar | 114 | throw new OIDplusConfigInitializationException(trim(_L('Connection to the database failed!').' '.$message)); |
635 | daniel-mar | 115 | } |
116 | |||
117 | $this->prepare_cache = array(); |
||
118 | $this->last_error = null; |
||
119 | |||
120 | $this->query("SET NAMES 'utf8'"); |
||
121 | } |
||
122 | |||
123 | protected function doDisconnect()/*: void*/ { |
||
124 | $this->prepare_cache = array(); |
||
125 | if (!is_null($this->conn)) { |
||
126 | $this->conn->close(); |
||
127 | $this->conn = null; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | private $intransaction = false; |
||
132 | |||
133 | public function transaction_supported(): bool { |
||
134 | return true; |
||
135 | } |
||
136 | |||
137 | public function transaction_level(): int { |
||
138 | return $this->intransaction ? 1 : 0; |
||
139 | } |
||
140 | |||
141 | public function transaction_begin()/*: void*/ { |
||
142 | if ($this->intransaction) throw new OIDplusException(_L('Nested transactions are not supported by this database plugin.')); |
||
143 | $this->conn->autocommit(false); |
||
144 | $this->conn->begin_transaction(); |
||
145 | $this->intransaction = true; |
||
146 | } |
||
147 | |||
148 | public function transaction_commit()/*: void*/ { |
||
149 | $this->conn->commit(); |
||
150 | $this->conn->autocommit(true); |
||
151 | $this->intransaction = false; |
||
152 | } |
||
153 | |||
154 | public function transaction_rollback()/*: void*/ { |
||
155 | $this->conn->rollback(); |
||
156 | $this->conn->autocommit(true); |
||
157 | $this->intransaction = false; |
||
158 | } |
||
159 | |||
160 | public function sqlDate(): string { |
||
161 | return 'now()'; |
||
162 | } |
||
163 | |||
164 | public static function nativeDriverAvailable(): bool { |
||
165 | return function_exists('mysqli_fetch_all') && (OIDplus::baseConfig()->getValue('MYSQL_FORCE_MYSQLND_SUPPLEMENT', false) === false); |
||
166 | } |
||
167 | |||
168 | private static function bind_placeholder_vars(&$stmt,$params): bool { |
||
169 | // Credit to: Dave Morgan |
||
170 | // Code taken from: http://www.devmorgan.com/blog/2009/03/27/dydl-part-3-dynamic-binding-with-mysqli-php/ |
||
171 | // https://stackoverflow.com/questions/17219214/how-to-bind-in-mysqli-dynamically |
||
172 | if ($params != null) { |
||
173 | $types = ''; //initial sting with types |
||
174 | foreach ($params as $param) { //for each element, determine type and add |
||
175 | if (is_int($param)) { |
||
176 | $types .= 'i'; //integer |
||
177 | } elseif (is_float($param)) { |
||
178 | $types .= 'd'; //double |
||
179 | } elseif (is_string($param)) { |
||
180 | $types .= 's'; //string |
||
181 | } else { |
||
182 | $types .= 'b'; //blob and unknown |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $bind_names = array(); |
||
187 | $bind_names[] = $types; //first param needed is the type string, e.g.: 'issss' |
||
188 | |||
189 | for ($i=0; $i<count($params);$i++) { //go through incoming params and added em to array |
||
190 | $bind_name = 'bind' . $i; //give them an arbitrary name |
||
191 | $$bind_name = $params[$i]; //add the parameter to the variable variable |
||
192 | $bind_names[] = &$$bind_name; //now associate the variable as an element in an array |
||
193 | } |
||
194 | |||
195 | //error_log("better_mysqli has params ".print_r($bind_names, 1)); |
||
196 | //call the function bind_param with dynamic params |
||
197 | call_user_func_array(array($stmt,'bind_param'),$bind_names); |
||
198 | return true; |
||
199 | } else { |
||
200 | return false; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | protected function doGetSlang(bool $mustExist=true)/*: ?OIDplusSqlSlangPlugin*/ { |
||
205 | $slang = OIDplus::getSqlSlangPlugin('mysql'); |
||
206 | if (is_null($slang)) { |
||
207 | 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 TAR.GZ file.','mysql')); |
||
208 | } |
||
209 | return $slang; |
||
210 | } |
||
211 | } |