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
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* An array of fields to select.
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-08-09 07:57:44 +02:00
|
|
|
public $select = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of join clauses. The first one is just the table name.
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-08-09 07:57:44 +02:00
|
|
|
public $from = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of filters.
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-08-09 07:57:44 +02:00
|
|
|
public $where = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An ORDER BY clause.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $orderby;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of fields to group by.
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-08-09 07:57:44 +02:00
|
|
|
public $groupby = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of having clauses.
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-08-09 07:57:44 +02:00
|
|
|
public $having = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A limit clause.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $limit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is true DISTINCT will be added to the SQL.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-08-09 07:57:44 +02:00
|
|
|
public $distinct = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is true, this statement will delete rather than select.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-08-09 07:57:44 +02:00
|
|
|
public $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
|
|
|
|
*/
|
2008-08-11 04:25:44 +02:00
|
|
|
public $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.
|
|
|
|
*/
|
|
|
|
private $replacementsOld = array(), $replacementsNew = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Construct a new SQLQuery.
|
2008-08-09 06:38:44 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param array $select An array of fields to select.
|
|
|
|
* @param array $from An array of join clauses. The first one should be just the table name.
|
|
|
|
* @param array $where An array of filters, to be inserted into the WHERE clause.
|
|
|
|
* @param string $orderby An ORDER BY clause.
|
|
|
|
* @param array $groupby An array of fields to group by.
|
|
|
|
* @param array $having An array of having clauses.
|
|
|
|
* @param string $limit A LIMIT clause.
|
2009-09-17 02:07:32 +02:00
|
|
|
*
|
|
|
|
* TODO: perhaps we can quote things here instead of requiring all the parameters to be quoted
|
|
|
|
* by this stage.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-08-09 06:38:44 +02:00
|
|
|
function __construct($select = "*", $from = array(), $where = "", $orderby = "", $groupby = "", $having = "", $limit = "") {
|
2008-08-09 07:57:44 +02:00
|
|
|
$this->select($select);
|
|
|
|
// @todo
|
2008-11-22 04:51:04 +01:00
|
|
|
$this->from = is_array($from) ? $from : array(str_replace(array('"','`'),'',$from) => $from);
|
2008-08-09 07:57:44 +02:00
|
|
|
$this->where($where);
|
|
|
|
$this->orderby($orderby);
|
|
|
|
$this->groupby($groupby);
|
|
|
|
$this->having($having);
|
|
|
|
$this->limit($limit);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 06:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify the list of columns to be selected by the query.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* // pass fields to select as single parameter array
|
|
|
|
* $query->select(array("Col1","Col2"))->from("MyTable");
|
|
|
|
*
|
|
|
|
* // pass fields to select as multiple parameters
|
|
|
|
* $query->select("Col1", "Col2")->from("MyTable");
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param mixed $fields
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function select($fields) {
|
|
|
|
if (func_num_args() > 1) {
|
|
|
|
$this->select = func_get_args();
|
|
|
|
} else {
|
|
|
|
$this->select = is_array($fields) ? $fields : array($fields);
|
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Add addition columns to the select clause
|
|
|
|
*/
|
|
|
|
public function selectMore($fields) {
|
|
|
|
if (func_num_args() > 1) $fields = func_get_args();
|
|
|
|
if(is_array($fields)) {
|
|
|
|
foreach($fields as $field) $this->select[] = $field;
|
|
|
|
} else {
|
|
|
|
$this->select[] = $fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the SQL expression for the given field
|
|
|
|
* @todo This should be refactored after $this->select is changed to make that easier
|
|
|
|
*/
|
|
|
|
public function expressionForField($field) {
|
|
|
|
foreach($this->select as $sel) {
|
|
|
|
if(preg_match('/AS +"?([^"]*)"?/i', $sel, $matches)) $selField = $matches[1];
|
|
|
|
else if(preg_match('/"([^"]*)"\."([^"]*)"/', $sel, $matches)) $selField = $matches[2];
|
|
|
|
else if(preg_match('/"?([^"]*)"?/', $sel, $matches)) $selField = $matches[2];
|
|
|
|
if($selField == $field) return $sel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
|
|
|
* Specify the target table to select from.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $query->from("MyTable"); // SELECT * FROM MyTable
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string $table
|
2008-08-09 07:57:44 +02:00
|
|
|
* @return SQLQuery This instance
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
|
|
|
public function from($table) {
|
2008-11-22 04:51:04 +01:00
|
|
|
$this->from[str_replace(array('"','`'),'',$table)] = $table;
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2010-10-19 00:58:43 +02:00
|
|
|
* @param String $table Table name (unquoted)
|
|
|
|
* @param String $onPredicate The "ON" SQL fragment in a "LEFT JOIN ... AS ... ON ..." statement.
|
|
|
|
* Needs to be valid (quoted) SQL.
|
|
|
|
* @param String $tableAlias Optional alias which makes it easier to identify and replace joins later on
|
2008-08-09 07:57:44 +02:00
|
|
|
* @return SQLQuery This instance
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
2010-10-19 00:58:43 +02:00
|
|
|
public function leftJoin($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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an INNER JOIN criteria to the FROM clause.
|
|
|
|
*
|
2010-10-19 00:58:43 +02:00
|
|
|
* @param String $table Table name (unquoted)
|
|
|
|
* @param String $onPredicate The "ON" SQL fragment in a "LEFT JOIN ... AS ... ON ..." statement.
|
|
|
|
* Needs to be valid (quoted) SQL.
|
|
|
|
* @param String $tableAlias Optional alias which makes it easier to identify and replace joins later on
|
2008-08-09 08:53:26 +02:00
|
|
|
* @return SQLQuery This instance
|
|
|
|
*/
|
2010-10-19 00:58:43 +02:00
|
|
|
public function innerJoin($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;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Add an additional filter (part of the ON clause) on a join
|
|
|
|
*/
|
|
|
|
public function addFilterToJoin($tableAlias, $filter) {
|
|
|
|
$this->from[$tableAlias]['filter'][] = $filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the existing filter (ON clause) on a join
|
|
|
|
*/
|
|
|
|
public function setJoinFilter($tableAlias, $filter) {
|
|
|
|
if(is_string($this->from[$tableAlias])) {Debug::message($tableAlias); Debug::dump($this->from);}
|
|
|
|
$this->from[$tableAlias]['filter'] = array($filter);
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
/**
|
|
|
|
* Returns true if we are already joining to the given table alias
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public function queriedTables() {
|
|
|
|
$tables = array();
|
|
|
|
foreach($this->from as $key => $tableClause) {
|
2011-03-30 07:06:33 +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);
|
|
|
|
}
|
|
|
|
return $tables;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param string|array $limit
|
|
|
|
* @return SQLQuery This instance
|
|
|
|
*/
|
|
|
|
public function limit($limit) {
|
2009-11-22 04:40:32 +01:00
|
|
|
if($limit && is_numeric($limit)) {
|
|
|
|
$this->limit = array(
|
|
|
|
'start' => 0,
|
|
|
|
'limit' => $limit,
|
|
|
|
);
|
|
|
|
} else if($limit && is_string($limit)) {
|
|
|
|
if(strpos($limit,',') !== false) list($start, $innerLimit) = explode(',', $limit, 2);
|
|
|
|
else list($innerLimit, $start) = explode(' OFFSET ', strtoupper($limit), 2);
|
|
|
|
$this->limit = array(
|
|
|
|
'start' => trim($start),
|
|
|
|
'limit' => trim($innerLimit),
|
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->limit = $limit;
|
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass ORDER BY clause either as SQL snippet or in array format.
|
|
|
|
*
|
|
|
|
* @todo Implement passing of multiple orderby pairs in nested array syntax,
|
|
|
|
* e.g. array(array('sort'=>'A','dir'=>'asc'),array('sort'=>'B'))
|
|
|
|
*
|
|
|
|
* @param string|array $orderby
|
|
|
|
* @return SQLQuery This instance
|
|
|
|
*/
|
|
|
|
public function orderby($orderby) {
|
|
|
|
// if passed as an array, assume two array values with column and direction (asc|desc)
|
|
|
|
if(is_array($orderby)) {
|
|
|
|
if(!array_key_exists('sort', $orderby)) user_error('SQLQuery::orderby(): Wrong format for $orderby array', E_USER_ERROR);
|
|
|
|
|
|
|
|
if(isset($orderby['sort']) && !empty($orderby['sort']) && isset($orderby['dir']) && !empty($orderby['dir'])) {
|
2008-11-22 04:51:04 +01:00
|
|
|
$combinedOrderby = "\"" . Convert::raw2sql($orderby['sort']) . "\" " . Convert::raw2sql(strtoupper($orderby['dir']));
|
2008-08-09 07:57:44 +02:00
|
|
|
} elseif(isset($orderby['sort']) && !empty($orderby['sort'])) {
|
2008-11-22 04:51:04 +01:00
|
|
|
$combinedOrderby = "\"" . Convert::raw2sql($orderby['sort']) . "\"";
|
2008-08-09 07:57:44 +02:00
|
|
|
} else {
|
|
|
|
$combinedOrderby = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$combinedOrderby = $orderby;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2009-09-17 02:07:32 +02:00
|
|
|
if($combinedOrderby && strpos($combinedOrderby,'(') !== false && strtoupper(trim($combinedOrderby)) != DB::getConn()->random()) {
|
2008-08-09 07:57:44 +02:00
|
|
|
// Sort can be "Col1 DESC|ASC, Col2 DESC|ASC", we need to handle that
|
|
|
|
$sortParts = explode(",", $combinedOrderby);
|
|
|
|
|
|
|
|
// If you have select if(X,A,B),C then the array will return 'if(X','A','B)','C'.
|
|
|
|
// Turn this into 'if(X,A,B)','C' by counting brackets
|
|
|
|
while(list($i,$sortPart) = each($sortParts)) {
|
|
|
|
while(substr_count($sortPart,'(') > substr_count($sortPart,')')) {
|
|
|
|
list($i,$nextSortPart) = each($sortParts);
|
|
|
|
if($i === null) break;
|
|
|
|
$sortPart .= ',' . $nextSortPart;
|
|
|
|
}
|
|
|
|
$lumpedSortParts[] = $sortPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($lumpedSortParts as $i => $sortPart) {
|
|
|
|
$sortPart = trim($sortPart);
|
|
|
|
if(substr(strtolower($sortPart),-5) == ' desc') {
|
2010-10-19 03:25:19 +02:00
|
|
|
$this->select[] = substr($sortPart,0,-5) . " AS \"_SortColumn{$i}\"";
|
|
|
|
$newSorts[] = "\"_SortColumn{$i}\" DESC";
|
2008-08-09 07:57:44 +02:00
|
|
|
} else if(substr(strtolower($sortPart),-4) == ' asc') {
|
2010-10-19 03:25:19 +02:00
|
|
|
$this->select[] = substr($sortPart,0,-4) . " AS \"_SortColumn{$i}\"";
|
|
|
|
$newSorts[] = "\"_SortColumn{$i}\" ASC";
|
2008-08-09 07:57:44 +02:00
|
|
|
} else {
|
2010-10-19 03:25:19 +02:00
|
|
|
$this->select[] = "$sortPart AS \"_SortColumn{$i}\"";
|
|
|
|
$newSorts[] = "\"_SortColumn{$i}\" ASC";
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$combinedOrderby = implode(", ", $newSorts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($combinedOrderby)) $this->orderby = $combinedOrderby;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a GROUP BY clause.
|
|
|
|
*
|
|
|
|
* @param string|array $groupby
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function groupby($groupby) {
|
|
|
|
if(is_array($groupby)) {
|
|
|
|
$this->groupby = array_merge($this->groupby, $groupby);
|
|
|
|
} elseif(!empty($groupby)) {
|
|
|
|
$this->groupby[] = $groupby;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a HAVING clause.
|
|
|
|
*
|
|
|
|
* @param string|array $having
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
|
|
|
public function having($having) {
|
|
|
|
if(is_array($having)) {
|
|
|
|
$this->having = array_merge($this->having, $having);
|
|
|
|
} elseif(!empty($having)) {
|
|
|
|
$this->having[] = $having;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
|
|
|
* Apply a predicate filter to the where clause.
|
|
|
|
*
|
|
|
|
* Accepts a variable length of arguments, which represent
|
|
|
|
* different ways of formatting a predicate in a where clause:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* // the entire predicate as a single string
|
|
|
|
* $query->where("Column = 'Value'");
|
|
|
|
*
|
|
|
|
* // an exact match predicate with a key value pair
|
|
|
|
* $query->where("Column", "Value");
|
|
|
|
*
|
|
|
|
* // a predicate with user defined operator
|
|
|
|
* $query->where("Column", "!=", "Value");
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function where() {
|
|
|
|
$args = func_get_args();
|
|
|
|
if (func_num_args() == 3) {
|
|
|
|
$filter = "{$args[0]} {$args[1]} '{$args[2]}'";
|
|
|
|
} elseif (func_num_args() == 2) {
|
|
|
|
$filter = "{$args[0]} = '{$args[1]}'";
|
|
|
|
} else {
|
|
|
|
$filter = $args[0];
|
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
|
|
|
|
if(is_array($filter)) {
|
|
|
|
$this->where = array_merge($this->where,$filter);
|
|
|
|
} elseif(!empty($filter)) {
|
|
|
|
$this->where[] = $filter;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @param string $old Name of the old table.
|
|
|
|
* @param string $new Name of the new table.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
* @param string $old The old text.
|
|
|
|
* @param string $new The new text.
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Return an SQL WHERE clause to filter a SELECT query.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getFilter() {
|
2008-08-09 06:38:44 +02:00
|
|
|
return ($this->where) ? implode(") {$this->connective} (" , $this->where) : '';
|
2008-08-09 06:06:52 +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() {
|
2011-03-30 07:06:33 +02:00
|
|
|
// TODO: Don't require this internal-state manipulate-and-preserve - let sqlQueryToString() handle the new syntax
|
|
|
|
$origFrom = $this->from;
|
2011-09-26 23:18:23 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// 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']) . ")";
|
|
|
|
|
|
|
|
$this->from[$alias] = strtoupper($join['type']) . " JOIN \"{$join['table']}\" AS \"$alias\" ON $filter";
|
|
|
|
}
|
|
|
|
}
|
2011-09-27 02:28:19 +02:00
|
|
|
|
2009-06-27 15:00:51 +02:00
|
|
|
$sql = DB::getConn()->sqlQueryToString($this);
|
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
|
|
|
|
|
|
|
$this->from = $origFrom;
|
|
|
|
|
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
|
|
|
|
*/
|
2009-01-05 07:19:48 +01: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();
|
|
|
|
$countQuery->select = array("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 {
|
2010-10-13 06:00:14 +02:00
|
|
|
$clone->select = array("count(*)");
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2010-10-13 06:00:14 +02:00
|
|
|
} else {
|
|
|
|
$clone->select = array("count($column)");
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2010-10-13 06:00:14 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$clone->groupby = null;
|
|
|
|
return $clone->execute()->value();
|
|
|
|
}
|
2010-04-13 04:06:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this query can be sorted by the given field.
|
|
|
|
* Note that the implementation of this method is a little crude at the moment, it wil return
|
|
|
|
* "false" more often that is strictly necessary.
|
|
|
|
*/
|
|
|
|
function canSortBy($fieldName) {
|
2010-04-13 04:16:50 +02:00
|
|
|
$fieldName = preg_replace('/(\s+?)(A|DE)SC$/', '', $fieldName);
|
|
|
|
|
2010-04-13 04:06:12 +02:00
|
|
|
$sql = $this->sql();
|
|
|
|
|
|
|
|
$selects = $this->select;
|
|
|
|
foreach($selects as $i => $sel) {
|
|
|
|
if (preg_match('/"(.*)"\."(.*)"/', $sel, $matches)) $selects[$i] = $matches[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
$SQL_fieldName = Convert::raw2sql($fieldName);
|
|
|
|
return (in_array($SQL_fieldName,$selects) || stripos($sql,"AS {$SQL_fieldName}"));
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
* TODO Respect HAVING and GROUPBY, which can affect the result-count
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new SQLQuery that calls the given aggregate functions on this data.
|
|
|
|
* @param $columns An aggregate expression, such as 'MAX("Balance")', or an array of them.
|
|
|
|
*/
|
|
|
|
function aggregate($columns) {
|
|
|
|
if(!is_array($columns)) $columns = array($columns);
|
|
|
|
|
|
|
|
if($this->groupby || $this->limit) {
|
|
|
|
throw new Exception("SQLQuery::aggregate() doesn't work with groupby or limit, yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->limit = null;
|
|
|
|
$clone->orderby = null;
|
|
|
|
$clone->groupby = null;
|
|
|
|
$clone->select = $columns;
|
|
|
|
|
|
|
|
return $clone;
|
|
|
|
}
|
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;
|
|
|
|
$query->limit(array('start' => $offset, 'limit' => 1));
|
|
|
|
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;
|
|
|
|
$query->limit(array('start' => $this->count() + $offset - 1, 'limit' => 1));
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|