2006-06-14 18:02:37 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* MySQLi layer for DBO
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-06-14 18:02:37 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-06-14 18:02:37 +00:00
|
|
|
* @package cake
|
2006-12-01 02:34:56 +00:00
|
|
|
* @subpackage cake.cake.libs.model.datasources.dbo
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 1.1.4.2974
|
2006-06-14 18:02:37 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2006-10-18 19:35:29 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
|
|
|
* Long description for class
|
|
|
|
*
|
|
|
|
* @package cake
|
2006-12-01 02:34:56 +00:00
|
|
|
* @subpackage cake.cake.libs.model.datasources.dbo
|
2006-06-14 18:02:37 +00:00
|
|
|
*/
|
|
|
|
class DboMysqli extends DboSource {
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $description = "Mysqli DBO Driver";
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $startQuote = "`";
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
|
|
|
var $endQuote = "`";
|
|
|
|
/**
|
|
|
|
* Base configuration settings for Mysqli driver
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $_baseConfig = array('persistent' => true,
|
|
|
|
'host' => 'localhost',
|
|
|
|
'login' => 'root',
|
|
|
|
'password' => '',
|
|
|
|
'database' => 'cake',
|
|
|
|
'port' => '3306',
|
2007-03-23 08:57:42 +00:00
|
|
|
'connect' => 'mysqli_connect');
|
2006-06-14 18:02:37 +00:00
|
|
|
/**
|
|
|
|
* Mysqli column definition
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'),
|
|
|
|
'string' => array('name' => 'varchar', 'limit' => '255'),
|
|
|
|
'text' => array('name' => 'text'),
|
|
|
|
'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
|
|
|
|
'float' => array('name' => 'float', 'formatter' => 'floatval'),
|
|
|
|
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
|
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
|
'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
|
|
|
|
'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
|
|
|
|
'binary' => array('name' => 'blob'),
|
|
|
|
'boolean' => array('name' => 'tinyint', 'limit' => '1'));
|
|
|
|
/**
|
|
|
|
* Connects to the database using options in the given configuration array.
|
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the database could be connected, else false
|
2006-06-14 18:02:37 +00:00
|
|
|
*/
|
|
|
|
function connect() {
|
|
|
|
$config = $this->config;
|
2007-04-05 17:09:52 +00:00
|
|
|
$this->connected = false;
|
2007-03-23 10:56:42 +00:00
|
|
|
$this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], $config['database'], $config['port']);
|
2007-04-05 17:09:52 +00:00
|
|
|
|
|
|
|
if ($this->connection !== false) {
|
|
|
|
$this->connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($config['encoding'])) {
|
|
|
|
$this->setEncoding($config['encoding']);
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
return $this->connected;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Disconnects from database.
|
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the database could be disconnected, else false
|
2006-06-14 18:02:37 +00:00
|
|
|
*/
|
|
|
|
function disconnect() {
|
2006-11-23 20:23:43 +00:00
|
|
|
@mysqli_free_result($this->results);
|
2006-06-14 18:02:37 +00:00
|
|
|
$this->connected = !@mysqli_close($this->connection);
|
|
|
|
return !$this->connected;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Executes given SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return resource Result resource identifier
|
|
|
|
* @access protected
|
|
|
|
*/
|
2006-09-16 02:39:06 +00:00
|
|
|
function _execute($sql) {
|
|
|
|
return mysqli_query($this->connection, $sql);
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns an array of sources (tables) in the database.
|
|
|
|
*
|
|
|
|
* @return array Array of tablenames in the database
|
|
|
|
*/
|
2006-09-16 02:39:06 +00:00
|
|
|
function listSources() {
|
2006-06-14 18:02:37 +00:00
|
|
|
$cache = parent::listSources();
|
|
|
|
if ($cache != null) {
|
|
|
|
return $cache;
|
2006-09-16 02:39:06 +00:00
|
|
|
}
|
2007-05-13 14:52:17 +00:00
|
|
|
$result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
|
2007-05-12 06:35:32 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
if (!$result) {
|
|
|
|
return array();
|
|
|
|
} else {
|
|
|
|
$tables = array();
|
2007-05-12 06:35:32 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
while ($line = mysqli_fetch_array($result)) {
|
|
|
|
$tables[] = $line[0];
|
|
|
|
}
|
|
|
|
parent::listSources($tables);
|
|
|
|
return $tables;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns an array of the fields in given table name.
|
|
|
|
*
|
|
|
|
* @param string $tableName Name of database table to inspect
|
|
|
|
* @return array Fields in table. Keys are name and type
|
|
|
|
*/
|
2006-09-16 02:39:06 +00:00
|
|
|
function describe(&$model) {
|
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
$cache = parent::describe($model);
|
|
|
|
if ($cache != null) {
|
|
|
|
return $cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = false;
|
2006-09-16 02:39:06 +00:00
|
|
|
$cols = $this->query('DESCRIBE ' . $this->fullTableName($model));
|
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
foreach ($cols as $column) {
|
|
|
|
$colKey = array_keys($column);
|
|
|
|
if (isset($column[$colKey[0]]) && !isset($column[0])) {
|
|
|
|
$column[0] = $column[$colKey[0]];
|
|
|
|
}
|
|
|
|
if (isset($column[0])) {
|
2007-08-21 21:46:59 +00:00
|
|
|
$fields[$column[0]['Field']] = array(
|
2007-05-12 06:35:32 +00:00
|
|
|
'type' => $this->column($column[0]['Type']),
|
|
|
|
'null' => ($column[0]['Null'] == 'YES' ? true : false),
|
|
|
|
'default' => $column[0]['Default'],
|
|
|
|
'length' => $this->length($column[0]['Type'])
|
2006-07-21 22:01:26 +00:00
|
|
|
);
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-09-16 02:39:06 +00:00
|
|
|
}
|
|
|
|
|
2007-05-12 06:35:32 +00:00
|
|
|
$this->__cacheDescription($this->fullTableName($model, false), $fields);
|
2006-06-14 18:02:37 +00:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns a quoted and escaped string of $data for use in an SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $data String to be prepared for use in an SQL statement
|
|
|
|
* @param string $column The column into which this data will be inserted
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
|
2006-06-14 18:02:37 +00:00
|
|
|
* @return string Quoted and escaped data
|
|
|
|
*/
|
|
|
|
function value($data, $column = null, $safe = false) {
|
|
|
|
$parent = parent::value($data, $column, $safe);
|
|
|
|
|
|
|
|
if ($parent != null) {
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($data === null) {
|
|
|
|
return 'NULL';
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($data === '') {
|
2006-06-14 18:02:37 +00:00
|
|
|
return "''";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($column) {
|
|
|
|
case 'boolean':
|
|
|
|
$data = $this->boolean((bool)$data);
|
|
|
|
break;
|
2007-03-04 04:05:57 +00:00
|
|
|
case 'integer' :
|
|
|
|
case 'float' :
|
|
|
|
case null :
|
2007-10-20 12:40:19 +00:00
|
|
|
if (is_numeric($data) && strpos($data, ',') === false && $data[0] != '0' && strpos($data, 'e') === false) {
|
2007-03-04 04:05:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
default:
|
2007-03-04 04:05:57 +00:00
|
|
|
$data = "'" . mysqli_real_escape_string($this->connection, $data) . "'";
|
2006-06-14 18:02:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-03-04 04:05:57 +00:00
|
|
|
return $data;
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Begin a transaction
|
|
|
|
*
|
|
|
|
* @param unknown_type $model
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True on success, false on fail
|
2006-06-14 18:02:37 +00:00
|
|
|
* (i.e. if the database/model does not support transactions).
|
|
|
|
*/
|
|
|
|
function begin(&$model) {
|
|
|
|
if (parent::begin($model)) {
|
|
|
|
if ($this->execute('START TRANSACTION')) {
|
2007-02-17 08:28:09 +00:00
|
|
|
$this->_transactionStarted = true;
|
2006-06-14 18:02:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Commit a transaction
|
|
|
|
*
|
|
|
|
* @param unknown_type $model
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True on success, false on fail
|
2006-06-14 18:02:37 +00:00
|
|
|
* (i.e. if the database/model does not support transactions,
|
|
|
|
* or a transaction has not started).
|
|
|
|
*/
|
|
|
|
function commit(&$model) {
|
|
|
|
if (parent::commit($model)) {
|
2007-02-17 08:28:09 +00:00
|
|
|
$this->_transactionStarted = false;
|
2006-06-14 18:02:37 +00:00
|
|
|
return $this->execute('COMMIT');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Rollback a transaction
|
|
|
|
*
|
|
|
|
* @param unknown_type $model
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True on success, false on fail
|
2006-06-14 18:02:37 +00:00
|
|
|
* (i.e. if the database/model does not support transactions,
|
|
|
|
* or a transaction has not started).
|
|
|
|
*/
|
|
|
|
function rollback(&$model) {
|
|
|
|
if (parent::rollback($model)) {
|
|
|
|
return $this->execute('ROLLBACK');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns a formatted error message from previous database operation.
|
|
|
|
*
|
|
|
|
* @return string Error message with error number
|
|
|
|
*/
|
|
|
|
function lastError() {
|
|
|
|
if (mysqli_errno($this->connection)) {
|
|
|
|
return mysqli_errno($this->connection).': '.mysqli_error($this->connection);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns number of affected rows in previous database operation. If no previous operation exists,
|
|
|
|
* this returns false.
|
|
|
|
*
|
2007-10-22 16:54:36 +00:00
|
|
|
* @return integer Number of affected rows
|
2006-06-14 18:02:37 +00:00
|
|
|
*/
|
|
|
|
function lastAffected() {
|
|
|
|
if ($this->_result) {
|
|
|
|
return mysqli_affected_rows($this->connection);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
|
* this returns false.
|
|
|
|
*
|
2007-10-22 16:54:36 +00:00
|
|
|
* @return integer Number of rows in resultset
|
2006-06-14 18:02:37 +00:00
|
|
|
*/
|
|
|
|
function lastNumRows() {
|
2006-07-21 22:01:26 +00:00
|
|
|
if ($this->_result and is_object($this->_result)) {
|
2006-06-14 18:02:37 +00:00
|
|
|
return @mysqli_num_rows($this->_result);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the ID generated from the previous INSERT operation.
|
|
|
|
*
|
|
|
|
* @param unknown_type $source
|
|
|
|
* @return in
|
|
|
|
*/
|
|
|
|
function lastInsertId($source = null) {
|
2007-05-24 05:33:55 +00:00
|
|
|
$id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false);
|
|
|
|
if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) {
|
|
|
|
return $id[0]['insertID'];
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2007-05-12 06:35:32 +00:00
|
|
|
|
2007-04-07 01:42:12 +00:00
|
|
|
return null;
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts database-layer column types to basic types
|
|
|
|
*
|
|
|
|
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
|
|
|
* @return string Abstract column type (i.e. "string")
|
|
|
|
*/
|
|
|
|
function column($real) {
|
|
|
|
if (is_array($real)) {
|
|
|
|
$col = $real['name'];
|
2007-05-12 06:35:32 +00:00
|
|
|
if (isset($real['limit'])) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$col .= '('.$real['limit'].')';
|
|
|
|
}
|
|
|
|
return $col;
|
|
|
|
}
|
|
|
|
|
2007-12-08 06:08:03 +00:00
|
|
|
$col = str_replace(')', '', $real);
|
2007-05-12 06:35:32 +00:00
|
|
|
$limit = $this->length($real);
|
|
|
|
@list($col,$vals) = explode('(', $col);
|
2006-06-14 18:02:37 +00:00
|
|
|
|
|
|
|
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
|
|
|
|
return $col;
|
|
|
|
}
|
2007-05-12 06:35:32 +00:00
|
|
|
if ($col == 'tinyint' && $limit == 1) {
|
2006-06-14 18:02:37 +00:00
|
|
|
return 'boolean';
|
|
|
|
}
|
|
|
|
if (strpos($col, 'int') !== false) {
|
|
|
|
return 'integer';
|
|
|
|
}
|
|
|
|
if (strpos($col, 'char') !== false || $col == 'tinytext') {
|
|
|
|
return 'string';
|
|
|
|
}
|
|
|
|
if (strpos($col, 'text') !== false) {
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
if (strpos($col, 'blob') !== false) {
|
|
|
|
return 'binary';
|
|
|
|
}
|
|
|
|
if (in_array($col, array('float', 'double', 'decimal'))) {
|
|
|
|
return 'float';
|
|
|
|
}
|
|
|
|
if (strpos($col, 'enum') !== false) {
|
2007-05-12 06:35:32 +00:00
|
|
|
return "enum($vals)";
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-06-19 02:55:53 +00:00
|
|
|
if ($col == 'boolean') {
|
|
|
|
return $col;
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
return 'text';
|
|
|
|
}
|
2007-05-12 06:35:32 +00:00
|
|
|
/**
|
|
|
|
* Gets the length of a database-native column description, or null if no length
|
|
|
|
*
|
|
|
|
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
2007-10-22 16:54:36 +00:00
|
|
|
* @return integer An integer representing the length of the column
|
2007-05-12 06:35:32 +00:00
|
|
|
*/
|
|
|
|
function length($real) {
|
2007-12-08 06:08:03 +00:00
|
|
|
$col = str_replace(array(')', 'unsigned'), '', $real);
|
2007-05-12 06:35:32 +00:00
|
|
|
$limit = null;
|
|
|
|
|
|
|
|
if (strpos($col, '(') !== false) {
|
|
|
|
list($col, $limit) = explode('(', $col);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit != null) {
|
|
|
|
return intval($limit);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $results
|
|
|
|
*/
|
2006-09-16 02:39:06 +00:00
|
|
|
function resultSet(&$results) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$this->results =& $results;
|
|
|
|
$this->map = array();
|
2006-09-16 02:39:06 +00:00
|
|
|
$num_fields = mysqli_num_fields($results);
|
2006-06-14 18:02:37 +00:00
|
|
|
$index = 0;
|
|
|
|
$j = 0;
|
|
|
|
while ($j < $num_fields) {
|
2006-09-16 02:39:06 +00:00
|
|
|
$column = mysqli_fetch_field_direct($results, $j);
|
2006-06-14 18:02:37 +00:00
|
|
|
if (!empty($column->table)) {
|
|
|
|
$this->map[$index++] = array($column->table, $column->name);
|
2006-09-16 02:39:06 +00:00
|
|
|
} else {
|
2006-06-14 18:02:37 +00:00
|
|
|
$this->map[$index++] = array(0, $column->name);
|
|
|
|
}
|
|
|
|
$j++;
|
2006-09-16 02:39:06 +00:00
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Fetches the next row from the current result set
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-09-16 02:39:06 +00:00
|
|
|
function fetchResult() {
|
|
|
|
if ($row = mysqli_fetch_row($this->results)) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$resultRow = array();
|
2006-09-16 02:39:06 +00:00
|
|
|
$i = 0;
|
|
|
|
foreach ($row as $index => $field) {
|
|
|
|
@list($table, $column) = $this->map[$index];
|
|
|
|
$resultRow[$table][$column] = $row[$index];
|
|
|
|
$i++;
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
return $resultRow;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-04-05 17:09:52 +00:00
|
|
|
/**
|
|
|
|
* Sets the database encoding
|
|
|
|
*
|
|
|
|
* @param string $enc Database encoding
|
|
|
|
*/
|
|
|
|
function setEncoding($enc) {
|
|
|
|
return $this->_execute('SET NAMES ' . $enc) != false;
|
|
|
|
}
|
2007-05-12 06:35:32 +00:00
|
|
|
/**
|
|
|
|
* Gets the database encoding
|
|
|
|
*
|
|
|
|
* @return string The database encoding
|
|
|
|
*/
|
|
|
|
function getEncoding() {
|
2007-05-31 06:00:52 +00:00
|
|
|
return mysqli_client_encoding($this->connection);
|
2007-05-12 06:35:32 +00:00
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2007-05-03 04:35:25 +00:00
|
|
|
?>
|