You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
337 lines
9.2 KiB
337 lines
9.2 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\elasticsearch;
|
||
|
|
||
11 years ago
|
use yii\base\InvalidParamException;
|
||
11 years ago
|
use yii\base\NotSupportedException;
|
||
|
|
||
|
/**
|
||
11 years ago
|
* QueryBuilder builds an elasticsearch query based on the specification given as a [[Query]] object.
|
||
11 years ago
|
*
|
||
|
*
|
||
11 years ago
|
* @author Carsten Brandt <mail@cebe.cc>
|
||
11 years ago
|
* @since 2.0
|
||
|
*/
|
||
|
class QueryBuilder extends \yii\base\Object
|
||
|
{
|
||
|
/**
|
||
|
* @var Connection the database connection.
|
||
|
*/
|
||
|
public $db;
|
||
|
|
||
|
/**
|
||
|
* Constructor.
|
||
|
* @param Connection $connection the database connection.
|
||
|
* @param array $config name-value pairs that will be used to initialize the object properties
|
||
|
*/
|
||
11 years ago
|
public function __construct($connection, $config = [])
|
||
11 years ago
|
{
|
||
|
$this->db = $connection;
|
||
|
parent::__construct($config);
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Generates query from a [[Query]] object.
|
||
|
* @param Query $query the [[Query]] object from which the query will be generated
|
||
11 years ago
|
* @return array the generated SQL statement (the first array element) and the corresponding
|
||
|
* parameters to be bound to the SQL statement (the second array element).
|
||
|
*/
|
||
|
public function build($query)
|
||
|
{
|
||
11 years ago
|
$parts = [];
|
||
11 years ago
|
|
||
11 years ago
|
if ($query->fields !== null) {
|
||
|
$parts['fields'] = (array) $query->fields;
|
||
11 years ago
|
}
|
||
11 years ago
|
if ($query->limit !== null && $query->limit >= 0) {
|
||
|
$parts['size'] = $query->limit;
|
||
11 years ago
|
}
|
||
11 years ago
|
if ($query->offset > 0) {
|
||
|
$parts['from'] = (int) $query->offset;
|
||
|
}
|
||
|
|
||
11 years ago
|
$filters = empty($query->filter) ? [] : [$query->filter];
|
||
|
$whereFilter = $this->buildCondition($query->where);
|
||
|
if (!empty($whereFilter)) {
|
||
|
$filters[] = $whereFilter;
|
||
|
}
|
||
|
if (!empty($filters)) {
|
||
|
$parts['filter'] = count($filters) > 1 ? ['and' => $filters] : $filters[0];
|
||
|
}
|
||
|
|
||
|
$sort = $this->buildOrderBy($query->orderBy);
|
||
|
if (!empty($sort)) {
|
||
|
$parts['sort'] = $sort;
|
||
|
}
|
||
11 years ago
|
|
||
|
if (empty($parts['query'])) {
|
||
|
$parts['query'] = ["match_all" => (object)[]];
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
$options = [];
|
||
|
if ($query->timeout !== null) {
|
||
|
$options['timeout'] = $query->timeout;
|
||
|
}
|
||
|
|
||
11 years ago
|
return [
|
||
|
'queryParts' => $parts,
|
||
|
'index' => $query->index,
|
||
|
'type' => $query->type,
|
||
11 years ago
|
'options' => $options,
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* adds order by condition to the query
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function buildOrderBy($columns)
|
||
11 years ago
|
{
|
||
|
if (empty($columns)) {
|
||
11 years ago
|
return [];
|
||
11 years ago
|
}
|
||
11 years ago
|
$orders = [];
|
||
11 years ago
|
foreach ($columns as $name => $direction) {
|
||
11 years ago
|
if (is_string($direction)) {
|
||
|
$column = $direction;
|
||
|
$direction = SORT_ASC;
|
||
|
} else {
|
||
|
$column = $name;
|
||
|
}
|
||
|
if ($column == ActiveRecord::PRIMARY_KEY_NAME) {
|
||
|
$column = '_id';
|
||
|
}
|
||
|
|
||
11 years ago
|
// allow elasticsearch extended syntax as described in http://www.elasticsearch.org/guide/reference/api/search/sort/
|
||
|
if (is_array($direction)) {
|
||
11 years ago
|
$orders[] = [$column => $direction];
|
||
11 years ago
|
} else {
|
||
11 years ago
|
$orders[] = [$column => ($direction === SORT_DESC ? 'desc' : 'asc')];
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
return $orders;
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Parses the condition specification and generates the corresponding SQL expression.
|
||
|
* @param string|array $condition the condition specification. Please refer to [[Query::where()]]
|
||
|
* on how to specify a condition.
|
||
|
* @param array $params the binding parameters to be populated
|
||
|
* @return string the generated SQL expression
|
||
|
* @throws \yii\db\Exception if the condition is in bad format
|
||
|
*/
|
||
11 years ago
|
public function buildCondition($condition)
|
||
11 years ago
|
{
|
||
|
static $builders = array(
|
||
11 years ago
|
'and' => 'buildAndCondition',
|
||
|
'or' => 'buildAndCondition',
|
||
|
'between' => 'buildBetweenCondition',
|
||
|
'not between' => 'buildBetweenCondition',
|
||
|
'in' => 'buildInCondition',
|
||
|
'not in' => 'buildInCondition',
|
||
|
'like' => 'buildLikeCondition',
|
||
|
'not like' => 'buildLikeCondition',
|
||
|
'or like' => 'buildLikeCondition',
|
||
|
'or not like' => 'buildLikeCondition',
|
||
11 years ago
|
);
|
||
|
|
||
|
if (empty($condition)) {
|
||
11 years ago
|
return [];
|
||
11 years ago
|
}
|
||
|
if (!is_array($condition)) {
|
||
11 years ago
|
throw new NotSupportedException('String conditions in where() are not supported by elasticsearch.');
|
||
11 years ago
|
}
|
||
|
if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
|
||
11 years ago
|
$operator = strtolower($condition[0]);
|
||
11 years ago
|
if (isset($builders[$operator])) {
|
||
|
$method = $builders[$operator];
|
||
|
array_shift($condition);
|
||
11 years ago
|
return $this->$method($operator, $condition);
|
||
11 years ago
|
} else {
|
||
11 years ago
|
throw new InvalidParamException('Found unknown operator in query: ' . $operator);
|
||
11 years ago
|
}
|
||
|
} else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
|
||
11 years ago
|
return $this->buildHashCondition($condition);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
private function buildHashCondition($condition)
|
||
11 years ago
|
{
|
||
11 years ago
|
$parts = [];
|
||
11 years ago
|
foreach($condition as $attribute => $value) {
|
||
11 years ago
|
if ($attribute == ActiveRecord::PRIMARY_KEY_NAME) {
|
||
11 years ago
|
if ($value == null) { // there is no null pk
|
||
|
$parts[] = ['script' => ['script' => '0==1']];
|
||
|
} else {
|
||
|
$parts[] = ['ids' => ['values' => is_array($value) ? $value : [$value]]];
|
||
|
}
|
||
11 years ago
|
} else {
|
||
11 years ago
|
if (is_array($value)) { // IN condition
|
||
|
$parts[] = ['in' => [$attribute => $value]];
|
||
11 years ago
|
} else {
|
||
11 years ago
|
if ($value === null) {
|
||
|
$parts[] = ['missing' => ['field' => $attribute, 'existence' => true, 'null_value' => true]];
|
||
|
} else {
|
||
|
$parts[] = ['term' => [$attribute => $value]];
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
return count($parts) === 1 ? $parts[0] : ['and' => $parts];
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
private function buildAndCondition($operator, $operands)
|
||
11 years ago
|
{
|
||
11 years ago
|
$parts = [];
|
||
11 years ago
|
foreach ($operands as $operand) {
|
||
|
if (is_array($operand)) {
|
||
11 years ago
|
$operand = $this->buildCondition($operand);
|
||
11 years ago
|
}
|
||
11 years ago
|
if (!empty($operand)) {
|
||
11 years ago
|
$parts[] = $operand;
|
||
|
}
|
||
|
}
|
||
|
if (!empty($parts)) {
|
||
11 years ago
|
return [$operator => $parts];
|
||
11 years ago
|
} else {
|
||
11 years ago
|
return [];
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
private function buildBetweenCondition($operator, $operands)
|
||
11 years ago
|
{
|
||
|
if (!isset($operands[0], $operands[1], $operands[2])) {
|
||
11 years ago
|
throw new InvalidParamException("Operator '$operator' requires three operands.");
|
||
11 years ago
|
}
|
||
|
|
||
|
list($column, $value1, $value2) = $operands;
|
||
11 years ago
|
if ($column == ActiveRecord::PRIMARY_KEY_NAME) {
|
||
11 years ago
|
throw new NotSupportedException('Between condition is not supported for primaryKey.');
|
||
|
}
|
||
11 years ago
|
$filter = ['range' => [$column => ['gte' => $value1, 'lte' => $value2]]];
|
||
|
if ($operator == 'not between') {
|
||
|
$filter = ['not' => $filter];
|
||
11 years ago
|
}
|
||
11 years ago
|
return $filter;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
private function buildInCondition($operator, $operands)
|
||
11 years ago
|
{
|
||
|
if (!isset($operands[0], $operands[1])) {
|
||
11 years ago
|
throw new InvalidParamException("Operator '$operator' requires two operands.");
|
||
11 years ago
|
}
|
||
|
|
||
|
list($column, $values) = $operands;
|
||
|
|
||
|
$values = (array)$values;
|
||
|
|
||
11 years ago
|
if (empty($values) || $column === []) {
|
||
11 years ago
|
return $operator === 'in' ? ['script' => ['script' => '0==1']] : [];
|
||
11 years ago
|
}
|
||
|
|
||
|
if (count($column) > 1) {
|
||
|
return $this->buildCompositeInCondition($operator, $column, $values, $params);
|
||
|
} elseif (is_array($column)) {
|
||
|
$column = reset($column);
|
||
|
}
|
||
11 years ago
|
$canBeNull = false;
|
||
11 years ago
|
foreach ($values as $i => $value) {
|
||
|
if (is_array($value)) {
|
||
11 years ago
|
$values[$i] = $value = isset($value[$column]) ? $value[$column] : null;
|
||
11 years ago
|
}
|
||
|
if ($value === null) {
|
||
11 years ago
|
$canBeNull = true;
|
||
|
unset($values[$i]);
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
if ($column == ActiveRecord::PRIMARY_KEY_NAME) {
|
||
11 years ago
|
if (empty($values) && $canBeNull) { // there is no null pk
|
||
|
$filter = ['script' => ['script' => '0==1']];
|
||
|
} else {
|
||
|
$filter = ['ids' => ['values' => array_values($values)]];
|
||
|
if ($canBeNull) {
|
||
|
$filter = ['or' => [$filter, ['missing' => ['field' => $column, 'existence' => true, 'null_value' => true]]]];
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
} else {
|
||
|
if (empty($values) && $canBeNull) {
|
||
|
$filter = ['missing' => ['field' => $column, 'existence' => true, 'null_value' => true]];
|
||
|
} else {
|
||
|
$filter = ['in' => [$column => array_values($values)]];
|
||
|
if ($canBeNull) {
|
||
|
$filter = ['or' => [$filter, ['missing' => ['field' => $column, 'existence' => true, 'null_value' => true]]]];
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
if ($operator == 'not in') {
|
||
|
$filter = ['not' => $filter];
|
||
|
}
|
||
|
return $filter;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
protected function buildCompositeInCondition($operator, $columns, $values)
|
||
11 years ago
|
{
|
||
11 years ago
|
throw new NotSupportedException('composite in is not supported by elasticsearch.');
|
||
11 years ago
|
$vss = array();
|
||
|
foreach ($values as $value) {
|
||
|
$vs = array();
|
||
|
foreach ($columns as $column) {
|
||
|
if (isset($value[$column])) {
|
||
|
$phName = self::PARAM_PREFIX . count($params);
|
||
|
$params[$phName] = $value[$column];
|
||
|
$vs[] = $phName;
|
||
|
} else {
|
||
|
$vs[] = 'NULL';
|
||
|
}
|
||
|
}
|
||
|
$vss[] = '(' . implode(', ', $vs) . ')';
|
||
|
}
|
||
|
foreach ($columns as $i => $column) {
|
||
|
if (strpos($column, '(') === false) {
|
||
|
$columns[$i] = $this->db->quoteColumnName($column);
|
||
|
}
|
||
|
}
|
||
|
return '(' . implode(', ', $columns) . ") $operator (" . implode(', ', $vss) . ')';
|
||
|
}
|
||
|
|
||
11 years ago
|
private function buildLikeCondition($operator, $operands)
|
||
11 years ago
|
{
|
||
11 years ago
|
throw new NotSupportedException('like conditions is not supported by elasticsearch.');
|
||
11 years ago
|
if (!isset($operands[0], $operands[1])) {
|
||
|
throw new Exception("Operator '$operator' requires two operands.");
|
||
|
}
|
||
|
|
||
|
list($column, $values) = $operands;
|
||
|
|
||
|
$values = (array)$values;
|
||
|
|
||
|
if (empty($values)) {
|
||
11 years ago
|
return $operator === 'LIKE' || $operator === 'OR LIKE' ? '0==1' : '';
|
||
11 years ago
|
}
|
||
|
|
||
|
if ($operator === 'LIKE' || $operator === 'NOT LIKE') {
|
||
|
$andor = ' AND ';
|
||
|
} else {
|
||
|
$andor = ' OR ';
|
||
|
$operator = $operator === 'OR LIKE' ? 'LIKE' : 'NOT LIKE';
|
||
|
}
|
||
|
|
||
|
if (strpos($column, '(') === false) {
|
||
|
$column = $this->db->quoteColumnName($column);
|
||
|
}
|
||
|
|
||
|
$parts = array();
|
||
|
foreach ($values as $value) {
|
||
|
$phName = self::PARAM_PREFIX . count($params);
|
||
|
$params[$phName] = $value;
|
||
|
$parts[] = "$column $operator $phName";
|
||
|
}
|
||
|
|
||
|
return implode($andor, $parts);
|
||
|
}
|
||
|
}
|