2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Object representing a SQL query.
|
|
|
|
* The various parts of the SQL query can be manipulated individually.
|
2008-08-09 06:39:35 +02:00
|
|
|
*
|
|
|
|
* Caution: Only supports SELECT (default) and DELETE at the moment.
|
|
|
|
*
|
|
|
|
* @todo Add support for INSERT and UPDATE queries
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-13 00:28:47 +02:00
|
|
|
class SQLQuery {
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-05-07 07:11:10 +02:00
|
|
|
* An array of SELECT fields, keyed by an optional alias.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $select = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-07 07:11:10 +02:00
|
|
|
* An array of FROM clauses. The first one is just the table name.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $from = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-07 07:11:10 +02:00
|
|
|
* An array of WHERE clauses.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $where = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-07 07:11:10 +02:00
|
|
|
* An array of ORDER BY clauses, functions. Stores as an associative
|
2012-04-15 10:34:10 +02:00
|
|
|
* array of column / function to direction.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $orderby = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-07 07:11:10 +02:00
|
|
|
* An array of GROUP BY clauses.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $groupby = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of having clauses.
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $having = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-07 07:11:10 +02:00
|
|
|
* An array containing limit and offset keys for LIMIT clause.
|
|
|
|
* @var array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-05-07 07:07:30 +02:00
|
|
|
protected $limit = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is true DISTINCT will be added to the SQL.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $distinct = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is true, this statement will delete rather than select.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $delete = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* The logical connective used to join WHERE clauses. Defaults to AND.
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $connective = 'AND';
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2009-06-27 15:00:51 +02:00
|
|
|
/**
|
|
|
|
* Keep an internal register of find/replace pairs to execute when it's time to actually get the
|
|
|
|
* query SQL.
|
2012-05-03 09:34:16 +02:00
|
|
|
* @var array
|
2009-06-27 15:00:51 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
protected $replacementsOld = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep an internal register of find/replace pairs to execute when it's time to actually get the
|
|
|
|
* query SQL.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $replacementsNew = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Construct a new SQLQuery.
|
2012-05-07 07:07:30 +02:00
|
|
|
*
|
|
|
|
* @param array $select An array of SELECT fields.
|
|
|
|
* @param array $from An array of FROM clauses. The first one should be just the table name.
|
|
|
|
* @param array $where An array of WHERE clauses.
|
|
|
|
* @param array $orderby An array ORDER BY clause.
|
|
|
|
* @param array $groupby An array of GROUP BY clauses.
|
|
|
|
* @param array $having An array of HAVING clauses.
|
|
|
|
* @param array|string $limit A LIMIT clause or array with limit and offset keys
|
|
|
|
*/
|
|
|
|
function __construct($select = "*", $from = array(), $where = array(), $orderby = array(), $groupby = array(), $having = array(), $limit = array()) {
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->setSelect($select);
|
2012-05-07 07:07:30 +02:00
|
|
|
$this->setFrom($from);
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->setWhere($where);
|
|
|
|
$this->setOrderBy($orderby);
|
|
|
|
$this->setGroupBy($groupby);
|
|
|
|
$this->setHaving($having);
|
|
|
|
$this->setLimit($limit);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
function __get($field) {
|
|
|
|
if(strtolower($field) == 'select') Deprecation::notice('3.0', 'Please use getSlect() instead');
|
|
|
|
if(strtolower($field) == 'from') Deprecation::notice('3.0', 'Please use getFrom() instead');
|
|
|
|
if(strtolower($field) == 'groupby') Deprecation::notice('3.0', 'Please use getGroupBy() instead');
|
|
|
|
if(strtolower($field) == 'orderby') Deprecation::notice('3.0', 'Please use getOrderBy() instead');
|
|
|
|
if(strtolower($field) == 'having') Deprecation::notice('3.0', 'Please use getHaving() instead');
|
|
|
|
if(strtolower($field) == 'limit') Deprecation::notice('3.0', 'Please use getLimit() instead');
|
|
|
|
if(strtolower($field) == 'delete') Deprecation::notice('3.0', 'Please use getDelete() instead');
|
|
|
|
if(strtolower($field) == 'connective') Deprecation::notice('3.0', 'Please use getConnective() instead');
|
|
|
|
if(strtolower($field) == 'distinct') Deprecation::notice('3.0', 'Please use getDistinct() instead');
|
|
|
|
|
|
|
|
return $this->$field;
|
2012-05-01 06:42:14 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
function __set($field, $value) {
|
|
|
|
if(strtolower($field) == 'select') Deprecation::notice('3.0', 'Please use setSelect() or addSelect() instead');
|
|
|
|
if(strtolower($field) == 'from') Deprecation::notice('3.0', 'Please use setFrom() or addFrom() instead');
|
|
|
|
if(strtolower($field) == 'groupby') Deprecation::notice('3.0', 'Please use setGroupBy() or addGroupBy() instead');
|
|
|
|
if(strtolower($field) == 'orderby') Deprecation::notice('3.0', 'Please use setOrderBy() or addOrderBy() instead');
|
|
|
|
if(strtolower($field) == 'having') Deprecation::notice('3.0', 'Please use setHaving() or addHaving() instead');
|
|
|
|
if(strtolower($field) == 'limit') Deprecation::notice('3.0', 'Please use setLimit() instead');
|
|
|
|
if(strtolower($field) == 'delete') Deprecation::notice('3.0', 'Please use setDelete() instead');
|
|
|
|
if(strtolower($field) == 'connective') Deprecation::notice('3.0', 'Please use setConnective() instead');
|
|
|
|
if(strtolower($field) == 'distinct') Deprecation::notice('3.0', 'Please use setDistinct() instead');
|
|
|
|
|
|
|
|
return $this->$field = $value;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Set the list of columns to be selected by the query.
|
2008-08-09 06:38:44 +02:00
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* // pass fields to select as single parameter array
|
2012-05-11 01:39:06 +02:00
|
|
|
* $query->setSelect(array("Col1","Col2"))->setFrom("MyTable");
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
2008-08-09 06:38:44 +02:00
|
|
|
* // pass fields to select as multiple parameters
|
2012-05-11 01:39:06 +02:00
|
|
|
* $query->setSelect("Col1", "Col2")->setFrom("MyTable");
|
2008-08-09 06:38:44 +02:00
|
|
|
* </code>
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
|
|
|
* @param string|array $fields
|
|
|
|
* @param boolean $clear Clear existing select fields?
|
2008-08-09 06:38:44 +02:00
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function setSelect($fields) {
|
|
|
|
$this->select = array();
|
2008-08-09 06:38:44 +02:00
|
|
|
if (func_num_args() > 1) {
|
2012-05-01 07:44:31 +02:00
|
|
|
$fields = func_get_args();
|
|
|
|
} else if(!is_array($fields)) {
|
|
|
|
$fields = array($fields);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
return $this->addSelect($fields);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Add to the list of columns to be selected by the query.
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2012-05-03 09:34:16 +02:00
|
|
|
* <code>
|
|
|
|
* // pass fields to select as single parameter array
|
2012-05-11 01:39:06 +02:00
|
|
|
* $query->addSelect(array("Col1","Col2"))->setFrom("MyTable");
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
|
|
|
* // pass fields to select as multiple parameters
|
2012-05-11 01:39:06 +02:00
|
|
|
* $query->addSelect("Col1", "Col2")->setFrom("MyTable");
|
2012-05-03 09:34:16 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string|array $fields
|
|
|
|
* @param boolean $clear Clear existing select fields?
|
|
|
|
* @return SQLQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function addSelect($fields) {
|
2012-05-01 07:44:31 +02:00
|
|
|
if (func_num_args() > 1) {
|
|
|
|
$fields = func_get_args();
|
|
|
|
} else if(!is_array($fields)) {
|
|
|
|
$fields = array($fields);
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2012-05-01 07:44:31 +02:00
|
|
|
foreach($fields as $idx => $field) {
|
|
|
|
if(preg_match('/^(.*) +AS +"?([^"]*)"?/i', $field, $matches)) {
|
|
|
|
Deprecation::notice("3.0", "Use selectField() to specify column aliases");
|
|
|
|
$this->selectField($matches[1], $matches[2]);
|
|
|
|
} else {
|
|
|
|
$this->selectField($field, is_numeric($idx) ? null : $idx);
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
return $this;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
public function select($fields) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setSelect() or addSelect() instead!');
|
|
|
|
$this->setSelect($fields);
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
2012-05-07 06:57:55 +02:00
|
|
|
* Select an additional field.
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param $field String The field to select (escaped SQL statement)
|
|
|
|
* @param $alias String The alias of that field (escaped SQL statement).
|
|
|
|
* Defaults to the unquoted column name of the $field parameter.
|
2012-05-07 06:57:55 +02:00
|
|
|
* @return SQLQuery
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2012-05-01 06:42:14 +02:00
|
|
|
public function selectField($field, $alias = null) {
|
2012-05-01 07:44:31 +02:00
|
|
|
if(!$alias) {
|
|
|
|
if(preg_match('/"([^"]+)"$/', $field, $matches)) $alias = $matches[1];
|
|
|
|
else $alias = $field;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2012-05-01 07:44:31 +02:00
|
|
|
$this->select[$alias] = $field;
|
2012-05-07 06:57:55 +02:00
|
|
|
return $this;
|
2012-05-01 06:42:14 +02:00
|
|
|
}
|
2012-04-15 10:34:10 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-01 06:42:14 +02:00
|
|
|
* Return the SQL expression for the given field alias.
|
|
|
|
* Returns null if the given alias doesn't exist.
|
2012-05-16 11:29:49 +02:00
|
|
|
* See {@link selectField()} for details on alias generation.
|
|
|
|
*
|
|
|
|
* @param String $field
|
|
|
|
* @return String
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
|
|
|
public function expressionForField($field) {
|
2012-05-01 07:44:31 +02:00
|
|
|
return isset($this->select[$field]) ? $this->select[$field] : null;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Set table for the SELECT clause.
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @example $query->setFrom("MyTable"); // SELECT * FROM MyTable
|
2008-08-09 06:38:44 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $from Escaped SQL statement, usually an unquoted table name
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function setFrom($from) {
|
|
|
|
$this->from = array();
|
|
|
|
return $this->addFrom($from);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a table to the SELECT clause.
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @example $query->addFrom("MyTable"); // SELECT * FROM MyTable
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $from Escaped SQL statement, usually an unquoted table name
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function addFrom($from) {
|
|
|
|
if(is_array($from)) {
|
|
|
|
$this->from = array_merge($this->from, $from);
|
|
|
|
} elseif(!empty($from)) {
|
|
|
|
$this->from[str_replace(array('"','`'), '', $from)] = $from;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
public function from($from) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setFrom() or addFrom() instead!');
|
|
|
|
return $this->setFrom($from);
|
|
|
|
}
|
2008-08-09 06:38:44 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
2008-08-09 07:57:44 +02:00
|
|
|
* Add a LEFT JOIN criteria to the FROM clause.
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string $table Unquoted table name
|
|
|
|
* @param string $onPredicate The "ON" SQL fragment in a "LEFT JOIN ... AS ... ON ..." statement,
|
2010-10-19 00:58:43 +02:00
|
|
|
* Needs to be valid (quoted) SQL.
|
2012-05-03 09:34:16 +02:00
|
|
|
* @param string $tableAlias Optional alias which makes it easier to identify and replace joins later on
|
|
|
|
* @return SQLQuery
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function addLeftJoin($table, $onPredicate, $tableAlias = null) {
|
|
|
|
if(!$tableAlias) $tableAlias = $table;
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->from[$tableAlias] = array('type' => 'LEFT', 'table' => $table, 'filter' => array($onPredicate));
|
2008-08-09 08:53:26 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
public function leftjoin($table, $onPredicate, $tableAlias = null) {
|
|
|
|
Deprecation::notice('3.0', 'Please use addLeftJoin() instead!');
|
|
|
|
$this->addLeftJoin($table, $onPredicate, $tableAlias);
|
|
|
|
}
|
|
|
|
|
2008-08-09 08:53:26 +02:00
|
|
|
/**
|
|
|
|
* Add an INNER JOIN criteria to the FROM clause.
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string $table Unquoted table name
|
2012-05-03 09:34:16 +02:00
|
|
|
* @param string $onPredicate The "ON" SQL fragment in an "INNER JOIN ... AS ... ON ..." statement.
|
2010-10-19 00:58:43 +02:00
|
|
|
* Needs to be valid (quoted) SQL.
|
2012-05-03 09:34:16 +02:00
|
|
|
* @param string $tableAlias Optional alias which makes it easier to identify and replace joins later on
|
|
|
|
* @return SQLQuery
|
2008-08-09 08:53:26 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function addInnerJoin($table, $onPredicate, $tableAlias = null) {
|
|
|
|
if(!$tableAlias) $tableAlias = $table;
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->from[$tableAlias] = array('type' => 'INNER', 'table' => $table, 'filter' => array($onPredicate));
|
2008-08-09 07:57:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
public function innerjoin($table, $onPredicate, $tableAlias = null) {
|
|
|
|
Deprecation::notice('3.0', 'Please use addInnerJoin() instead!');
|
|
|
|
return $this->addInnerJoin($table, $onPredicate, $tableAlias);
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Add an additional filter (part of the ON clause) on a join.
|
|
|
|
*
|
|
|
|
* @param string $table Table to join on from the original join
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string $filter The "ON" SQL fragment (escaped)
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function addFilterToJoin($table, $filter) {
|
|
|
|
$this->from[$table]['filter'][] = $filter;
|
|
|
|
return $this;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Set the filter (part of the ON clause) on a join.
|
|
|
|
*
|
|
|
|
* @param string $table Table to join on from the original join
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string $filter The "ON" SQL fragment (escaped)
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function setJoinFilter($table, $filter) {
|
|
|
|
$this->from[$table]['filter'] = array($filter);
|
|
|
|
return $this;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
/**
|
|
|
|
* Returns true if we are already joining to the given table alias
|
2012-05-16 11:29:49 +02:00
|
|
|
*
|
|
|
|
* @return boolean
|
2008-08-11 01:29:30 +02:00
|
|
|
*/
|
|
|
|
public function isJoinedTo($tableAlias) {
|
|
|
|
return isset($this->from[$tableAlias]);
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Return a list of tables that this query is selecting from.
|
2012-05-16 11:29:49 +02:00
|
|
|
*
|
|
|
|
* @return array Unquoted table names
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function queriedTables() {
|
|
|
|
$tables = array();
|
2012-04-15 10:34:10 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($this->from as $key => $tableClause) {
|
2012-04-15 10:34:10 +02:00
|
|
|
if(is_array($tableClause)) $table = '"'.$tableClause['table'].'"';
|
2009-11-22 06:16:38 +01:00
|
|
|
else if(is_string($tableClause) && preg_match('/JOIN +("[^"]+") +(AS|ON) +/i', $tableClause, $matches)) $table = $matches[1];
|
|
|
|
else $table = $tableClause;
|
|
|
|
|
|
|
|
// Handle string replacements
|
|
|
|
if($this->replacementsOld) $table = str_replace($this->replacementsOld, $this->replacementsNew, $table);
|
|
|
|
|
|
|
|
$tables[] = preg_replace('/^"|"$/','',$table);
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
return $tables;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
/**
|
|
|
|
* Set distinct property.
|
|
|
|
* @param boolean $value
|
|
|
|
*/
|
|
|
|
public function setDistinct($value) {
|
|
|
|
$this->distinct = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the distinct property.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getDistinct() {
|
|
|
|
return $this->distinct;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the delete property.
|
|
|
|
* @param boolean $value
|
|
|
|
*/
|
|
|
|
public function setDelete($value) {
|
|
|
|
$this->delete = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the delete property.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getDelete() {
|
|
|
|
return $this->delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the connective property.
|
|
|
|
* @param boolean $value
|
|
|
|
*/
|
|
|
|
public function setConnective($value) {
|
|
|
|
$this->connective = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the connective property.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getConnective() {
|
|
|
|
return $this->connective;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-07 07:07:30 +02:00
|
|
|
* Get the limit property.
|
|
|
|
* @return array
|
2012-05-03 09:34:16 +02:00
|
|
|
*/
|
|
|
|
public function getLimit() {
|
|
|
|
return $this->limit;
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
/**
|
|
|
|
* Pass LIMIT clause either as SQL snippet or in array format.
|
2009-11-22 04:40:32 +01:00
|
|
|
* Internally, limit will always be stored as a map containing the keys 'start' and 'limit'
|
2008-08-09 07:57:44 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param int|string|array $limit If passed as a string or array, assumes SQL escaped data.
|
|
|
|
* @param int $offset
|
2012-06-29 09:40:28 +02:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*
|
2008-08-09 07:57:44 +02:00
|
|
|
* @return SQLQuery This instance
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function setLimit($limit, $offset = 0) {
|
2012-06-29 09:40:28 +02:00
|
|
|
if((is_numeric($limit) && $limit < 0) || (is_numeric($offset) && $offset < 0)) {
|
|
|
|
throw new InvalidArgumentException("SQLQuery::setLimit() only takes positive values");
|
|
|
|
}
|
|
|
|
|
2009-11-22 04:40:32 +01:00
|
|
|
if($limit && is_numeric($limit)) {
|
|
|
|
$this->limit = array(
|
2012-03-09 02:02:37 +01:00
|
|
|
'start' => $offset,
|
2009-11-22 04:40:32 +01:00
|
|
|
'limit' => $limit,
|
|
|
|
);
|
|
|
|
} else if($limit && is_string($limit)) {
|
2012-06-29 09:40:28 +02:00
|
|
|
if(strpos($limit, ',') !== false) {
|
|
|
|
list($start, $innerLimit) = explode(',', $limit, 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list($innerLimit, $start) = explode(' OFFSET ', strtoupper($limit), 2);
|
|
|
|
}
|
|
|
|
|
2009-11-22 04:40:32 +01:00
|
|
|
$this->limit = array(
|
|
|
|
'start' => trim($start),
|
|
|
|
'limit' => trim($innerLimit),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->limit = $limit;
|
|
|
|
}
|
2012-05-07 07:07:30 +02:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
public function limit($limit, $offset = 0) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setLimit() instead!');
|
|
|
|
return $this->setLimit($limit, $offset);
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Set ORDER BY clause either as SQL snippet or in array format.
|
2008-08-09 07:57:44 +02:00
|
|
|
*
|
2012-04-15 10:34:10 +02:00
|
|
|
* @example $sql->orderby("Column");
|
|
|
|
* @example $sql->orderby("Column DESC");
|
|
|
|
* @example $sql->orderby("Column DESC, ColumnTwo ASC");
|
|
|
|
* @example $sql->orderby("Column", "DESC");
|
|
|
|
* @example $sql->orderby(array("Column" => "ASC", "ColumnTwo" => "DESC"));
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $orderby Clauses to add (escaped SQL statement)
|
2012-05-03 09:34:16 +02:00
|
|
|
* @param string $dir Sort direction, ASC or DESC
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
|
|
|
* @return SQLQuery
|
2008-08-09 07:57:44 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function setOrderBy($clauses = null, $direction = null) {
|
|
|
|
$this->orderby = array();
|
|
|
|
return $this->addOrderBy($clauses, $direction);
|
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
/**
|
|
|
|
* Add ORDER BY clause either as SQL snippet or in array format.
|
|
|
|
*
|
|
|
|
* @example $sql->orderby("Column");
|
|
|
|
* @example $sql->orderby("Column DESC");
|
|
|
|
* @example $sql->orderby("Column DESC, ColumnTwo ASC");
|
|
|
|
* @example $sql->orderby("Column", "DESC");
|
|
|
|
* @example $sql->orderby(array("Column" => "ASC", "ColumnTwo" => "DESC"));
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $orderby Clauses to add (escaped SQL statements)
|
2012-05-03 09:34:16 +02:00
|
|
|
* @param string $dir Sort direction, ASC or DESC
|
|
|
|
*
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function addOrderBy($clauses = null, $direction = null) {
|
2012-04-15 10:34:10 +02:00
|
|
|
if(!$clauses) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_string($clauses)) {
|
2012-05-03 09:34:16 +02:00
|
|
|
if(strpos($clauses, "(") !== false) {
|
2012-04-15 10:34:10 +02:00
|
|
|
$sort = preg_split("/,(?![^()]*+\\))/", $clauses);
|
2012-05-03 09:34:16 +02:00
|
|
|
} else {
|
2012-04-15 10:34:10 +02:00
|
|
|
$sort = explode(",", $clauses);
|
|
|
|
}
|
2012-05-01 07:09:57 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
$clauses = array();
|
|
|
|
|
|
|
|
foreach($sort as $clause) {
|
2012-05-01 07:09:57 +02:00
|
|
|
list($column, $direction) = $this->getDirectionFromString($clause, $direction);
|
|
|
|
$clauses[$column] = $direction;
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
if(is_array($clauses)) {
|
|
|
|
foreach($clauses as $key => $value) {
|
|
|
|
if(!is_numeric($key)) {
|
|
|
|
$column = trim($key);
|
2012-05-01 07:09:57 +02:00
|
|
|
$columnDir = strtoupper(trim($value));
|
2012-05-03 09:34:16 +02:00
|
|
|
} else {
|
2012-05-01 07:09:57 +02:00
|
|
|
list($column, $columnDir) = $this->getDirectionFromString($value);
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2012-05-01 07:09:57 +02:00
|
|
|
$this->orderby[$column] = $columnDir;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
} else {
|
2012-04-15 10:34:10 +02:00
|
|
|
user_error('SQLQuery::orderby() incorrect format for $orderby', E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If sort contains a function call, let's move the sort clause into a
|
|
|
|
// separate selected field.
|
|
|
|
//
|
|
|
|
// Some versions of MySQL choke if you have a group function referenced
|
|
|
|
// directly in the ORDER BY
|
|
|
|
if($this->orderby) {
|
|
|
|
$i = 0;
|
|
|
|
foreach($this->orderby as $clause => $dir) {
|
2012-05-01 07:09:57 +02:00
|
|
|
// Function calls and multi-word columns like "CASE WHEN ..."
|
|
|
|
if(strpos($clause, '(') !== false || strpos($clause, " ") !== false ) {
|
2012-04-15 10:34:10 +02:00
|
|
|
// remove the old orderby
|
|
|
|
unset($this->orderby[$clause]);
|
|
|
|
|
|
|
|
$clause = trim($clause);
|
|
|
|
$column = "_SortColumn{$i}";
|
2012-05-03 02:02:21 +02:00
|
|
|
|
2012-05-01 07:44:31 +02:00
|
|
|
$this->selectField($clause, $column);
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->addOrderBy('"' . $column . '"', $dir);
|
2012-04-15 10:34:10 +02:00
|
|
|
$i++;
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
public function orderby($clauses = null, $direction = null) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setOrderBy() instead!');
|
|
|
|
return $this->setOrderBy($clauses, $direction);
|
|
|
|
}
|
|
|
|
|
2012-05-01 07:09:57 +02:00
|
|
|
/**
|
|
|
|
* Extract the direction part of a single-column order by clause.
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param String
|
|
|
|
* @param String
|
|
|
|
* @return Array A two element array: array($column, $direction)
|
2012-05-01 07:09:57 +02:00
|
|
|
*/
|
|
|
|
private function getDirectionFromString($value, $defaultDirection = null) {
|
|
|
|
if(preg_match('/^(.*)(asc|desc)$/i', $value, $matches)) {
|
|
|
|
$column = trim($matches[1]);
|
|
|
|
$direction = strtoupper($matches[2]);
|
|
|
|
} else {
|
|
|
|
$column = $value;
|
|
|
|
$direction = $defaultDirection ? $defaultDirection : "ASC";
|
|
|
|
}
|
|
|
|
return array($column, $direction);
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
|
|
|
* Returns the current order by as array if not already. To handle legacy
|
2012-05-03 09:34:16 +02:00
|
|
|
* statements which are stored as strings. Without clauses and directions,
|
2012-04-15 10:34:10 +02:00
|
|
|
* convert the orderby clause to something readable.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getOrderBy() {
|
|
|
|
$orderby = $this->orderby;
|
2012-05-03 09:34:16 +02:00
|
|
|
if(!$orderby) $orderby = array();
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
if(!is_array($orderby)) {
|
|
|
|
// spilt by any commas not within brackets
|
2012-05-03 09:34:16 +02:00
|
|
|
$orderby = preg_split('/,(?![^()]*+\\))/', $orderby);
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
foreach($orderby as $k => $v) {
|
2012-05-03 09:34:16 +02:00
|
|
|
if(strpos($v, ' ') !== false) {
|
2012-04-15 10:34:10 +02:00
|
|
|
unset($orderby[$k]);
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$rule = explode(' ', trim($v));
|
2012-04-15 10:34:10 +02:00
|
|
|
$clause = $rule[0];
|
2012-05-03 09:34:16 +02:00
|
|
|
$dir = (isset($rule[1])) ? $rule[1] : 'ASC';
|
2012-04-15 10:34:10 +02:00
|
|
|
|
|
|
|
$orderby[$clause] = $dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $orderby;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverses the order by clause by replacing ASC or DESC references in the
|
2012-05-03 09:34:16 +02:00
|
|
|
* current order by with it's corollary.
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function reverseOrderBy() {
|
|
|
|
$order = $this->getOrderBy();
|
|
|
|
$this->orderby = array();
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
foreach($order as $clause => $dir) {
|
|
|
|
$dir = (strtoupper($dir) == 'DESC') ? 'ASC' : 'DESC';
|
|
|
|
$this->addOrderBy($clause, $dir);
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
/**
|
|
|
|
* Set a GROUP BY clause.
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $groupby Escaped SQL statement
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function setGroupBy($groupby) {
|
|
|
|
$this->groupby = array();
|
|
|
|
return $this->addGroupBy($groupby);
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
/**
|
|
|
|
* Add a GROUP BY clause.
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $groupby Escaped SQL statement
|
2008-08-09 07:57:44 +02:00
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function addGroupBy($groupby) {
|
2008-08-09 07:57:44 +02:00
|
|
|
if(is_array($groupby)) {
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->groupby = array_merge($this->groupby, $groupby);
|
2008-08-09 07:57:44 +02:00
|
|
|
} elseif(!empty($groupby)) {
|
|
|
|
$this->groupby[] = $groupby;
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
public function groupby($where) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setGroupBy() or addHaving() instead!');
|
|
|
|
return $this->setGroupBy($where);
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Set a HAVING clause.
|
2008-08-09 07:57:44 +02:00
|
|
|
*
|
|
|
|
* @param string|array $having
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function setHaving($having) {
|
|
|
|
$this->having = array();
|
|
|
|
return $this->addHaving($having);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a HAVING clause
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $having Escaped SQL statement
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function addHaving($having) {
|
2008-08-09 07:57:44 +02:00
|
|
|
if(is_array($having)) {
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->having = array_merge($this->having, $having);
|
2008-08-09 07:57:44 +02:00
|
|
|
} elseif(!empty($having)) {
|
|
|
|
$this->having[] = $having;
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
return $this;
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
public function having($having) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setHaving() or addHaving() instead!');
|
|
|
|
return $this->setHaving($having);
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Set a WHERE clause.
|
|
|
|
*
|
|
|
|
* There are two different ways of doing this:
|
|
|
|
*
|
2008-08-09 06:38:44 +02:00
|
|
|
* <code>
|
|
|
|
* // the entire predicate as a single string
|
|
|
|
* $query->where("Column = 'Value'");
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
|
|
|
* // multiple predicates as an array
|
|
|
|
* $query->where(array("Column = 'Value'", "Column != 'Value'"));
|
2008-08-09 06:38:44 +02:00
|
|
|
* </code>
|
2012-05-03 09:34:16 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $where Predicate(s) to set, as escaped SQL statements.
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function setWhere($where) {
|
|
|
|
$this->where = array();
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$args = func_get_args();
|
2012-05-03 09:34:16 +02:00
|
|
|
if(isset($args[1])) {
|
|
|
|
Deprecation::notice('3.0', 'Multiple arguments to where is deprecated. Pleas use where("Column = Something") syntax instead');
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
return $this->addWhere($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a WHERE predicate.
|
|
|
|
*
|
|
|
|
* There are two different ways of doing this:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* // the entire predicate as a single string
|
|
|
|
* $query->where("Column = 'Value'");
|
|
|
|
*
|
|
|
|
* // multiple predicates as an array
|
|
|
|
* $query->where(array("Column = 'Value'", "Column != 'Value'"));
|
|
|
|
* </code>
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string|array $where Predicate(s) to set, as escaped SQL statements.
|
2012-05-03 09:34:16 +02:00
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function addWhere($where) {
|
|
|
|
if(is_array($where)) {
|
|
|
|
$this->where = array_merge($this->where, $where);
|
|
|
|
} elseif(!empty($where)) {
|
|
|
|
$this->where[] = $where;
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2011-12-09 14:09:07 +01:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
public function where($where) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setWhere() or addWhere() instead!');
|
|
|
|
return $this->setWhere($where);
|
|
|
|
}
|
|
|
|
|
2012-06-22 04:34:06 +02:00
|
|
|
public function whereAny($where) {
|
|
|
|
Deprecation::notice('3.0', 'Please use setWhereAny() or setWhereAny() instead!');
|
|
|
|
return $this->setWhereAny($where);
|
|
|
|
}
|
|
|
|
|
2011-12-09 14:09:07 +01:00
|
|
|
/**
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param String|array $filters Predicate(s) to set, as escaped SQL statements.
|
2011-12-09 14:09:07 +01:00
|
|
|
*/
|
2012-06-22 04:34:06 +02:00
|
|
|
function setWhereAny($filters) {
|
2011-12-09 14:09:07 +01:00
|
|
|
if(is_string($filters)) $filters = func_get_args();
|
|
|
|
$clause = implode(" OR ", $filters);
|
2012-05-03 09:34:16 +02:00
|
|
|
return $this->setWhere($clause);
|
2011-12-09 14:09:07 +01:00
|
|
|
}
|
2012-06-22 04:34:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String|array $filters Predicate(s) to set, as escaped SQL statements.
|
|
|
|
*/
|
|
|
|
function addWhereAny($filters) {
|
|
|
|
if(is_string($filters)) $filters = func_get_args();
|
|
|
|
$clause = implode(" OR ", $filters);
|
|
|
|
return $this->addWhere($clause);
|
|
|
|
}
|
2011-12-09 14:09:07 +01:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Use the disjunctive operator 'OR' to join filter expressions in the WHERE clause.
|
|
|
|
*/
|
|
|
|
public function useDisjunction() {
|
|
|
|
$this->connective = 'OR';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use the conjunctive operator 'AND' to join filter expressions in the WHERE clause.
|
|
|
|
*/
|
|
|
|
public function useConjunction() {
|
|
|
|
$this->connective = 'AND';
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap the use of one table with another.
|
2012-05-16 11:29:49 +02:00
|
|
|
*
|
|
|
|
* @param string $old Name of the old table (unquoted, escaped)
|
|
|
|
* @param string $new Name of the new table (unquoted, escaped)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function renameTable($old, $new) {
|
|
|
|
$this->replaceText("`$old`", "`$new`");
|
2008-11-22 04:51:04 +01:00
|
|
|
$this->replaceText("\"$old\"", "\"$new\"");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap some text in the SQL query with another.
|
2012-05-16 11:29:49 +02:00
|
|
|
*
|
|
|
|
* @param string $old The old text (escaped)
|
|
|
|
* @param string $new The new text (escaped)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function replaceText($old, $new) {
|
2009-06-27 15:00:51 +02:00
|
|
|
$this->replacementsOld[] = $old;
|
|
|
|
$this->replacementsNew[] = $new;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-04-27 06:56:07 +02:00
|
|
|
public function getFilter() {
|
2012-05-15 21:29:43 +02:00
|
|
|
Deprecation::notice('3.0', 'Please use itemized filters in getWhere() instead of getFilter()');
|
|
|
|
return DB::getConn()->sqlWhereToString($this->getWhere(), $this->getConnective());
|
2012-04-27 06:56:07 +02:00
|
|
|
}
|
2012-05-01 07:44:31 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Return a list of FROM clauses used internally.
|
|
|
|
* @return array
|
2012-05-01 07:44:31 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function getFrom() {
|
|
|
|
return $this->from;
|
2012-05-01 06:42:14 +02:00
|
|
|
}
|
2012-04-27 06:56:07 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Return a list of HAVING clauses used internally.
|
|
|
|
* @return array
|
2008-08-09 06:06:52 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function getHaving() {
|
|
|
|
return $this->having;
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Return a list of GROUP BY clauses used internally.
|
|
|
|
* @return array
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function getGroupBy() {
|
|
|
|
return $this->groupby;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Return a list of WHERE clauses used internally.
|
|
|
|
* @return array
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function getWhere() {
|
|
|
|
return $this->where;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
2012-05-03 09:34:16 +02:00
|
|
|
* Return an itemised select list as a map, where keys are the aliases, and values are the column sources.
|
|
|
|
* Aliases will always be provided (if the alias is implicit, the alias value will be inferred), and won't be quoted.
|
|
|
|
* E.g., 'Title' => '"SiteTree"."Title"'.
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2012-05-03 09:34:16 +02:00
|
|
|
public function getSelect() {
|
|
|
|
return $this->select;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate the SQL statement for this query.
|
2008-08-09 06:38:44 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function sql() {
|
2012-04-15 10:34:10 +02:00
|
|
|
// TODO: Don't require this internal-state manipulate-and-preserve - let sqlQueryToString() handle the new syntax
|
|
|
|
$origFrom = $this->from;
|
|
|
|
|
|
|
|
// Build from clauses
|
|
|
|
foreach($this->from as $alias => $join) {
|
|
|
|
// $join can be something like this array structure
|
|
|
|
// array('type' => 'inner', 'table' => 'SiteTree', 'filter' => array("SiteTree.ID = 1", "Status = 'approved'"))
|
|
|
|
if(is_array($join)) {
|
|
|
|
if(is_string($join['filter'])) $filter = $join['filter'];
|
|
|
|
else if(sizeof($join['filter']) == 1) $filter = $join['filter'][0];
|
|
|
|
else $filter = "(" . implode(") AND (", $join['filter']) . ")";
|
2011-09-26 23:18:23 +02:00
|
|
|
|
2012-05-16 11:29:49 +02:00
|
|
|
$aliasClause = ($alias != $join['table']) ? " AS \"" . Convert::raw2sql($alias) . "\"" : "";
|
2012-06-30 12:37:58 +02:00
|
|
|
$this->from[$alias] = strtoupper($join['type']) . " JOIN \"" . $join['table'] . "\"$aliasClause ON $filter";
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-27 02:28:19 +02:00
|
|
|
|
2009-06-27 15:00:51 +02:00
|
|
|
$sql = DB::getConn()->sqlQueryToString($this);
|
2012-04-15 10:34:10 +02:00
|
|
|
|
2011-09-27 02:28:19 +02:00
|
|
|
if($this->replacementsOld) {
|
|
|
|
$sql = str_replace($this->replacementsOld, $this->replacementsNew, $sql);
|
|
|
|
}
|
2011-03-30 07:06:33 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
$this->from = $origFrom;
|
2011-03-30 07:06:33 +02:00
|
|
|
|
2011-09-27 02:28:19 +02:00
|
|
|
// The query was most likely just created and then exectued.
|
|
|
|
if($sql === 'SELECT *') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2009-06-27 15:00:51 +02:00
|
|
|
return $sql;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
|
|
|
* Return the generated SQL string for this query
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function __toString() {
|
2011-03-21 08:01:28 +01:00
|
|
|
try {
|
|
|
|
return $this->sql();
|
|
|
|
} catch(Exception $e) {
|
|
|
|
return "<sql query>";
|
|
|
|
}
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Execute this query.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_Query
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function execute() {
|
2009-09-17 02:24:36 +02:00
|
|
|
return DB::query($this->sql(), E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-27 07:16:44 +02:00
|
|
|
/**
|
|
|
|
* Checks whether this query is for a specific ID in a table
|
2009-01-10 12:35:50 +01:00
|
|
|
*
|
|
|
|
* @todo Doesn't work with combined statements (e.g. "Foo='bar' AND ID=5")
|
2008-08-27 07:16:44 +02:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function filtersOnID() {
|
2009-08-10 06:36:17 +02:00
|
|
|
$regexp = '/^(.*\.)?("|`)?ID("|`)?\s?=/';
|
|
|
|
|
|
|
|
// Sometimes the ID filter will be the 2nd element, if there's a ClasssName filter first.
|
|
|
|
if(isset($this->where[0]) && preg_match($regexp, $this->where[0])) return true;
|
|
|
|
if(isset($this->where[1]) && preg_match($regexp, $this->where[1])) return true;
|
|
|
|
|
|
|
|
return false;
|
2009-01-10 12:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether this query is filtering on a foreign key, ie finding a has_many relationship
|
|
|
|
*
|
|
|
|
* @todo Doesn't work with combined statements (e.g. "Foo='bar' AND ParentID=5")
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function filtersOnFK() {
|
|
|
|
return (
|
|
|
|
$this->where
|
|
|
|
&& preg_match('/^(.*\.)?("|`)?[a-zA-Z]+ID("|`)?\s?=/', $this->where[0])
|
2008-08-27 07:16:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/// VARIOUS TRANSFORMATIONS BELOW
|
|
|
|
|
2010-10-13 06:04:32 +02:00
|
|
|
/**
|
|
|
|
* Return the number of rows in this query if the limit were removed. Useful in paged data sets.
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-03-27 06:04:11 +02:00
|
|
|
function unlimitedRowCount($column = null) {
|
2010-04-13 04:05:45 +02:00
|
|
|
// we can't clear the select if we're relying on its output by a HAVING clause
|
|
|
|
if(count($this->having)) {
|
|
|
|
$records = $this->execute();
|
|
|
|
return $records->numRecords();
|
|
|
|
}
|
|
|
|
|
2010-10-13 06:00:14 +02:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->limit = null;
|
|
|
|
$clone->orderby = null;
|
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
// Choose a default column
|
|
|
|
if($column == null) {
|
|
|
|
if($this->groupby) {
|
2010-10-13 06:00:14 +02:00
|
|
|
$countQuery = new SQLQuery();
|
2012-05-01 07:44:31 +02:00
|
|
|
$countQuery->select("count(*)");
|
2010-10-19 02:51:53 +02:00
|
|
|
$countQuery->from = array('(' . $clone->sql() . ') all_distinct');
|
2010-10-13 06:00:14 +02:00
|
|
|
|
|
|
|
return $countQuery->execute()->value();
|
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
} else {
|
2012-05-11 01:39:06 +02:00
|
|
|
$clone->setSelect(array("count(*)"));
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2010-10-13 06:00:14 +02:00
|
|
|
} else {
|
2012-05-11 01:39:06 +02:00
|
|
|
$clone->setSelect(array("count($column)"));
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2010-10-13 06:00:14 +02:00
|
|
|
|
2012-05-11 01:39:06 +02:00
|
|
|
$clone->setGroupBy(array());;
|
2007-07-19 12:40:28 +02:00
|
|
|
return $clone->execute()->value();
|
|
|
|
}
|
2010-04-13 04:06:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this query can be sorted by the given field.
|
|
|
|
*/
|
|
|
|
function canSortBy($fieldName) {
|
2010-04-13 04:16:50 +02:00
|
|
|
$fieldName = preg_replace('/(\s+?)(A|DE)SC$/', '', $fieldName);
|
|
|
|
|
2012-05-01 07:44:31 +02:00
|
|
|
return isset($this->select[$fieldName]);
|
2010-04-13 04:06:12 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 04:42:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of rows in this query if the limit were removed. Useful in paged data sets.
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @todo Respect HAVING and GROUPBY, which can affect the result-count
|
|
|
|
*
|
|
|
|
* @param String $column Quoted, escaped column name
|
|
|
|
* @return int
|
2009-11-22 04:42:29 +01:00
|
|
|
*/
|
|
|
|
function count( $column = null) {
|
|
|
|
// Choose a default column
|
|
|
|
if($column == null) {
|
|
|
|
if($this->groupby) {
|
|
|
|
$column = 'DISTINCT ' . implode(", ", $this->groupby);
|
|
|
|
} else {
|
|
|
|
$column = '*';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->select = array("count($column)");
|
|
|
|
$clone->limit = null;
|
|
|
|
$clone->orderby = null;
|
|
|
|
$clone->groupby = null;
|
|
|
|
|
|
|
|
$count = $clone->execute()->value();
|
|
|
|
// If there's a limit set, then that limit is going to heavily affect the count
|
|
|
|
if($this->limit) {
|
|
|
|
if($count >= ($this->limit['start'] + $this->limit['limit']))
|
|
|
|
return $this->limit['limit'];
|
|
|
|
else
|
|
|
|
return max(0, $count - $this->limit['start']);
|
|
|
|
|
|
|
|
// Otherwise, the count is going to be the output of the SQL query
|
|
|
|
} else {
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2012-05-11 01:39:06 +02:00
|
|
|
/**
|
|
|
|
* Return a new SQLQuery that calls the given aggregate functions on this data.
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param $column An aggregate expression, such as 'MAX("Balance")', or a set of them (as an escaped SQL statement)
|
2012-05-11 01:39:06 +02:00
|
|
|
*/
|
|
|
|
function aggregate($column) {
|
2009-11-22 06:16:38 +01:00
|
|
|
if($this->groupby || $this->limit) {
|
2012-05-11 01:39:06 +02:00
|
|
|
throw new Exception("SQLQuery::aggregate() doesn't work with groupby or limit, yet");
|
|
|
|
}
|
2012-05-01 07:44:31 +02:00
|
|
|
|
|
|
|
$clone = clone $this;
|
2012-05-11 01:39:06 +02:00
|
|
|
$clone->setLimit(array());
|
|
|
|
$clone->setOrderBy(array());
|
|
|
|
$clone->setGroupBy(array());
|
|
|
|
$clone->setSelect($column);
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2012-05-01 07:44:31 +02:00
|
|
|
return $clone;
|
2012-05-11 01:39:06 +02:00
|
|
|
}
|
2009-11-22 04:42:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a query that returns only the first row of this query
|
|
|
|
*/
|
|
|
|
function firstRow() {
|
|
|
|
$query = clone $this;
|
|
|
|
$offset = $this->limit ? $this->limit['start'] : 0;
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setLimit(1, $offset);
|
2009-11-22 04:42:29 +01:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a query that returns only the last row of this query
|
|
|
|
*/
|
|
|
|
function lastRow() {
|
|
|
|
$query = clone $this;
|
|
|
|
$offset = $this->limit ? $this->limit['start'] : 0;
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setLimit(1, $this->count() + $offset - 1);
|
2009-11-22 04:42:29 +01:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|