miniflux-legacy/vendor/fguillot/picodb/lib/PicoDb/Builder/ConditionBuilder.php

378 lines
8.9 KiB
PHP
Raw Normal View History

2015-08-15 03:33:39 +02:00
<?php
2016-07-31 00:41:42 +02:00
namespace PicoDb\Builder;
use PicoDb\Database;
use PicoDb\Table;
2015-08-15 03:33:39 +02:00
/**
* Handle SQL conditions
*
2016-07-31 00:41:42 +02:00
* @package PicoDb\Builder
* @author Frederic Guillot
2015-08-15 03:33:39 +02:00
*/
2016-07-31 00:41:42 +02:00
class ConditionBuilder
2015-08-15 03:33:39 +02:00
{
/**
* Database instance
*
* @access private
* @var Database
*/
private $db;
/**
* Condition values
*
* @access private
* @var array
*/
private $values = array();
/**
2016-07-31 00:41:42 +02:00
* SQL AND conditions
2015-08-15 03:33:39 +02:00
*
* @access private
2016-07-31 00:41:42 +02:00
* @var string[]
2015-08-15 03:33:39 +02:00
*/
private $conditions = array();
/**
* SQL OR conditions
*
* @access private
2016-07-31 00:41:42 +02:00
* @var OrConditionBuilder[]
2015-08-15 03:33:39 +02:00
*/
2016-07-31 00:41:42 +02:00
private $orConditions = array();
2015-08-15 03:33:39 +02:00
/**
2016-07-31 00:41:42 +02:00
* SQL condition offset
2015-08-15 03:33:39 +02:00
*
* @access private
2016-07-31 00:41:42 +02:00
* @var int
2015-08-15 03:33:39 +02:00
*/
2016-07-31 00:41:42 +02:00
private $orConditionOffset = 0;
2015-08-15 03:33:39 +02:00
/**
* Constructor
*
* @access public
* @param Database $db
*/
public function __construct(Database $db)
{
$this->db = $db;
}
/**
* Build the SQL condition
*
* @access public
* @return string
*/
public function build()
{
return empty($this->conditions) ? '' : ' WHERE '.implode(' AND ', $this->conditions);
}
/**
* Get condition values
*
* @access public
* @return array
*/
public function getValues()
{
return $this->values;
}
/**
* Returns true if there is some conditions
*
* @access public
* @return boolean
*/
public function hasCondition()
{
return ! empty($this->conditions);
}
/**
* Add custom condition
*
* @access public
* @param string $sql
*/
public function addCondition($sql)
{
2016-07-31 00:41:42 +02:00
if ($this->orConditionOffset > 0) {
$this->orConditions[$this->orConditionOffset]->withCondition($sql);
2015-08-15 03:33:39 +02:00
}
else {
$this->conditions[] = $sql;
}
}
/**
* Start OR condition
*
* @access public
*/
public function beginOr()
{
2016-07-31 00:41:42 +02:00
$this->orConditionOffset++;
$this->orConditions[$this->orConditionOffset] = new OrConditionBuilder();
2015-08-15 03:33:39 +02:00
}
2016-07-31 00:41:42 +02:00
2015-08-15 03:33:39 +02:00
/**
* Close OR condition
*
* @access public
*/
public function closeOr()
{
2016-07-31 00:41:42 +02:00
$condition = $this->orConditions[$this->orConditionOffset]->build();
$this->orConditionOffset--;
2015-08-15 03:33:39 +02:00
2016-07-31 00:41:42 +02:00
if ($this->orConditionOffset > 0) {
$this->orConditions[$this->orConditionOffset]->withCondition($condition);
} else {
$this->conditions[] = $condition;
2015-08-15 03:33:39 +02:00
}
}
/**
* Equal condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function eq($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' = ?');
$this->values[] = $value;
}
/**
* Not equal condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function neq($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' != ?');
$this->values[] = $value;
}
/**
* IN condition
*
* @access public
* @param string $column
* @param array $values
*/
public function in($column, array $values)
{
if (! empty($values)) {
$this->addCondition($this->db->escapeIdentifier($column).' IN ('.implode(', ', array_fill(0, count($values), '?')).')');
$this->values = array_merge($this->values, $values);
}
}
2016-07-31 00:41:42 +02:00
/**
* IN condition with a subquery
*
* @access public
* @param string $column
* @param Table $subquery
*/
public function inSubquery($column, Table $subquery)
{
$this->addCondition($this->db->escapeIdentifier($column).' IN ('.$subquery->buildSelectQuery().')');
$this->values = array_merge($this->values, $subquery->getConditionBuilder()->getValues());
}
2015-08-15 03:33:39 +02:00
/**
* NOT IN condition
*
* @access public
* @param string $column
* @param array $values
*/
2016-07-31 00:41:42 +02:00
public function notIn($column, array $values)
2015-08-15 03:33:39 +02:00
{
if (! empty($values)) {
$this->addCondition($this->db->escapeIdentifier($column).' NOT IN ('.implode(', ', array_fill(0, count($values), '?')).')');
$this->values = array_merge($this->values, $values);
}
}
2016-07-31 00:41:42 +02:00
/**
* NOT IN condition with a subquery
*
* @access public
* @param string $column
* @param Table $subquery
*/
public function notInSubquery($column, Table $subquery)
{
$this->addCondition($this->db->escapeIdentifier($column).' NOT IN ('.$subquery->buildSelectQuery().')');
$this->values = array_merge($this->values, $subquery->getConditionBuilder()->getValues());
}
2015-08-15 03:33:39 +02:00
/**
* LIKE condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function like($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' '.$this->db->getDriver()->getOperator('LIKE').' ?');
$this->values[] = $value;
}
/**
* ILIKE condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function ilike($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' '.$this->db->getDriver()->getOperator('ILIKE').' ?');
$this->values[] = $value;
}
/**
* Greater than condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function gt($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' > ?');
$this->values[] = $value;
}
2016-07-31 00:41:42 +02:00
/**
* Greater than condition with subquery
*
* @access public
* @param string $column
* @param Table $subquery
*/
public function gtSubquery($column, Table $subquery)
{
$this->addCondition($this->db->escapeIdentifier($column).' > ('.$subquery->buildSelectQuery().')');
$this->values = array_merge($this->values, $subquery->getConditionBuilder()->getValues());
}
2015-08-15 03:33:39 +02:00
/**
* Lower than condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function lt($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' < ?');
$this->values[] = $value;
}
2016-07-31 00:41:42 +02:00
/**
* Lower than condition with subquery
*
* @access public
* @param string $column
* @param Table $subquery
*/
public function ltSubquery($column, Table $subquery)
{
$this->addCondition($this->db->escapeIdentifier($column).' < ('.$subquery->buildSelectQuery().')');
$this->values = array_merge($this->values, $subquery->getConditionBuilder()->getValues());
}
2015-08-15 03:33:39 +02:00
/**
* Greater than or equals condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function gte($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' >= ?');
$this->values[] = $value;
}
2016-07-31 00:41:42 +02:00
/**
* Greater than or equal condition with subquery
*
* @access public
* @param string $column
* @param Table $subquery
*/
public function gteSubquery($column, Table $subquery)
{
$this->addCondition($this->db->escapeIdentifier($column).' >= ('.$subquery->buildSelectQuery().')');
$this->values = array_merge($this->values, $subquery->getConditionBuilder()->getValues());
}
2015-08-15 03:33:39 +02:00
/**
* Lower than or equals condition
*
* @access public
* @param string $column
* @param mixed $value
*/
public function lte($column, $value)
{
$this->addCondition($this->db->escapeIdentifier($column).' <= ?');
$this->values[] = $value;
}
2016-07-31 00:41:42 +02:00
/**
* Lower than or equal condition with subquery
*
* @access public
* @param string $column
* @param Table $subquery
*/
public function lteSubquery($column, Table $subquery)
{
$this->addCondition($this->db->escapeIdentifier($column).' <= ('.$subquery->buildSelectQuery().')');
$this->values = array_merge($this->values, $subquery->getConditionBuilder()->getValues());
}
2015-08-15 03:33:39 +02:00
/**
* IS NULL condition
*
* @access public
* @param string $column
*/
public function isNull($column)
{
$this->addCondition($this->db->escapeIdentifier($column).' IS NULL');
}
/**
* IS NOT NULL condition
*
* @access public
* @param string $column
*/
public function notNull($column)
{
$this->addCondition($this->db->escapeIdentifier($column).' IS NOT NULL');
}
}