miniflux-legacy/vendor/fguillot/picodb/lib/PicoDb/Table.php

722 lines
17 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoDb;
use PDO;
2015-08-15 03:33:39 +02:00
use Closure;
2016-07-31 00:41:42 +02:00
use PicoDb\Builder\ConditionBuilder;
use PicoDb\Builder\InsertBuilder;
use PicoDb\Builder\UpdateBuilder;
2015-08-15 03:33:39 +02:00
/**
* Table
*
2016-07-31 00:41:42 +02:00
* @package PicoDb
* @author Frederic Guillot
2015-08-15 03:33:39 +02:00
*
2016-07-31 00:41:42 +02:00
* @method $this addCondition($sql)
* @method $this beginOr()
* @method $this closeOr()
* @method $this eq($column, $value)
* @method $this neq($column, $value)
* @method $this in($column, array $values)
* @method $this inSubquery($column, Table $subquery)
* @method $this notIn($column, array $values)
* @method $this notInSubquery($column, Table $subquery)
* @method $this like($column, $value)
* @method $this ilike($column, $value)
* @method $this gt($column, $value)
* @method $this gtSubquery($column, Table $subquery)
* @method $this lt($column, $value)
* @method $this ltSubquery($column, Table $subquery)
* @method $this gte($column, $value)
* @method $this gteSubquery($column, Table $subquery)
* @method $this lte($column, $value)
* @method $this lteSubquery($column, Table $subquery)
* @method $this isNull($column)
* @method $this notNull($column)
2015-08-15 03:33:39 +02:00
*/
2013-02-18 03:48:21 +01:00
class Table
{
2015-08-15 03:33:39 +02:00
/**
* Sorting direction
*
* @access public
* @var string
*/
const SORT_ASC = 'ASC';
const SORT_DESC = 'DESC';
2015-08-15 03:33:39 +02:00
/**
* Condition instance
*
2016-07-31 00:41:42 +02:00
* @access protected
* @var ConditionBuilder
2015-08-15 03:33:39 +02:00
*/
2016-07-31 00:41:42 +02:00
protected $conditionBuilder;
2015-08-15 03:33:39 +02:00
/**
* Database instance
*
* @access protected
* @var Database
*/
2015-01-28 02:13:16 +01:00
protected $db;
2015-08-15 03:33:39 +02:00
/**
* Table name
*
* @access protected
* @var string
*/
protected $name = '';
/**
* Columns list for SELECT query
*
* @access private
* @var array
*/
2015-06-21 15:56:36 +02:00
private $columns = array();
2015-08-15 03:33:39 +02:00
/**
* Columns to sum during update
*
* @access private
* @var array
*/
private $sumColumns = array();
2015-06-21 15:56:36 +02:00
2015-08-15 03:33:39 +02:00
/**
* SQL limit
*
* @access private
* @var string
*/
private $sqlLimit = '';
/**
* SQL offset
*
* @access private
* @var string
*/
private $sqlOffset = '';
2015-06-21 15:56:36 +02:00
2015-08-15 03:33:39 +02:00
/**
* SQL order
*
* @access private
* @var string
*/
private $sqlOrder = '';
/**
* SQL custom SELECT value
*
* @access private
* @var string
*/
private $sqlSelect = '';
2015-06-21 15:56:36 +02:00
2015-08-15 03:33:39 +02:00
/**
* SQL joins
*
* @access private
* @var array
*/
private $joins = array();
/**
* Use DISTINCT or not?
*
* @access private
* @var boolean
*/
2013-07-13 02:26:47 +02:00
private $distinct = false;
2015-06-21 15:56:36 +02:00
2015-08-15 03:33:39 +02:00
/**
* Group by those columns
*
* @access private
* @var array
*/
private $groupBy = array();
/**
* Callback for result filtering
*
* @access private
* @var Closure
*/
private $callback = null;
2013-02-18 03:48:21 +01:00
/**
* Constructor
*
* @access public
2015-08-15 03:33:39 +02:00
* @param Database $db
* @param string $name
*/
2015-08-15 03:33:39 +02:00
public function __construct(Database $db, $name)
2013-02-18 03:48:21 +01:00
{
$this->db = $db;
2015-08-15 03:33:39 +02:00
$this->name = $name;
2016-07-31 00:41:42 +02:00
$this->conditionBuilder = new ConditionBuilder($db);
2015-08-15 03:33:39 +02:00
}
/**
* Return the table name
*
* @access public
* @return string
*/
public function getName()
{
return $this->name;
2013-02-18 03:48:21 +01:00
}
2016-07-31 00:41:42 +02:00
/**
* Return ConditionBuilder object
*
* @access public
* @return ConditionBuilder
*/
public function getConditionBuilder()
{
return $this->conditionBuilder;
}
/**
* Insert or update
*
* @access public
* @param array $data
* @return boolean
*/
2013-02-18 03:48:21 +01:00
public function save(array $data)
{
2016-07-31 00:41:42 +02:00
return $this->conditionBuilder->hasCondition() ? $this->update($data) : $this->insert($data);
2013-02-18 03:48:21 +01:00
}
2014-09-16 11:50:39 +02:00
/**
* Update
*
* @access public
* @param array $data
* @return boolean
2014-09-16 11:50:39 +02:00
*/
2015-08-15 03:33:39 +02:00
public function update(array $data = array())
2013-02-18 03:48:21 +01:00
{
2016-07-31 00:41:42 +02:00
$values = array_merge(array_values($data), array_values($this->sumColumns), $this->conditionBuilder->getValues());
$sql = UpdateBuilder::getInstance($this->db, $this->conditionBuilder)
->withTable($this->name)
->withColumns(array_keys($data))
->withSumColumns(array_keys($this->sumColumns))
->build();
2013-02-18 03:48:21 +01:00
return $this->db->execute($sql, $values) !== false;
2013-02-18 03:48:21 +01:00
}
/**
* Insert
*
* @access public
* @param array $data
* @return boolean
*/
2013-02-18 03:48:21 +01:00
public function insert(array $data)
{
2016-07-31 00:41:42 +02:00
return $this->db->getStatementHandler()
->withSql(InsertBuilder::getInstance($this->db, $this->conditionBuilder)
->withTable($this->name)
->withColumns(array_keys($data))
->build()
)
->withNamedParams($data)
->execute() !== false;
}
2013-02-18 03:48:21 +01:00
2016-07-31 00:41:42 +02:00
/**
* Insert a new row and return the ID of the primary key
*
* @access public
* @param array $data
* @return bool|int
*/
public function persist(array $data)
{
if ($this->insert($data)) {
return $this->db->getLastId();
2013-02-18 03:48:21 +01:00
}
2016-07-31 00:41:42 +02:00
return false;
2013-02-18 03:48:21 +01:00
}
/**
* Remove
*
* @access public
* @return boolean
*/
2013-02-18 03:48:21 +01:00
public function remove()
{
$sql = sprintf(
'DELETE FROM %s %s',
2015-08-15 03:33:39 +02:00
$this->db->escapeIdentifier($this->name),
2016-07-31 00:41:42 +02:00
$this->conditionBuilder->build()
2013-02-18 03:48:21 +01:00
);
2016-07-31 00:41:42 +02:00
$result = $this->db->execute($sql, $this->conditionBuilder->getValues());
return $result->rowCount() > 0;
2013-02-18 03:48:21 +01:00
}
/**
* Fetch all rows
*
* @access public
* @return array
*/
2013-02-18 03:48:21 +01:00
public function findAll()
{
2016-07-31 00:41:42 +02:00
$rq = $this->db->execute($this->buildSelectQuery(), $this->conditionBuilder->getValues());
$results = $rq->fetchAll(PDO::FETCH_ASSOC);
2015-08-15 03:33:39 +02:00
if (is_callable($this->callback) && ! empty($results)) {
return call_user_func($this->callback, $results);
}
return $results;
2013-02-18 03:48:21 +01:00
}
/**
* Find all with a single column
*
* @access public
* @param string $column
2015-08-15 03:33:39 +02:00
* @return mixed
*/
2013-07-13 02:26:47 +02:00
public function findAllByColumn($column)
{
$this->columns = array($column);
2016-07-31 00:41:42 +02:00
$rq = $this->db->execute($this->buildSelectQuery(), $this->conditionBuilder->getValues());
2013-07-13 02:26:47 +02:00
return $rq->fetchAll(PDO::FETCH_COLUMN, 0);
2013-07-13 02:26:47 +02:00
}
/**
* Fetch one row
*
* @access public
2015-08-15 03:33:39 +02:00
* @return array|null
*/
2013-02-18 03:48:21 +01:00
public function findOne()
{
$this->limit(1);
$result = $this->findAll();
return isset($result[0]) ? $result[0] : null;
}
/**
* Fetch one column, first row
*
* @access public
* @param string $column
* @return string
*/
2013-05-26 19:07:45 +02:00
public function findOneColumn($column)
{
$this->limit(1);
$this->columns = array($column);
2016-07-31 00:41:42 +02:00
return $this->db->execute($this->buildSelectQuery(), $this->conditionBuilder->getValues())->fetchColumn();
2013-05-26 19:07:45 +02:00
}
/**
2015-08-15 03:33:39 +02:00
* Build a subquery with an alias
*
* @access public
2015-08-15 03:33:39 +02:00
* @param string $sql
* @param string $alias
2016-07-31 00:41:42 +02:00
* @return $this
*/
2015-08-15 03:33:39 +02:00
public function subquery($sql, $alias)
2013-05-26 19:07:45 +02:00
{
2015-08-15 03:33:39 +02:00
$this->columns[] = '('.$sql.') AS '.$this->db->escapeIdentifier($alias);
return $this;
}
2014-10-19 20:42:31 +02:00
2015-08-15 03:33:39 +02:00
/**
* Exists
*
* @access public
* @return integer
*/
public function exists()
{
$sql = sprintf(
2016-07-31 00:41:42 +02:00
'SELECT 1 FROM %s '.implode(' ', $this->joins).$this->conditionBuilder->build(),
2015-08-15 03:33:39 +02:00
$this->db->escapeIdentifier($this->name)
2013-05-26 19:07:45 +02:00
);
2015-08-15 03:33:39 +02:00
2016-07-31 00:41:42 +02:00
$rq = $this->db->execute($sql, $this->conditionBuilder->getValues());
2015-08-15 03:33:39 +02:00
$result = $rq->fetchColumn();
return $result ? true : false;
2013-05-26 19:07:45 +02:00
}
/**
* Count
*
* @access public
* @return integer
*/
2013-02-18 03:48:21 +01:00
public function count()
{
$sql = sprintf(
2016-07-31 00:41:42 +02:00
'SELECT COUNT(*) FROM %s '.implode(' ', $this->joins).$this->conditionBuilder->build().$this->sqlOrder.$this->sqlLimit.$this->sqlOffset,
2015-08-15 03:33:39 +02:00
$this->db->escapeIdentifier($this->name)
2013-02-18 03:48:21 +01:00
);
2016-07-31 00:41:42 +02:00
$rq = $this->db->execute($sql, $this->conditionBuilder->getValues());
2013-05-26 19:07:45 +02:00
$result = $rq->fetchColumn();
2015-06-21 15:56:36 +02:00
2013-05-26 19:07:45 +02:00
return $result ? (int) $result : 0;
2013-02-18 03:48:21 +01:00
}
2015-06-21 15:56:36 +02:00
/**
* Sum
*
* @access public
* @param string $column
* @return float
*/
public function sum($column)
{
$sql = sprintf(
2016-07-31 00:41:42 +02:00
'SELECT SUM(%s) FROM %s '.implode(' ', $this->joins).$this->conditionBuilder->build().$this->sqlOrder.$this->sqlLimit.$this->sqlOffset,
2015-06-21 15:56:36 +02:00
$this->db->escapeIdentifier($column),
2015-08-15 03:33:39 +02:00
$this->db->escapeIdentifier($this->name)
2015-06-21 15:56:36 +02:00
);
2016-07-31 00:41:42 +02:00
$rq = $this->db->execute($sql, $this->conditionBuilder->getValues());
2015-06-21 15:56:36 +02:00
$result = $rq->fetchColumn();
return $result ? (float) $result : 0;
}
2016-07-31 00:41:42 +02:00
/**
* Increment column value
*
* @access public
* @param string $column
* @param string $value
* @return boolean
*/
public function increment($column, $value)
{
$sql = sprintf(
'UPDATE %s SET %s=%s+%d '.$this->conditionBuilder->build(),
$this->db->escapeIdentifier($this->name),
$this->db->escapeIdentifier($column),
$this->db->escapeIdentifier($column),
$value
);
return $this->db->execute($sql, $this->conditionBuilder->getValues()) !== false;
}
/**
* Decrement column value
*
* @access public
* @param string $column
* @param string $value
* @return boolean
*/
public function decrement($column, $value)
{
$sql = sprintf(
'UPDATE %s SET %s=%s-%d '.$this->conditionBuilder->build(),
$this->db->escapeIdentifier($this->name),
$this->db->escapeIdentifier($column),
$this->db->escapeIdentifier($column),
$value
);
return $this->db->execute($sql, $this->conditionBuilder->getValues()) !== false;
}
/**
* Left join
*
* @access public
* @param string $table Join table
* @param string $foreign_column Foreign key on the join table
* @param string $local_column Local column
* @param string $local_table Local table
2015-06-21 15:56:36 +02:00
* @param string $alias Join table alias
2016-07-31 00:41:42 +02:00
* @return $this
*/
2015-06-21 15:56:36 +02:00
public function join($table, $foreign_column, $local_column, $local_table = '', $alias = '')
2013-02-18 03:48:21 +01:00
{
$this->joins[] = sprintf(
'LEFT JOIN %s ON %s=%s',
$this->db->escapeIdentifier($table),
2015-06-21 15:56:36 +02:00
$this->db->escapeIdentifier($alias ?: $table).'.'.$this->db->escapeIdentifier($foreign_column),
2015-08-15 03:33:39 +02:00
$this->db->escapeIdentifier($local_table ?: $this->name).'.'.$this->db->escapeIdentifier($local_column)
2013-02-18 03:48:21 +01:00
);
return $this;
}
/**
2015-06-21 15:56:36 +02:00
* Left join
*
* @access public
2015-06-21 15:56:36 +02:00
* @param string $table1
* @param string $alias1
* @param string $column1
* @param string $table2
* @param string $column2
2016-07-31 00:41:42 +02:00
* @return $this
2015-06-21 15:56:36 +02:00
*/
public function left($table1, $alias1, $column1, $table2, $column2)
{
$this->joins[] = sprintf(
'LEFT JOIN %s AS %s ON %s=%s',
$this->db->escapeIdentifier($table1),
$this->db->escapeIdentifier($alias1),
$this->db->escapeIdentifier($alias1).'.'.$this->db->escapeIdentifier($column1),
$this->db->escapeIdentifier($table2).'.'.$this->db->escapeIdentifier($column2)
);
return $this;
}
2016-07-31 00:41:42 +02:00
/**
* Inner join
*
* @access public
* @param string $table1
* @param string $alias1
* @param string $column1
* @param string $table2
* @param string $column2
* @return $this
*/
public function inner($table1, $alias1, $column1, $table2, $column2)
{
$this->joins[] = sprintf(
'JOIN %s AS %s ON %s=%s',
$this->db->escapeIdentifier($table1),
$this->db->escapeIdentifier($alias1),
$this->db->escapeIdentifier($alias1).'.'.$this->db->escapeIdentifier($column1),
$this->db->escapeIdentifier($table2).'.'.$this->db->escapeIdentifier($column2)
);
return $this;
}
/**
* Order by
*
* @access public
* @param string $column Column name
* @param string $order Direction ASC or DESC
2016-07-31 00:41:42 +02:00
* @return $this
*/
public function orderBy($column, $order = self::SORT_ASC)
2013-07-17 01:54:44 +02:00
{
2013-09-15 01:05:52 +02:00
$order = strtoupper($order);
$order = $order === self::SORT_ASC || $order === self::SORT_DESC ? $order : self::SORT_ASC;
2013-07-17 01:54:44 +02:00
2015-08-15 03:33:39 +02:00
if ($this->sqlOrder === '') {
$this->sqlOrder = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.$order;
2013-07-17 01:54:44 +02:00
}
else {
2015-08-15 03:33:39 +02:00
$this->sqlOrder .= ', '.$this->db->escapeIdentifier($column).' '.$order;
2013-07-17 01:54:44 +02:00
}
return $this;
}
/**
* Ascending sort
*
* @access public
* @param string $column
2016-07-31 00:41:42 +02:00
* @return $this
*/
2013-02-18 03:48:21 +01:00
public function asc($column)
{
2015-08-15 03:33:39 +02:00
$this->orderBy($column, self::SORT_ASC);
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Descending sort
*
* @access public
* @param string $column
2016-07-31 00:41:42 +02:00
* @return $this
*/
2013-02-18 03:48:21 +01:00
public function desc($column)
{
2015-08-15 03:33:39 +02:00
$this->orderBy($column, self::SORT_DESC);
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Limit
*
* @access public
* @param integer $value
2016-07-31 00:41:42 +02:00
* @return $this
*/
2013-02-18 03:48:21 +01:00
public function limit($value)
{
if (! is_null($value)) {
2015-08-15 03:33:39 +02:00
$this->sqlLimit = ' LIMIT '.(int) $value;
}
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Offset
*
* @access public
* @param integer $value
2016-07-31 00:41:42 +02:00
* @return $this
*/
2013-02-18 03:48:21 +01:00
public function offset($value)
{
if (! is_null($value)) {
2015-08-15 03:33:39 +02:00
$this->sqlOffset = ' OFFSET '.(int) $value;
}
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Group by
*
* @access public
2016-07-31 00:41:42 +02:00
* @return $this
*/
2013-07-13 02:26:47 +02:00
public function groupBy()
{
2015-08-15 03:33:39 +02:00
$this->groupBy = func_get_args();
return $this;
}
/**
* Custom select
*
* @access public
* @param string $select
2016-07-31 00:41:42 +02:00
* @return $this
2015-08-15 03:33:39 +02:00
*/
public function select($select)
{
$this->sqlSelect = $select;
2013-07-13 02:26:47 +02:00
return $this;
}
/**
* Define the columns for the select
*
* @access public
2016-07-31 00:41:42 +02:00
* @return $this
*/
2013-02-18 03:48:21 +01:00
public function columns()
{
$this->columns = func_get_args();
2013-02-18 03:48:21 +01:00
return $this;
}
2015-08-15 03:33:39 +02:00
/**
* Sum column
*
* @access public
* @param string $column
* @param mixed $value
2016-07-31 00:41:42 +02:00
* @return $this
2015-08-15 03:33:39 +02:00
*/
public function sumColumn($column, $value)
{
$this->sumColumns[$column] = $value;
return $this;
}
/**
* Distinct
*
* @access public
2016-07-31 00:41:42 +02:00
* @return $this
*/
2013-07-13 02:26:47 +02:00
public function distinct()
2013-02-18 03:48:21 +01:00
{
$this->columns = func_get_args();
2013-07-13 02:26:47 +02:00
$this->distinct = true;
return $this;
}
2013-02-18 03:48:21 +01:00
/**
2015-08-15 03:33:39 +02:00
* Add callback to alter the resultset
*
* @access public
2015-08-15 03:33:39 +02:00
* @param Closure|array $callback
2016-07-31 00:41:42 +02:00
* @return $this
*/
2015-08-15 03:33:39 +02:00
public function callback($callback)
2013-07-13 02:26:47 +02:00
{
2015-08-15 03:33:39 +02:00
$this->callback = $callback;
return $this;
}
2013-02-18 03:48:21 +01:00
2015-08-15 03:33:39 +02:00
/**
* Build a select query
*
* @access public
* @return string
*/
public function buildSelectQuery()
{
if (empty($this->sqlSelect)) {
$this->columns = $this->db->escapeIdentifierList($this->columns);
$this->sqlSelect = ($this->distinct ? 'DISTINCT ' : '').(empty($this->columns) ? '*' : implode(', ', $this->columns));
}
2013-02-18 03:48:21 +01:00
2015-08-15 03:33:39 +02:00
$this->groupBy = $this->db->escapeIdentifierList($this->groupBy);
2013-02-18 03:48:21 +01:00
2015-08-15 03:33:39 +02:00
return trim(sprintf(
'SELECT %s FROM %s %s %s %s %s %s %s',
$this->sqlSelect,
$this->db->escapeIdentifier($this->name),
implode(' ', $this->joins),
2016-07-31 00:41:42 +02:00
$this->conditionBuilder->build(),
2015-08-15 03:33:39 +02:00
empty($this->groupBy) ? '' : 'GROUP BY '.implode(', ', $this->groupBy),
$this->sqlOrder,
$this->sqlLimit,
$this->sqlOffset
));
}
2013-02-18 03:48:21 +01:00
2015-08-15 03:33:39 +02:00
/**
* Magic method for sql conditions
*
* @access public
* @param string $name
* @param array $arguments
2016-07-31 00:41:42 +02:00
* @return $this
2015-08-15 03:33:39 +02:00
*/
public function __call($name, array $arguments)
{
2016-07-31 00:41:42 +02:00
call_user_func_array(array($this->conditionBuilder, $name), $arguments);
2013-02-18 03:48:21 +01:00
return $this;
}
2013-07-17 01:54:44 +02:00
}