2005-07-04 01:07:14 +00:00
|
|
|
<?php
|
2005-08-21 06:49:02 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-02 23:04:20 +00:00
|
|
|
|
I've merged in Olle's changes.
Larry, I've merged in _some_ of your changes, I'll merge in the scaffolding and joins code when you tell me it's ready. But I don't want to break how the Controller class works, can't we really do without the constructClasses() method call? Which reminds me, with your joins code, will we be able to use constructs like $user->post->findAll() and $user->post->save()?
Also, what are your changes to the DBO_MySQL class? I mean the mysqlResultSet(), and fetchResult() methods. I didn't see any MySQL-specific code inside them, perhaps they belong to the DBO class itself?
- I've changed the headers on user-editable files in /app and /config. I hope they will constitute a compromise between readability and legality. I've left file Id, copyright, and licence notices.
- /libs/basic.php::uses() function logs included files in global $loaded. Please, consider it a note to myself. Also, I've moved the NeatArray class out of the /libs/basics.php (into /libs/neat_array.php).
- Some cleanups in the Controller and Dispatcher classes.
- DBO::Prepare() accepts strings _and_ arrays now. It's a step towards a unified params theory.
- I think I've added some comments to DBO sub-classes, but it might have been Olle.
- A fix in Model class (findAll didn't work properly)
- Object's constructor sets $this->db to &DBO, which means all Object-descendand classes have default access to the database if it's connected. We need to clean up the code accordingly (some classes set their own $this->db references).
git-svn-id: https://svn.cakephp.org/repo/trunk/cake@236 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-06-05 11:05:24 +00:00
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* 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-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2006-01-12 02:10:47 +00:00
|
|
|
* @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-05-26 05:29:17 +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 0.2.9
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Create an include path required PEAR libraries.
|
|
|
|
*/
|
2005-06-12 20:50:12 +00:00
|
|
|
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . PEAR);
|
2006-05-26 05:29:17 +00:00
|
|
|
vendor ('Pear/DB');
|
2005-06-12 20:50:12 +00:00
|
|
|
|
2005-06-02 23:04:20 +00:00
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
* Long description for class
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
2006-12-01 02:34:56 +00:00
|
|
|
* @subpackage cake.cake.libs.model.datasources.dbo
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2006-12-01 02:34:56 +00:00
|
|
|
class DboPear extends DboSource{
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
/**
|
|
|
|
* PEAR::DB object with which we connect.
|
|
|
|
*
|
|
|
|
* @var DB The connection object.
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $_pear = null;
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +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
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function connect($config) {
|
|
|
|
$this->config =$config;
|
|
|
|
$dsn =$config['driver'] . '://' . $config['login'] . ':' . $config['password'] . '@'
|
|
|
|
. $config['host'] . '/' . $config['database'];
|
2006-12-05 09:49:59 +00:00
|
|
|
$options=array('debug' => Configure::read() - 1,
|
2006-05-26 05:29:17 +00:00
|
|
|
'portability' => DB_PORTABILITY_ALL,);
|
2005-06-12 20:50:12 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$this->_pear =&DB::connect($dsn, $options);
|
|
|
|
$this->connected=$this->_pear ? true : false;
|
|
|
|
return !(PEAR::isError($this->_pear));
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Disconnects from database.
|
|
|
|
*
|
|
|
|
* @return boolean True if the database could be disconnected, else false
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function disconnect() {
|
2006-12-22 22:49:47 +00:00
|
|
|
die (__('Please implement DBO::disconnect() first.'));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Executes given SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return resource Result resource identifier
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function execute($sql) {
|
|
|
|
return $this->_pear->query($sql);
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns a row from given resultset as an array .
|
|
|
|
*
|
|
|
|
* @return array The fetched row as an array
|
|
|
|
*/
|
2007-01-06 06:02:37 +00:00
|
|
|
function fetchRow($sql = null) {
|
|
|
|
if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
|
|
|
|
if (!$this->execute($sql)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-07-13 14:55:48 +00:00
|
|
|
return $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
|
|
|
|
* :WARNING: :TODO: POSTGRESQL & MYSQL ONLY! PEAR::DB doesn't support universal table listing.
|
|
|
|
*
|
|
|
|
* @return array Array of tablenames in the database
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function tablesList() {
|
|
|
|
$driver=$this->config['driver'];
|
|
|
|
$tables=array();
|
|
|
|
|
|
|
|
if ('postgres' == $driver) {
|
|
|
|
$sql ="SELECT a.relname AS name
|
|
|
|
FROM pg_class a, pg_user b
|
|
|
|
WHERE ( relkind = 'r') and relname !~ '^pg_' AND relname !~ '^sql_'
|
|
|
|
AND relname !~ '^xin[vx][0-9]+' AND b.usesysid = a.relowner
|
|
|
|
AND NOT (EXISTS (SELECT viewname FROM pg_views WHERE viewname=a.relname));";
|
|
|
|
|
|
|
|
$result=$this->all($sql);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($result as $item) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$tables[] = $item['name'];
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif ('mysql' == $driver) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$result=array();
|
|
|
|
$result=mysql_list_tables($this->config['database']);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
while ($item = mysql_fetch_array($result)) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$tables[] = $item[0];
|
|
|
|
}
|
|
|
|
} else {
|
2006-12-22 22:49:47 +00:00
|
|
|
die (__('Please implement DBO_Pear::tablesList() for your database driver.'));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
trigger_error(ERROR_NO_TABLE_LIST, E_USER_ERROR);
|
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
return $tables;
|
|
|
|
}
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* 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-05-26 05:29:17 +00:00
|
|
|
function fields($tableName) {
|
|
|
|
$data =$this->_pear->tableInfo($tableName);
|
|
|
|
$fields=false;
|
2005-06-02 23:04:20 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($data as $item) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$fields[] = array('name' => $item['name'],
|
|
|
|
'type' => $item['type']);
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
return $fields;
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +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
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function prepareValue($data) {
|
|
|
|
return $this->_pear->quoteSmart($data);
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns a formatted error message from previous database operation.
|
|
|
|
*
|
|
|
|
* @return string Error message
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function lastError() {
|
|
|
|
return PEAR::isError($this->_result) ? $this->_result->getMessage() : null;
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +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
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function lastAffected() {
|
|
|
|
return $this->_pear->affectedRows();
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
|
* this returns false.
|
|
|
|
*
|
|
|
|
* @return int Number of rows in resultset
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function lastNumRows() {
|
|
|
|
if (method_exists($this->_result, 'numRows')) {
|
|
|
|
return $this->_result->numRows();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns the ID generated from the previous INSERT operation.
|
|
|
|
*
|
|
|
|
* @param string $table Name of the database table
|
|
|
|
* @return int
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function lastInsertId($table) {
|
|
|
|
return $this->field('id', "SELECT MAX(id) FROM {$table}");
|
|
|
|
}
|
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
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function selectLimit($limit, $offset = '0') {
|
|
|
|
return ' ' . $this->_pear->modifyLimitQuery('', $offset, $limit);
|
|
|
|
}
|
2005-06-02 23:04:20 +00:00
|
|
|
}
|
|
|
|
?>
|