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

585 lines
14 KiB
PHP
Raw Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoDb;
use PDO;
2013-02-18 03:48:21 +01:00
class Table
{
const SORT_ASC = 'ASC';
const SORT_DESC = 'DESC';
2013-02-18 03:48:21 +01:00
private $table_name = '';
private $sql_limit = '';
private $sql_offset = '';
private $sql_order = '';
private $joins = array();
private $conditions = array();
private $or_conditions = array();
private $is_or_condition = false;
private $columns = array();
private $values = array();
2013-07-13 02:26:47 +02:00
private $distinct = false;
private $group_by = array();
2013-02-18 03:48:21 +01:00
private $db;
/**
* Constructor
*
* @access public
* @param \PicoDb\Database $db
* @param string $table_name
*/
2013-02-18 03:48:21 +01:00
public function __construct(Database $db, $table_name)
{
$this->db = $db;
$this->table_name = $table_name;
}
/**
* Insert or update
*
* @access public
* @param array $data
* @return boolean
*/
2013-02-18 03:48:21 +01:00
public function save(array $data)
{
if (! empty($this->conditions)) {
return $this->update($data);
}
else {
return $this->insert($data);
}
}
2014-09-16 11:50:39 +02:00
/**
* Update
*
* Note: Do not use `rowCount()` for update the behaviour is different across drivers
*
* @access public
* @param array $data
* @return boolean
2014-09-16 11:50:39 +02:00
*/
2013-02-18 03:48:21 +01:00
public function update(array $data)
{
$columns = array();
$values = array();
foreach ($data as $column => $value) {
$columns[] = $this->db->escapeIdentifier($column).'=?';
$values[] = $value;
}
foreach ($this->values as $value) {
$values[] = $value;
}
$sql = sprintf(
'UPDATE %s SET %s %s',
$this->db->escapeIdentifier($this->table_name),
implode(', ', $columns),
$this->conditions()
);
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)
{
$columns = array();
foreach ($data as $column => $value) {
$columns[] = $this->db->escapeIdentifier($column);
}
$sql = sprintf(
'INSERT INTO %s (%s) VALUES (%s)',
$this->db->escapeIdentifier($this->table_name),
implode(', ', $columns),
implode(', ', array_fill(0, count($data), '?'))
);
return $this->db->execute($sql, array_values($data)) !== 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',
$this->db->escapeIdentifier($this->table_name),
$this->conditions()
);
$result = $this->db->execute($sql, $this->values);
return $result->rowCount() > 0;
2013-02-18 03:48:21 +01:00
}
/**
* Hashmap result [ [column1 => column2], [], ...]
*
* @access public
* @param string $key Column 1
* @param string $value Column 2
* @return array
*/
2013-02-18 03:48:21 +01:00
public function listing($key, $value)
{
$listing = array();
$this->columns($key, $value);
$rq = $this->db->execute($this->buildSelectQuery(), $this->values);
2013-02-18 03:48:21 +01:00
$rows = $rq->fetchAll(PDO::FETCH_NUM);
2013-02-18 03:48:21 +01:00
foreach ($rows as $row) {
$listing[$row[0]] = $row[1];
2013-02-18 03:48:21 +01:00
}
return $listing;
}
/**
* Fetch all rows
*
* @access public
* @return array
*/
2013-02-18 03:48:21 +01:00
public function findAll()
{
2013-05-26 19:07:45 +02:00
$rq = $this->db->execute($this->buildSelectQuery(), $this->values);
return $rq->fetchAll(PDO::FETCH_ASSOC);
2013-02-18 03:48:21 +01:00
}
/**
* Find all with a single column
*
* @access public
* @param string $column
* @return boolean
*/
2013-07-13 02:26:47 +02:00
public function findAllByColumn($column)
{
$this->columns = array($column);
2013-07-13 02:26:47 +02:00
$rq = $this->db->execute($this->buildSelectQuery(), $this->values);
return $rq->fetchAll(PDO::FETCH_COLUMN, 0);
2013-07-13 02:26:47 +02:00
}
/**
* Fetch one row
*
* @access public
* @param array $data
* @return boolean
*/
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);
$rq = $this->db->execute($this->buildSelectQuery(), $this->values);
return $rq->fetchColumn();
}
/**
* Build a select query
*
* @access public
* @return string
*/
2013-05-26 19:07:45 +02:00
public function buildSelectQuery()
{
2014-10-19 20:42:31 +02:00
foreach ($this->columns as $key => $value) {
$this->columns[$key] = $this->db->escapeIdentifier($value);
}
2013-05-26 19:07:45 +02:00
return sprintf(
2013-07-13 02:26:47 +02:00
'SELECT %s %s FROM %s %s %s %s %s %s %s',
$this->distinct ? 'DISTINCT' : '',
2013-05-26 19:07:45 +02:00
empty($this->columns) ? '*' : implode(', ', $this->columns),
$this->db->escapeIdentifier($this->table_name),
implode(' ', $this->joins),
$this->conditions(),
2013-07-13 02:26:47 +02:00
empty($this->group_by) ? '' : 'GROUP BY '.implode(', ', $this->group_by),
2013-05-26 19:07:45 +02:00
$this->sql_order,
$this->sql_limit,
$this->sql_offset
);
}
/**
* Count
*
* @access public
* @return integer
*/
2013-02-18 03:48:21 +01:00
public function count()
{
$sql = sprintf(
2013-05-26 19:07:45 +02:00
'SELECT COUNT(*) FROM %s'.$this->conditions().$this->sql_order.$this->sql_limit.$this->sql_offset,
2013-02-18 03:48:21 +01:00
$this->db->escapeIdentifier($this->table_name)
);
$rq = $this->db->execute($sql, $this->values);
2013-05-26 19:07:45 +02:00
$result = $rq->fetchColumn();
return $result ? (int) $result : 0;
2013-02-18 03:48:21 +01:00
}
/**
* 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
* @return \PicoDb\Table
*/
public function join($table, $foreign_column, $local_column, $local_table = '')
2013-02-18 03:48:21 +01:00
{
$this->joins[] = sprintf(
'LEFT JOIN %s ON %s=%s',
$this->db->escapeIdentifier($table),
$this->db->escapeIdentifier($table).'.'.$this->db->escapeIdentifier($foreign_column),
$this->db->escapeIdentifier($local_table ?: $this->table_name).'.'.$this->db->escapeIdentifier($local_column)
2013-02-18 03:48:21 +01:00
);
return $this;
}
/**
* Build conditions
*
* @access public
* @return string
*/
2013-02-18 03:48:21 +01:00
public function conditions()
{
return empty($this->conditions) ? '' : ' WHERE '.implode(' AND ', $this->conditions);
2013-02-18 03:48:21 +01:00
}
/**
* Add new condition
*
* @access public
* @param string $sql
*/
2013-02-18 03:48:21 +01:00
public function addCondition($sql)
{
if ($this->is_or_condition) {
$this->or_conditions[] = $sql;
}
else {
$this->conditions[] = $sql;
}
}
/**
* Start OR condition
*
* @access public
* @return \PicoDb\Table
*/
2013-02-18 03:48:21 +01:00
public function beginOr()
{
$this->is_or_condition = true;
$this->or_conditions = array();
return $this;
}
/**
* Close OR condition
*
* @access public
* @return \PicoDb\Table
*/
2013-02-18 03:48:21 +01:00
public function closeOr()
{
$this->is_or_condition = false;
if (! empty($this->or_conditions)) {
$this->conditions[] = '('.implode(' OR ', $this->or_conditions).')';
}
return $this;
}
/**
* Order by
*
* @access public
* @param string $column Column name
* @param string $order Direction ASC or DESC
* @return \PicoDb\Table
*/
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
if ($this->sql_order === '') {
$this->sql_order = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.$order;
}
else {
$this->sql_order .= ', '.$this->db->escapeIdentifier($column).' '.$order;
}
return $this;
}
/**
* Ascending sort
*
* @access public
* @param string $column
* @return \PicoDb\Table
*/
2013-02-18 03:48:21 +01:00
public function asc($column)
{
2013-07-06 04:37:19 +02:00
if ($this->sql_order === '') {
$this->sql_order = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.self::SORT_ASC;
2013-07-06 04:37:19 +02:00
}
else {
$this->sql_order .= ', '.$this->db->escapeIdentifier($column).' '.self::SORT_ASC;
2013-07-06 04:37:19 +02:00
}
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Descending sort
*
* @access public
* @param string $column
* @return \PicoDb\Table
*/
2013-02-18 03:48:21 +01:00
public function desc($column)
{
2013-07-06 04:37:19 +02:00
if ($this->sql_order === '') {
$this->sql_order = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.self::SORT_DESC;
2013-07-06 04:37:19 +02:00
}
else {
$this->sql_order .= ', '.$this->db->escapeIdentifier($column).' '.self::SORT_DESC;
2013-07-06 04:37:19 +02:00
}
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Limit
*
* @access public
* @param integer $value
* @return \PicoDb\Table
*/
2013-02-18 03:48:21 +01:00
public function limit($value)
{
if (! is_null($value)) {
$this->sql_limit = ' LIMIT '.(int) $value;
}
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Offset
*
* @access public
* @param integer $value
* @return \PicoDb\Table
*/
2013-02-18 03:48:21 +01:00
public function offset($value)
{
if (! is_null($value)) {
$this->sql_offset = ' OFFSET '.(int) $value;
}
2013-02-18 03:48:21 +01:00
return $this;
}
/**
* Group by
*
* @access public
* @return \PicoDb\Table
*/
2013-07-13 02:26:47 +02:00
public function groupBy()
{
$this->group_by = func_get_args();
2013-07-13 02:26:47 +02:00
return $this;
}
/**
* Define the columns for the select
*
* @access public
* @return \PicoDb\Table
*/
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;
}
/**
* Distinct
*
* @access public
* @return \PicoDb\Table
*/
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
/**
* Magic method for sql conditions
*
* @access public
* @param string $name
* @param array $arguments
* @return \PicoDb\Table
*/
2013-07-13 02:26:47 +02:00
public function __call($name, array $arguments)
{
2013-02-18 03:48:21 +01:00
$column = $arguments[0];
$sql = '';
2013-07-13 02:26:47 +02:00
switch (strtolower($name)) {
2013-02-18 03:48:21 +01:00
case 'in':
2014-10-19 20:42:31 +02:00
if (isset($arguments[1]) && is_array($arguments[1]) && ! empty($arguments[1])) {
2013-02-18 03:48:21 +01:00
$sql = sprintf(
'%s IN (%s)',
$this->db->escapeIdentifier($column),
implode(', ', array_fill(0, count($arguments[1]), '?'))
);
}
break;
2013-04-23 04:43:38 +02:00
case 'notin':
2014-10-19 20:42:31 +02:00
if (isset($arguments[1]) && is_array($arguments[1]) && ! empty($arguments[1])) {
2013-04-23 04:43:38 +02:00
$sql = sprintf(
'%s NOT IN (%s)',
$this->db->escapeIdentifier($column),
implode(', ', array_fill(0, count($arguments[1]), '?'))
);
}
break;
2013-02-18 03:48:21 +01:00
case 'like':
$sql = sprintf(
'%s %s ?',
$this->db->escapeIdentifier($column),
$this->db->getConnection()->operatorLikeCaseSensitive()
);
break;
case 'ilike':
$sql = sprintf(
'%s %s ?',
$this->db->escapeIdentifier($column),
$this->db->getConnection()->operatorLikeNotCaseSensitive()
);
2013-02-18 03:48:21 +01:00
break;
case 'eq':
case 'equal':
case 'equals':
$sql = sprintf('%s = ?', $this->db->escapeIdentifier($column));
break;
case 'neq':
case 'notequal':
case 'notequals':
$sql = sprintf('%s != ?', $this->db->escapeIdentifier($column));
break;
2013-02-18 03:48:21 +01:00
case 'gt':
2013-07-13 02:26:47 +02:00
case 'greaterthan':
2013-02-18 03:48:21 +01:00
$sql = sprintf('%s > ?', $this->db->escapeIdentifier($column));
break;
case 'lt':
2013-07-13 02:26:47 +02:00
case 'lowerthan':
2013-02-18 03:48:21 +01:00
$sql = sprintf('%s < ?', $this->db->escapeIdentifier($column));
break;
case 'gte':
2013-07-13 02:26:47 +02:00
case 'greaterthanorequals':
2013-02-18 03:48:21 +01:00
$sql = sprintf('%s >= ?', $this->db->escapeIdentifier($column));
break;
case 'lte':
2013-07-13 02:26:47 +02:00
case 'lowerthanorequals':
2013-02-18 03:48:21 +01:00
$sql = sprintf('%s <= ?', $this->db->escapeIdentifier($column));
break;
2013-07-13 02:26:47 +02:00
case 'isnull':
$sql = sprintf('%s IS NULL', $this->db->escapeIdentifier($column));
break;
case 'notnull':
$sql = sprintf('%s IS NOT NULL', $this->db->escapeIdentifier($column));
break;
2013-02-18 03:48:21 +01:00
}
2013-07-13 02:26:47 +02:00
if ($sql !== '') {
2013-02-18 03:48:21 +01:00
$this->addCondition($sql);
2013-07-13 02:26:47 +02:00
if (isset($arguments[1])) {
2013-02-18 03:48:21 +01:00
2013-07-13 02:26:47 +02:00
if (is_array($arguments[1])) {
2013-02-18 03:48:21 +01:00
2013-07-13 02:26:47 +02:00
foreach ($arguments[1] as $value) {
$this->values[] = $value;
}
2013-02-18 03:48:21 +01:00
}
2013-07-13 02:26:47 +02:00
else {
2013-02-18 03:48:21 +01:00
2013-07-13 02:26:47 +02:00
$this->values[] = $arguments[1];
}
2013-02-18 03:48:21 +01:00
}
}
return $this;
}
2013-07-17 01:54:44 +02:00
}