2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object representing a query of data from the DataObject's supporting database.
|
2014-12-03 21:20:39 +01:00
|
|
|
* Acts as a wrapper over {@link SQLQuery} and performs all of the query generation.
|
2012-04-15 10:34:10 +02:00
|
|
|
* Used extensively by {@link DataList}.
|
|
|
|
*
|
2012-09-20 13:28:54 +02:00
|
|
|
* Unlike DataList, modifiers on DataQuery modify the object rather than returning a clone.
|
|
|
|
* DataList is immutable, DataQuery is mutable.
|
|
|
|
*
|
2012-04-15 10:34:10 +02:00
|
|
|
* @subpackage model
|
2012-06-20 23:59:16 +02:00
|
|
|
* @package framework
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
class DataQuery {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
2013-05-20 12:18:07 +02:00
|
|
|
* @var string
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2009-11-22 06:16:38 +01:00
|
|
|
protected $dataClass;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
2014-12-03 21:20:39 +01:00
|
|
|
* @var SQLQuery
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2009-11-22 06:16:38 +01:00
|
|
|
protected $query;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-11-22 06:16:38 +01:00
|
|
|
protected $collidingFields = array();
|
2012-04-20 00:08:17 +02:00
|
|
|
|
|
|
|
private $queriedColumns = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
2017-08-29 01:40:20 +02:00
|
|
|
* @var boolean
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2009-11-22 06:16:38 +01:00
|
|
|
private $queryFinalised = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// TODO: replace subclass_access with this
|
|
|
|
protected $querySubclasses = true;
|
|
|
|
// TODO: replace restrictclasses with this
|
|
|
|
protected $filterByClassName = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Create a new DataQuery.
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string The name of the DataObject class that you wish to query
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($dataClass) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->dataClass = $dataClass;
|
|
|
|
$this->initialiseQuery();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Clone this object
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __clone() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->query = clone $this->query;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Return the {@link DataObject} class that is being queried.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataClass() {
|
2012-05-03 09:34:16 +02:00
|
|
|
return $this->dataClass;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-03 21:20:39 +01:00
|
|
|
* Return the {@link SQLQuery} object that represents the current query; note that it will
|
2009-11-22 06:16:38 +01:00
|
|
|
* be a clone of the object.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @return SQLQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function query() {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->getFinalisedQuery();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Remove a filter from the query
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array $fieldExpression The predicate of the condition to remove
|
|
|
|
* (ignoring parameters). The expression will be considered a match if it's
|
|
|
|
* contained within any other predicate.
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeFilterOn($fieldExpression) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$matched = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// If given a parameterised condition extract only the condition
|
|
|
|
if(is_array($fieldExpression)) {
|
|
|
|
reset($fieldExpression);
|
|
|
|
$fieldExpression = key($fieldExpression);
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
$where = $this->query->toAppropriateExpression()->getWhere();
|
2013-06-21 00:32:08 +02:00
|
|
|
// Iterate through each condition
|
|
|
|
foreach($where as $i => $condition) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Rewrite condition groups as plain conditions before comparison
|
|
|
|
if($condition instanceof SQLConditionGroup) {
|
|
|
|
$predicate = $condition->conditionSQL($parameters);
|
|
|
|
$condition = array($predicate => $parameters);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// As each condition is a single length array, do a single
|
|
|
|
// iteration to extract the predicate and parameters
|
|
|
|
foreach($condition as $predicate => $parameters) {
|
2014-12-03 21:20:39 +01:00
|
|
|
// @see SQLQuery::addWhere for why this is required here
|
2013-06-21 00:32:08 +02:00
|
|
|
if(strpos($predicate, $fieldExpression) !== false) {
|
|
|
|
unset($where[$i]);
|
|
|
|
$matched = true;
|
|
|
|
}
|
|
|
|
// Enforce single-item condition predicate => parameters structure
|
|
|
|
break;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
|
|
|
// set the entire where clause back, but clear the original one first
|
|
|
|
if($matched) {
|
|
|
|
$this->query->setWhere($where);
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Couldn't find $fieldExpression in the query filter.");
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-04-20 00:08:17 +02:00
|
|
|
* Set up the simplest initial query
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function initialiseQuery() {
|
2012-04-20 00:08:17 +02:00
|
|
|
// Get the tables to join to.
|
|
|
|
// Don't get any subclass tables - let lazy loading do that.
|
|
|
|
$tableClasses = ClassInfo::ancestry($this->dataClass, true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Error checking
|
|
|
|
if(!$tableClasses) {
|
2011-05-01 07:12:26 +02:00
|
|
|
if(!SS_ClassLoader::instance()->hasManifest()) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("DataObjects have been requested before the manifest is loaded. Please ensure you are not"
|
|
|
|
. " querying the database in _config.php.", E_USER_ERROR);
|
2009-11-22 06:16:38 +01:00
|
|
|
} else {
|
2013-01-23 20:41:37 +01:00
|
|
|
user_error("DataList::create Can't find data classes (classes linked to tables) for"
|
2012-09-26 23:34:00 +02:00
|
|
|
. " $this->dataClass. Please ensure you run dev/build after creating a new DataObject.",
|
|
|
|
E_USER_ERROR);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$baseClass = array_shift($tableClasses);
|
|
|
|
|
|
|
|
// Build our intial query
|
2014-12-03 21:20:39 +01:00
|
|
|
$this->query = new SQLQuery(array());
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->query->setDistinct(true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
if($sort = singleton($this->dataClass)->stat('default_sort')) {
|
|
|
|
$this->sort($sort);
|
|
|
|
}
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->query->setFrom("\"$baseClass\"");
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2012-06-11 12:40:47 +02:00
|
|
|
$obj = Injector::inst()->get($baseClass);
|
|
|
|
$obj->extend('augmentDataQueryCreation', $this->query, $this);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 01:40:20 +02:00
|
|
|
/**
|
|
|
|
* @param array $queriedColumns
|
|
|
|
* @return $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setQueriedColumns($queriedColumns) {
|
2012-04-20 00:08:17 +02:00
|
|
|
$this->queriedColumns = $queriedColumns;
|
2017-08-29 01:40:20 +02:00
|
|
|
return $this;
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Ensure that the query is ready to execute.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array|null $queriedColumns Any columns to filter the query by
|
2014-12-03 21:20:39 +01:00
|
|
|
* @return SQLQuery The finalised sql query
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFinalisedQuery($queriedColumns = null) {
|
2016-05-21 02:56:51 +02:00
|
|
|
if(!$queriedColumns) {
|
|
|
|
$queriedColumns = $this->queriedColumns;
|
|
|
|
}
|
2012-04-20 00:08:17 +02:00
|
|
|
if($queriedColumns) {
|
|
|
|
$queriedColumns = array_merge($queriedColumns, array('Created', 'LastEdited', 'ClassName'));
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
$query = clone $this->query;
|
2014-04-08 22:46:52 +02:00
|
|
|
$ancestorTables = ClassInfo::ancestry($this->dataClass, true);
|
2011-10-29 06:27:21 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
// Generate the list of tables to iterate over and the list of columns required
|
|
|
|
// by any existing where clauses. This second step is skipped if we're fetching
|
|
|
|
// the whole dataobject as any required columns will get selected regardless.
|
2012-04-20 00:08:17 +02:00
|
|
|
if($queriedColumns) {
|
2014-04-08 22:46:52 +02:00
|
|
|
// Specifying certain columns allows joining of child tables
|
2012-04-20 00:08:17 +02:00
|
|
|
$tableClasses = ClassInfo::dataClassesFor($this->dataClass);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-21 02:56:51 +02:00
|
|
|
// Ensure that any filtered columns are included in the selected columns
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach ($query->getWhereParameterised($parameters) as $where) {
|
2016-05-21 02:56:51 +02:00
|
|
|
// Check for any columns in the form '"Column" = ?' or '"Table"."Column"' = ?
|
|
|
|
if(preg_match_all(
|
|
|
|
'/(?:"(?<table>[^"]+)"\.)?"(?<column>[^"]+)"(?:[^\.]|$)/',
|
|
|
|
$where, $matches, PREG_SET_ORDER
|
|
|
|
)) {
|
|
|
|
foreach($matches as $match) {
|
|
|
|
$column = $match['column'];
|
|
|
|
if (!in_array($column, $queriedColumns)) {
|
|
|
|
$queriedColumns[] = $column;
|
|
|
|
}
|
|
|
|
}
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-08 22:46:52 +02:00
|
|
|
} else {
|
|
|
|
$tableClasses = $ancestorTables;
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
$tableNames = array_values($tableClasses);
|
2012-04-20 00:08:17 +02:00
|
|
|
$baseClass = $tableNames[0];
|
|
|
|
|
|
|
|
// Iterate over the tables and check what we need to select from them. If any selects are made (or the table is
|
|
|
|
// required for a select)
|
|
|
|
foreach($tableClasses as $tableClass) {
|
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
// Determine explicit columns to select
|
|
|
|
$selectColumns = null;
|
2012-04-20 00:08:17 +02:00
|
|
|
if ($queriedColumns) {
|
2014-04-08 22:46:52 +02:00
|
|
|
// Restrict queried columns to that on the selected table
|
2013-06-21 00:32:08 +02:00
|
|
|
$tableFields = DataObject::database_fields($tableClass, false);
|
2014-04-08 22:46:52 +02:00
|
|
|
$selectColumns = array_intersect($queriedColumns, array_keys($tableFields));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
// If this is a subclass without any explicitly requested columns, omit this from the query
|
|
|
|
if(!in_array($tableClass, $ancestorTables) && empty($selectColumns)) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
// Select necessary columns (unless an explicitly empty array)
|
|
|
|
if($selectColumns !== array()) {
|
2012-04-20 00:08:17 +02:00
|
|
|
$this->selectColumnsFromTable($query, $tableClass, $selectColumns);
|
|
|
|
}
|
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
// Join if not the base table
|
|
|
|
if($tableClass !== $baseClass) {
|
|
|
|
$query->addLeftJoin($tableClass, "\"$tableClass\".\"ID\" = \"$baseClass\".\"ID\"", $tableClass, 10);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Resolve colliding fields
|
|
|
|
if($this->collidingFields) {
|
|
|
|
foreach($this->collidingFields as $k => $collisions) {
|
|
|
|
$caseClauses = array();
|
|
|
|
foreach($collisions as $collision) {
|
|
|
|
if(preg_match('/^"([^"]+)"/', $collision, $matches)) {
|
|
|
|
$collisionBase = $matches[1];
|
2015-08-07 04:20:01 +02:00
|
|
|
if(class_exists($collisionBase)) {
|
|
|
|
$collisionClasses = ClassInfo::subclassesFor($collisionBase);
|
|
|
|
$collisionClasses = Convert::raw2sql($collisionClasses, true);
|
|
|
|
$caseClauses[] = "WHEN \"$baseClass\".\"ClassName\" IN ("
|
|
|
|
. implode(", ", $collisionClasses) . ") THEN $collision";
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
} else {
|
|
|
|
user_error("Bad collision item '$collision'", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
2012-05-01 06:42:14 +02:00
|
|
|
$query->selectField("CASE " . implode( " ", $caseClauses) . " ELSE NULL END", $k);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if($this->filterByClassName) {
|
|
|
|
// If querying the base class, don't bother filtering on class name
|
|
|
|
if($this->dataClass != $baseClass) {
|
|
|
|
// Get the ClassName values to filter to
|
|
|
|
$classNames = ClassInfo::subclassesFor($this->dataClass);
|
2012-03-19 03:27:52 +01:00
|
|
|
if(!$classNames) user_error("DataList::create() Can't find data sub-classes for '$callerClass'");
|
2013-06-21 00:32:08 +02:00
|
|
|
$classNamesPlaceholders = DB::placeholders($classNames);
|
|
|
|
$query->addWhere(array(
|
|
|
|
"\"$baseClass\".\"ClassName\" IN ($classNamesPlaceholders)" => $classNames
|
|
|
|
));
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-01 06:42:14 +02:00
|
|
|
$query->selectField("\"$baseClass\".\"ID\"", "ID");
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->selectField("
|
|
|
|
CASE WHEN \"$baseClass\".\"ClassName\" IS NOT NULL THEN \"$baseClass\".\"ClassName\"
|
|
|
|
ELSE ".Convert::raw2sql($baseClass, true)." END",
|
|
|
|
"RecordClassName"
|
|
|
|
);
|
2011-10-29 06:27:21 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// TODO: Versioned, Translatable, SiteTreeSubsites, etc, could probably be better implemented as subclasses
|
|
|
|
// of DataQuery
|
2012-06-11 12:40:47 +02:00
|
|
|
|
2012-08-02 14:06:45 +02:00
|
|
|
$obj = Injector::inst()->get($this->dataClass);
|
2012-06-11 12:40:47 +02:00
|
|
|
$obj->extend('augmentSQL', $query, $this);
|
2011-10-29 06:27:21 +02:00
|
|
|
|
|
|
|
$this->ensureSelectContainsOrderbyColumns($query);
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
return $query;
|
|
|
|
}
|
2011-10-29 06:27:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that if a query has an order by clause, those columns are present in the select.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-03 21:20:39 +01:00
|
|
|
* @param SQLQuery $query
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param array $originalSelect
|
2011-10-29 06:27:21 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2012-05-01 07:09:57 +02:00
|
|
|
protected function ensureSelectContainsOrderbyColumns($query, $originalSelect = array()) {
|
2011-10-29 06:27:21 +02:00
|
|
|
$tableClasses = ClassInfo::dataClassesFor($this->dataClass);
|
|
|
|
$baseClass = array_shift($tableClasses);
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
if($orderby = $query->getOrderBy()) {
|
2013-10-03 03:20:31 +02:00
|
|
|
$newOrderby = array();
|
2014-08-26 04:56:45 +02:00
|
|
|
$i = 0;
|
2012-04-15 10:34:10 +02:00
|
|
|
foreach($orderby as $k => $dir) {
|
2013-10-03 03:20:31 +02:00
|
|
|
$newOrderby[$k] = $dir;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
// don't touch functions in the ORDER BY or public function calls
|
2012-04-15 10:34:10 +02:00
|
|
|
// selected as fields
|
2012-05-01 07:09:57 +02:00
|
|
|
if(strpos($k, '(') !== false) continue;
|
2012-05-03 02:02:21 +02:00
|
|
|
|
|
|
|
$col = str_replace('"', '', trim($k));
|
|
|
|
$parts = explode('.', $col);
|
|
|
|
|
2012-05-01 07:09:57 +02:00
|
|
|
// Pull through SortColumn references from the originalSelect variables
|
2012-05-03 02:02:21 +02:00
|
|
|
if(preg_match('/_SortColumn/', $col)) {
|
2014-08-26 04:56:45 +02:00
|
|
|
if(isset($originalSelect[$col])) {
|
|
|
|
$query->selectField($originalSelect[$col], $col);
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
continue;
|
2012-05-01 07:09:57 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-29 06:27:21 +02:00
|
|
|
if(count($parts) == 1) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if(DataObject::has_own_table_database_field($baseClass, $parts[0])) {
|
2011-10-29 06:27:21 +02:00
|
|
|
$qualCol = "\"$baseClass\".\"{$parts[0]}\"";
|
|
|
|
} else {
|
|
|
|
$qualCol = "\"$parts[0]\"";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-03 03:20:31 +02:00
|
|
|
// remove original sort
|
|
|
|
unset($newOrderby[$k]);
|
2014-08-26 04:56:45 +02:00
|
|
|
|
2013-10-03 03:20:31 +02:00
|
|
|
// add new columns sort
|
|
|
|
$newOrderby[$qualCol] = $dir;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
// To-do: Remove this if block once SQLQuery::$select has been refactored to store getSelect()
|
2012-05-01 06:42:14 +02:00
|
|
|
// format internally; then this check can be part of selectField()
|
2012-05-03 09:34:16 +02:00
|
|
|
$selects = $query->getSelect();
|
|
|
|
if(!isset($selects[$col]) && !in_array($qualCol, $selects)) {
|
2012-05-01 06:42:14 +02:00
|
|
|
$query->selectField($qualCol);
|
2011-10-29 06:27:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$qualCol = '"' . implode('"."', $parts) . '"';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
if(!in_array($qualCol, $query->getSelect())) {
|
2014-08-26 04:56:45 +02:00
|
|
|
unset($newOrderby[$k]);
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2014-08-26 04:56:45 +02:00
|
|
|
$newOrderby["\"_SortColumn$i\""] = $dir;
|
|
|
|
$query->selectField($qualCol, "_SortColumn$i");
|
|
|
|
|
|
|
|
$i++;
|
2011-10-29 06:27:21 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-26 04:56:45 +02:00
|
|
|
|
2011-10-29 06:27:21 +02:00
|
|
|
}
|
|
|
|
|
2013-10-03 03:20:31 +02:00
|
|
|
$query->setOrderBy($newOrderby);
|
2011-10-29 06:27:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Execute the query and return the result as {@link Query} object.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @return SS_Query
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function execute() {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->getFinalisedQuery()->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return this query's SQL
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array $parameters Out variable for parameters required for this query
|
|
|
|
* @return string The resulting SQL query (may be paramaterised)
|
|
|
|
*/
|
|
|
|
public function sql(&$parameters = array()) {
|
|
|
|
return $this->getFinalisedQuery()->sql($parameters);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of records in this query.
|
|
|
|
* Note that this will issue a separate SELECT COUNT() query.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @return int
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function count() {
|
2012-05-19 02:55:49 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->getFinalisedQuery()->count("DISTINCT \"$baseClass\".\"ID\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the maximum value of the given field in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $field Unquoted database column name. Will be ANSI quoted
|
2013-06-21 00:32:08 +02:00
|
|
|
* automatically so must not contain double quotes.
|
2016-11-09 23:13:02 +01:00
|
|
|
* @return string
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-20 13:28:54 +02:00
|
|
|
public function max($field) {
|
2016-10-19 05:49:47 +02:00
|
|
|
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
2016-11-09 23:13:02 +01:00
|
|
|
if (!$table || $table === 'DataObject') {
|
|
|
|
return $this->aggregate("MAX(\"$field\")");
|
|
|
|
}
|
2016-10-19 05:49:47 +02:00
|
|
|
return $this->aggregate("MAX(\"$table\".\"$field\")");
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the minimum value of the given field in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $field Unquoted database column name. Will be ANSI quoted
|
2013-06-21 00:32:08 +02:00
|
|
|
* automatically so must not contain double quotes.
|
2016-11-09 23:13:02 +01:00
|
|
|
* @return string
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function min($field) {
|
2016-10-19 05:49:47 +02:00
|
|
|
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
2016-11-09 23:13:02 +01:00
|
|
|
if (!$table || $table === 'DataObject') {
|
|
|
|
return $this->aggregate("MIN(\"$field\")");
|
|
|
|
}
|
2016-10-19 05:49:47 +02:00
|
|
|
return $this->aggregate("MIN(\"$table\".\"$field\")");
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Return the average value of the given field in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $field Unquoted database column name. Will be ANSI quoted
|
2013-06-21 00:32:08 +02:00
|
|
|
* automatically so must not contain double quotes.
|
2016-11-09 23:13:02 +01:00
|
|
|
* @return string
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function avg($field) {
|
2016-10-19 05:49:47 +02:00
|
|
|
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
2016-11-09 23:13:02 +01:00
|
|
|
if (!$table || $table === 'DataObject') {
|
|
|
|
return $this->aggregate("AVG(\"$field\")");
|
|
|
|
}
|
2016-10-19 05:49:47 +02:00
|
|
|
return $this->aggregate("AVG(\"$table\".\"$field\")");
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the sum of the values of the given field in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $field Unquoted database column name. Will be ANSI quoted
|
2013-06-21 00:32:08 +02:00
|
|
|
* automatically so must not contain double quotes.
|
2016-11-09 23:13:02 +01:00
|
|
|
* @return string
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sum($field) {
|
2016-10-19 05:49:47 +02:00
|
|
|
$table = ClassInfo::table_for_object_field($this->dataClass, $field);
|
2016-11-09 23:13:02 +01:00
|
|
|
if (!$table || $table === 'DataObject') {
|
|
|
|
return $this->aggregate("SUM(\"$field\")");
|
|
|
|
}
|
2016-10-19 05:49:47 +02:00
|
|
|
return $this->aggregate("SUM(\"$table\".\"$field\")");
|
2012-05-29 00:03:22 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-29 00:03:22 +02:00
|
|
|
/**
|
|
|
|
* Runs a raw aggregate expression. Please handle escaping yourself
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @param string $expression
|
|
|
|
* @return string
|
2012-05-29 00:03:22 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function aggregate($expression) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->getFinalisedQuery()->aggregate($expression)->execute()->value();
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the first row that would be returned by this full DataQuery
|
|
|
|
* Note that this will issue a separate SELECT ... LIMIT 1 query.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @return SQLSelect
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function firstRow() {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->getFinalisedQuery()->firstRow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the last row that would be returned by this full DataQuery
|
|
|
|
* Note that this will issue a separate SELECT ... LIMIT query.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @return SQLSelect
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function lastRow() {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->getFinalisedQuery()->lastRow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the SELECT clause of the query with the columns from the given table
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @param SQLQuery $query
|
|
|
|
* @param string $tableClass
|
|
|
|
* @param array $columns
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2014-12-03 21:20:39 +01:00
|
|
|
protected function selectColumnsFromTable(SQLQuery &$query, $tableClass, $columns = null) {
|
2012-04-20 00:08:17 +02:00
|
|
|
// Add SQL for multi-value fields
|
2013-06-21 00:32:08 +02:00
|
|
|
$databaseFields = DataObject::database_fields($tableClass, false);
|
2012-04-20 00:08:17 +02:00
|
|
|
$compositeFields = DataObject::composite_fields($tableClass, false);
|
|
|
|
if($databaseFields) foreach($databaseFields as $k => $v) {
|
|
|
|
if((is_null($columns) || in_array($k, $columns)) && !isset($compositeFields[$k])) {
|
|
|
|
// Update $collidingFields if necessary
|
2012-05-01 06:42:14 +02:00
|
|
|
if($expressionForField = $query->expressionForField($k)) {
|
|
|
|
if(!isset($this->collidingFields[$k])) $this->collidingFields[$k] = array($expressionForField);
|
2012-04-20 00:08:17 +02:00
|
|
|
$this->collidingFields[$k][] = "\"$tableClass\".\"$k\"";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
} else {
|
2012-05-01 06:42:14 +02:00
|
|
|
$query->selectField("\"$tableClass\".\"$k\"", $k);
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($compositeFields) foreach($compositeFields as $k => $v) {
|
|
|
|
if((is_null($columns) || in_array($k, $columns)) && $v) {
|
2017-11-30 18:49:46 +01:00
|
|
|
$dbO = SS_Object::create_from_string($v, $k);
|
2018-01-26 08:46:50 +01:00
|
|
|
$dbO->setTable($tableClass);
|
2012-04-20 00:08:17 +02:00
|
|
|
$dbO->addToQuery($query);
|
|
|
|
}
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-29 16:58:32 +02:00
|
|
|
/**
|
2014-04-15 03:07:54 +02:00
|
|
|
* Append a GROUP BY clause to this query.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $groupby Escaped SQL statement
|
|
|
|
* @return $this
|
2013-05-29 16:58:32 +02:00
|
|
|
*/
|
|
|
|
public function groupby($groupby) {
|
|
|
|
$this->query->addGroupBy($groupby);
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2014-04-15 03:07:54 +02:00
|
|
|
* Append a HAVING clause to this query.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $having Escaped SQL statement
|
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function having($having) {
|
2013-05-29 16:58:32 +02:00
|
|
|
$this->query->addHaving($having);
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a disjunctive subgroup.
|
|
|
|
*
|
|
|
|
* That is a subgroup joined by OR
|
|
|
|
*
|
|
|
|
* @return DataQuery_SubGroup
|
|
|
|
*/
|
|
|
|
public function disjunctiveGroup() {
|
|
|
|
return new DataQuery_SubGroup($this, 'OR');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a conjunctive subgroup
|
|
|
|
*
|
|
|
|
* That is a subgroup joined by AND
|
|
|
|
*
|
|
|
|
* @return DataQuery_SubGroup
|
|
|
|
*/
|
|
|
|
public function conjunctiveGroup() {
|
|
|
|
return new DataQuery_SubGroup($this, 'AND');
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Adds a WHERE clause.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-03 21:20:39 +01:00
|
|
|
* @see SQLQuery::addWhere() for syntax examples, although DataQuery
|
|
|
|
* won't expand multiple arguments as SQLQuery does.
|
2012-05-16 11:29:49 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
|
|
|
* paramaterised queries
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function where($filter) {
|
2009-11-22 06:16:38 +01:00
|
|
|
if($filter) {
|
2012-09-20 13:28:54 +02:00
|
|
|
$this->query->addWhere($filter);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2011-12-09 14:09:07 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-15 03:07:54 +02:00
|
|
|
* Append a WHERE with OR.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-03 21:20:39 +01:00
|
|
|
* @see SQLQuery::addWhere() for syntax examples, although DataQuery
|
|
|
|
* won't expand multiple method arguments as SQLQuery does.
|
2011-12-09 14:09:07 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
|
|
|
* paramaterised queries
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2011-12-09 14:09:07 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function whereAny($filter) {
|
2011-12-09 14:09:07 +01:00
|
|
|
if($filter) {
|
2012-09-20 13:28:54 +02:00
|
|
|
$this->query->addWhereAny($filter);
|
2011-12-09 14:09:07 +01:00
|
|
|
}
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
2011-12-09 14:09:07 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Set the ORDER BY clause of this query
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2014-12-03 21:20:39 +01:00
|
|
|
* @see SQLQuery::orderby()
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $sort Column to sort on (escaped SQL statement)
|
|
|
|
* @param string $direction Direction ("ASC" or "DESC", escaped SQL statement)
|
|
|
|
* @param boolean $clear Clear existing values
|
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sort($sort = null, $direction = null, $clear = true) {
|
2012-05-03 09:34:16 +02:00
|
|
|
if($clear) {
|
2012-09-20 13:28:54 +02:00
|
|
|
$this->query->setOrderBy($sort, $direction);
|
2012-05-03 09:34:16 +02:00
|
|
|
} else {
|
2012-09-20 13:28:54 +02:00
|
|
|
$this->query->addOrderBy($sort, $direction);
|
2012-05-03 09:34:16 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
|
|
|
* Reverse order by clause
|
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function reverseSort() {
|
2012-09-20 13:28:54 +02:00
|
|
|
$this->query->reverseOrderBy();
|
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-05-16 11:29:49 +02:00
|
|
|
* Set the limit of this query.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function limit($limit, $offset = 0) {
|
2012-09-20 13:28:54 +02:00
|
|
|
$this->query->setLimit($limit, $offset);
|
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2014-09-03 08:53:47 +02:00
|
|
|
/**
|
|
|
|
* Set whether this query should be distinct or not.
|
|
|
|
*
|
|
|
|
* @param bool $value
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2014-09-03 08:53:47 +02:00
|
|
|
*/
|
|
|
|
public function distinct($value) {
|
|
|
|
$this->query->setDistinct($value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-05-16 11:29:49 +02:00
|
|
|
* Add an INNER JOIN clause to this query.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $table The unquoted table name to join to.
|
|
|
|
* @param string $onClause The filter for the join (escaped SQL statement)
|
|
|
|
* @param string $alias An optional alias name (unquoted)
|
2014-09-15 02:27:55 +02:00
|
|
|
* @param int $order A numerical index to control the order that joins are added to the query; lower order values
|
|
|
|
* will cause the query to appear first. The default is 20, and joins created automatically by the
|
|
|
|
* ORM have a value of 10.
|
|
|
|
* @param array $parameters Any additional parameters if the join is a parameterised subquery
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2014-09-15 02:27:55 +02:00
|
|
|
public function innerJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) {
|
2009-11-22 06:16:38 +01:00
|
|
|
if($table) {
|
2014-09-15 02:27:55 +02:00
|
|
|
$this->query->addInnerJoin($table, $onClause, $alias, $order, $parameters);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-16 11:29:49 +02:00
|
|
|
* Add a LEFT JOIN clause to this query.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-09-15 02:27:55 +02:00
|
|
|
* @param string $table The unquoted table to join to.
|
|
|
|
* @param string $onClause The filter for the join (escaped SQL statement).
|
|
|
|
* @param string $alias An optional alias name (unquoted)
|
|
|
|
* @param int $order A numerical index to control the order that joins are added to the query; lower order values
|
|
|
|
* will cause the query to appear first. The default is 20, and joins created automatically by the
|
|
|
|
* ORM have a value of 10.
|
|
|
|
* @param array $parameters Any additional parameters if the join is a parameterised subquery
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2014-09-15 02:27:55 +02:00
|
|
|
*/
|
|
|
|
public function leftJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) {
|
2009-11-22 06:16:38 +01:00
|
|
|
if($table) {
|
2014-09-15 02:27:55 +02:00
|
|
|
$this->query->addLeftJoin($table, $onClause, $alias, $order, $parameters);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2011-03-21 09:37:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse the relationship fields, and add the table
|
|
|
|
* mappings to the query object state. This has to be called
|
|
|
|
* in any overloaded {@link SearchFilter->apply()} methods manually.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string|array $relation The array/dot-syntax relation to follow
|
|
|
|
* @return string The model class of the related item
|
2011-03-21 09:37:55 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function applyRelation($relation) {
|
2012-12-08 12:20:20 +01:00
|
|
|
// NO-OP
|
|
|
|
if(!$relation) return $this->dataClass;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
if(is_string($relation)) $relation = explode(".", $relation);
|
|
|
|
|
|
|
|
$modelClass = $this->dataClass;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
foreach($relation as $rel) {
|
|
|
|
$model = singleton($modelClass);
|
2015-02-25 16:21:33 +01:00
|
|
|
if ($component = $model->hasOneComponent($rel)) {
|
2012-12-08 12:20:20 +01:00
|
|
|
if(!$this->query->isJoinedTo($component)) {
|
2015-03-02 10:52:39 +01:00
|
|
|
$foreignKey = $rel;
|
2014-11-10 17:32:58 +01:00
|
|
|
$realModelClass = ClassInfo::table_for_object_field($modelClass, "{$foreignKey}ID");
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->query->addLeftJoin($component,
|
2014-11-10 17:32:58 +01:00
|
|
|
"\"$component\".\"ID\" = \"{$realModelClass}\".\"{$foreignKey}ID\"");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* add join clause to the component's ancestry classes so that the search filter could search on
|
|
|
|
* its ancestor fields.
|
|
|
|
*/
|
|
|
|
$ancestry = ClassInfo::ancestry($component, true);
|
|
|
|
if(!empty($ancestry)){
|
|
|
|
$ancestry = array_reverse($ancestry);
|
|
|
|
foreach($ancestry as $ancestor){
|
|
|
|
if($ancestor != $component){
|
|
|
|
$this->query->addInnerJoin($ancestor, "\"$component\".\"ID\" = \"$ancestor\".\"ID\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$modelClass = $component;
|
|
|
|
|
2015-02-25 16:21:33 +01:00
|
|
|
} elseif ($component = $model->hasManyComponent($rel)) {
|
2012-12-08 12:20:20 +01:00
|
|
|
if(!$this->query->isJoinedTo($component)) {
|
|
|
|
$ancestry = $model->getClassAncestry();
|
|
|
|
$foreignKey = $model->getRemoteJoinField($rel);
|
|
|
|
$this->query->addLeftJoin($component,
|
|
|
|
"\"$component\".\"{$foreignKey}\" = \"{$ancestry[0]}\".\"ID\"");
|
|
|
|
/**
|
|
|
|
* add join clause to the component's ancestry classes so that the search filter could search on
|
|
|
|
* its ancestor fields.
|
|
|
|
*/
|
|
|
|
$ancestry = ClassInfo::ancestry($component, true);
|
|
|
|
if(!empty($ancestry)){
|
|
|
|
$ancestry = array_reverse($ancestry);
|
|
|
|
foreach($ancestry as $ancestor){
|
|
|
|
if($ancestor != $component){
|
|
|
|
$this->query->addInnerJoin($ancestor, "\"$component\".\"ID\" = \"$ancestor\".\"ID\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$modelClass = $component;
|
|
|
|
|
2015-02-25 16:21:33 +01:00
|
|
|
} elseif ($component = $model->manyManyComponent($rel)) {
|
2012-12-08 12:20:20 +01:00
|
|
|
list($parentClass, $componentClass, $parentField, $componentField, $relationTable) = $component;
|
|
|
|
$parentBaseClass = ClassInfo::baseDataClass($parentClass);
|
|
|
|
$componentBaseClass = ClassInfo::baseDataClass($componentClass);
|
2017-11-15 18:34:41 +01:00
|
|
|
$this->query->addLeftJoin($relationTable,
|
2012-12-08 12:20:20 +01:00
|
|
|
"\"$relationTable\".\"$parentField\" = \"$parentBaseClass\".\"ID\"");
|
2015-01-30 14:47:02 +01:00
|
|
|
if (!$this->query->isJoinedTo($componentBaseClass)) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->query->addLeftJoin($componentBaseClass,
|
|
|
|
"\"$relationTable\".\"$componentField\" = \"$componentBaseClass\".\"ID\"");
|
2015-01-30 14:47:02 +01:00
|
|
|
}
|
2016-05-10 18:02:37 +02:00
|
|
|
if(ClassInfo::hasTable($componentClass) && !$this->query->isJoinedTo($componentClass)) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->query->addLeftJoin($componentClass,
|
|
|
|
"\"$relationTable\".\"$componentField\" = \"$componentClass\".\"ID\"");
|
|
|
|
}
|
|
|
|
$modelClass = $componentClass;
|
|
|
|
|
|
|
|
}
|
2011-03-21 09:37:55 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-21 09:37:55 +01:00
|
|
|
return $modelClass;
|
2012-01-25 23:53:12 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 23:53:12 +01:00
|
|
|
/**
|
|
|
|
* Removes the result of query from this query.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-25 23:53:12 +01:00
|
|
|
* @param DataQuery $subtractQuery
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $field
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return $this
|
2012-01-25 23:53:12 +01:00
|
|
|
*/
|
|
|
|
public function subtract(DataQuery $subtractQuery, $field='ID') {
|
2012-12-17 05:15:26 +01:00
|
|
|
$fieldExpression = $subtractQuery->expressionForField($field);
|
|
|
|
$subSelect = $subtractQuery->getFinalisedQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$subSelect->setSelect(array());
|
2012-05-01 07:44:31 +02:00
|
|
|
$subSelect->selectField($fieldExpression, $field);
|
2012-08-23 02:35:20 +02:00
|
|
|
$subSelect->setOrderBy(null);
|
2013-06-21 00:32:08 +02:00
|
|
|
$subSelectSQL = $subSelect->sql($subSelectParameters);
|
|
|
|
$this->where(array($this->expressionForField($field)." NOT IN ($subSelectSQL)" => $subSelectParameters));
|
2012-05-01 06:42:14 +02:00
|
|
|
|
|
|
|
return $this;
|
2012-01-25 23:53:12 +01:00
|
|
|
}
|
2011-03-21 09:37:55 +01:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2017-03-23 12:34:02 +01:00
|
|
|
* Select only the given fields from the given table.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $table Unquoted table name (will be escaped automatically)
|
|
|
|
* @param array $fields Database column names (will be escaped automatically)
|
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function selectFromTable($table, $fields) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$fieldExpressions = array_map(function($item) use($table) {
|
2017-03-23 12:34:02 +01:00
|
|
|
return Convert::symbol2sql("{$table}.{$item}");
|
2013-06-21 00:32:08 +02:00
|
|
|
}, $fields);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->query->setSelect($fieldExpressions);
|
2012-05-01 06:42:14 +02:00
|
|
|
|
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 12:34:02 +01:00
|
|
|
/**
|
|
|
|
* Add the given fields from the given table to the select statement.
|
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $table Unquoted table name (will be escaped automatically)
|
|
|
|
* @param array $fields Database column names (will be escaped automatically)
|
|
|
|
* @return $this
|
2017-03-23 12:34:02 +01:00
|
|
|
*/
|
|
|
|
public function addSelectFromTable($table, $fields) {
|
|
|
|
$fieldExpressions = array_map(function($item) use($table) {
|
|
|
|
return Convert::symbol2sql("{$table}.{$item}");
|
|
|
|
}, $fields);
|
|
|
|
|
|
|
|
$this->query->addSelect($fieldExpressions);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Query the given field column from the database and return as an array.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-04-08 22:46:52 +02:00
|
|
|
* @param string $field See {@link expressionForField()}.
|
|
|
|
* @return array List of column values for the specified column
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function column($field = 'ID') {
|
2012-12-17 05:15:26 +01:00
|
|
|
$fieldExpression = $this->expressionForField($field);
|
2012-04-20 00:08:17 +02:00
|
|
|
$query = $this->getFinalisedQuery(array($field));
|
2012-05-03 09:34:16 +02:00
|
|
|
$originalSelect = $query->getSelect();
|
|
|
|
$query->setSelect(array());
|
2012-05-01 07:44:31 +02:00
|
|
|
$query->selectField($fieldExpression, $field);
|
2012-05-01 07:09:57 +02:00
|
|
|
$this->ensureSelectContainsOrderbyColumns($query, $originalSelect);
|
2011-10-29 06:27:21 +02:00
|
|
|
|
|
|
|
return $query->execute()->column($field);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-16 11:29:49 +02:00
|
|
|
/**
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $field Select statement identifier, either the unquoted column name,
|
2014-12-03 21:20:39 +01:00
|
|
|
* the full composite SQL statement, or the alias set through {@link SQLQuery->selectField()}.
|
2017-08-29 01:40:20 +02:00
|
|
|
* @return string|null The expression used to query this field via this DataQuery
|
2012-05-16 11:29:49 +02:00
|
|
|
*/
|
2012-12-17 05:15:26 +01:00
|
|
|
protected function expressionForField($field) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Prepare query object for selecting this field
|
|
|
|
$query = $this->getFinalisedQuery(array($field));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Allow query to define the expression for this field
|
|
|
|
$expression = $query->expressionForField($field);
|
|
|
|
if(!empty($expression)) return $expression;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Special case for ID, if not provided
|
|
|
|
if($field === 'ID') {
|
2009-11-22 06:16:38 +01:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2014-08-15 08:53:05 +02:00
|
|
|
return "\"$baseClass\".\"ID\"";
|
2012-05-03 09:34:16 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-16 11:29:49 +02:00
|
|
|
* Select the given field expressions.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-08-29 01:40:20 +02:00
|
|
|
* @param string $fieldExpression The field to select (escaped SQL statement)
|
|
|
|
* @param string $alias The alias of that field (escaped SQL statement)
|
|
|
|
*
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-05-01 06:42:14 +02:00
|
|
|
protected function selectField($fieldExpression, $alias = null) {
|
|
|
|
$this->query->selectField($fieldExpression, $alias);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//// QUERY PARAMS
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An arbitrary store of query parameters that can be used by decorators.
|
|
|
|
* @todo This will probably be made obsolete if we have subclasses of DataList and/or DataQuery.
|
|
|
|
*/
|
|
|
|
private $queryParams;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.
|
|
|
|
* It's expected that the $key will be namespaced, e.g, 'Versioned.stage' instead of just 'stage'.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setQueryParam($key, $value) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->queryParams[$key] = $value;
|
2017-08-29 01:40:20 +02:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.
|
2017-08-29 01:40:20 +02:00
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getQueryParam($key) {
|
2009-11-22 06:16:38 +01:00
|
|
|
if(isset($this->queryParams[$key])) return $this->queryParams[$key];
|
|
|
|
else return null;
|
|
|
|
}
|
2013-02-12 04:44:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all query parameters
|
|
|
|
* @return array query parameters array
|
|
|
|
*/
|
|
|
|
public function getQueryParams() {
|
|
|
|
return $this->queryParams;
|
|
|
|
}
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a subgroup inside a WHERE clause in a {@link DataQuery}
|
|
|
|
*
|
2015-02-27 01:10:32 +01:00
|
|
|
* Stores the clauses for the subgroup inside a specific {@link SQLQuery}
|
2013-05-20 12:18:07 +02:00
|
|
|
* object.
|
|
|
|
*
|
2012-09-20 13:28:54 +02:00
|
|
|
* All non-where methods call their DataQuery versions, which uses the base
|
|
|
|
* query object.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
|
|
|
* @package framework
|
2012-09-20 13:28:54 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
class DataQuery_SubGroup extends DataQuery implements SQLConditionGroup {
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var SQLQuery
|
|
|
|
*/
|
2012-09-20 13:28:54 +02:00
|
|
|
protected $whereQuery;
|
|
|
|
|
|
|
|
public function __construct(DataQuery $base, $connective) {
|
|
|
|
$this->dataClass = $base->dataClass;
|
|
|
|
$this->query = $base->query;
|
2014-12-03 21:20:39 +01:00
|
|
|
$this->whereQuery = new SQLQuery();
|
2012-09-20 13:28:54 +02:00
|
|
|
$this->whereQuery->setConnective($connective);
|
|
|
|
|
|
|
|
$base->where($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function where($filter) {
|
|
|
|
if($filter) {
|
|
|
|
$this->whereQuery->addWhere($filter);
|
|
|
|
}
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-20 13:28:54 +02:00
|
|
|
public function whereAny($filter) {
|
|
|
|
if($filter) {
|
|
|
|
$this->whereQuery->addWhereAny($filter);
|
|
|
|
}
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2012-09-20 13:28:54 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
public function conditionSQL(&$parameters) {
|
|
|
|
$parameters = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Ignore empty conditions
|
2015-06-15 08:35:01 +02:00
|
|
|
$query = $this->whereQuery->toAppropriateExpression();
|
|
|
|
$where = $query->getWhere();
|
|
|
|
if(empty($where)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Allow database to manage joining of conditions
|
2015-06-15 08:35:01 +02:00
|
|
|
$sql = DB::get_conn()->getQueryBuilder()->buildWhereFragment($query, $parameters);
|
2013-06-21 00:32:08 +02:00
|
|
|
return preg_replace('/^\s*WHERE\s*/i', '', $sql);
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|