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
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs.model.dbo
|
2005-08-21 06:49:02 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2005-10-03 04:48:00 +00:00
|
|
|
uses('model'.DS.'dbo'.DS.'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
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs.model.dbo
|
2005-08-21 06:49:02 +00:00
|
|
|
* @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;
|
2005-10-24 09:54:24 +00:00
|
|
|
$this->_conn = $config['connect']($config['host'],$config['login'],$config['password']);
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
$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)
|
|
|
|
{
|
[1285]
Author: phpnut
Date: 10:09:03 PM, Monday, October 31, 2005
Message:
Removed references in the Session class
[1283]
Author: phpnut
Date: 8:47:37 PM, Monday, October 31, 2005
Message:
Added fix to the Controller::constructClassess().
The database should have an instance available if a component will use it.
[1282]
Author: phpnut
Date: 8:36:07 PM, Monday, October 31, 2005
Message:
Updated the Model association methods to correct and error I introduced when reactoring last week.
Added a return from each of the settings in Security::inactiveMins(); This class is not fully implemented.
Updated scaffold and dipatcher with changes to the session class.
Fixed problem with session not working properly.
Added a regenrate id for sessions.
When CAKE_SECURITY is set to high this will regenrate a new session key on each request.
The old session file will be removed from the file system. This is a added security measure.
[1270]
Author: phpnut
Date: 1:55:28 PM, Sunday, October 30, 2005
Message:
Updated Session class to regenrate a new session key on each request when security level set to high.
Updated doc comments in some classes
[1269]
Author: phpnut
Date: 9:49:43 AM, Sunday, October 30, 2005
Message:
Added a fix for Ticket #105
git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1286 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-11-01 04:20:28 +00:00
|
|
|
list($table, $column) = $this->map[$index];
|
|
|
|
$resultRow[$table][$column] = $row[$index];
|
|
|
|
$i++;
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
return $resultRow;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
[1285]
Author: phpnut
Date: 10:09:03 PM, Monday, October 31, 2005
Message:
Removed references in the Session class
[1283]
Author: phpnut
Date: 8:47:37 PM, Monday, October 31, 2005
Message:
Added fix to the Controller::constructClassess().
The database should have an instance available if a component will use it.
[1282]
Author: phpnut
Date: 8:36:07 PM, Monday, October 31, 2005
Message:
Updated the Model association methods to correct and error I introduced when reactoring last week.
Added a return from each of the settings in Security::inactiveMins(); This class is not fully implemented.
Updated scaffold and dipatcher with changes to the session class.
Fixed problem with session not working properly.
Added a regenrate id for sessions.
When CAKE_SECURITY is set to high this will regenrate a new session key on each request.
The old session file will be removed from the file system. This is a added security measure.
[1270]
Author: phpnut
Date: 1:55:28 PM, Sunday, October 30, 2005
Message:
Updated Session class to regenrate a new session key on each request when security level set to high.
Updated doc comments in some classes
[1269]
Author: phpnut
Date: 9:49:43 AM, Sunday, October 30, 2005
Message:
Added a fix for Ticket #105
git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1286 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-11-01 04:20:28 +00:00
|
|
|
|
2005-06-12 20:50:12 +00:00
|
|
|
}
|
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
?>
|