2005-07-04 01:07:14 +00:00
|
|
|
<?php
|
2005-08-21 06:49:02 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
2005-08-21 06:49:02 +00:00
|
|
|
* MySQL layer for DBO
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright (c) 2005, CakePHP Authors/Developers
|
|
|
|
*
|
|
|
|
* Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com>
|
|
|
|
* Larry E. Masters aka PhpNut <nut@phpnut.com>
|
|
|
|
* Kamil Dzielinski aka Brego <brego.dk@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @author CakePHP Authors/Developers
|
|
|
|
* @copyright Copyright (c) 2005, CakePHP Authors/Developers
|
|
|
|
* @link https://trac.cakephp.org/wiki/Authors Authors/Developers
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs.dbo
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Include DBO.
|
|
|
|
*/
|
|
|
|
uses('dbo');
|
2005-06-14 19:57:01 +00:00
|
|
|
|
2005-06-12 20:50:12 +00:00
|
|
|
/**
|
2005-08-21 06:49:02 +00:00
|
|
|
* Short description for class.
|
|
|
|
*
|
|
|
|
* Long description for class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs.dbo
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
*/
|
2005-06-14 19:57:01 +00:00
|
|
|
class DBO_MySQL extends DBO
|
|
|
|
{
|
|
|
|
|
2005-06-12 20:50:12 +00:00
|
|
|
/**
|
|
|
|
* Connects to the database using options in the given configuration array.
|
|
|
|
*
|
|
|
|
* @param array $config Configuration array for connecting
|
|
|
|
* @return boolean True if the database could be connected, else false
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function connect ($config)
|
|
|
|
{
|
|
|
|
if ($config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->_conn = mysql_pconnect($config['host'],$config['login'],$config['password']);
|
|
|
|
}
|
|
|
|
$this->connected = $this->_conn? true: false;
|
|
|
|
|
|
|
|
if($this->connected)
|
|
|
|
return mysql_select_db($config['database'], $this->_conn);
|
2005-08-21 06:49:02 +00:00
|
|
|
else{
|
|
|
|
//die('Could not connect to DB.');
|
|
|
|
}
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects from database.
|
|
|
|
*
|
|
|
|
* @return boolean True if the database could be disconnected, else false
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function disconnect ()
|
|
|
|
{
|
|
|
|
return mysql_close();
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes given SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
2005-06-14 19:57:01 +00:00
|
|
|
* @return resource Result resource identifier
|
2005-06-12 20:50:12 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function execute ($sql)
|
|
|
|
{
|
|
|
|
return mysql_query($sql);
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a row from given resultset as an array .
|
|
|
|
*
|
2005-06-14 19:57:01 +00:00
|
|
|
* @param bool $assoc Associative array only, or both?
|
2005-06-12 20:50:12 +00:00
|
|
|
* @return array The fetched row as an array
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function fetchRow ($assoc=false)
|
|
|
|
{
|
|
|
|
//return mysql_fetch_array($this->_result, $assoc? MYSQL_ASSOC: MYSQL_BOTH);
|
|
|
|
$this->mysqlResultSet($this->_result);
|
|
|
|
$resultRow = $this->fetchResult();
|
|
|
|
return $resultRow;
|
|
|
|
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
|
|
|
|
*
|
|
|
|
* @return array Array of tablenames in the database
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function tablesList ()
|
|
|
|
{
|
|
|
|
$result = mysql_list_tables($this->config['database']);
|
|
|
|
|
|
|
|
if (!$result)
|
|
|
|
{
|
|
|
|
trigger_error(ERROR_NO_TABLE_LIST, E_USER_NOTICE);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$tables = array();
|
|
|
|
while ($line = mysql_fetch_array($result))
|
|
|
|
{
|
|
|
|
$tables[] = $line[0];
|
|
|
|
}
|
|
|
|
return $tables;
|
|
|
|
}
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of the fields in given table name.
|
|
|
|
*
|
2005-09-18 13:25:20 +00:00
|
|
|
* @param string $tableName Name of database table to inspect
|
2005-06-12 20:50:12 +00:00
|
|
|
* @return array Fields in table. Keys are name and type
|
|
|
|
*/
|
2005-09-18 13:25:20 +00:00
|
|
|
function fields ($tableName)
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
|
|
|
$fields = false;
|
2005-09-18 13:25:20 +00:00
|
|
|
$cols = $this->all("DESC {$tableName}");
|
2005-06-12 20:50:12 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
foreach ($cols as $column)
|
2005-09-17 02:22:07 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
// $fields[] = array('name'=>$column['Field'], 'type'=>$column['Type']);
|
2005-09-17 02:22:07 +00:00
|
|
|
if(isset($column['COLUMNS']) && !isset($column[0]))
|
|
|
|
{
|
|
|
|
$column[0] = $column['COLUMNS'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($column[0]))
|
|
|
|
{
|
|
|
|
$fields[] = array('name' => $column[0]['Field'], 'type' => $column[0]['Type']);
|
|
|
|
}
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
return $fields;
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return string Quoted and escaped
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function prepareValue ($data)
|
|
|
|
{
|
|
|
|
return "'".mysql_real_escape_string($data)."'";
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a formatted error message from previous database operation.
|
|
|
|
*
|
|
|
|
* @return string Error message with error number
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function lastError ()
|
|
|
|
{
|
|
|
|
return mysql_errno()? mysql_errno().': '.mysql_error(): null;
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of affected rows in previous database operation. If no previous operation exists, this returns false.
|
|
|
|
*
|
|
|
|
* @return int Number of affected rows
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function lastAffected ()
|
|
|
|
{
|
|
|
|
return $this->_result? mysql_affected_rows(): false;
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
|
* this returns false.
|
|
|
|
*
|
2005-06-14 19:57:01 +00:00
|
|
|
* @return int Number of rows in resultset
|
2005-06-12 20:50:12 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function lastNumRows ()
|
|
|
|
{
|
|
|
|
return $this->_result? @mysql_num_rows($this->_result): false;
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID generated from the previous INSERT operation.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function lastInsertId ()
|
|
|
|
{
|
|
|
|
return mysql_insert_id();
|
|
|
|
}
|
2005-06-14 19:57:01 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
/**
|
|
|
|
* Returns a limit statement in the correct format for the particular database.
|
|
|
|
*
|
|
|
|
* @param int $limit Limit of results returned
|
|
|
|
* @param int $offset Offset from which to start results
|
|
|
|
* @return string SQL limit/offset statement
|
|
|
|
*/
|
|
|
|
function selectLimit ($limit, $offset=null)
|
|
|
|
{
|
2005-09-17 02:22:07 +00:00
|
|
|
return $limit? " LIMIT".($offset? " {$offset},": null)." {$limit}": null;
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $results
|
|
|
|
*/
|
2005-08-28 16:33:50 +00:00
|
|
|
function mysqlResultSet(&$results)
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-08-28 16:33:50 +00:00
|
|
|
$this->results =& $results;
|
2005-07-10 05:08:19 +00:00
|
|
|
$this->map = array();
|
|
|
|
$index = 0;
|
|
|
|
$num_fields = mysql_num_fields($results);
|
|
|
|
$j=0;
|
|
|
|
|
|
|
|
while ($j < $num_fields)
|
|
|
|
{
|
|
|
|
$column = mysql_fetch_field($results,$j);
|
|
|
|
|
|
|
|
if(!empty($column->table))
|
|
|
|
{
|
|
|
|
$this->map[$index++] = array($column->table, $column->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->map[$index++] = array(0, $column->name);
|
|
|
|
}
|
|
|
|
$j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function fetchResult()
|
|
|
|
{
|
|
|
|
if ($row = mysql_fetch_row($this->results))
|
|
|
|
{
|
|
|
|
$resultRow = array();
|
|
|
|
$i =0;
|
|
|
|
|
|
|
|
foreach ($row as $index => $field)
|
|
|
|
{
|
|
|
|
list($table, $column) = $this->map[$index];
|
|
|
|
$resultRow[Inflector::singularize($table)][$column] = $row[$index];
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return $resultRow;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2005-06-12 20:50:12 +00:00
|
|
|
}
|
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
?>
|