silverstripe-framework/src/ORM/DataQuery_SubGroup.php

67 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\ORM;
use SilverStripe\ORM\Queries\SQLConditionGroup;
use SilverStripe\ORM\Queries\SQLSelect;
/**
* Represents a subgroup inside a WHERE clause in a {@link DataQuery}
*
* Stores the clauses for the subgroup inside a specific {@link SQLSelect} object.
* All non-where methods call their DataQuery versions, which uses the base
* query object.
*/
class DataQuery_SubGroup extends DataQuery implements SQLConditionGroup
{
2016-11-29 00:31:16 +01:00
/**
*
* @var SQLSelect
*/
protected $whereQuery;
2016-11-29 00:31:16 +01:00
public function __construct(DataQuery $base, $connective)
{
parent::__construct($base->dataClass);
$this->query = $base->query;
$this->whereQuery = new SQLSelect();
$this->whereQuery->setConnective($connective);
2016-11-29 00:31:16 +01:00
$base->where($this);
}
2016-11-29 00:31:16 +01:00
public function where($filter)
{
if ($filter) {
$this->whereQuery->addWhere($filter);
}
2016-11-29 00:31:16 +01:00
return $this;
}
2016-11-29 00:31:16 +01:00
public function whereAny($filter)
{
if ($filter) {
$this->whereQuery->addWhereAny($filter);
}
2016-11-29 00:31:16 +01:00
return $this;
}
2016-11-29 00:31:16 +01:00
public function conditionSQL(&$parameters)
{
$parameters = array();
2016-11-29 00:31:16 +01:00
// Ignore empty conditions
$where = $this->whereQuery->getWhere();
if (empty($where)) {
return null;
}
2016-11-29 00:31:16 +01:00
// Allow database to manage joining of conditions
$sql = DB::get_conn()->getQueryBuilder()->buildWhereFragment($this->whereQuery, $parameters);
return preg_replace('/^\s*WHERE\s*/i', '', $sql);
}
}