2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Dbo Source
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model.datasources
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 03:45:18 +00:00
|
|
|
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('DataSource', 'Model/Datasource');
|
2010-12-07 03:18:30 +00:00
|
|
|
App::uses('String', 'Utility');
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('View', 'View');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DboSource
|
|
|
|
*
|
|
|
|
* Creates DBO-descendant objects from a given db connection configuration
|
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model.datasources
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class DboSource extends DataSource {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Description string for this Database Data Source.
|
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* @var string
|
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $description = "Database Data Source";
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* index definition, standard cake, primary, index, unique
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $index = array('PRI' => 'primary', 'MUL' => 'index', 'UNI' => 'unique');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-11-10 18:57:50 +00:00
|
|
|
* Database keyword used to assign aliases to identifiers.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-11-10 18:57:50 +00:00
|
|
|
* @var string
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $alias = 'AS ';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-06-30 12:52:39 +00:00
|
|
|
* Caches result from query parsing operations. Cached results for both DboSource::name() and
|
2010-09-05 18:22:45 +00:00
|
|
|
* DboSource::conditions() will be stored here. Method caching uses `crc32()` which is
|
2010-06-30 12:52:39 +00:00
|
|
|
* fast but can collisions more easily than other hashing algorithms. If you have problems
|
|
|
|
* with collisions, set DboSource::$cacheMethods to false.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $methodCache = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-03-14 03:55:18 +00:00
|
|
|
/**
|
|
|
|
* Whether or not to cache the results of DboSource::name() and DboSource::conditions()
|
|
|
|
* into the memory cache. Set to false to disable the use of the memory cache.
|
|
|
|
*
|
|
|
|
* @var boolean.
|
|
|
|
* @access public
|
|
|
|
*/
|
2011-02-25 00:12:58 +00:00
|
|
|
public $cacheMethods = true;
|
2010-03-14 03:55:18 +00:00
|
|
|
|
2011-02-25 00:06:30 +00:00
|
|
|
/**
|
|
|
|
* Print full query debug info?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $fullDebug = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error description of last query
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $error = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String to hold how many rows were affected by the last SQL operation.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $affected = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of rows in current resultset
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $numRows = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time the last query took
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $took = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Result
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $_result = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries count.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $_queriesCnt = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Total duration of all queries.
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $_queriesTime = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log of queries executed by this DataSource
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $_queriesLog = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum number of items in query log
|
|
|
|
*
|
|
|
|
* This is to prevent query log taking over too much memory.
|
|
|
|
*
|
|
|
|
* @var int Maximum number of queries in the queries log.
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $_queriesLogMax = 200;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caches serialzed results of executed queries
|
|
|
|
*
|
|
|
|
* @var array Maximum number of queries in the queries log.
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $_queryCache = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the physical connection of this DataSource
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $connection = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The DataSource configuration key name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $configKeyName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The starting character that this DataSource uses for quoted identifiers.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $startQuote = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ending character that this DataSource uses for quoted identifiers.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $endQuote = null;
|
2010-03-14 03:55:18 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-11-10 18:57:50 +00:00
|
|
|
* Bypass automatic adding of joined fields/associations.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-11-10 18:57:50 +00:00
|
|
|
* @var boolean
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access private
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__bypass = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The set of valid SQL operations usable in a WHERE statement
|
|
|
|
*
|
|
|
|
* @var array
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access private
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__sqlOps = array('like', 'ilike', 'or', 'not', 'in', 'between', 'regexp', 'similar to');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-11-27 04:16:34 +00:00
|
|
|
/**
|
|
|
|
* Indicates the level of nested transactions
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $_transactionNesting = 0;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Index of basic SQL commands
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_commands = array(
|
2009-08-19 17:03:36 +00:00
|
|
|
'begin' => 'BEGIN',
|
|
|
|
'commit' => 'COMMIT',
|
2008-05-30 11:40:08 +00:00
|
|
|
'rollback' => 'ROLLBACK'
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-22 04:58:54 +00:00
|
|
|
/**
|
|
|
|
* Separator string for virtualField composition
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-06-26 16:29:20 +00:00
|
|
|
public $virtualFieldSeparator = '__';
|
2010-05-22 04:58:54 +00:00
|
|
|
|
2009-09-29 15:25:42 +00:00
|
|
|
/**
|
|
|
|
* List of table engine specific parameters used on table creating
|
|
|
|
*
|
|
|
|
* @var array
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access public
|
2009-09-29 15:25:42 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $tableParameters = array();
|
2009-09-29 15:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of engine specific additional field parameters used on table creating
|
|
|
|
*
|
|
|
|
* @var array
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access public
|
2009-09-29 15:25:42 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $fieldParameters = array();
|
2009-09-29 15:25:42 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
2010-01-16 12:32:04 +00:00
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* @param array $config Array of configuration information for the Datasource.
|
|
|
|
* @param boolean $autoConnect Whether or not the datasource should automatically connect.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function __construct($config = null, $autoConnect = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($config['prefix'])) {
|
|
|
|
$config['prefix'] = '';
|
|
|
|
}
|
|
|
|
parent::__construct($config);
|
2010-07-06 01:50:36 +00:00
|
|
|
$this->fullDebug = Configure::read('debug') > 1;
|
2009-10-20 20:11:31 +00:00
|
|
|
if (!$this->enabled()) {
|
2011-02-25 00:16:03 +00:00
|
|
|
return;
|
2009-10-20 20:11:31 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($autoConnect) {
|
2011-02-25 00:16:03 +00:00
|
|
|
$this->connect();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Reconnects to database server with optional new settings
|
|
|
|
*
|
|
|
|
* @param array $config An array defining the new configuration settings
|
|
|
|
* @return boolean True on success, false on failure
|
|
|
|
*/
|
2010-10-12 01:52:10 +00:00
|
|
|
function reconnect($config = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->disconnect();
|
2008-08-01 04:54:54 +00:00
|
|
|
$this->setConfig($config);
|
2008-08-12 21:08:51 +00:00
|
|
|
$this->_sources = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->connect();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-10-18 04:07:56 +00:00
|
|
|
* Disconnects from database.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-18 04:07:56 +00:00
|
|
|
* @return boolean True if the database could be disconnected, else false
|
|
|
|
*/
|
|
|
|
function disconnect() {
|
2010-12-04 18:28:43 +00:00
|
|
|
if ($this->_result instanceof PDOStatement) {
|
2010-10-18 04:07:56 +00:00
|
|
|
$this->_result->closeCursor();
|
|
|
|
}
|
|
|
|
unset($this->_connection);
|
|
|
|
$this->connected = false;
|
2011-02-25 04:02:37 +00:00
|
|
|
return true;
|
2010-10-18 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2010-12-04 18:28:43 +00:00
|
|
|
/**
|
|
|
|
* Get the underlying connection object.
|
|
|
|
*
|
|
|
|
* @return PDOConnection
|
|
|
|
*/
|
2010-10-18 04:07:56 +00:00
|
|
|
public function getConnection() {
|
|
|
|
return $this->_connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2008-10-30 05:03:01 +00:00
|
|
|
* @param string $column The column into which this data will be inserted
|
2010-10-18 04:08:55 +00:00
|
|
|
* @return string Quoted and escaped data
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-02 02:37:27 +00:00
|
|
|
function value($data, $column = null) {
|
2008-06-10 16:17:44 +00:00
|
|
|
if (is_array($data) && !empty($data)) {
|
2008-10-30 05:03:01 +00:00
|
|
|
return array_map(
|
|
|
|
array(&$this, 'value'),
|
2011-01-02 02:37:27 +00:00
|
|
|
$data, array_fill(0, count($data), $column)
|
2008-10-30 05:03:01 +00:00
|
|
|
);
|
2011-02-26 17:06:50 +00:00
|
|
|
} elseif (is_object($data) && isset($data->type, $data->value)) {
|
2008-10-30 05:03:01 +00:00
|
|
|
if ($data->type == 'identifier') {
|
2008-05-31 12:36:38 +00:00
|
|
|
return $this->name($data->value);
|
2008-10-30 05:03:01 +00:00
|
|
|
} elseif ($data->type == 'expression') {
|
|
|
|
return $data->value;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) {
|
|
|
|
return $data;
|
2011-02-25 00:12:58 +00:00
|
|
|
}
|
2010-10-18 04:08:55 +00:00
|
|
|
|
|
|
|
if ($data === null || (is_array($data) && empty($data))) {
|
|
|
|
return 'NULL';
|
|
|
|
}
|
2010-10-18 04:25:11 +00:00
|
|
|
|
2010-10-18 04:08:55 +00:00
|
|
|
if (empty($column)) {
|
|
|
|
$column = $this->introspectType($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($column) {
|
|
|
|
case 'binary':
|
2010-10-18 04:25:11 +00:00
|
|
|
return $this->_connection->quote($data, PDO::PARAM_LOB);
|
2010-10-18 04:08:55 +00:00
|
|
|
break;
|
|
|
|
case 'boolean':
|
2011-01-02 02:37:27 +00:00
|
|
|
return $this->_connection->quote($this->boolean($data, true), PDO::PARAM_BOOL);
|
2010-10-18 04:08:55 +00:00
|
|
|
break;
|
2010-10-18 04:25:11 +00:00
|
|
|
case 'string':
|
|
|
|
case 'text':
|
|
|
|
return $this->_connection->quote($data, PDO::PARAM_STR);
|
|
|
|
default:
|
2010-10-18 04:08:55 +00:00
|
|
|
if ($data === '') {
|
|
|
|
return 'NULL';
|
|
|
|
}
|
|
|
|
if (is_float($data)) {
|
|
|
|
return sprintf('%F', $data);
|
|
|
|
}
|
|
|
|
if ((is_int($data) || $data === '0') || (
|
|
|
|
is_numeric($data) && strpos($data, ',') === false &&
|
|
|
|
$data[0] != '0' && strpos($data, 'e') === false)
|
|
|
|
) {
|
|
|
|
return $data;
|
|
|
|
}
|
2010-10-18 04:25:11 +00:00
|
|
|
return $this->_connection->quote($data);
|
2010-10-18 04:08:55 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-18 04:08:55 +00:00
|
|
|
|
2008-05-31 12:36:38 +00:00
|
|
|
/**
|
2010-12-23 03:33:17 +00:00
|
|
|
* Returns an object to represent a database identifier in a query. Expression objects
|
|
|
|
* are not sanitized or esacped.
|
2008-05-31 12:36:38 +00:00
|
|
|
*
|
2010-12-23 03:33:17 +00:00
|
|
|
* @param string $identifier A SQL expression to be used as an identifier
|
2008-05-31 12:36:38 +00:00
|
|
|
* @return object An object representing a database identifier to be used in a query
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function identifier($identifier) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$obj = new stdClass();
|
|
|
|
$obj->type = 'identifier';
|
|
|
|
$obj->value = $identifier;
|
|
|
|
return $obj;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 12:36:38 +00:00
|
|
|
/**
|
2010-12-23 03:33:17 +00:00
|
|
|
* Returns an object to represent a database expression in a query. Expression objects
|
|
|
|
* are not sanitized or esacped.
|
2008-05-31 12:36:38 +00:00
|
|
|
*
|
2010-12-23 03:33:17 +00:00
|
|
|
* @param string $expression An arbitrary SQL expression to be inserted into a query.
|
2008-05-31 12:36:38 +00:00
|
|
|
* @return object An object representing a database expression to be used in a query
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function expression($expression) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$obj = new stdClass();
|
|
|
|
$obj->type = 'expression';
|
|
|
|
$obj->value = $expression;
|
|
|
|
return $obj;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Executes given SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
2010-12-23 03:33:17 +00:00
|
|
|
* @param array $params Additional options for the query.
|
2009-12-19 18:06:32 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-10-15 21:32:37 +00:00
|
|
|
public function rawQuery($sql, $params = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->took = $this->error = $this->numRows = false;
|
2010-10-15 21:32:37 +00:00
|
|
|
return $this->execute($sql, $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Queries the database with given SQL statement, and obtains some metadata about the result
|
|
|
|
* (rows affected, timing, any errors, number of rows in resultset). The query is also logged.
|
2009-12-19 18:06:32 +00:00
|
|
|
* If Configure::read('debug') is set, the log is shown all the time, else it is only shown on errors.
|
|
|
|
*
|
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - log - Whether or not the query should be logged to the memory log.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $sql
|
2008-09-12 02:09:38 +00:00
|
|
|
* @param array $options
|
2010-10-17 00:58:07 +00:00
|
|
|
* @param array $params values to be bided to the query
|
2008-05-31 12:36:38 +00:00
|
|
|
* @return mixed Resource or object representing the result set, or false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-10-17 00:58:07 +00:00
|
|
|
public function execute($sql, $options = array(), $params = array()) {
|
2011-02-25 04:02:37 +00:00
|
|
|
$options += array('log' => $this->fullDebug);
|
2010-10-17 15:58:11 +00:00
|
|
|
$this->error = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-04-15 16:58:28 +00:00
|
|
|
$t = microtime(true);
|
2010-10-15 21:35:30 +00:00
|
|
|
$this->_result = $this->_execute($sql, $params);
|
2008-09-12 02:09:38 +00:00
|
|
|
|
|
|
|
if ($options['log']) {
|
2010-04-15 16:58:28 +00:00
|
|
|
$this->took = round((microtime(true) - $t) * 1000, 0);
|
2010-11-16 06:32:08 +00:00
|
|
|
$this->numRows = $this->affected = $this->lastAffected();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->logQuery($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->error) {
|
|
|
|
$this->showQuery($sql);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-21 20:57:39 +00:00
|
|
|
return $this->_result;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-15 23:31:28 +00:00
|
|
|
/**
|
|
|
|
* Executes given SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @param array $params list of params to be bound to query
|
2010-10-17 00:58:07 +00:00
|
|
|
* @return PDOStatement if query executes with no problem, true as the result of a succesfull
|
|
|
|
* query returning no rows, suchs as a CREATE statement, false otherwise
|
2010-10-15 23:31:28 +00:00
|
|
|
*/
|
|
|
|
protected function _execute($sql, $params = array()) {
|
2010-10-21 04:27:00 +00:00
|
|
|
$sql = trim($sql);
|
2010-12-04 18:28:43 +00:00
|
|
|
if (preg_match('/^(?:CREATE|ALTER|DROP)/i', $sql)) {
|
2010-10-21 04:27:00 +00:00
|
|
|
$statements = array_filter(explode(';', $sql));
|
|
|
|
if (count($statements) > 1) {
|
|
|
|
$result = array_map(array($this, '_execute'), $statements);
|
|
|
|
return array_search(false, $result) === false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-29 04:20:18 +00:00
|
|
|
try {
|
|
|
|
$query = $this->_connection->prepare($sql);
|
|
|
|
$query->setFetchMode(PDO::FETCH_LAZY);
|
|
|
|
if (!$query->execute($params)) {
|
|
|
|
$this->_results = $query;
|
|
|
|
$this->error = $this->lastError($query);
|
|
|
|
$query->closeCursor();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!$query->columnCount()) {
|
|
|
|
$query->closeCursor();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->_results = null;
|
|
|
|
$this->error = $e->getMessage();
|
2010-12-02 04:49:43 +00:00
|
|
|
return false;
|
2010-10-17 00:58:07 +00:00
|
|
|
}
|
2010-10-15 23:31:28 +00:00
|
|
|
}
|
|
|
|
|
2010-10-22 01:18:26 +00:00
|
|
|
/**
|
|
|
|
* Returns a formatted error message from previous database operation.
|
|
|
|
*
|
|
|
|
* @param PDOStatement $query the query to extract the error from if any
|
|
|
|
* @return string Error message with error number
|
|
|
|
*/
|
|
|
|
function lastError(PDOStatement $query = null) {
|
|
|
|
$error = $query->errorInfo();
|
|
|
|
if (empty($error[2])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $error[1] . ': ' . $error[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of affected rows in previous database operation. If no previous operation exists,
|
|
|
|
* this returns false.
|
|
|
|
*
|
|
|
|
* @return integer Number of affected rows
|
|
|
|
*/
|
2011-02-25 04:02:37 +00:00
|
|
|
function lastAffected() {
|
2010-10-22 01:18:26 +00:00
|
|
|
if ($this->hasResult()) {
|
|
|
|
return $this->_result->rowCount();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
|
* this returns false.
|
|
|
|
*
|
|
|
|
* @return integer Number of rows in resultset
|
|
|
|
*/
|
2011-02-25 04:02:37 +00:00
|
|
|
function lastNumRows() {
|
2011-02-25 16:05:08 +00:00
|
|
|
return $this->lastAffected();
|
2010-10-22 01:18:26 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* DataSource Query abstraction
|
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* @return resource Result resource identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function query() {
|
2008-06-27 05:14:52 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$fields = null;
|
|
|
|
$order = null;
|
|
|
|
$limit = null;
|
|
|
|
$page = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$recursive = null;
|
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (count($args) === 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->fetchAll($args[0]);
|
2010-12-04 18:45:42 +00:00
|
|
|
} elseif (count($args) > 1 && (strpos($args[0], 'findBy') === 0 || strpos($args[0], 'findAllBy') === 0)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$params = $args[1];
|
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (substr($args[0], 0, 6) === 'findBy') {
|
2010-12-04 18:45:42 +00:00
|
|
|
$all = false;
|
2011-02-25 04:02:37 +00:00
|
|
|
$field = Inflector::underscore(substr($args[0], 6));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-12-04 18:45:42 +00:00
|
|
|
$all = true;
|
2011-02-25 04:02:37 +00:00
|
|
|
$field = Inflector::underscore(substr($args[0], 9));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$or = (strpos($field, '_or_') !== false);
|
|
|
|
if ($or) {
|
|
|
|
$field = explode('_or_', $field);
|
|
|
|
} else {
|
|
|
|
$field = explode('_and_', $field);
|
|
|
|
}
|
|
|
|
$off = count($field) - 1;
|
|
|
|
|
|
|
|
if (isset($params[1 + $off])) {
|
|
|
|
$fields = $params[1 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params[2 + $off])) {
|
|
|
|
$order = $params[2 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists(0, $params)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = 0;
|
2008-08-01 04:54:54 +00:00
|
|
|
$conditions = array();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($field as $f) {
|
2011-02-25 04:02:37 +00:00
|
|
|
$conditions[$args[2]->alias . '.' . $f] = $params[$c++];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($or) {
|
2008-08-01 04:54:54 +00:00
|
|
|
$conditions = array('OR' => $conditions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($all) {
|
|
|
|
if (isset($params[3 + $off])) {
|
|
|
|
$limit = $params[3 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params[4 + $off])) {
|
|
|
|
$page = $params[4 + $off];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params[5 + $off])) {
|
|
|
|
$recursive = $params[5 + $off];
|
|
|
|
}
|
2008-08-01 04:54:54 +00:00
|
|
|
return $args[2]->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if (isset($params[3 + $off])) {
|
|
|
|
$recursive = $params[3 + $off];
|
|
|
|
}
|
2008-08-01 04:54:54 +00:00
|
|
|
return $args[2]->find('first', compact('conditions', 'fields', 'order', 'recursive'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isset($args[1]) && $args[1] === true) {
|
|
|
|
return $this->fetchAll($args[0], true);
|
|
|
|
} else if (isset($args[1]) && !is_array($args[1]) ) {
|
|
|
|
return $this->fetchAll($args[0], false);
|
|
|
|
} else if (isset($args[1]) && is_array($args[1])) {
|
|
|
|
$offset = 0;
|
|
|
|
if (isset($args[2])) {
|
|
|
|
$cache = $args[2];
|
|
|
|
} else {
|
|
|
|
$cache = true;
|
|
|
|
}
|
2010-10-17 14:59:01 +00:00
|
|
|
return $this->fetchAll($args[0], $args[1], array('cache' => $cache));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a row from current resultset as an array
|
|
|
|
*
|
2010-12-23 03:33:17 +00:00
|
|
|
* @param string $sql Some SQL to be executed.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array The fetched row as an array
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function fetchRow($sql = null) {
|
2011-02-25 04:02:37 +00:00
|
|
|
if (is_string($sql) && strlen($sql) > 5 && !$this->execute($sql)) {
|
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-06-18 17:28:17 +00:00
|
|
|
if ($this->hasResult()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->resultSet($this->_result);
|
|
|
|
$resultRow = $this->fetchResult();
|
2011-02-25 22:55:34 +00:00
|
|
|
if (isset($resultRow[0])) {
|
2009-11-12 05:05:08 +00:00
|
|
|
$this->fetchVirtualField($resultRow);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return $resultRow;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of all result rows for a given SQL query.
|
|
|
|
* Returns false if no rows matched.
|
|
|
|
*
|
2010-10-17 14:59:01 +00:00
|
|
|
*
|
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - cache - Returns the cached version of the query, if exists and stores the result in cache
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $sql SQL statement
|
2010-10-17 14:59:01 +00:00
|
|
|
* @param array $params parameters to be bound as values for the SQL statement
|
|
|
|
* @param array $options additional options for the query.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Array of resultset rows, or false if no rows matched
|
|
|
|
*/
|
2010-10-17 14:59:01 +00:00
|
|
|
public function fetchAll($sql, $params = array(), $options = array()) {
|
|
|
|
if (is_string($options)) {
|
|
|
|
$options = array('modelName' => $options);
|
|
|
|
}
|
|
|
|
if (is_bool($params)) {
|
|
|
|
$options['cache'] = $params;
|
|
|
|
$params = array();
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
$options += array('cache' => true);
|
2010-10-17 14:59:01 +00:00
|
|
|
$cache = $options['cache'];
|
2010-10-17 15:21:54 +00:00
|
|
|
if ($cache && ($cached = $this->getQueryCache($sql, $params)) !== false) {
|
2010-06-05 03:37:29 +00:00
|
|
|
return $cached;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-03 19:11:39 +00:00
|
|
|
if ($result = $this->execute($sql, array(), $params)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = array();
|
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if ($this->hasResult()) {
|
|
|
|
$first = $this->fetchRow();
|
2011-02-25 23:18:12 +00:00
|
|
|
if ($first != null) {
|
2011-02-25 04:02:37 +00:00
|
|
|
$out[] = $first;
|
|
|
|
}
|
|
|
|
while ($item = $this->fetchResult()) {
|
2011-02-25 22:55:34 +00:00
|
|
|
if (isset($item[0])) {
|
|
|
|
$this->fetchVirtualField($item);
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
$out[] = $item;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 19:11:39 +00:00
|
|
|
if (!is_bool($result) && $cache) {
|
2010-10-17 15:21:54 +00:00
|
|
|
$this->_writeQueryCache($sql, $out, $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-18 04:08:55 +00:00
|
|
|
|
2009-10-30 00:31:29 +00:00
|
|
|
if (empty($out) && is_bool($this->_result)) {
|
|
|
|
return $this->_result;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return $out;
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the next row from the current result set
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function fetchResult() {
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-12 05:05:08 +00:00
|
|
|
/**
|
|
|
|
* Modifies $result array to place virtual fields in model entry where they belongs to
|
|
|
|
*
|
2010-12-23 03:33:17 +00:00
|
|
|
* @param array $resut Reference to the fetched row
|
2009-11-12 05:05:08 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-11 05:25:18 +00:00
|
|
|
public function fetchVirtualField(&$result) {
|
2009-11-10 05:38:07 +00:00
|
|
|
if (isset($result[0]) && is_array($result[0])) {
|
|
|
|
foreach ($result[0] as $field => $value) {
|
2010-05-22 04:58:54 +00:00
|
|
|
if (strpos($field, $this->virtualFieldSeparator) === false) {
|
2009-11-10 05:38:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-22 04:58:54 +00:00
|
|
|
list($alias, $virtual) = explode($this->virtualFieldSeparator, $field);
|
2009-11-10 05:38:07 +00:00
|
|
|
|
|
|
|
if (!ClassRegistry::isKeySet($alias)) {
|
2009-12-17 05:15:42 +00:00
|
|
|
return;
|
2009-11-10 05:38:07 +00:00
|
|
|
}
|
|
|
|
$model = ClassRegistry::getObject($alias);
|
2009-12-07 02:46:00 +00:00
|
|
|
if ($model->isVirtualField($virtual)) {
|
2009-11-10 05:38:07 +00:00
|
|
|
$result[$alias][$virtual] = $value;
|
|
|
|
unset($result[0][$field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($result[0])) {
|
|
|
|
unset($result[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a single field of the first of query results for a given SQL query, or false if empty.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the field
|
|
|
|
* @param string $sql SQL query
|
2009-12-19 17:40:32 +00:00
|
|
|
* @return mixed Value of field read.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function field($name, $sql) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = $this->fetchRow($sql);
|
2011-02-25 04:02:37 +00:00
|
|
|
if (empty($data[$name])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
return $data[$name];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-03-14 03:55:18 +00:00
|
|
|
/**
|
|
|
|
* Empties the method caches.
|
|
|
|
* These caches are used by DboSource::name() and DboSource::conditions()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-11 05:25:18 +00:00
|
|
|
public function flushMethodCache() {
|
2010-03-14 03:55:18 +00:00
|
|
|
$this->methodCache = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache a value into the methodCaches. Will respect the value of DboSource::$cacheMethods.
|
|
|
|
* Will retrieve a value from the cache if $value is null.
|
|
|
|
*
|
|
|
|
* If caching is disabled and a write is attempted, the $value will be returned.
|
|
|
|
* A read will either return the value or null.
|
|
|
|
*
|
|
|
|
* @param string $method Name of the method being cached.
|
|
|
|
* @param string $key The keyname for the cache operation.
|
|
|
|
* @param mixed $value The value to cache into memory.
|
|
|
|
* @return mixed Either null on failure, or the value if its set.
|
|
|
|
*/
|
2010-04-11 05:25:18 +00:00
|
|
|
public function cacheMethod($method, $key, $value = null) {
|
2010-03-14 03:55:18 +00:00
|
|
|
if ($this->cacheMethods === false) {
|
2010-03-15 04:41:44 +00:00
|
|
|
return $value;
|
2010-03-14 03:55:18 +00:00
|
|
|
}
|
|
|
|
if ($value === null) {
|
|
|
|
return (isset($this->methodCache[$method][$key])) ? $this->methodCache[$method][$key] : null;
|
|
|
|
}
|
|
|
|
return $this->methodCache[$method][$key] = $value;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a quoted name of $data for use in an SQL statement.
|
|
|
|
* Strips fields out of SQL functions before quoting.
|
|
|
|
*
|
2010-06-30 12:52:39 +00:00
|
|
|
* Results of this method are stored in a memory cache. This improves performance, but
|
|
|
|
* because the method uses a simple hashing algorithm it can infrequently have collisions.
|
|
|
|
* Setting DboSource::$cacheMethods to false will disable the memory cache.
|
|
|
|
*
|
2010-09-05 18:22:45 +00:00
|
|
|
* @param mixed $data Either a string with a column to quote. An array of columns to quote or an
|
2010-06-30 12:52:39 +00:00
|
|
|
* object from DboSource::expression() or DboSource::identifier()
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string SQL field
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function name($data) {
|
2008-10-01 15:27:29 +00:00
|
|
|
if (is_object($data) && isset($data->type)) {
|
|
|
|
return $data->value;
|
|
|
|
}
|
2010-01-17 04:14:19 +00:00
|
|
|
if ($data === '*') {
|
2008-05-30 11:40:08 +00:00
|
|
|
return '*';
|
|
|
|
}
|
2010-01-17 04:14:19 +00:00
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as $i => $dataItem) {
|
|
|
|
$data[$i] = $this->name($dataItem);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-17 04:14:19 +00:00
|
|
|
return $data;
|
|
|
|
}
|
2010-02-23 14:39:11 +00:00
|
|
|
$cacheKey = crc32($this->startQuote.$data.$this->endQuote);
|
2010-03-14 03:55:18 +00:00
|
|
|
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
|
|
|
|
return $return;
|
2010-01-29 15:27:40 +00:00
|
|
|
}
|
2010-01-17 04:14:19 +00:00
|
|
|
$data = trim($data);
|
2010-11-25 11:52:23 +00:00
|
|
|
if (preg_match('/^[\w-]+(?:\.[^ \*]*)*$/', $data)) { // string, string.string
|
2010-01-17 04:14:19 +00:00
|
|
|
if (strpos($data, '.') === false) { // string
|
2010-03-14 03:55:18 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . $data . $this->endQuote);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-17 04:14:19 +00:00
|
|
|
$items = explode('.', $data);
|
2010-03-17 21:32:58 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey,
|
2010-03-14 03:55:18 +00:00
|
|
|
$this->startQuote . implode($this->endQuote . '.' . $this->startQuote, $items) . $this->endQuote
|
|
|
|
);
|
2010-01-17 04:14:19 +00:00
|
|
|
}
|
2010-02-20 16:42:17 +00:00
|
|
|
if (preg_match('/^[\w-]+\.\*$/', $data)) { // string.*
|
2010-03-17 21:32:58 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey,
|
2010-03-14 03:55:18 +00:00
|
|
|
$this->startQuote . str_replace('.*', $this->endQuote . '.*', $data)
|
|
|
|
);
|
2010-01-17 04:14:19 +00:00
|
|
|
}
|
2010-02-20 16:42:17 +00:00
|
|
|
if (preg_match('/^([\w-]+)\((.*)\)$/', $data, $matches)) { // Functions
|
2010-03-14 03:55:18 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey,
|
|
|
|
$matches[1] . '(' . $this->name($matches[2]) . ')'
|
|
|
|
);
|
2010-01-17 04:14:19 +00:00
|
|
|
}
|
2010-03-14 03:55:18 +00:00
|
|
|
if (
|
2011-01-27 01:58:16 +00:00
|
|
|
preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+' . preg_quote($this->alias) . '\s*([\w-]+)$/i', $data, $matches
|
2010-03-14 03:55:18 +00:00
|
|
|
)) {
|
|
|
|
return $this->cacheMethod(
|
|
|
|
__FUNCTION__, $cacheKey,
|
|
|
|
preg_replace(
|
|
|
|
'/\s{2,}/', ' ', $this->name($matches[1]) . ' ' . $this->alias . ' ' . $this->name($matches[3])
|
|
|
|
)
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-09-22 00:34:27 +00:00
|
|
|
if (preg_match('/^[\w-_\s]*[\w-_]+/', $data)) {
|
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . $data . $this->endQuote);
|
|
|
|
}
|
2010-03-14 03:55:18 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-19 18:06:32 +00:00
|
|
|
* Checks if the source is connected to the database.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return boolean True if the database is connected, else false
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isConnected() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->connected;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-18 17:28:17 +00:00
|
|
|
/**
|
|
|
|
* Checks if the result is valid
|
|
|
|
*
|
|
|
|
* @return boolean True if the result is valid else false
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function hasResult() {
|
2010-10-15 21:35:30 +00:00
|
|
|
return is_a($this->_result, 'PDOStatement');
|
2008-06-18 17:28:17 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-09-02 03:23:47 +00:00
|
|
|
* Get the query log as an array.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-09-02 03:23:47 +00:00
|
|
|
* @param boolean $sorted Get the queries sorted by time taken, defaults to false.
|
2010-12-23 03:33:17 +00:00
|
|
|
* @param boolean $clear If True the existing log will cleared.
|
2009-09-02 03:23:47 +00:00
|
|
|
* @return array Array of queries run as an array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getLog($sorted = false, $clear = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($sorted) {
|
|
|
|
$log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC);
|
|
|
|
} else {
|
|
|
|
$log = $this->_queriesLog;
|
|
|
|
}
|
2009-12-04 04:29:28 +00:00
|
|
|
if ($clear) {
|
|
|
|
$this->_queriesLog = array();
|
|
|
|
}
|
2009-12-04 03:41:31 +00:00
|
|
|
return array('log' => $log, 'count' => $this->_queriesCnt, 'time' => $this->_queriesTime);
|
2009-09-02 03:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-19 18:06:32 +00:00
|
|
|
* Outputs the contents of the queries log. If in a non-CLI environment the sql_log element
|
|
|
|
* will be rendered and output. If in a CLI environment, a plain text log is generated.
|
2009-09-02 03:23:47 +00:00
|
|
|
*
|
2009-12-19 17:40:32 +00:00
|
|
|
* @param boolean $sorted Get the queries sorted by time taken, defaults to false.
|
|
|
|
* @return void
|
2009-09-02 03:23:47 +00:00
|
|
|
*/
|
2010-04-11 05:25:18 +00:00
|
|
|
public function showLog($sorted = false) {
|
2009-12-04 04:29:28 +00:00
|
|
|
$log = $this->getLog($sorted, false);
|
|
|
|
if (empty($log['log'])) {
|
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-12 05:11:34 +00:00
|
|
|
if (PHP_SAPI != 'cli') {
|
2009-12-04 04:29:28 +00:00
|
|
|
$controller = null;
|
2010-11-13 04:05:44 +00:00
|
|
|
$View = new View($controller, false);
|
2009-12-04 04:29:28 +00:00
|
|
|
$View->set('logs', array($this->configKeyName => $log));
|
2010-08-27 01:24:09 +00:00
|
|
|
echo $View->element('sql_dump', array('_forced_from_dbo_' => true));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-12-04 04:29:28 +00:00
|
|
|
foreach ($log['log'] as $k => $i) {
|
2008-05-30 11:40:08 +00:00
|
|
|
print (($k + 1) . ". {$i['query']} {$i['error']}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Log given SQL query.
|
|
|
|
*
|
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @todo: Add hook to log errors instead of returning false
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function logQuery($sql) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_queriesCnt++;
|
|
|
|
$this->_queriesTime += $this->took;
|
|
|
|
$this->_queriesLog[] = array(
|
2011-02-25 04:02:37 +00:00
|
|
|
'query' => $sql,
|
2008-05-30 11:40:08 +00:00
|
|
|
'error' => $this->error,
|
|
|
|
'affected' => $this->affected,
|
|
|
|
'numRows' => $this->numRows,
|
|
|
|
'took' => $this->took
|
|
|
|
);
|
|
|
|
if (count($this->_queriesLog) > $this->_queriesLogMax) {
|
|
|
|
array_pop($this->_queriesLog);
|
|
|
|
}
|
|
|
|
if ($this->error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Output information about an SQL query. The SQL statement, number of rows in resultset,
|
|
|
|
* and execution time in microseconds. If the query fails, an error is output instead.
|
|
|
|
*
|
|
|
|
* @param string $sql Query to show information on.
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function showQuery($sql) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$error = $this->error;
|
2010-07-06 01:50:36 +00:00
|
|
|
if (strlen($sql) > 200 && !$this->fullDebug && Configure::read('debug') > 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$sql = substr($sql, 0, 200) . '[...]';
|
|
|
|
}
|
2010-07-06 01:50:36 +00:00
|
|
|
if (Configure::read('debug') > 0) {
|
2008-06-14 19:45:26 +00:00
|
|
|
$out = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($error) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error('<span style="color:Red;text-align:left"><b>' . __d('cake_dev', 'SQL Error:') . "</b> {$this->error}</span>", E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-03-20 15:35:43 +00:00
|
|
|
$out = ('<small>[' . __d('cake_dev', 'Aff:%s Num:%s Took:%sms', $this->affected, $this->numRows, $this->took) . ']</small>');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-03-20 15:35:43 +00:00
|
|
|
pr(sprintf('<p style="text-align:left"><b>' . __d('cake_dev', 'Query:') . '</b> %s %s</p>', $sql, $out));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets full table name including prefix
|
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* @param mixed $model Either a Model object or a string table name.
|
|
|
|
* @param boolean $quote Whether you want the table name quoted.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Full quoted table name
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function fullTableName($model, $quote = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_object($model)) {
|
|
|
|
$table = $model->tablePrefix . $model->table;
|
|
|
|
} elseif (isset($this->config['prefix'])) {
|
|
|
|
$table = $this->config['prefix'] . strval($model);
|
|
|
|
} else {
|
|
|
|
$table = strval($model);
|
|
|
|
}
|
|
|
|
if ($quote) {
|
|
|
|
return $this->name($table);
|
|
|
|
}
|
|
|
|
return $table;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The "C" in CRUD
|
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* Creates new records in the database.
|
|
|
|
*
|
|
|
|
* @param Model $model Model object that the record is for.
|
2010-01-16 12:32:04 +00:00
|
|
|
* @param array $fields An array of field names to insert. If null, $model->data will be
|
2009-12-19 18:06:32 +00:00
|
|
|
* used to generate field names.
|
|
|
|
* @param array $values An array of values with keys matching the fields. If null, $model->data will
|
|
|
|
* be used to generate values.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function create($model, $fields = null, $values = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$id = null;
|
|
|
|
|
|
|
|
if ($fields == null) {
|
|
|
|
unset($fields, $values);
|
|
|
|
$fields = array_keys($model->data);
|
|
|
|
$values = array_values($model->data);
|
|
|
|
}
|
|
|
|
$count = count($fields);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2008-10-30 05:03:01 +00:00
|
|
|
$valueInsert[] = $this->value($values[$i], $model->getColumnType($fields[$i]), false);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
$fieldInsert[] = $this->name($fields[$i]);
|
|
|
|
if ($fields[$i] == $model->primaryKey) {
|
|
|
|
$id = $values[$i];
|
|
|
|
}
|
|
|
|
}
|
2008-10-30 05:03:01 +00:00
|
|
|
$query = array(
|
|
|
|
'table' => $this->fullTableName($model),
|
2009-11-19 22:13:35 +00:00
|
|
|
'fields' => implode(', ', $fieldInsert),
|
|
|
|
'values' => implode(', ', $valueInsert)
|
2008-10-30 05:03:01 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-30 05:03:01 +00:00
|
|
|
if ($this->execute($this->renderStatement('create', $query))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($id)) {
|
|
|
|
$id = $this->lastInsertId($this->fullTableName($model, false), $model->primaryKey);
|
|
|
|
}
|
|
|
|
$model->setInsertID($id);
|
|
|
|
$model->id = $id;
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
$model->onError();
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The "R" in CRUD
|
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* Reads record(s) from the database.
|
|
|
|
*
|
|
|
|
* @param Model $model A Model object that the query is for.
|
|
|
|
* @param array $queryData An array of queryData information containing keys similar to Model::find()
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param integer $recursive Number of levels of association
|
2009-12-19 18:06:32 +00:00
|
|
|
* @return mixed boolean false on error/failure. An array of results on success.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function read($model, $queryData = array(), $recursive = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$queryData = $this->__scrubQueryData($queryData);
|
2008-09-01 19:31:28 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$null = null;
|
|
|
|
$array = array();
|
|
|
|
$linkedModels = array();
|
|
|
|
$this->__bypass = false;
|
|
|
|
|
|
|
|
if ($recursive === null && isset($queryData['recursive'])) {
|
|
|
|
$recursive = $queryData['recursive'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($recursive)) {
|
|
|
|
$_recursive = $model->recursive;
|
|
|
|
$model->recursive = $recursive;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($queryData['fields'])) {
|
|
|
|
$this->__bypass = true;
|
|
|
|
$queryData['fields'] = $this->fields($model, null, $queryData['fields']);
|
|
|
|
} else {
|
|
|
|
$queryData['fields'] = $this->fields($model);
|
|
|
|
}
|
|
|
|
|
2010-04-04 08:17:43 +00:00
|
|
|
$_associations = $model->associations();
|
2009-07-02 23:14:38 +00:00
|
|
|
|
|
|
|
if ($model->recursive == -1) {
|
|
|
|
$_associations = array();
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif ($model->recursive == 0) {
|
2009-07-02 23:14:38 +00:00
|
|
|
unset($_associations[2], $_associations[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($_associations as $type) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($model->{$type} as $assoc => $assocData) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$linkModel = $model->{$assoc};
|
2009-07-02 23:14:38 +00:00
|
|
|
$external = isset($assocData['external']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-02-23 05:13:56 +00:00
|
|
|
$linkModel->getDataSource();
|
2011-02-26 07:17:42 +00:00
|
|
|
if ($model->useDbConfig === $linkModel->useDbConfig) {
|
2009-07-02 23:14:38 +00:00
|
|
|
if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
|
|
|
|
$linkedModels[$type . '/' . $assoc] = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 07:17:42 +00:00
|
|
|
$query = trim($this->generateAssociationQuery($model, null, null, null, null, $queryData, false, $null));
|
2008-09-01 19:31:28 +00:00
|
|
|
|
2010-10-17 15:02:02 +00:00
|
|
|
$resultSet = $this->fetchAll($query, $model->cacheQueries);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($resultSet === false) {
|
|
|
|
$model->onError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-25 16:26:14 +00:00
|
|
|
$filtered = $this->_filterResults($resultSet, $model);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-07-02 23:14:38 +00:00
|
|
|
if ($model->recursive > -1) {
|
|
|
|
foreach ($_associations as $type) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($model->{$type} as $assoc => $assocData) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$linkModel = $model->{$assoc};
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-02-26 07:31:13 +00:00
|
|
|
if (!isset($linkedModels[$type . '/' . $assoc])) {
|
|
|
|
if ($model->useDbConfig === $linkModel->useDbConfig) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$db = $this;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-10-17 23:50:21 +00:00
|
|
|
$db = ConnectionManager::getDataSource($linkModel->useDbConfig);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-26 07:31:13 +00:00
|
|
|
} elseif ($model->recursive > 1 && ($type === 'belongsTo' || $type === 'hasOne')) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$db = $this;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-11 17:06:33 +00:00
|
|
|
if (isset($db) && method_exists($db, 'queryAssociation')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$stack = array($assoc);
|
|
|
|
$db->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultSet, $model->recursive - 1, $stack);
|
|
|
|
unset($db);
|
2010-09-05 18:22:45 +00:00
|
|
|
|
|
|
|
if ($type === 'hasMany') {
|
2011-02-26 07:31:13 +00:00
|
|
|
$filtered[] = $assoc;
|
2010-09-05 18:22:45 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-25 16:26:14 +00:00
|
|
|
$this->_filterResults($resultSet, $model, $filtered);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($recursive)) {
|
|
|
|
$model->recursive = $_recursive;
|
|
|
|
}
|
|
|
|
return $resultSet;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-19 18:06:32 +00:00
|
|
|
* Passes association results thru afterFind filters of corresponding model
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $results Reference of resultset to be filtered
|
|
|
|
* @param object $model Instance of model to operate against
|
|
|
|
* @param array $filtered List of classes already filtered, to be skipped
|
2009-12-19 18:06:32 +00:00
|
|
|
* @return array Array of results that have been filtered through $model->afterFind
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-02-25 22:06:45 +00:00
|
|
|
protected function _filterResults(&$results, Model $model, $filtered = array()) {
|
2011-02-26 08:23:59 +00:00
|
|
|
$current = current($results);
|
|
|
|
if (!is_array($current)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$keys = array_diff(array_keys($current), $filtered, array($model->alias));
|
2008-05-30 11:40:08 +00:00
|
|
|
$filtering = array();
|
2011-02-26 08:23:59 +00:00
|
|
|
foreach ($keys as $className) {
|
|
|
|
if (!isset($model->{$className}) || !is_object($model->{$className})) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$linkedModel = $model->{$className};
|
|
|
|
$filtering[] = $className;
|
|
|
|
foreach ($results as &$result) {
|
|
|
|
$data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
|
|
|
|
if (isset($data[0][$className])) {
|
|
|
|
$result[$className] = $data[0][$className];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $filtering;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-19 18:06:32 +00:00
|
|
|
* Queries associations. Used to fetch results on recursive models.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* @param Model $model Primary Model object
|
2010-01-16 12:32:04 +00:00
|
|
|
* @param Model $linkModel Linked model that
|
2009-12-19 18:06:32 +00:00
|
|
|
* @param string $type Association type, one of the model association types ie. hasMany
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param unknown_type $association
|
|
|
|
* @param unknown_type $assocData
|
2009-12-19 18:06:32 +00:00
|
|
|
* @param array $queryData
|
|
|
|
* @param boolean $external Whether or not the association query is on an external datasource.
|
|
|
|
* @param array $resultSet Existing results
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param integer $recursive Number of levels of association
|
|
|
|
* @param array $stack
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function queryAssociation($model, &$linkModel, $type, $association, $assocData, &$queryData, $external = false, &$resultSet, $recursive, $stack) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) {
|
2011-02-25 04:02:37 +00:00
|
|
|
if (!is_array($resultSet)) {
|
2010-07-06 01:50:36 +00:00
|
|
|
if (Configure::read('debug') > 0) {
|
2011-03-20 15:35:43 +00:00
|
|
|
echo '<div style = "font: Verdana bold 12px; color: #FF0000">' . __d('cake_dev', 'SQL Error in model %s:', $model->alias) . ' ';
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($this->error) && $this->error != null) {
|
2009-07-23 13:35:14 +00:00
|
|
|
echo $this->error;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-23 13:35:14 +00:00
|
|
|
echo '</div>';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) {
|
|
|
|
$ins = $fetch = array();
|
2011-02-26 17:03:17 +00:00
|
|
|
foreach ($resultSet as &$result) {
|
|
|
|
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$ins[] = $in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($ins)) {
|
2010-04-19 01:22:09 +00:00
|
|
|
$ins = array_unique($ins);
|
2008-05-30 11:40:08 +00:00
|
|
|
$fetch = $this->fetchAssociated($model, $query, $ins);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($fetch) && is_array($fetch)) {
|
|
|
|
if ($recursive > 0) {
|
2010-04-04 08:17:43 +00:00
|
|
|
foreach ($linkModel->associations() as $type1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$deepModel = $linkModel->{$assoc1};
|
2008-05-30 11:40:08 +00:00
|
|
|
$tmpStack = $stack;
|
|
|
|
$tmpStack[] = $assoc1;
|
|
|
|
|
|
|
|
if ($linkModel->useDbConfig === $deepModel->useDbConfig) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$db = $this;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-10-17 23:50:21 +00:00
|
|
|
$db = ConnectionManager::getDataSource($deepModel->useDbConfig);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-25 16:26:14 +00:00
|
|
|
$this->_filterResults($fetch, $model);
|
2011-02-26 19:19:50 +00:00
|
|
|
return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($type === 'hasAndBelongsToMany') {
|
|
|
|
$ins = $fetch = array();
|
2011-02-26 17:03:17 +00:00
|
|
|
foreach ($resultSet as &$result) {
|
|
|
|
if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$ins[] = $in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($ins)) {
|
2010-04-19 01:22:09 +00:00
|
|
|
$ins = array_unique($ins);
|
2008-06-27 05:14:52 +00:00
|
|
|
if (count($ins) > 1) {
|
2009-11-19 22:13:35 +00:00
|
|
|
$query = str_replace('{$__cakeID__$}', '(' .implode(', ', $ins) .')', $query);
|
2008-06-27 05:14:52 +00:00
|
|
|
$query = str_replace('= (', 'IN (', $query);
|
|
|
|
} else {
|
2011-02-25 04:02:37 +00:00
|
|
|
$query = str_replace('{$__cakeID__$}', $ins[0], $query);
|
2008-06-27 05:14:52 +00:00
|
|
|
}
|
|
|
|
|
2009-11-15 22:48:41 +00:00
|
|
|
$query = str_replace(' WHERE 1 = 1', '', $query);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$foreignKey = $model->hasAndBelongsToMany[$association]['foreignKey'];
|
|
|
|
$joinKeys = array($foreignKey, $model->hasAndBelongsToMany[$association]['associationForeignKey']);
|
|
|
|
list($with, $habtmFields) = $model->joinModel($model->hasAndBelongsToMany[$association]['with'], $joinKeys);
|
|
|
|
$habtmFieldsCount = count($habtmFields);
|
|
|
|
$q = $this->insertQueryData($query, null, $association, $assocData, $model, $linkModel, $stack);
|
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if ($q !== false) {
|
2010-10-17 15:02:02 +00:00
|
|
|
$fetch = $this->fetchAll($q, $model->cacheQueries);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$fetch = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 19:19:50 +00:00
|
|
|
$modelAlias = $model->alias;
|
|
|
|
$modelPK = $model->primaryKey;
|
2011-02-26 17:03:17 +00:00
|
|
|
foreach ($resultSet as &$row) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($type !== 'hasAndBelongsToMany') {
|
2011-02-26 17:03:17 +00:00
|
|
|
$q = $this->insertQueryData($query, $row, $association, $assocData, $model, $linkModel, $stack);
|
2011-02-25 04:02:37 +00:00
|
|
|
if ($q !== false) {
|
2010-10-17 15:02:02 +00:00
|
|
|
$fetch = $this->fetchAll($q, $model->cacheQueries);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$fetch = null;
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 17:03:17 +00:00
|
|
|
$selfJoin = $linkModel->name === $model->name;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($fetch) && is_array($fetch)) {
|
|
|
|
if ($recursive > 0) {
|
2010-04-04 08:17:43 +00:00
|
|
|
foreach ($linkModel->associations() as $type1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$deepModel = $linkModel->{$assoc1};
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-02-26 19:19:50 +00:00
|
|
|
if ($type1 === 'belongsTo' || ($deepModel->alias === $modelAlias && $type === 'belongsTo') || ($deepModel->alias !== $modelAlias)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$tmpStack = $stack;
|
|
|
|
$tmpStack[] = $assoc1;
|
|
|
|
if ($linkModel->useDbConfig == $deepModel->useDbConfig) {
|
2010-10-17 23:50:21 +00:00
|
|
|
$db = $this;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-10-17 23:50:21 +00:00
|
|
|
$db = ConnectionManager::getDataSource($deepModel->useDbConfig);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 17:03:17 +00:00
|
|
|
if ($type === 'hasAndBelongsToMany') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$uniqueIds = $merge = array();
|
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($fetch as $j => $data) {
|
2011-02-26 19:19:50 +00:00
|
|
|
if (isset($data[$with]) && $data[$with][$foreignKey] === $row[$modelAlias][$modelPK]) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($habtmFieldsCount <= 2) {
|
|
|
|
unset($data[$with]);
|
|
|
|
}
|
|
|
|
$merge[] = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($merge) && !isset($row[$association])) {
|
|
|
|
$row[$association] = $merge;
|
|
|
|
} else {
|
2011-02-26 17:03:17 +00:00
|
|
|
$this->__mergeAssociation($row, $merge, $association, $type);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-02-26 17:03:17 +00:00
|
|
|
$this->__mergeAssociation($row, $fetch, $association, $type, $selfJoin);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-26 17:03:17 +00:00
|
|
|
if (isset($row[$association])) {
|
|
|
|
$row[$association] = $linkModel->afterFind($row[$association], false);
|
2008-08-02 21:56:55 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$tempArray[0][$association] = false;
|
2011-02-26 17:03:17 +00:00
|
|
|
$this->__mergeAssociation($row, $tempArray, $association, $type, $selfJoin);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-27 05:14:52 +00:00
|
|
|
* A more efficient way to fetch associations. Woohoo!
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* @param model $model Primary model object
|
|
|
|
* @param string $query Association query
|
|
|
|
* @param array $ids Array of IDs of associated records
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Association results
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function fetchAssociated($model, $query, $ids) {
|
2009-11-19 22:13:35 +00:00
|
|
|
$query = str_replace('{$__cakeID__$}', implode(', ', $ids), $query);
|
2008-06-27 05:34:37 +00:00
|
|
|
if (count($ids) > 1) {
|
|
|
|
$query = str_replace('= (', 'IN (', $query);
|
|
|
|
}
|
2010-10-17 15:02:02 +00:00
|
|
|
return $this->fetchAll($query, $model->cacheQueries);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-20 17:25:39 +00:00
|
|
|
/**
|
|
|
|
* mergeHasMany - Merge the results of hasMany relations.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param array $resultSet Data to merge into
|
|
|
|
* @param array $merge Data to merge
|
|
|
|
* @param string $association Name of Model being Merged
|
|
|
|
* @param object $model Model being merged onto
|
|
|
|
* @param object $linkModel Model being merged
|
2008-08-01 01:04:03 +00:00
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-02-26 19:19:50 +00:00
|
|
|
function __mergeHasMany(&$resultSet, $merge, $association, $model, $linkModel) {
|
|
|
|
$modelAlias = $model->alias;
|
|
|
|
$modelPK = $model->primaryKey;
|
|
|
|
$modelFK = $model->hasMany[$association]['foreignKey'];
|
|
|
|
foreach ($resultSet as &$result) {
|
|
|
|
if (!isset($result[$modelAlias])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$merged = array();
|
|
|
|
foreach ($merge as $data) {
|
|
|
|
if ($result[$modelAlias][$modelPK] === $data[$association][$modelFK]) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (count($data) > 1) {
|
|
|
|
$data = array_merge($data[$association], $data);
|
|
|
|
unset($data[$association]);
|
|
|
|
foreach ($data as $key => $name) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$data[$association][] = $name;
|
|
|
|
unset($data[$key]);
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 19:19:50 +00:00
|
|
|
$merged[] = $data;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-02-26 19:19:50 +00:00
|
|
|
$merged[] = $data[$association];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 19:19:50 +00:00
|
|
|
$result = Set::pushDiff($result, array($association => $merged));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $data
|
|
|
|
* @param unknown_type $merge
|
|
|
|
* @param unknown_type $association
|
|
|
|
* @param unknown_type $type
|
|
|
|
* @param boolean $selfJoin
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access private
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-02-26 19:19:50 +00:00
|
|
|
function __mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($merge[0]) && !isset($merge[0][$association])) {
|
|
|
|
$association = Inflector::pluralize($association);
|
|
|
|
}
|
|
|
|
|
2011-02-26 19:19:50 +00:00
|
|
|
if ($type === 'belongsTo' || $type === 'hasOne') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($merge[$association])) {
|
|
|
|
$data[$association] = $merge[$association][0];
|
|
|
|
} else {
|
|
|
|
if (count($merge[0][$association]) > 1) {
|
|
|
|
foreach ($merge[0] as $assoc => $data2) {
|
2011-02-26 19:19:50 +00:00
|
|
|
if ($assoc !== $association) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$merge[0][$association][$assoc] = $data2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isset($data[$association])) {
|
|
|
|
if ($merge[0][$association] != null) {
|
|
|
|
$data[$association] = $merge[0][$association];
|
|
|
|
} else {
|
|
|
|
$data[$association] = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (is_array($merge[0][$association])) {
|
|
|
|
foreach ($data[$association] as $k => $v) {
|
|
|
|
if (!is_array($v)) {
|
|
|
|
$dataAssocTmp[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($merge[0][$association] as $k => $v) {
|
|
|
|
if (!is_array($v)) {
|
|
|
|
$mergeAssocTmp[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$dataKeys = array_keys($data);
|
|
|
|
$mergeKeys = array_keys($merge[0]);
|
|
|
|
|
|
|
|
if ($mergeKeys[0] === $dataKeys[0] || $mergeKeys === $dataKeys) {
|
|
|
|
$data[$association][$association] = $merge[0][$association];
|
|
|
|
} else {
|
|
|
|
$diff = Set::diff($dataAssocTmp, $mergeAssocTmp);
|
|
|
|
$data[$association] = array_merge($merge[0][$association], $diff);
|
|
|
|
}
|
|
|
|
} elseif ($selfJoin && array_key_exists($association, $merge[0])) {
|
|
|
|
$data[$association] = array_merge($data[$association], array($association => array()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-08-02 21:56:55 +00:00
|
|
|
if (isset($merge[0][$association]) && $merge[0][$association] === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($data[$association])) {
|
|
|
|
$data[$association] = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($merge as $i => $row) {
|
2011-02-25 04:02:37 +00:00
|
|
|
if (count($row) === 1) {
|
2008-06-22 17:34:05 +00:00
|
|
|
if (empty($data[$association]) || (isset($data[$association]) && !in_array($row[$association], $data[$association]))) {
|
|
|
|
$data[$association][] = $row[$association];
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif (!empty($row)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$tmp = array_merge($row[$association], $row);
|
|
|
|
unset($tmp[$association]);
|
|
|
|
$data[$association][] = $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates an array representing a query or part of a query from a single model or two associated models
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param Model $linkModel
|
|
|
|
* @param string $type
|
|
|
|
* @param string $association
|
|
|
|
* @param array $assocData
|
|
|
|
* @param array $queryData
|
|
|
|
* @param boolean $external
|
|
|
|
* @param array $resultSet
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2011-02-26 07:17:42 +00:00
|
|
|
public function generateAssociationQuery($model, $linkModel, $type, $association = null, $assocData = array(), &$queryData, $external = false, &$resultSet) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$queryData = $this->__scrubQueryData($queryData);
|
|
|
|
$assocData = $this->__scrubQueryData($assocData);
|
2011-02-26 07:17:42 +00:00
|
|
|
$modelAlias = $model->alias;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($queryData['fields'])) {
|
2011-02-26 07:17:42 +00:00
|
|
|
$queryData['fields'] = $this->fields($model, $modelAlias);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (!empty($model->hasMany) && $model->recursive > -1) {
|
2011-02-26 07:17:42 +00:00
|
|
|
$assocFields = $this->fields($model, $modelAlias, array("{$modelAlias}.{$model->primaryKey}"));
|
|
|
|
$passedFields = $this->fields($model, $modelAlias, $queryData['fields']);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (count($passedFields) === 1) {
|
2011-02-26 07:17:42 +00:00
|
|
|
if (strpos($passedFields[0], $assocFields[0]) === false && !preg_match('/^[a-z]+\(/i', $passedFields[0])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$queryData['fields'] = array_merge($passedFields, $assocFields);
|
|
|
|
} else {
|
|
|
|
$queryData['fields'] = $passedFields;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$queryData['fields'] = array_merge($passedFields, $assocFields);
|
|
|
|
}
|
|
|
|
unset($assocFields, $passedFields);
|
|
|
|
}
|
|
|
|
|
2011-02-26 07:17:42 +00:00
|
|
|
if ($linkModel === null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->buildStatement(
|
|
|
|
array(
|
|
|
|
'fields' => array_unique($queryData['fields']),
|
|
|
|
'table' => $this->fullTableName($model),
|
2011-02-26 07:17:42 +00:00
|
|
|
'alias' => $modelAlias,
|
2008-05-30 11:40:08 +00:00
|
|
|
'limit' => $queryData['limit'],
|
|
|
|
'offset' => $queryData['offset'],
|
|
|
|
'joins' => $queryData['joins'],
|
|
|
|
'conditions' => $queryData['conditions'],
|
|
|
|
'order' => $queryData['order'],
|
|
|
|
'group' => $queryData['group']
|
|
|
|
),
|
|
|
|
$model
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($external && !empty($assocData['finderQuery'])) {
|
|
|
|
return $assocData['finderQuery'];
|
|
|
|
}
|
|
|
|
|
2011-02-26 07:17:42 +00:00
|
|
|
$self = $model->name === $linkModel->name;
|
2008-05-30 11:40:08 +00:00
|
|
|
$fields = array();
|
|
|
|
|
2011-02-26 07:17:42 +00:00
|
|
|
if ($external || (in_array($type, array('hasOne', 'belongsTo')) && $this->__bypass === false)) {
|
|
|
|
$fields = $this->fields($linkModel, $association, $assocData['fields']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (empty($assocData['offset']) && !empty($assocData['page'])) {
|
|
|
|
$assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
|
|
|
|
}
|
|
|
|
$assocData['limit'] = $this->limit($assocData['limit'], $assocData['offset']);
|
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
switch ($type) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'hasOne':
|
|
|
|
case 'belongsTo':
|
|
|
|
$conditions = $this->__mergeConditions(
|
|
|
|
$assocData['conditions'],
|
2011-02-26 07:17:42 +00:00
|
|
|
$this->getConstraint($type, $model, $linkModel, $association, array_merge($assocData, compact('external', 'self')))
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2008-09-01 19:31:28 +00:00
|
|
|
|
2008-09-22 20:08:31 +00:00
|
|
|
if (!$self && $external) {
|
|
|
|
foreach ($conditions as $key => $condition) {
|
2011-02-26 07:17:42 +00:00
|
|
|
if (is_numeric($key) && strpos($condition, $modelAlias . '.') !== false) {
|
2008-09-22 20:08:31 +00:00
|
|
|
unset($conditions[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($external) {
|
|
|
|
$query = array_merge($assocData, array(
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
|
|
|
'fields' => $fields,
|
2011-02-26 07:17:42 +00:00
|
|
|
'alias' => $association,
|
2008-05-30 11:40:08 +00:00
|
|
|
'group' => null
|
|
|
|
));
|
2011-02-26 07:17:42 +00:00
|
|
|
$query += array('order' => $assocData['order'], 'limit' => $assocData['limit']);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$join = array(
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
2011-02-26 07:17:42 +00:00
|
|
|
'alias' => $association,
|
2008-05-30 11:40:08 +00:00
|
|
|
'type' => isset($assocData['type']) ? $assocData['type'] : 'LEFT',
|
|
|
|
'conditions' => trim($this->conditions($conditions, true, false, $model))
|
|
|
|
);
|
|
|
|
$queryData['fields'] = array_merge($queryData['fields'], $fields);
|
|
|
|
|
|
|
|
if (!empty($assocData['order'])) {
|
|
|
|
$queryData['order'][] = $assocData['order'];
|
|
|
|
}
|
|
|
|
if (!in_array($join, $queryData['joins'])) {
|
|
|
|
$queryData['joins'][] = $join;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'hasMany':
|
2011-02-26 07:17:42 +00:00
|
|
|
$assocData['fields'] = $this->fields($linkModel, $association, $assocData['fields']);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($assocData['foreignKey'])) {
|
2011-02-26 07:17:42 +00:00
|
|
|
$assocData['fields'] = array_merge($assocData['fields'], $this->fields($linkModel, $association, array("{$association}.{$assocData['foreignKey']}")));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$query = array(
|
2011-02-26 07:17:42 +00:00
|
|
|
'conditions' => $this->__mergeConditions($this->getConstraint('hasMany', $model, $linkModel, $association, $assocData), $assocData['conditions']),
|
2008-05-30 11:40:08 +00:00
|
|
|
'fields' => array_unique($assocData['fields']),
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
2011-02-26 07:17:42 +00:00
|
|
|
'alias' => $association,
|
2008-05-30 11:40:08 +00:00
|
|
|
'order' => $assocData['order'],
|
|
|
|
'limit' => $assocData['limit'],
|
2008-05-31 12:36:38 +00:00
|
|
|
'group' => null
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'hasAndBelongsToMany':
|
|
|
|
$joinFields = array();
|
|
|
|
$joinAssoc = null;
|
|
|
|
|
|
|
|
if (isset($assocData['with']) && !empty($assocData['with'])) {
|
|
|
|
$joinKeys = array($assocData['foreignKey'], $assocData['associationForeignKey']);
|
|
|
|
list($with, $joinFields) = $model->joinModel($assocData['with'], $joinKeys);
|
|
|
|
|
|
|
|
$joinTbl = $this->fullTableName($model->{$with});
|
|
|
|
$joinAlias = $joinTbl;
|
|
|
|
|
|
|
|
if (is_array($joinFields) && !empty($joinFields)) {
|
|
|
|
$joinAssoc = $joinAlias = $model->{$with}->alias;
|
2011-02-26 07:17:42 +00:00
|
|
|
$joinFields = $this->fields($model->{$with}, $joinAlias, $joinFields);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$joinFields = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$joinTbl = $this->fullTableName($assocData['joinTable']);
|
|
|
|
$joinAlias = $joinTbl;
|
|
|
|
}
|
|
|
|
$query = array(
|
|
|
|
'conditions' => $assocData['conditions'],
|
|
|
|
'limit' => $assocData['limit'],
|
|
|
|
'table' => $this->fullTableName($linkModel),
|
2011-02-26 07:17:42 +00:00
|
|
|
'alias' => $association,
|
|
|
|
'fields' => array_merge($this->fields($linkModel, $association, $assocData['fields']), $joinFields),
|
2008-05-30 11:40:08 +00:00
|
|
|
'order' => $assocData['order'],
|
|
|
|
'group' => null,
|
|
|
|
'joins' => array(array(
|
|
|
|
'table' => $joinTbl,
|
|
|
|
'alias' => $joinAssoc,
|
2011-02-26 07:17:42 +00:00
|
|
|
'conditions' => $this->getConstraint('hasAndBelongsToMany', $model, $linkModel, $joinAlias, $assocData, $association)
|
2008-05-31 12:36:38 +00:00
|
|
|
))
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isset($query)) {
|
|
|
|
return $this->buildStatement($query, $model);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a conditions array for the constraint between two models
|
|
|
|
*
|
|
|
|
* @param string $type Association type
|
|
|
|
* @param object $model Model object
|
|
|
|
* @param array $association Association array
|
|
|
|
* @return array Conditions array defining the constraint between $model and $association
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getConstraint($type, $model, $linkModel, $alias, $assoc, $alias2 = null) {
|
2011-02-25 04:02:37 +00:00
|
|
|
$assoc += array('external' => false, 'self' => false);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (empty($assoc['foreignKey'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (true) {
|
2011-02-25 04:02:37 +00:00
|
|
|
case ($assoc['external'] && $type === 'hasOne'):
|
2008-05-30 11:40:08 +00:00
|
|
|
return array("{$alias}.{$assoc['foreignKey']}" => '{$__cakeID__$}');
|
2011-02-25 04:02:37 +00:00
|
|
|
case ($assoc['external'] && $type === 'belongsTo'):
|
2008-05-30 11:40:08 +00:00
|
|
|
return array("{$alias}.{$linkModel->primaryKey}" => '{$__cakeForeignKey__$}');
|
2011-02-25 04:02:37 +00:00
|
|
|
case (!$assoc['external'] && $type === 'hasOne'):
|
2008-05-31 12:36:38 +00:00
|
|
|
return array("{$alias}.{$assoc['foreignKey']}" => $this->identifier("{$model->alias}.{$model->primaryKey}"));
|
2011-02-25 04:02:37 +00:00
|
|
|
case (!$assoc['external'] && $type === 'belongsTo'):
|
2008-05-31 12:36:38 +00:00
|
|
|
return array("{$model->alias}.{$assoc['foreignKey']}" => $this->identifier("{$alias}.{$linkModel->primaryKey}"));
|
2011-02-25 04:02:37 +00:00
|
|
|
case ($type === 'hasMany'):
|
2008-05-30 11:40:08 +00:00
|
|
|
return array("{$alias}.{$assoc['foreignKey']}" => array('{$__cakeID__$}'));
|
2011-02-25 04:02:37 +00:00
|
|
|
case ($type === 'hasAndBelongsToMany'):
|
2008-05-30 11:40:08 +00:00
|
|
|
return array(
|
|
|
|
array("{$alias}.{$assoc['foreignKey']}" => '{$__cakeID__$}'),
|
2008-05-31 12:36:38 +00:00
|
|
|
array("{$alias}.{$assoc['associationForeignKey']}" => $this->identifier("{$alias2}.{$linkModel->primaryKey}"))
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-27 05:14:52 +00:00
|
|
|
* Builds and generates a JOIN statement from an array. Handles final clean-up before conversion.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $join An array defining a JOIN statement in a query
|
|
|
|
* @return string An SQL JOIN statement to be used in a query
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see DboSource::renderJoinStatement()
|
|
|
|
* @see DboSource::buildStatement()
|
|
|
|
*/
|
2010-04-11 05:25:18 +00:00
|
|
|
public function buildJoinStatement($join) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = array_merge(array(
|
|
|
|
'type' => null,
|
|
|
|
'alias' => null,
|
|
|
|
'table' => 'join_table',
|
|
|
|
'conditions' => array()
|
|
|
|
), $join);
|
|
|
|
|
|
|
|
if (!empty($data['alias'])) {
|
|
|
|
$data['alias'] = $this->alias . $this->name($data['alias']);
|
|
|
|
}
|
|
|
|
if (!empty($data['conditions'])) {
|
|
|
|
$data['conditions'] = trim($this->conditions($data['conditions'], true, false));
|
|
|
|
}
|
|
|
|
return $this->renderJoinStatement($data);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-27 05:14:52 +00:00
|
|
|
* Builds and generates an SQL statement from an array. Handles final clean-up before conversion.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $query An array defining an SQL query
|
|
|
|
* @param object $model The model object which initiated the query
|
|
|
|
* @return string An executable SQL statement
|
2009-12-19 18:06:32 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see DboSource::renderStatement()
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function buildStatement($query, $model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$query = array_merge(array('offset' => null, 'joins' => array()), $query);
|
|
|
|
if (!empty($query['joins'])) {
|
2008-09-18 03:09:19 +00:00
|
|
|
$count = count($query['joins']);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($query['joins'][$i])) {
|
|
|
|
$query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->renderStatement('select', array(
|
|
|
|
'conditions' => $this->conditions($query['conditions'], true, true, $model),
|
2009-11-19 22:13:35 +00:00
|
|
|
'fields' => implode(', ', $query['fields']),
|
2008-05-30 11:40:08 +00:00
|
|
|
'table' => $query['table'],
|
|
|
|
'alias' => $this->alias . $this->name($query['alias']),
|
2009-12-17 05:15:42 +00:00
|
|
|
'order' => $this->order($query['order'], 'ASC', $model),
|
2008-05-30 11:40:08 +00:00
|
|
|
'limit' => $this->limit($query['limit'], $query['offset']),
|
2009-11-19 22:13:35 +00:00
|
|
|
'joins' => implode(' ', $query['joins']),
|
2010-01-09 19:29:49 +00:00
|
|
|
'group' => $this->group($query['group'], $model)
|
2008-05-30 11:40:08 +00:00
|
|
|
));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Renders a final SQL JOIN statement
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function renderJoinStatement($data) {
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($data);
|
|
|
|
return trim("{$type} JOIN {$table} {$alias} ON ({$conditions})");
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Renders a final SQL statement by putting together the component parts in the correct order
|
|
|
|
*
|
2009-12-19 18:06:32 +00:00
|
|
|
* @param string $type type of query being run. e.g select, create, update, delete, schema, alter.
|
|
|
|
* @param array $data Array of data to insert into the query.
|
|
|
|
* @return string Rendered SQL expression to be run.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function renderStatement($type, $data) {
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($data);
|
|
|
|
$aliases = null;
|
|
|
|
|
|
|
|
switch (strtolower($type)) {
|
|
|
|
case 'select':
|
2008-05-31 21:33:37 +00:00
|
|
|
return "SELECT {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order} {$limit}";
|
2008-10-30 05:03:01 +00:00
|
|
|
case 'create':
|
|
|
|
return "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'update':
|
|
|
|
if (!empty($alias)) {
|
|
|
|
$aliases = "{$this->alias}{$alias} {$joins} ";
|
|
|
|
}
|
|
|
|
return "UPDATE {$table} {$aliases}SET {$fields} {$conditions}";
|
|
|
|
case 'delete':
|
|
|
|
if (!empty($alias)) {
|
|
|
|
$aliases = "{$this->alias}{$alias} {$joins} ";
|
|
|
|
}
|
|
|
|
return "DELETE {$alias} FROM {$table} {$aliases}{$conditions}";
|
|
|
|
case 'schema':
|
2009-09-29 15:25:42 +00:00
|
|
|
foreach (array('columns', 'indexes', 'tableParameters') as $var) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array(${$var})) {
|
|
|
|
${$var} = "\t" . join(",\n\t", array_filter(${$var}));
|
2009-09-29 15:25:42 +00:00
|
|
|
} else {
|
|
|
|
${$var} = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
if (trim($indexes) !== '') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$columns .= ',';
|
|
|
|
}
|
2009-09-29 15:25:42 +00:00
|
|
|
return "CREATE TABLE {$table} (\n{$columns}{$indexes}){$tableParameters};";
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'alter':
|
2011-02-25 04:02:37 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Merges a mixed set of string/array conditions
|
|
|
|
*
|
2011-02-25 04:02:37 +00:00
|
|
|
* @param mixed $query
|
|
|
|
* @param mixed $assoc
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-02-25 04:02:37 +00:00
|
|
|
private function __mergeConditions($query, $assoc) {
|
2008-09-01 19:31:28 +00:00
|
|
|
if (empty($assoc)) {
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($query)) {
|
|
|
|
return array_merge((array)$assoc, $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($query)) {
|
|
|
|
$query = array($query);
|
|
|
|
if (is_array($assoc)) {
|
|
|
|
$query = array_merge($query, $assoc);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2008-09-01 19:31:28 +00:00
|
|
|
$query[] = $assoc;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-01 19:31:28 +00:00
|
|
|
return $query;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-01 19:31:28 +00:00
|
|
|
|
|
|
|
return $assoc;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates and executes an SQL UPDATE statement for given model, fields, and values.
|
|
|
|
* For databases that do not support aliases in UPDATE queries.
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param array $fields
|
|
|
|
* @param array $values
|
|
|
|
* @param mixed $conditions
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function update($model, $fields = array(), $values = null, $conditions = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($values == null) {
|
|
|
|
$combined = $fields;
|
|
|
|
} else {
|
|
|
|
$combined = array_combine($fields, $values);
|
|
|
|
}
|
2010-01-22 15:30:22 +00:00
|
|
|
|
2009-11-19 22:13:35 +00:00
|
|
|
$fields = implode(', ', $this->_prepareUpdateFields($model, $combined, empty($conditions)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$alias = $joins = null;
|
|
|
|
$table = $this->fullTableName($model);
|
|
|
|
$conditions = $this->_matchRecords($model, $conditions);
|
|
|
|
|
|
|
|
if ($conditions === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-30 05:03:01 +00:00
|
|
|
$query = compact('table', 'alias', 'joins', 'fields', 'conditions');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-30 05:03:01 +00:00
|
|
|
if (!$this->execute($this->renderStatement('update', $query))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$model->onError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Quotes and prepares fields and values for an SQL UPDATE statement
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param array $fields
|
|
|
|
* @param boolean $quoteValues If values should be quoted, or treated as SQL snippets
|
|
|
|
* @param boolean $alias Include the model alias in the field name
|
|
|
|
* @return array Fields and values, quoted and preparted
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
protected function _prepareUpdateFields($model, $fields, $quoteValues = true, $alias = false) {
|
2009-01-16 19:08:44 +00:00
|
|
|
$quotedAlias = $this->startQuote . $model->alias . $this->endQuote;
|
|
|
|
|
2009-08-02 06:47:28 +00:00
|
|
|
$updates = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($fields as $field => $value) {
|
|
|
|
if ($alias && strpos($field, '.') === false) {
|
|
|
|
$quoted = $model->escapeField($field);
|
|
|
|
} elseif (!$alias && strpos($field, '.') !== false) {
|
|
|
|
$quoted = $this->name(str_replace($quotedAlias . '.', '', str_replace(
|
|
|
|
$model->alias . '.', '', $field
|
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
$quoted = $this->name($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value === null) {
|
|
|
|
$updates[] = $quoted . ' = NULL';
|
2009-01-16 19:08:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$update = $quoted . ' = ';
|
|
|
|
|
|
|
|
if ($quoteValues) {
|
|
|
|
$update .= $this->value($value, $model->getColumnType($field), false);
|
|
|
|
} elseif (!$alias) {
|
|
|
|
$update .= str_replace($quotedAlias . '.', '', str_replace(
|
|
|
|
$model->alias . '.', '', $value
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-01-16 19:08:44 +00:00
|
|
|
$update .= $value;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-01-16 19:08:44 +00:00
|
|
|
$updates[] = $update;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $updates;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates and executes an SQL DELETE statement.
|
|
|
|
* For databases that do not support aliases in UPDATE queries.
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param mixed $conditions
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function delete($model, $conditions = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$alias = $joins = null;
|
|
|
|
$table = $this->fullTableName($model);
|
|
|
|
$conditions = $this->_matchRecords($model, $conditions);
|
|
|
|
|
|
|
|
if ($conditions === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
|
|
|
|
$model->onError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-27 05:14:52 +00:00
|
|
|
* Gets a list of record IDs for the given conditions. Used for multi-record updates and deletes
|
2008-05-30 11:40:08 +00:00
|
|
|
* in databases that do not support aliases in UPDATE/DELETE queries.
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param mixed $conditions
|
|
|
|
* @return array List of record IDs
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
protected function _matchRecords($model, $conditions = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($conditions === true) {
|
|
|
|
$conditions = $this->conditions(true);
|
|
|
|
} elseif ($conditions === null) {
|
|
|
|
$conditions = $this->conditions($this->defaultConditions($model, $conditions, false), true, true, $model);
|
|
|
|
} else {
|
2010-03-16 02:55:14 +00:00
|
|
|
$noJoin = true;
|
|
|
|
foreach ($conditions as $field => $value) {
|
|
|
|
$originalField = $field;
|
|
|
|
if (strpos($field, '.') !== false) {
|
|
|
|
list($alias, $field) = explode('.', $field);
|
2010-03-16 21:19:57 +00:00
|
|
|
$field = ltrim($field, $this->startQuote);
|
|
|
|
$field = rtrim($field, $this->endQuote);
|
2010-03-16 02:55:14 +00:00
|
|
|
}
|
|
|
|
if (!$model->hasField($field)) {
|
|
|
|
$noJoin = false;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-16 04:17:09 +00:00
|
|
|
if ($field !== $originalField) {
|
|
|
|
$conditions[$field] = $value;
|
|
|
|
unset($conditions[$originalField]);
|
|
|
|
}
|
2010-03-16 02:55:14 +00:00
|
|
|
}
|
|
|
|
if ($noJoin === true) {
|
|
|
|
return $this->conditions($conditions);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$idList = $model->find('all', array(
|
|
|
|
'fields' => "{$model->alias}.{$model->primaryKey}",
|
|
|
|
'conditions' => $conditions
|
|
|
|
));
|
|
|
|
|
|
|
|
if (empty($idList)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$conditions = $this->conditions(array(
|
|
|
|
$model->primaryKey => Set::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}")
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return $conditions;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of SQL JOIN fragments from a model's associations
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _getJoins($model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$join = array();
|
|
|
|
$joins = array_merge($model->getAssociated('hasOne'), $model->getAssociated('belongsTo'));
|
|
|
|
|
|
|
|
foreach ($joins as $assoc) {
|
2011-02-23 05:19:37 +00:00
|
|
|
if (isset($model->{$assoc}) && $model->useDbConfig == $model->{$assoc}->useDbConfig && $model->{$assoc}->getDataSource()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$assocData = $model->getAssociated($assoc);
|
|
|
|
$join[] = $this->buildJoinStatement(array(
|
|
|
|
'table' => $this->fullTableName($model->{$assoc}),
|
|
|
|
'alias' => $assoc,
|
|
|
|
'type' => isset($assocData['type']) ? $assocData['type'] : 'LEFT',
|
|
|
|
'conditions' => trim($this->conditions(
|
2009-09-09 03:48:32 +00:00
|
|
|
$this->__mergeConditions($assocData['conditions'], $this->getConstraint($assocData['association'], $model, $model->{$assoc}, $assoc, $assocData)),
|
2008-05-30 11:40:08 +00:00
|
|
|
true, false, $model
|
|
|
|
))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $join;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-21 20:57:39 +00:00
|
|
|
* Returns an SQL calculation, i.e. COUNT() or MAX()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param model $model
|
|
|
|
* @param string $func Lowercase name of SQL function, i.e. 'count' or 'max'
|
|
|
|
* @param array $params Function parameters (any values must be quoted manually)
|
2009-07-21 20:57:39 +00:00
|
|
|
* @return string An SQL calculation function
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function calculate($model, $func, $params = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$params = (array)$params;
|
|
|
|
|
|
|
|
switch (strtolower($func)) {
|
|
|
|
case 'count':
|
|
|
|
if (!isset($params[0])) {
|
|
|
|
$params[0] = '*';
|
|
|
|
}
|
|
|
|
if (!isset($params[1])) {
|
|
|
|
$params[1] = 'count';
|
|
|
|
}
|
2009-12-07 02:46:00 +00:00
|
|
|
if (is_object($model) && $model->isVirtualField($params[0])){
|
2009-12-11 01:16:55 +00:00
|
|
|
$arg = $this->__quoteFields($model->getVirtualField($params[0]));
|
2009-12-07 01:25:49 +00:00
|
|
|
} else {
|
|
|
|
$arg = $this->name($params[0]);
|
|
|
|
}
|
|
|
|
return 'COUNT(' . $arg . ') AS ' . $this->name($params[1]);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'max':
|
|
|
|
case 'min':
|
|
|
|
if (!isset($params[1])) {
|
|
|
|
$params[1] = $params[0];
|
|
|
|
}
|
2009-12-07 02:46:00 +00:00
|
|
|
if (is_object($model) && $model->isVirtualField($params[0])) {
|
2009-12-11 01:16:55 +00:00
|
|
|
$arg = $this->__quoteFields($model->getVirtualField($params[0]));
|
2009-12-07 01:25:49 +00:00
|
|
|
} else {
|
|
|
|
$arg = $this->name($params[0]);
|
|
|
|
}
|
|
|
|
return strtoupper($func) . '(' . $arg . ') AS ' . $this->name($params[1]);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Deletes all the records in a table and resets the count of the auto-incrementing
|
|
|
|
* primary key, where applicable.
|
|
|
|
*
|
|
|
|
* @param mixed $table A string or model class representing the table to be truncated
|
|
|
|
* @return boolean SQL TRUNCATE TABLE statement, false if not applicable.
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function truncate($table) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->execute('TRUNCATE TABLE ' . $this->fullTableName($table));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Begin a transaction
|
|
|
|
*
|
|
|
|
* @return boolean True on success, false on fail
|
|
|
|
* (i.e. if the database/model does not support transactions,
|
|
|
|
* or a transaction has not started).
|
|
|
|
*/
|
2010-11-17 03:26:23 +00:00
|
|
|
public function begin() {
|
|
|
|
if ($this->_transactionStarted || $this->_connection->beginTransaction()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_transactionStarted = true;
|
2010-11-17 03:26:23 +00:00
|
|
|
$this->_transactionNesting++;
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Commit a transaction
|
|
|
|
*
|
|
|
|
* @return boolean True on success, false on fail
|
|
|
|
* (i.e. if the database/model does not support transactions,
|
|
|
|
* or a transaction has not started).
|
|
|
|
*/
|
2010-11-17 03:26:23 +00:00
|
|
|
public function commit() {
|
|
|
|
if ($this->_transactionStarted) {
|
|
|
|
$this->_transactionNesting--;
|
|
|
|
if ($this->_transactionNesting <= 0) {
|
|
|
|
$this->_transactionStarted = false;
|
|
|
|
$this->_transactionNesting = 0;
|
|
|
|
return $this->_connection->commit();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Rollback a transaction
|
|
|
|
*
|
|
|
|
* @return boolean True on success, false on fail
|
|
|
|
* (i.e. if the database/model does not support transactions,
|
|
|
|
* or a transaction has not started).
|
|
|
|
*/
|
2010-11-17 03:26:23 +00:00
|
|
|
public function rollback() {
|
|
|
|
if ($this->_transactionStarted && $this->_connection->rollBack()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_transactionStarted = false;
|
2010-11-17 03:26:23 +00:00
|
|
|
$this->_transactionNesting = 0;
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-11-29 03:15:56 +00:00
|
|
|
/**
|
|
|
|
* Returns the ID generated from the previous INSERT operation.
|
|
|
|
*
|
|
|
|
* @param unknown_type $source
|
|
|
|
* @return in
|
|
|
|
*/
|
|
|
|
function lastInsertId($source = null) {
|
|
|
|
return $this->_connection->lastInsertId();
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a default set of conditions from the model if $conditions is null/empty.
|
2010-01-22 15:30:22 +00:00
|
|
|
* If conditions are supplied then they will be returned. If a model doesn't exist and no conditions
|
|
|
|
* were provided either null or false will be returned based on what was input.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param object $model
|
2010-01-22 15:30:22 +00:00
|
|
|
* @param mixed $conditions Array of conditions, conditions string, null or false. If an array of conditions,
|
|
|
|
* or string conditions those conditions will be returned. With other values the model's existance will be checked.
|
|
|
|
* If the model doesn't exist a null or false will be returned depending on the input value.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param boolean $useAlias Use model aliases rather than table names when generating conditions
|
2010-01-22 15:30:22 +00:00
|
|
|
* @return mixed Either null, false, $conditions or an array of default conditions to use.
|
|
|
|
* @see DboSource::update()
|
|
|
|
* @see DboSource::conditions()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function defaultConditions($model, $conditions, $useAlias = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($conditions)) {
|
|
|
|
return $conditions;
|
|
|
|
}
|
2010-01-22 15:30:22 +00:00
|
|
|
$exists = $model->exists();
|
|
|
|
if (!$exists && $conditions !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
2010-01-22 15:30:22 +00:00
|
|
|
} elseif (!$exists) {
|
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$alias = $model->alias;
|
|
|
|
|
|
|
|
if (!$useAlias) {
|
|
|
|
$alias = $this->fullTableName($model, false);
|
|
|
|
}
|
|
|
|
return array("{$alias}.{$model->primaryKey}" => $model->getID());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a key formatted like a string Model.fieldname(i.e. Post.title, or Country.name)
|
|
|
|
*
|
|
|
|
* @param unknown_type $model
|
|
|
|
* @param unknown_type $key
|
|
|
|
* @param unknown_type $assoc
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function resolveKey($model, $key, $assoc = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($assoc)) {
|
|
|
|
$assoc = $model->alias;
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
if (strpos('.', $key) !== false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->name($model->alias) . '.' . $this->name($key);
|
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Private helper method to remove query metadata in given data array.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function __scrubQueryData($data) {
|
2011-02-26 02:28:03 +00:00
|
|
|
static $base = null;
|
|
|
|
if ($base === null) {
|
|
|
|
$base = array_fill_keys(array('conditions', 'fields', 'joins', 'order', 'limit', 'offset', 'group'), array());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-26 02:28:03 +00:00
|
|
|
return (array)$data + $base;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-12 05:05:08 +00:00
|
|
|
/**
|
|
|
|
* Converts model virtual fields into sql expressions to be fetched later
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param string $alias Alias tablename
|
|
|
|
* @param mixed $fields virtual fields to be used on query
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
protected function _constructVirtualFields($model, $alias, $fields) {
|
2009-11-10 05:38:07 +00:00
|
|
|
$virtual = array();
|
2009-11-12 03:42:03 +00:00
|
|
|
foreach ($fields as $field) {
|
2010-05-22 04:58:54 +00:00
|
|
|
$virtualField = $this->name($alias . $this->virtualFieldSeparator . $field);
|
2009-12-11 01:16:55 +00:00
|
|
|
$expression = $this->__quoteFields($model->getVirtualField($field));
|
2010-09-07 04:09:06 +00:00
|
|
|
$virtual[] = '(' . $expression . ") {$this->alias} {$virtualField}";
|
2009-11-10 05:38:07 +00:00
|
|
|
}
|
|
|
|
return $virtual;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates the fields list of an SQL query.
|
|
|
|
*
|
|
|
|
* @param Model $model
|
|
|
|
* @param string $alias Alias tablename
|
|
|
|
* @param mixed $fields
|
|
|
|
* @param boolean $quote If false, returns fields array unquoted
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function fields($model, $alias = null, $fields = array(), $quote = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($alias)) {
|
|
|
|
$alias = $model->alias;
|
|
|
|
}
|
2010-01-29 15:27:40 +00:00
|
|
|
$cacheKey = array(
|
|
|
|
$model->useDbConfig,
|
|
|
|
$model->table,
|
|
|
|
array_keys($model->schema()),
|
|
|
|
$model->name,
|
|
|
|
$model->getVirtualField(),
|
|
|
|
$alias,
|
|
|
|
$fields,
|
|
|
|
$quote
|
|
|
|
);
|
|
|
|
$cacheKey = crc32(serialize($cacheKey));
|
2010-10-21 02:28:31 +00:00
|
|
|
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
|
|
|
|
return $return;
|
2010-01-29 15:27:40 +00:00
|
|
|
}
|
2009-11-10 05:38:07 +00:00
|
|
|
$allFields = empty($fields);
|
|
|
|
if ($allFields) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$fields = array_keys($model->schema());
|
|
|
|
} elseif (!is_array($fields)) {
|
|
|
|
$fields = String::tokenize($fields);
|
|
|
|
}
|
|
|
|
$fields = array_values(array_filter($fields));
|
2010-04-04 17:54:39 +00:00
|
|
|
$allFields = $allFields || in_array('*', $fields) || in_array($model->alias . '.*', $fields);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-11-12 03:42:03 +00:00
|
|
|
$virtual = array();
|
2010-01-16 12:32:04 +00:00
|
|
|
$virtualFields = $model->getVirtualField();
|
2010-01-21 15:35:20 +00:00
|
|
|
if (!empty($virtualFields)) {
|
|
|
|
$virtualKeys = array_keys($virtualFields);
|
|
|
|
foreach ($virtualKeys as $field) {
|
|
|
|
$virtualKeys[] = $model->alias . '.' . $field;
|
2010-01-16 12:32:04 +00:00
|
|
|
}
|
2010-01-21 15:35:20 +00:00
|
|
|
$virtual = ($allFields) ? $virtualKeys : array_intersect($virtualKeys, $fields);
|
|
|
|
foreach ($virtual as $i => $field) {
|
|
|
|
if (strpos($field, '.') !== false) {
|
|
|
|
$virtual[$i] = str_replace($model->alias . '.', '', $field);
|
|
|
|
}
|
|
|
|
$fields = array_diff($fields, array($field));
|
2010-01-16 12:32:04 +00:00
|
|
|
}
|
2010-01-21 15:35:20 +00:00
|
|
|
$fields = array_values($fields);
|
2010-01-16 12:32:04 +00:00
|
|
|
}
|
2010-03-05 20:37:59 +00:00
|
|
|
|
|
|
|
if (!$quote) {
|
|
|
|
if (!empty($virtual)) {
|
|
|
|
$fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$count = count($fields);
|
|
|
|
|
|
|
|
if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2010-01-11 14:44:51 +00:00
|
|
|
if (is_string($fields[$i]) && in_array($fields[$i], $virtual)) {
|
2009-11-12 03:42:03 +00:00
|
|
|
unset($fields[$i]);
|
|
|
|
continue;
|
|
|
|
}
|
2009-12-13 20:52:07 +00:00
|
|
|
if (is_object($fields[$i]) && isset($fields[$i]->type) && $fields[$i]->type === 'expression') {
|
|
|
|
$fields[$i] = $fields[$i]->value;
|
|
|
|
} elseif (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){
|
2009-08-19 17:03:36 +00:00
|
|
|
continue;
|
|
|
|
} elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$prepend = '';
|
|
|
|
|
|
|
|
if (strpos($fields[$i], 'DISTINCT') !== false) {
|
|
|
|
$prepend = 'DISTINCT ';
|
|
|
|
$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
|
|
|
|
}
|
|
|
|
$dot = strpos($fields[$i], '.');
|
|
|
|
|
|
|
|
if ($dot === false) {
|
2009-01-16 19:08:44 +00:00
|
|
|
$prefix = !(
|
|
|
|
strpos($fields[$i], ' ') !== false ||
|
|
|
|
strpos($fields[$i], '(') !== false
|
|
|
|
);
|
2009-03-09 18:36:32 +00:00
|
|
|
$fields[$i] = $this->name(($prefix ? $alias . '.' : '') . $fields[$i]);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$value = array();
|
2011-02-25 04:02:37 +00:00
|
|
|
if (strpos($fields[$i], ',') === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$build = explode('.', $fields[$i]);
|
|
|
|
if (!Set::numeric($build)) {
|
2010-09-18 04:04:36 +00:00
|
|
|
$fields[$i] = $this->name(implode('.', $build));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fields[$i] = $prepend . $fields[$i];
|
|
|
|
} elseif (preg_match('/\(([\.\w]+)\)/', $fields[$i], $field)) {
|
|
|
|
if (isset($field[1])) {
|
|
|
|
if (strpos($field[1], '.') === false) {
|
|
|
|
$field[1] = $this->name($alias . '.' . $field[1]);
|
|
|
|
} else {
|
|
|
|
$field[0] = explode('.', $field[1]);
|
|
|
|
if (!Set::numeric($field[0])) {
|
2010-01-29 15:27:40 +00:00
|
|
|
$field[0] = implode('.', array_map(array(&$this, 'name'), $field[0]));
|
2008-05-30 11:40:08 +00:00
|
|
|
$fields[$i] = preg_replace('/\(' . $field[1] . '\)/', '(' . $field[0] . ')', $fields[$i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-12 03:42:03 +00:00
|
|
|
if (!empty($virtual)) {
|
2010-01-21 15:35:20 +00:00
|
|
|
$fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
|
2009-11-10 05:38:07 +00:00
|
|
|
}
|
2010-10-21 02:28:31 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, array_unique($fields));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-22 15:30:22 +00:00
|
|
|
* Creates a WHERE clause by parsing given conditions data. If an array or string
|
|
|
|
* conditions are provided those conditions will be parsed and quoted. If a boolean
|
|
|
|
* is given it will be integer cast as condition. Null will return 1 = 1.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-06-30 12:52:39 +00:00
|
|
|
* Results of this method are stored in a memory cache. This improves performance, but
|
|
|
|
* because the method uses a simple hashing algorithm it can infrequently have collisions.
|
|
|
|
* Setting DboSource::$cacheMethods to false will disable the memory cache.
|
|
|
|
*
|
2010-01-22 15:30:22 +00:00
|
|
|
* @param mixed $conditions Array or string of conditions, or any value.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param boolean $quoteValues If true, values should be quoted
|
|
|
|
* @param boolean $where If true, "WHERE " will be prepended to the return value
|
|
|
|
* @param Model $model A reference to the Model instance making the query
|
|
|
|
* @return string SQL fragment
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function conditions($conditions, $quoteValues = true, $where = true, $model = null) {
|
2010-01-29 15:27:40 +00:00
|
|
|
if (is_object($model)) {
|
|
|
|
$cacheKey = array(
|
|
|
|
$model->useDbConfig,
|
|
|
|
$model->table,
|
|
|
|
$model->schema(),
|
|
|
|
$model->name,
|
|
|
|
$model->getVirtualField(),
|
|
|
|
$conditions,
|
|
|
|
$quoteValues,
|
|
|
|
$where
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$cacheKey = array($conditions, $quoteValues, $where);
|
|
|
|
}
|
|
|
|
$cacheKey = crc32(serialize($cacheKey));
|
2010-03-14 04:00:08 +00:00
|
|
|
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
|
|
|
|
return $return;
|
2010-01-29 15:27:40 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$clause = $out = '';
|
2008-06-20 20:17:23 +00:00
|
|
|
|
|
|
|
if ($where) {
|
|
|
|
$clause = ' WHERE ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($conditions) && !empty($conditions)) {
|
2008-10-29 06:44:15 +00:00
|
|
|
$out = $this->conditionKeysToString($conditions, $quoteValues, $model);
|
|
|
|
|
|
|
|
if (empty($out)) {
|
2010-03-14 04:00:08 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . ' 1 = 1');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-03-14 04:00:08 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . implode(' AND ', $out));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
if (is_bool($conditions)) {
|
2010-03-14 04:00:08 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . (int)$conditions . ' = 1');
|
2010-01-22 15:30:22 +00:00
|
|
|
}
|
2008-06-20 20:17:23 +00:00
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (empty($conditions) || trim($conditions) === '') {
|
2010-03-14 04:00:08 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . '1 = 1');
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2008-10-30 05:03:01 +00:00
|
|
|
$clauses = '/^WHERE\\x20|^GROUP\\x20BY\\x20|^HAVING\\x20|^ORDER\\x20BY\\x20/i';
|
|
|
|
|
|
|
|
if (preg_match($clauses, $conditions, $match)) {
|
2008-06-20 20:17:23 +00:00
|
|
|
$clause = '';
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
$conditions = $this->__quoteFields($conditions);
|
2010-03-14 04:00:08 +00:00
|
|
|
return $this->cacheMethod(__FUNCTION__, $cacheKey, $clause . $conditions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a WHERE clause by parsing given conditions array. Used by DboSource::conditions().
|
|
|
|
*
|
|
|
|
* @param array $conditions Array or string of conditions
|
|
|
|
* @param boolean $quoteValues If true, values should be quoted
|
|
|
|
* @param Model $model A reference to the Model instance making the query
|
|
|
|
* @return string SQL fragment
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function conditionKeysToString($conditions, $quoteValues = true, $model = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = array();
|
2008-05-31 12:36:38 +00:00
|
|
|
$data = $columnType = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$bool = array('and', 'or', 'not', 'and not', 'or not', 'xor', '||', '&&');
|
|
|
|
|
|
|
|
foreach ($conditions as $key => $value) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$join = ' AND ';
|
|
|
|
$not = null;
|
|
|
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
$valueInsert = (
|
|
|
|
!empty($value) &&
|
2011-02-25 04:02:37 +00:00
|
|
|
(substr_count($key, '?') === count($value) || substr_count($key, ':') === count($value))
|
2008-05-31 12:36:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_numeric($key) && empty($value)) {
|
|
|
|
continue;
|
|
|
|
} elseif (is_numeric($key) && is_string($value)) {
|
|
|
|
$out[] = $not . $this->__quoteFields($value);
|
2008-05-31 12:36:38 +00:00
|
|
|
} elseif ((is_numeric($key) && is_array($value)) || in_array(strtolower(trim($key)), $bool)) {
|
|
|
|
if (in_array(strtolower(trim($key)), $bool)) {
|
|
|
|
$join = ' ' . strtoupper($key) . ' ';
|
|
|
|
} else {
|
|
|
|
$key = $join;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$value = $this->conditionKeysToString($value, $quoteValues, $model);
|
2008-06-19 16:15:49 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (strpos($join, 'NOT') !== false) {
|
2011-02-25 04:02:37 +00:00
|
|
|
if (strtoupper(trim($key)) === 'NOT') {
|
2008-05-31 12:36:38 +00:00
|
|
|
$key = 'AND ' . trim($key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$not = 'NOT ';
|
|
|
|
}
|
2008-06-19 16:15:49 +00:00
|
|
|
|
2008-06-14 19:45:26 +00:00
|
|
|
if (empty($value[1])) {
|
2008-06-19 16:12:57 +00:00
|
|
|
if ($not) {
|
|
|
|
$out[] = $not . '(' . $value[0] . ')';
|
|
|
|
} else {
|
2008-06-19 16:15:49 +00:00
|
|
|
$out[] = $value[0] ;
|
2008-06-19 16:12:57 +00:00
|
|
|
}
|
2008-06-14 19:45:26 +00:00
|
|
|
} else {
|
2009-11-19 22:13:35 +00:00
|
|
|
$out[] = '(' . $not . '(' . implode(') ' . strtoupper($key) . ' (', $value) . '))';
|
2008-06-14 19:45:26 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2008-05-31 12:36:38 +00:00
|
|
|
if (is_object($value) && isset($value->type)) {
|
2011-02-25 04:02:37 +00:00
|
|
|
if ($value->type === 'identifier') {
|
2008-05-31 12:36:38 +00:00
|
|
|
$data .= $this->name($key) . ' = ' . $this->name($value->value);
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif ($value->type === 'expression') {
|
2008-11-10 18:57:50 +00:00
|
|
|
if (is_numeric($key)) {
|
|
|
|
$data .= $value->value;
|
|
|
|
} else {
|
|
|
|
$data .= $this->name($key) . ' = ' . $value->value;
|
|
|
|
}
|
2008-05-31 12:36:38 +00:00
|
|
|
}
|
|
|
|
} elseif (is_array($value) && !empty($value) && !$valueInsert) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$keys = array_keys($value);
|
2010-04-19 01:22:09 +00:00
|
|
|
if ($keys === array_values($keys)) {
|
2008-06-27 05:14:52 +00:00
|
|
|
$count = count($value);
|
|
|
|
if ($count === 1) {
|
2009-07-22 01:50:11 +00:00
|
|
|
$data = $this->__quoteFields($key) . ' = (';
|
2009-01-15 00:01:19 +00:00
|
|
|
} else {
|
2009-07-22 01:50:11 +00:00
|
|
|
$data = $this->__quoteFields($key) . ' IN (';
|
2009-01-15 00:01:19 +00:00
|
|
|
}
|
2009-11-29 04:02:59 +00:00
|
|
|
if ($quoteValues) {
|
2008-05-31 12:36:38 +00:00
|
|
|
if (is_object($model)) {
|
|
|
|
$columnType = $model->getColumnType($key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-19 22:13:35 +00:00
|
|
|
$data .= implode(', ', $this->value($value, $columnType));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-05-31 12:36:38 +00:00
|
|
|
$data .= ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$ret = $this->conditionKeysToString($value, $quoteValues, $model);
|
|
|
|
if (count($ret) > 1) {
|
2009-11-19 22:13:35 +00:00
|
|
|
$data = '(' . implode(') AND (', $ret) . ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (isset($ret[0])) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$data = $ret[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif (is_numeric($key) && !empty($value)) {
|
|
|
|
$data = $this->__quoteFields($value);
|
2008-05-31 12:36:38 +00:00
|
|
|
} else {
|
|
|
|
$data = $this->__parseKey($model, trim($key), $value);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($data != null) {
|
|
|
|
$out[] = $data;
|
|
|
|
$data = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 12:36:38 +00:00
|
|
|
/**
|
2008-10-30 05:03:01 +00:00
|
|
|
* Extracts a Model.field identifier and an SQL condition operator from a string, formats
|
|
|
|
* and inserts values, and composes them into an SQL snippet.
|
2008-05-31 12:36:38 +00:00
|
|
|
*
|
|
|
|
* @param Model $model Model object initiating the query
|
|
|
|
* @param string $key An SQL key snippet containing a field and optional SQL operator
|
|
|
|
* @param mixed $value The value(s) to be inserted in the string
|
|
|
|
* @return string
|
|
|
|
* @access private
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
function __parseKey($model, $key, $value) {
|
2009-11-19 22:13:35 +00:00
|
|
|
$operatorMatch = '/^((' . implode(')|(', $this->__sqlOps);
|
2008-08-07 16:38:15 +00:00
|
|
|
$operatorMatch .= '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is';
|
2008-08-23 14:50:27 +00:00
|
|
|
$bound = (strpos($key, '?') !== false || (is_array($value) && strpos($key, ':') !== false));
|
2008-08-07 16:38:15 +00:00
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (strpos($key, ' ') === false) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$operator = '=';
|
|
|
|
} else {
|
2008-08-23 14:50:27 +00:00
|
|
|
list($key, $operator) = explode(' ', trim($key), 2);
|
2008-08-07 16:38:15 +00:00
|
|
|
|
2008-10-30 05:03:01 +00:00
|
|
|
if (!preg_match($operatorMatch, trim($operator)) && strpos($operator, ' ') !== false) {
|
2008-08-07 16:38:15 +00:00
|
|
|
$key = $key . ' ' . $operator;
|
2008-10-30 05:03:01 +00:00
|
|
|
$split = strrpos($key, ' ');
|
|
|
|
$operator = substr($key, $split);
|
|
|
|
$key = substr($key, 0, $split);
|
2008-08-07 16:38:15 +00:00
|
|
|
}
|
2008-05-31 12:36:38 +00:00
|
|
|
}
|
2009-03-09 20:03:02 +00:00
|
|
|
|
2009-12-07 01:00:15 +00:00
|
|
|
$virtual = false;
|
2009-12-07 02:46:00 +00:00
|
|
|
if (is_object($model) && $model->isVirtualField($key)) {
|
2009-12-11 01:16:55 +00:00
|
|
|
$key = $this->__quoteFields($model->getVirtualField($key));
|
2009-12-07 01:00:15 +00:00
|
|
|
$virtual = true;
|
|
|
|
}
|
2009-07-21 20:57:39 +00:00
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
$type = is_object($model) ? $model->getColumnType($key) : null;
|
|
|
|
$null = $value === null || (is_array($value) && empty($value));
|
2008-05-31 12:36:38 +00:00
|
|
|
|
|
|
|
if (strtolower($operator) === 'not') {
|
2008-10-30 05:03:01 +00:00
|
|
|
$data = $this->conditionKeysToString(
|
|
|
|
array($operator => array($key => $value)), true, $model
|
|
|
|
);
|
2008-05-31 12:36:38 +00:00
|
|
|
return $data[0];
|
|
|
|
}
|
2009-07-21 20:57:39 +00:00
|
|
|
|
2008-08-01 01:04:03 +00:00
|
|
|
$value = $this->value($value, $type);
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2009-12-07 01:00:15 +00:00
|
|
|
if (!$virtual && $key !== '?') {
|
2009-03-09 20:03:02 +00:00
|
|
|
$isKey = (strpos($key, '(') !== false || strpos($key, ')') !== false);
|
|
|
|
$key = $isKey ? $this->__quoteFields($key) : $this->name($key);
|
|
|
|
}
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2008-08-23 14:50:27 +00:00
|
|
|
if ($bound) {
|
2011-02-25 04:02:37 +00:00
|
|
|
return String::insert($key . ' ' . trim($operator), $value);
|
2008-08-23 14:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!preg_match($operatorMatch, trim($operator))) {
|
|
|
|
$operator .= ' =';
|
|
|
|
}
|
|
|
|
$operator = trim($operator);
|
|
|
|
|
|
|
|
if (is_array($value)) {
|
2009-11-19 22:13:35 +00:00
|
|
|
$value = implode(', ', $value);
|
2008-05-31 12:36:38 +00:00
|
|
|
|
|
|
|
switch ($operator) {
|
|
|
|
case '=':
|
|
|
|
$operator = 'IN';
|
|
|
|
break;
|
|
|
|
case '!=':
|
|
|
|
case '<>':
|
|
|
|
$operator = 'NOT IN';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$value = "({$value})";
|
|
|
|
} elseif ($null) {
|
|
|
|
switch ($operator) {
|
|
|
|
case '=':
|
|
|
|
$operator = 'IS';
|
|
|
|
break;
|
|
|
|
case '!=':
|
|
|
|
case '<>':
|
2008-06-10 15:45:54 +00:00
|
|
|
$operator = 'IS NOT';
|
2008-05-31 12:36:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 19:12:46 +00:00
|
|
|
if ($virtual) {
|
|
|
|
return "({$key}) {$operator} {$value}";
|
|
|
|
}
|
2008-05-31 12:36:38 +00:00
|
|
|
return "{$key} {$operator} {$value}";
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Quotes Model.fields
|
|
|
|
*
|
|
|
|
* @param string $conditions
|
|
|
|
* @return string or false if no match
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __quoteFields($conditions) {
|
2011-02-25 04:02:37 +00:00
|
|
|
$start = $end = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$original = $conditions;
|
|
|
|
|
|
|
|
if (!empty($this->startQuote)) {
|
|
|
|
$start = preg_quote($this->startQuote);
|
|
|
|
}
|
|
|
|
if (!empty($this->endQuote)) {
|
|
|
|
$end = preg_quote($this->endQuote);
|
|
|
|
}
|
|
|
|
$conditions = str_replace(array($start, $end), '', $conditions);
|
2010-01-29 15:27:40 +00:00
|
|
|
$conditions = preg_replace_callback('/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_' . $start . $end . ']*\\.[a-z0-9_' . $start . $end . ']*)/i', array(&$this, '__quoteMatchedField'), $conditions);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-01-29 15:27:40 +00:00
|
|
|
if ($conditions !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $conditions;
|
|
|
|
}
|
|
|
|
return $original;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-01-29 15:27:40 +00:00
|
|
|
/**
|
2010-03-25 20:35:45 +00:00
|
|
|
* Auxiliary function to quote matches `Model.fields` from a preg_replace_callback call
|
2010-01-29 15:27:40 +00:00
|
|
|
*
|
|
|
|
* @param string matched string
|
|
|
|
* @return string quoted strig
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __quoteMatchedField($match) {
|
|
|
|
if (is_numeric($match[0])) {
|
|
|
|
return $match[0];
|
|
|
|
}
|
|
|
|
return $this->name($match[0]);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a limit statement in the correct format for the particular database.
|
|
|
|
*
|
|
|
|
* @param integer $limit Limit of results returned
|
|
|
|
* @param integer $offset Offset from which to start results
|
|
|
|
* @return string SQL limit/offset statement
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function limit($limit, $offset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($limit) {
|
|
|
|
$rt = '';
|
2011-02-25 04:02:37 +00:00
|
|
|
if (!strpos(strtolower($limit), 'limit')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$rt = ' LIMIT';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset) {
|
|
|
|
$rt .= ' ' . $offset . ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
$rt .= ' ' . $limit;
|
|
|
|
return $rt;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an ORDER BY clause as a string.
|
|
|
|
*
|
|
|
|
* @param string $key Field reference, as a key (i.e. Post.title)
|
|
|
|
* @param string $direction Direction (ASC or DESC)
|
2009-12-06 06:40:33 +00:00
|
|
|
* @param object $model model reference (used to look for virtual field)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string ORDER BY clause
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function order($keys, $direction = 'ASC', $model = null) {
|
2009-12-06 05:58:21 +00:00
|
|
|
if (!is_array($keys)) {
|
|
|
|
$keys = array($keys);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-12-06 05:58:21 +00:00
|
|
|
$keys = array_filter($keys);
|
|
|
|
$result = array();
|
|
|
|
while (!empty($keys)) {
|
2009-12-17 05:15:42 +00:00
|
|
|
list($key, $dir) = each($keys);
|
2009-12-06 05:58:21 +00:00
|
|
|
array_shift($keys);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-12-06 05:58:21 +00:00
|
|
|
if (is_numeric($key)) {
|
|
|
|
$key = $dir;
|
|
|
|
$dir = $direction;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (is_string($key) && strpos($key, ',') !== false && !preg_match('/\(.+\,.+\)/', $key)) {
|
2009-12-06 05:58:21 +00:00
|
|
|
$key = array_map('trim', explode(',', $key));
|
|
|
|
}
|
|
|
|
if (is_array($key)) {
|
|
|
|
//Flatten the array
|
2009-12-17 05:15:42 +00:00
|
|
|
$key = array_reverse($key, true);
|
2009-12-06 05:58:21 +00:00
|
|
|
foreach ($key as $k => $v) {
|
|
|
|
if (is_numeric($k)) {
|
2009-12-17 05:15:42 +00:00
|
|
|
array_unshift($keys, $v);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-12-06 05:58:21 +00:00
|
|
|
$keys = array($k => $v) + $keys;
|
2008-12-18 20:49:25 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-12-06 05:58:21 +00:00
|
|
|
continue;
|
2010-05-19 08:54:51 +00:00
|
|
|
} elseif (is_object($key) && isset($key->type) && $key->type === 'expression') {
|
|
|
|
$result[] = $key->value;
|
|
|
|
continue;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-02-24 09:48:36 +00:00
|
|
|
if (preg_match('/\\x20(ASC|DESC).*/i', $key, $_dir)) {
|
2009-12-06 05:58:21 +00:00
|
|
|
$dir = $_dir[0];
|
2010-02-24 09:48:36 +00:00
|
|
|
$key = preg_replace('/\\x20(ASC|DESC).*/i', '', $key);
|
2009-12-06 05:58:21 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-31 00:30:58 +00:00
|
|
|
$key = trim($key);
|
|
|
|
|
|
|
|
if (is_object($model) && $model->isVirtualField($key)) {
|
|
|
|
$key = '(' . $this->__quoteFields($model->getVirtualField($key)) . ')';
|
|
|
|
}
|
|
|
|
|
2009-12-06 05:58:21 +00:00
|
|
|
if (strpos($key, '.')) {
|
2011-03-25 02:28:11 +00:00
|
|
|
$key = preg_replace_callback('/([a-zA-Z0-9_-]{1,})\\.([a-zA-Z0-9_-]{1,})/', array(&$this, '__quoteMatchedField'), $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
if (!preg_match('/\s/', $key) && strpos($key, '.') === false) {
|
2010-05-31 00:30:58 +00:00
|
|
|
$key = $this->name($key);
|
2009-12-06 05:58:21 +00:00
|
|
|
}
|
|
|
|
$key .= ' ' . trim($dir);
|
|
|
|
$result[] = $key;
|
|
|
|
}
|
|
|
|
if (!empty($result)) {
|
2009-12-17 05:15:42 +00:00
|
|
|
return ' ORDER BY ' . implode(', ', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-12-06 05:58:21 +00:00
|
|
|
return '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Create a GROUP BY SQL clause
|
|
|
|
*
|
|
|
|
* @param string $group Group By Condition
|
|
|
|
* @return mixed string condition or null
|
2008-05-31 12:36:38 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function group($group, $model = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($group) {
|
2010-01-11 16:36:27 +00:00
|
|
|
if (!is_array($group)) {
|
|
|
|
$group = array($group);
|
|
|
|
}
|
|
|
|
foreach($group as $index => $key) {
|
|
|
|
if ($model->isVirtualField($key)) {
|
|
|
|
$group[$index] = '(' . $model->getVirtualField($key) . ')';
|
2010-01-09 19:29:49 +00:00
|
|
|
}
|
2008-06-15 09:08:50 +00:00
|
|
|
}
|
2010-01-11 16:36:27 +00:00
|
|
|
$group = implode(', ', $group);
|
2008-05-30 11:40:08 +00:00
|
|
|
return ' GROUP BY ' . $this->__quoteFields($group);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-12-19 17:40:32 +00:00
|
|
|
* Disconnects database, kills the connection and says the connection is closed.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-12-19 17:40:32 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function close() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->disconnect();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks if the specified table contains any record matching specified SQL
|
|
|
|
*
|
|
|
|
* @param Model $model Model to search
|
|
|
|
* @param string $sql SQL WHERE clause (condition only, not the "WHERE" part)
|
|
|
|
* @return boolean True if the table has a matching record, else false
|
|
|
|
*/
|
2010-12-04 19:15:32 +00:00
|
|
|
public function hasAny($Model, $sql) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$sql = $this->conditions($sql);
|
2008-05-31 12:36:38 +00:00
|
|
|
$table = $this->fullTableName($Model);
|
2009-02-15 21:05:42 +00:00
|
|
|
$alias = $this->alias . $this->name($Model->alias);
|
|
|
|
$where = $sql ? "{$sql}" : ' WHERE 1 = 1';
|
|
|
|
$id = $Model->escapeField();
|
2008-05-31 12:36:38 +00:00
|
|
|
|
2009-02-15 21:05:42 +00:00
|
|
|
$out = $this->fetchRow("SELECT COUNT({$id}) {$this->alias}count FROM {$table} {$alias}{$where}");
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($out)) {
|
|
|
|
return $out[0]['count'];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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)")
|
|
|
|
* @return mixed An integer or string representing the length of the column
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function length($real) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!preg_match_all('/([\w\s]+)(?:\((\d+)(?:,(\d+))?\))?(\sunsigned)?(\szerofill)?/', $real, $result)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', "FIXME: Can't parse field: " . $real), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
$col = str_replace(array(')', 'unsigned'), '', $real);
|
|
|
|
$limit = null;
|
|
|
|
|
|
|
|
if (strpos($col, '(') !== false) {
|
|
|
|
list($col, $limit) = explode('(', $col);
|
|
|
|
}
|
2011-02-25 20:22:15 +00:00
|
|
|
if ($limit !== null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return intval($limit);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$types = array(
|
2008-05-31 12:36:38 +00:00
|
|
|
'int' => 1, 'tinyint' => 1, 'smallint' => 1, 'mediumint' => 1, 'integer' => 1, 'bigint' => 1
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
list($real, $type, $length, $offset, $sign, $zerofill) = $result;
|
|
|
|
$typeArr = $type;
|
|
|
|
$type = $type[0];
|
|
|
|
$length = $length[0];
|
|
|
|
$offset = $offset[0];
|
|
|
|
|
|
|
|
$isFloat = in_array($type, array('dec', 'decimal', 'float', 'numeric', 'double'));
|
|
|
|
if ($isFloat && $offset) {
|
2011-02-25 20:22:15 +00:00
|
|
|
return $length . ',' . $offset;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 20:22:15 +00:00
|
|
|
if (($real[0] == $type) && (count($real) === 1)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($types[$type])) {
|
|
|
|
$length += $types[$type];
|
|
|
|
if (!empty($sign)) {
|
|
|
|
$length--;
|
|
|
|
}
|
|
|
|
} elseif (in_array($type, array('enum', 'set'))) {
|
|
|
|
$length = 0;
|
|
|
|
foreach ($typeArr as $key => $enumValue) {
|
2011-02-25 20:22:15 +00:00
|
|
|
if ($key === 0) {
|
2008-05-30 11:40:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$tmpLength = strlen($enumValue);
|
|
|
|
if ($tmpLength > $length) {
|
|
|
|
$length = $tmpLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return intval($length);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Translates between PHP boolean values and Database (faked) boolean values
|
|
|
|
*
|
|
|
|
* @param mixed $data Value to be translated
|
2010-10-18 04:09:53 +00:00
|
|
|
* @return int Converted boolean value
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-02 02:37:27 +00:00
|
|
|
public function boolean($data, $quote = false) {
|
|
|
|
if ($quote) {
|
|
|
|
return !empty($data) ? '1' : '0';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-02 02:37:27 +00:00
|
|
|
return !empty($data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Inserts multiple values into a table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param string $fields
|
|
|
|
* @param array $values
|
2011-05-18 18:12:36 +00:00
|
|
|
* @param array $schema
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-18 18:12:36 +00:00
|
|
|
public function insertMulti($table, $fields, $values, $schema) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$table = $this->fullTableName($table);
|
2010-10-25 00:29:54 +00:00
|
|
|
$holder = implode(',', array_fill(0, count($fields), '?'));
|
|
|
|
$fields = implode(', ', array_map(array(&$this, 'name'), $fields));
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$count = count($values);
|
2010-10-25 00:29:54 +00:00
|
|
|
$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holder})";
|
|
|
|
$statement = $this->_connection->prepare($sql);
|
2010-11-17 03:44:48 +00:00
|
|
|
$this->begin();
|
2008-05-30 11:40:08 +00:00
|
|
|
for ($x = 0; $x < $count; $x++) {
|
2010-10-25 00:29:54 +00:00
|
|
|
$statement->execute($values[$x]);
|
|
|
|
$statement->closeCursor();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-11-17 03:44:48 +00:00
|
|
|
return $this->commit();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of the indexes in given datasource name.
|
|
|
|
*
|
|
|
|
* @param string $model Name of model to inspect
|
|
|
|
* @return array Fields in table. Keys are column and unique
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function index($model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generate a database-native schema for the given Schema object
|
|
|
|
*
|
|
|
|
* @param object $schema An instance of a subclass of CakeSchema
|
|
|
|
* @param string $tableName Optional. If specified only the table name given will be generated.
|
2009-02-04 05:00:59 +00:00
|
|
|
* Otherwise, all tables defined in the schema are generated.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function createSchema($schema, $tableName = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_a($schema, 'CakeSchema')) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Invalid schema object'), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$out = '';
|
|
|
|
|
|
|
|
foreach ($schema->tables as $curTable => $columns) {
|
|
|
|
if (!$tableName || $tableName == $curTable) {
|
2009-09-29 15:25:42 +00:00
|
|
|
$cols = $colList = $indexes = $tableParameters = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
$primary = null;
|
|
|
|
$table = $this->fullTableName($curTable);
|
|
|
|
|
|
|
|
foreach ($columns as $name => $col) {
|
|
|
|
if (is_string($col)) {
|
|
|
|
$col = array('type' => $col);
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
if (isset($col['key']) && $col['key'] === 'primary') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$primary = $name;
|
|
|
|
}
|
2009-09-29 15:25:42 +00:00
|
|
|
if ($name !== 'indexes' && $name !== 'tableParameters') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$col['name'] = $name;
|
|
|
|
if (!isset($col['type'])) {
|
|
|
|
$col['type'] = 'string';
|
|
|
|
}
|
|
|
|
$cols[] = $this->buildColumn($col);
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif ($name === 'indexes') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$indexes = array_merge($indexes, $this->buildIndex($col, $table));
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif ($name === 'tableParameters') {
|
2009-09-29 15:25:42 +00:00
|
|
|
$tableParameters = array_merge($tableParameters, $this->buildTableParameters($col, $table));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($indexes) && !empty($primary)) {
|
|
|
|
$col = array('PRIMARY' => array('column' => $primary, 'unique' => 1));
|
|
|
|
$indexes = array_merge($indexes, $this->buildIndex($col, $table));
|
|
|
|
}
|
|
|
|
$columns = $cols;
|
2009-09-29 15:25:42 +00:00
|
|
|
$out .= $this->renderStatement('schema', compact('table', 'columns', 'indexes', 'tableParameters')) . "\n\n";
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-06-27 05:14:52 +00:00
|
|
|
* Generate a alter syntax from CakeSchema::compare()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param unknown_type $schema
|
2009-12-19 18:06:32 +00:00
|
|
|
* @return boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function alterSchema($compare, $table = null) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generate a "drop table" statement for the given Schema object
|
|
|
|
*
|
2010-12-04 19:37:02 +00:00
|
|
|
* @param CakeSchema $schema An instance of a subclass of CakeSchema
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $table Optional. If specified only the table name given will be generated.
|
2009-02-04 05:00:59 +00:00
|
|
|
* Otherwise, all tables defined in the schema are generated.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-12-04 19:37:02 +00:00
|
|
|
public function dropSchema(CakeSchema $schema, $table = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = '';
|
|
|
|
|
|
|
|
foreach ($schema->tables as $curTable => $columns) {
|
|
|
|
if (!$table || $table == $curTable) {
|
|
|
|
$out .= 'DROP TABLE ' . $this->fullTableName($curTable) . ";\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generate a database-native column schema string
|
|
|
|
*
|
|
|
|
* @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
|
2009-02-04 05:00:59 +00:00
|
|
|
* where options can be 'default', 'length', or 'key'.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function buildColumn($column) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$name = $type = null;
|
|
|
|
extract(array_merge(array('null' => true), $column));
|
|
|
|
|
|
|
|
if (empty($name) || empty($type)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Column name or type not defined in schema'), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->columns[$type])) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$real = $this->columns[$type];
|
|
|
|
$out = $this->name($name) . ' ' . $real['name'];
|
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (isset($column['length'])) {
|
|
|
|
$length = $column['length'];
|
|
|
|
} elseif (isset($column['limit'])) {
|
|
|
|
$length = $column['limit'];
|
|
|
|
} elseif (isset($real['length'])) {
|
|
|
|
$length = $real['length'];
|
2011-02-25 23:18:12 +00:00
|
|
|
} elseif (isset($real['limit'])) {
|
2011-02-25 04:02:37 +00:00
|
|
|
$length = $real['limit'];
|
|
|
|
}
|
|
|
|
if (isset($length)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= '(' . $length . ')';
|
|
|
|
}
|
2008-09-04 15:29:48 +00:00
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (($column['type'] === 'integer' || $column['type'] === 'float' ) && isset($column['default']) && $column['default'] === '') {
|
2008-08-14 00:33:44 +00:00
|
|
|
$column['default'] = null;
|
|
|
|
}
|
2009-10-29 05:47:25 +00:00
|
|
|
$out = $this->_buildFieldParameters($out, $column, 'beforeDefault');
|
2009-09-29 15:25:42 +00:00
|
|
|
|
2011-02-25 04:02:37 +00:00
|
|
|
if (isset($column['key']) && $column['key'] === 'primary' && $type === 'integer') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= ' ' . $this->columns['primary_key']['name'];
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif (isset($column['key']) && $column['key'] === 'primary') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= ' NOT NULL';
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif (isset($column['default']) && isset($column['null']) && $column['null'] === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL';
|
|
|
|
} elseif (isset($column['default'])) {
|
|
|
|
$out .= ' DEFAULT ' . $this->value($column['default'], $type);
|
2010-01-21 21:01:34 +00:00
|
|
|
} elseif ($type !== 'timestamp' && !empty($column['null'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= ' DEFAULT NULL';
|
2010-01-21 21:01:34 +00:00
|
|
|
} elseif ($type === 'timestamp' && !empty($column['null'])) {
|
|
|
|
$out .= ' NULL';
|
2011-02-25 04:02:37 +00:00
|
|
|
} elseif (isset($column['null']) && $column['null'] === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= ' NOT NULL';
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
if ($type === 'timestamp' && isset($column['default']) && strtolower($column['default']) === 'current_timestamp') {
|
2010-01-21 21:01:34 +00:00
|
|
|
$out = str_replace(array("'CURRENT_TIMESTAMP'", "'current_timestamp'"), 'CURRENT_TIMESTAMP', $out);
|
|
|
|
}
|
2011-02-25 04:02:37 +00:00
|
|
|
return $this->_buildFieldParameters($out, $column, 'afterDefault');
|
2009-10-29 05:47:25 +00:00
|
|
|
}
|
2009-09-29 15:25:42 +00:00
|
|
|
|
2009-10-29 05:47:25 +00:00
|
|
|
/**
|
|
|
|
* Build the field parameters, in a position
|
|
|
|
*
|
|
|
|
* @param string $columnString The partially built column string
|
|
|
|
* @param array $columnData The array of column data.
|
|
|
|
* @param string $position The position type to use. 'beforeDefault' or 'afterDefault' are common
|
|
|
|
* @return string a built column with the field parameters added.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function _buildFieldParameters($columnString, $columnData, $position) {
|
2009-09-29 15:25:42 +00:00
|
|
|
foreach ($this->fieldParameters as $paramName => $value) {
|
2009-10-29 05:47:25 +00:00
|
|
|
if (isset($columnData[$paramName]) && $value['position'] == $position) {
|
2009-10-29 06:03:21 +00:00
|
|
|
if (isset($value['options']) && !in_array($columnData[$paramName], $value['options'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-10-29 05:47:25 +00:00
|
|
|
$val = $columnData[$paramName];
|
2009-09-29 15:25:42 +00:00
|
|
|
if ($value['quote']) {
|
|
|
|
$val = $this->value($val);
|
|
|
|
}
|
2009-10-29 05:47:25 +00:00
|
|
|
$columnString .= ' ' . $value['value'] . $value['join'] . $val;
|
2009-09-29 15:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-29 05:47:25 +00:00
|
|
|
return $columnString;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Format indexes for create table
|
|
|
|
*
|
|
|
|
* @param array $indexes
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function buildIndex($indexes, $table = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$join = array();
|
|
|
|
foreach ($indexes as $name => $value) {
|
|
|
|
$out = '';
|
2011-02-25 04:02:37 +00:00
|
|
|
if ($name === 'PRIMARY') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= 'PRIMARY ';
|
|
|
|
$name = null;
|
|
|
|
} else {
|
|
|
|
if (!empty($value['unique'])) {
|
|
|
|
$out .= 'UNIQUE ';
|
|
|
|
}
|
2009-08-02 06:47:28 +00:00
|
|
|
$name = $this->startQuote . $name . $this->endQuote;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (is_array($value['column'])) {
|
2009-11-19 22:13:35 +00:00
|
|
|
$out .= 'KEY ' . $name . ' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-08-02 06:47:28 +00:00
|
|
|
$out .= 'KEY ' . $name . ' (' . $this->name($value['column']) . ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$join[] = $out;
|
|
|
|
}
|
|
|
|
return $join;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-09-29 15:25:42 +00:00
|
|
|
/**
|
|
|
|
* Read additional table parameters
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function readTableParameters($name) {
|
2009-09-29 15:25:42 +00:00
|
|
|
$parameters = array();
|
2011-02-25 01:51:43 +00:00
|
|
|
if (method_exists($this, 'listDetailedSources')) {
|
2009-09-29 15:25:42 +00:00
|
|
|
$currentTableDetails = $this->listDetailedSources($name);
|
|
|
|
foreach ($this->tableParameters as $paramName => $parameter) {
|
|
|
|
if (!empty($parameter['column']) && !empty($currentTableDetails[$parameter['column']])) {
|
|
|
|
$parameters[$paramName] = $currentTableDetails[$parameter['column']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format parameters for create table
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function buildTableParameters($parameters, $table = null) {
|
2009-09-29 15:25:42 +00:00
|
|
|
$result = array();
|
|
|
|
foreach ($parameters as $name => $value) {
|
|
|
|
if (isset($this->tableParameters[$name])) {
|
|
|
|
if ($this->tableParameters[$name]['quote']) {
|
|
|
|
$value = $this->value($value);
|
|
|
|
}
|
|
|
|
$result[] = $this->tableParameters[$name]['value'] . $this->tableParameters[$name]['join'] . $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Guesses the data type of an array
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function introspectType($value) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_array($value)) {
|
2011-02-25 04:02:37 +00:00
|
|
|
if (is_bool($value)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return 'boolean';
|
|
|
|
}
|
|
|
|
if (is_float($value) && floatval($value) === $value) {
|
|
|
|
return 'float';
|
|
|
|
}
|
|
|
|
if (is_int($value) && intval($value) === $value) {
|
|
|
|
return 'integer';
|
|
|
|
}
|
|
|
|
if (is_string($value) && strlen($value) > 255) {
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
return 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
$isAllFloat = $isAllInt = true;
|
|
|
|
$containsFloat = $containsInt = $containsString = false;
|
|
|
|
foreach ($value as $key => $valElement) {
|
|
|
|
$valElement = trim($valElement);
|
|
|
|
if (!is_float($valElement) && !preg_match('/^[\d]+\.[\d]+$/', $valElement)) {
|
|
|
|
$isAllFloat = false;
|
|
|
|
} else {
|
|
|
|
$containsFloat = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!is_int($valElement) && !preg_match('/^[\d]+$/', $valElement)) {
|
|
|
|
$isAllInt = false;
|
|
|
|
} else {
|
|
|
|
$containsInt = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$containsString = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isAllFloat) {
|
|
|
|
return 'float';
|
|
|
|
}
|
|
|
|
if ($isAllInt) {
|
|
|
|
return 'integer';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($containsInt && !$containsString) {
|
|
|
|
return 'integer';
|
|
|
|
}
|
|
|
|
return 'string';
|
|
|
|
}
|
2010-06-05 03:37:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a new key for the in memory sql query cache
|
|
|
|
*
|
|
|
|
* @param string $sql SQL query
|
|
|
|
* @param mixed $data result of $sql query
|
2010-10-17 15:21:54 +00:00
|
|
|
* @param array $params query params bound as values
|
2010-06-05 03:37:29 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-10-17 15:21:54 +00:00
|
|
|
protected function _writeQueryCache($sql, $data, $params = array()) {
|
|
|
|
if (preg_match('/^\s*select/i', $sql)) {
|
|
|
|
$this->_queryCache[$sql][serialize($params)] = $data;
|
2010-06-05 03:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the result for a sql query if it is already cached
|
|
|
|
*
|
|
|
|
* @param string $sql SQL query
|
2010-10-17 15:21:54 +00:00
|
|
|
* @param array $params query params bound as values
|
2010-06-05 03:37:29 +00:00
|
|
|
* @return mixed results for query if it is cached, false otherwise
|
|
|
|
*/
|
2010-10-17 15:21:54 +00:00
|
|
|
public function getQueryCache($sql, $params = array()) {
|
2010-06-05 03:37:29 +00:00
|
|
|
if (isset($this->_queryCache[$sql]) && preg_match('/^\s*select/i', $sql)) {
|
2010-10-17 15:21:54 +00:00
|
|
|
$serialized = serialize($params);
|
|
|
|
if (isset($this->_queryCache[$sql][$serialized])) {
|
|
|
|
return $this->_queryCache[$sql][$serialized];
|
|
|
|
}
|
2010-06-05 03:37:29 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|