| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: dbo_mysqli.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* MySQLi layer for DBO
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs.model.datasources.dbo
|
|
|
21 |
* @since CakePHP(tm) v 1.1.4.2974
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
App::import('Core', 'DboMysql');
|
|
|
28 |
/**
|
|
|
29 |
* MySQLi DBO driver object
|
|
|
30 |
*
|
|
|
31 |
* Provides connection and SQL generation for MySQL RDMS using PHP's MySQLi Interface
|
|
|
32 |
*
|
|
|
33 |
* @package cake
|
|
|
34 |
* @subpackage cake.cake.libs.model.datasources.dbo
|
|
|
35 |
*/
|
|
|
36 |
class DboMysqli extends DboMysqlBase {
|
|
|
37 |
/**
|
|
|
38 |
* Enter description here...
|
|
|
39 |
*
|
|
|
40 |
* @var unknown_type
|
|
|
41 |
*/
|
|
|
42 |
var $description = "Mysqli DBO Driver";
|
|
|
43 |
/**
|
|
|
44 |
* Base configuration settings for Mysqli driver
|
|
|
45 |
*
|
|
|
46 |
* @var array
|
|
|
47 |
*/
|
|
|
48 |
var $_baseConfig = array(
|
|
|
49 |
'persistent' => true,
|
|
|
50 |
'host' => 'localhost',
|
|
|
51 |
'login' => 'root',
|
|
|
52 |
'password' => '',
|
|
|
53 |
'database' => 'cake',
|
|
|
54 |
'port' => '3306',
|
|
|
55 |
'connect' => 'mysqli_connect'
|
|
|
56 |
);
|
|
|
57 |
/**
|
|
|
58 |
* Connects to the database using options in the given configuration array.
|
|
|
59 |
*
|
|
|
60 |
* @return boolean True if the database could be connected, else false
|
|
|
61 |
*/
|
|
|
62 |
function connect() {
|
|
|
63 |
$config = $this->config;
|
|
|
64 |
$this->connected = false;
|
|
|
65 |
|
|
|
66 |
if (is_numeric($config['port'])) {
|
|
|
67 |
$config['socket'] = null;
|
|
|
68 |
} else {
|
|
|
69 |
$config['socket'] = $config['port'];
|
|
|
70 |
$config['port'] = null;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], $config['database'], $config['port'], $config['socket']);
|
|
|
74 |
|
|
|
75 |
if ($this->connection !== false) {
|
|
|
76 |
$this->connected = true;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$this->_useAlias = (bool)version_compare(mysqli_get_server_info($this->connection), "4.1", ">=");
|
|
|
80 |
|
|
|
81 |
if (!empty($config['encoding'])) {
|
|
|
82 |
$this->setEncoding($config['encoding']);
|
|
|
83 |
}
|
|
|
84 |
return $this->connected;
|
|
|
85 |
}
|
|
|
86 |
/**
|
|
|
87 |
* Disconnects from database.
|
|
|
88 |
*
|
|
|
89 |
* @return boolean True if the database could be disconnected, else false
|
|
|
90 |
*/
|
|
|
91 |
function disconnect() {
|
|
|
92 |
if (isset($this->results) && is_resource($this->results)) {
|
|
|
93 |
mysqli_free_result($this->results);
|
|
|
94 |
}
|
|
|
95 |
$this->connected = !@mysqli_close($this->connection);
|
|
|
96 |
return !$this->connected;
|
|
|
97 |
}
|
|
|
98 |
/**
|
|
|
99 |
* Executes given SQL statement.
|
|
|
100 |
*
|
|
|
101 |
* @param string $sql SQL statement
|
|
|
102 |
* @return resource Result resource identifier
|
|
|
103 |
* @access protected
|
|
|
104 |
*/
|
|
|
105 |
function _execute($sql) {
|
|
|
106 |
if (preg_match('/^\s*call/i', $sql)) {
|
|
|
107 |
return $this->_executeProcedure($sql);
|
|
|
108 |
} else {
|
|
|
109 |
return mysqli_query($this->connection, $sql);
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
/**
|
|
|
113 |
* Executes given SQL statement (procedure call).
|
|
|
114 |
*
|
|
|
115 |
* @param string $sql SQL statement (procedure call)
|
|
|
116 |
* @return resource Result resource identifier for first recordset
|
|
|
117 |
* @access protected
|
|
|
118 |
*/
|
|
|
119 |
function _executeProcedure($sql) {
|
|
|
120 |
$answer = mysqli_multi_query($this->connection, $sql);
|
|
|
121 |
|
|
|
122 |
$firstResult = mysqli_store_result($this->connection);
|
|
|
123 |
|
|
|
124 |
if (mysqli_more_results($this->connection)) {
|
|
|
125 |
while ($lastResult = mysqli_next_result($this->connection));
|
|
|
126 |
}
|
|
|
127 |
return $firstResult;
|
|
|
128 |
}
|
|
|
129 |
/**
|
|
|
130 |
* Returns an array of sources (tables) in the database.
|
|
|
131 |
*
|
|
|
132 |
* @return array Array of tablenames in the database
|
|
|
133 |
*/
|
|
|
134 |
function listSources() {
|
|
|
135 |
$cache = parent::listSources();
|
|
|
136 |
if ($cache != null) {
|
|
|
137 |
return $cache;
|
|
|
138 |
}
|
|
|
139 |
$result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
|
|
|
140 |
|
|
|
141 |
if (!$result) {
|
|
|
142 |
return array();
|
|
|
143 |
} else {
|
|
|
144 |
$tables = array();
|
|
|
145 |
|
|
|
146 |
while ($line = mysqli_fetch_array($result)) {
|
|
|
147 |
$tables[] = $line[0];
|
|
|
148 |
}
|
|
|
149 |
parent::listSources($tables);
|
|
|
150 |
return $tables;
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
/**
|
|
|
154 |
* Returns an array of the fields in given table name.
|
|
|
155 |
*
|
|
|
156 |
* @param string $tableName Name of database table to inspect
|
|
|
157 |
* @return array Fields in table. Keys are name and type
|
|
|
158 |
*/
|
|
|
159 |
function describe(&$model) {
|
|
|
160 |
|
|
|
161 |
$cache = parent::describe($model);
|
|
|
162 |
if ($cache != null) {
|
|
|
163 |
return $cache;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
$fields = false;
|
|
|
167 |
$cols = $this->query('DESCRIBE ' . $this->fullTableName($model));
|
|
|
168 |
|
|
|
169 |
foreach ($cols as $column) {
|
|
|
170 |
$colKey = array_keys($column);
|
|
|
171 |
if (isset($column[$colKey[0]]) && !isset($column[0])) {
|
|
|
172 |
$column[0] = $column[$colKey[0]];
|
|
|
173 |
}
|
|
|
174 |
if (isset($column[0])) {
|
|
|
175 |
$fields[$column[0]['Field']] = array(
|
|
|
176 |
'type' => $this->column($column[0]['Type']),
|
|
|
177 |
'null' => ($column[0]['Null'] == 'YES' ? true : false),
|
|
|
178 |
'default' => $column[0]['Default'],
|
|
|
179 |
'length' => $this->length($column[0]['Type'])
|
|
|
180 |
);
|
|
|
181 |
if (!empty($column[0]['Key']) && isset($this->index[$column[0]['Key']])) {
|
|
|
182 |
$fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']];
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
$this->__cacheDescription($this->fullTableName($model, false), $fields);
|
|
|
188 |
return $fields;
|
|
|
189 |
}
|
|
|
190 |
/**
|
|
|
191 |
* Returns a quoted and escaped string of $data for use in an SQL statement.
|
|
|
192 |
*
|
|
|
193 |
* @param string $data String to be prepared for use in an SQL statement
|
|
|
194 |
* @param string $column The column into which this data will be inserted
|
|
|
195 |
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
|
|
|
196 |
* @return string Quoted and escaped data
|
|
|
197 |
*/
|
|
|
198 |
function value($data, $column = null, $safe = false) {
|
|
|
199 |
$parent = parent::value($data, $column, $safe);
|
|
|
200 |
|
|
|
201 |
if ($parent != null) {
|
|
|
202 |
return $parent;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
if ($data === null) {
|
|
|
206 |
return 'NULL';
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
|
|
|
210 |
return "''";
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
switch ($column) {
|
|
|
214 |
case 'boolean':
|
|
|
215 |
$data = $this->boolean((bool)$data);
|
|
|
216 |
break;
|
|
|
217 |
case 'integer' :
|
|
|
218 |
case 'float' :
|
|
|
219 |
case null :
|
|
|
220 |
if ($data === '') {
|
|
|
221 |
return 'NULL';
|
|
|
222 |
}
|
|
|
223 |
if ((is_int($data) || is_float($data) || $data === '0') || (
|
|
|
224 |
is_numeric($data) && strpos($data, ',') === false &&
|
|
|
225 |
$data[0] != '0' && strpos($data, 'e') === false)) {
|
|
|
226 |
return $data;
|
|
|
227 |
}
|
|
|
228 |
default:
|
|
|
229 |
$data = "'" . mysqli_real_escape_string($this->connection, $data) . "'";
|
|
|
230 |
break;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
return $data;
|
|
|
234 |
}
|
|
|
235 |
/**
|
|
|
236 |
* Begin a transaction
|
|
|
237 |
*
|
|
|
238 |
* @param unknown_type $model
|
|
|
239 |
* @return boolean True on success, false on fail
|
|
|
240 |
* (i.e. if the database/model does not support transactions).
|
|
|
241 |
*/
|
|
|
242 |
function begin(&$model) {
|
|
|
243 |
if (parent::begin($model) && $this->execute('START TRANSACTION')) {
|
|
|
244 |
$this->_transactionStarted = true;
|
|
|
245 |
return true;
|
|
|
246 |
}
|
|
|
247 |
return false;
|
|
|
248 |
}
|
|
|
249 |
/**
|
|
|
250 |
* Returns a formatted error message from previous database operation.
|
|
|
251 |
*
|
|
|
252 |
* @return string Error message with error number
|
|
|
253 |
*/
|
|
|
254 |
function lastError() {
|
|
|
255 |
if (mysqli_errno($this->connection)) {
|
|
|
256 |
return mysqli_errno($this->connection).': '.mysqli_error($this->connection);
|
|
|
257 |
}
|
|
|
258 |
return null;
|
|
|
259 |
}
|
|
|
260 |
/**
|
|
|
261 |
* Returns number of affected rows in previous database operation. If no previous operation exists,
|
|
|
262 |
* this returns false.
|
|
|
263 |
*
|
|
|
264 |
* @return integer Number of affected rows
|
|
|
265 |
*/
|
|
|
266 |
function lastAffected() {
|
|
|
267 |
if ($this->_result) {
|
|
|
268 |
return mysqli_affected_rows($this->connection);
|
|
|
269 |
}
|
|
|
270 |
return null;
|
|
|
271 |
}
|
|
|
272 |
/**
|
|
|
273 |
* Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
274 |
* this returns false.
|
|
|
275 |
*
|
|
|
276 |
* @return integer Number of rows in resultset
|
|
|
277 |
*/
|
|
|
278 |
function lastNumRows() {
|
|
|
279 |
if ($this->hasResult()) {
|
|
|
280 |
return mysqli_num_rows($this->_result);
|
|
|
281 |
}
|
|
|
282 |
return null;
|
|
|
283 |
}
|
|
|
284 |
/**
|
|
|
285 |
* Returns the ID generated from the previous INSERT operation.
|
|
|
286 |
*
|
|
|
287 |
* @param unknown_type $source
|
|
|
288 |
* @return in
|
|
|
289 |
*/
|
|
|
290 |
function lastInsertId($source = null) {
|
|
|
291 |
$id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false);
|
|
|
292 |
if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) {
|
|
|
293 |
return $id[0]['insertID'];
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
return null;
|
|
|
297 |
}
|
|
|
298 |
/**
|
|
|
299 |
* Converts database-layer column types to basic types
|
|
|
300 |
*
|
|
|
301 |
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
|
|
302 |
* @return string Abstract column type (i.e. "string")
|
|
|
303 |
*/
|
|
|
304 |
function column($real) {
|
|
|
305 |
if (is_array($real)) {
|
|
|
306 |
$col = $real['name'];
|
|
|
307 |
if (isset($real['limit'])) {
|
|
|
308 |
$col .= '('.$real['limit'].')';
|
|
|
309 |
}
|
|
|
310 |
return $col;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
$col = str_replace(')', '', $real);
|
|
|
314 |
$limit = $this->length($real);
|
|
|
315 |
if (strpos($col, '(') !== false) {
|
|
|
316 |
list($col, $vals) = explode('(', $col);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
|
|
|
320 |
return $col;
|
|
|
321 |
}
|
|
|
322 |
if (($col == 'tinyint' && $limit == 1) || $col == 'boolean') {
|
|
|
323 |
return 'boolean';
|
|
|
324 |
}
|
|
|
325 |
if (strpos($col, 'int') !== false) {
|
|
|
326 |
return 'integer';
|
|
|
327 |
}
|
|
|
328 |
if (strpos($col, 'char') !== false || $col == 'tinytext') {
|
|
|
329 |
return 'string';
|
|
|
330 |
}
|
|
|
331 |
if (strpos($col, 'text') !== false) {
|
|
|
332 |
return 'text';
|
|
|
333 |
}
|
|
|
334 |
if (strpos($col, 'blob') !== false || $col == 'binary') {
|
|
|
335 |
return 'binary';
|
|
|
336 |
}
|
|
|
337 |
if (strpos($col, 'float') !== false || strpos($col, 'double') !== false || strpos($col, 'decimal') !== false) {
|
|
|
338 |
return 'float';
|
|
|
339 |
}
|
|
|
340 |
if (strpos($col, 'enum') !== false) {
|
|
|
341 |
return "enum($vals)";
|
|
|
342 |
}
|
|
|
343 |
return 'text';
|
|
|
344 |
}
|
|
|
345 |
/**
|
|
|
346 |
* Gets the length of a database-native column description, or null if no length
|
|
|
347 |
*
|
|
|
348 |
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
|
|
349 |
* @return integer An integer representing the length of the column
|
|
|
350 |
*/
|
|
|
351 |
function length($real) {
|
|
|
352 |
$col = str_replace(array(')', 'unsigned'), '', $real);
|
|
|
353 |
$limit = null;
|
|
|
354 |
|
|
|
355 |
if (strpos($col, '(') !== false) {
|
|
|
356 |
list($col, $limit) = explode('(', $col);
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
if ($limit != null) {
|
|
|
360 |
return intval($limit);
|
|
|
361 |
}
|
|
|
362 |
return null;
|
|
|
363 |
}
|
|
|
364 |
/**
|
|
|
365 |
* Enter description here...
|
|
|
366 |
*
|
|
|
367 |
* @param unknown_type $results
|
|
|
368 |
*/
|
|
|
369 |
function resultSet(&$results) {
|
|
|
370 |
if (isset($this->results) && is_resource($this->results) && $this->results != $results) {
|
|
|
371 |
mysqli_free_result($this->results);
|
|
|
372 |
}
|
|
|
373 |
$this->results =& $results;
|
|
|
374 |
$this->map = array();
|
|
|
375 |
$numFields = mysqli_num_fields($results);
|
|
|
376 |
$index = 0;
|
|
|
377 |
$j = 0;
|
|
|
378 |
while ($j < $numFields) {
|
|
|
379 |
$column = mysqli_fetch_field_direct($results, $j);
|
|
|
380 |
if (!empty($column->table)) {
|
|
|
381 |
$this->map[$index++] = array($column->table, $column->name);
|
|
|
382 |
} else {
|
|
|
383 |
$this->map[$index++] = array(0, $column->name);
|
|
|
384 |
}
|
|
|
385 |
$j++;
|
|
|
386 |
}
|
|
|
387 |
}
|
|
|
388 |
/**
|
|
|
389 |
* Fetches the next row from the current result set
|
|
|
390 |
*
|
|
|
391 |
* @return unknown
|
|
|
392 |
*/
|
|
|
393 |
function fetchResult() {
|
|
|
394 |
if ($row = mysqli_fetch_row($this->results)) {
|
|
|
395 |
$resultRow = array();
|
|
|
396 |
$i = 0;
|
|
|
397 |
foreach ($row as $index => $field) {
|
|
|
398 |
$table = $column = null;
|
|
|
399 |
if (count($this->map[$index]) == 2) {
|
|
|
400 |
list($table, $column) = $this->map[$index];
|
|
|
401 |
}
|
|
|
402 |
$resultRow[$table][$column] = $row[$index];
|
|
|
403 |
$i++;
|
|
|
404 |
}
|
|
|
405 |
return $resultRow;
|
|
|
406 |
} else {
|
|
|
407 |
return false;
|
|
|
408 |
}
|
|
|
409 |
}
|
|
|
410 |
/**
|
|
|
411 |
* Gets the database encoding
|
|
|
412 |
*
|
|
|
413 |
* @return string The database encoding
|
|
|
414 |
*/
|
|
|
415 |
function getEncoding() {
|
|
|
416 |
return mysqli_client_encoding($this->connection);
|
|
|
417 |
}
|
|
|
418 |
/**
|
|
|
419 |
* Checks if the result is valid
|
|
|
420 |
*
|
|
|
421 |
* @return boolean True if the result is valid, else false
|
|
|
422 |
*/
|
|
|
423 |
function hasResult() {
|
|
|
424 |
return is_object($this->_result);
|
|
|
425 |
}
|
|
|
426 |
}
|
|
|
427 |
?>
|