2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoDb;
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
use PDO;
|
2015-08-15 03:33:39 +02:00
|
|
|
use Closure;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table
|
|
|
|
*
|
|
|
|
* @author Frederic Guillot
|
|
|
|
*
|
|
|
|
* @method Table addCondition($sql)
|
|
|
|
* @method Table beginOr()
|
|
|
|
* @method Table closeOr()
|
|
|
|
* @method Table eq($column, $value)
|
|
|
|
* @method Table neq($column, $value)
|
|
|
|
* @method Table in($column, array $values)
|
|
|
|
* @method Table notin($column, array $values)
|
|
|
|
* @method Table like($column, $value)
|
|
|
|
* @method Table ilike($column, $value)
|
|
|
|
* @method Table gt($column, $value)
|
|
|
|
* @method Table lt($column, $value)
|
|
|
|
* @method Table gte($column, $value)
|
|
|
|
* @method Table lte($column, $value)
|
|
|
|
* @method Table isNull($column)
|
|
|
|
* @method Table notNull($column)
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
class Table
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Sorting direction
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-09-15 13:23:55 +02:00
|
|
|
const SORT_ASC = 'ASC';
|
|
|
|
const SORT_DESC = 'DESC';
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
/**
|
|
|
|
* Condition instance
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var Condition
|
|
|
|
*/
|
|
|
|
public $condition;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @access public
|
2015-08-15 03:33:39 +02:00
|
|
|
* @param Database $db
|
|
|
|
* @param string $name
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
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;
|
|
|
|
$this->condition = new Condition($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the table name
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Insert or update
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function save(array $data)
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return $this->condition->hasCondition() ? $this->update($data) : $this->insert($data);
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2014-09-16 11:50:39 +02:00
|
|
|
/**
|
|
|
|
* Update
|
|
|
|
*
|
2015-01-07 01:08:10 +01:00
|
|
|
* 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
|
|
|
*/
|
2015-08-15 03:33:39 +02:00
|
|
|
public function update(array $data = array())
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
|
|
|
$columns = array();
|
|
|
|
$values = array();
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
// Split columns and values
|
2013-02-18 03:48:21 +01:00
|
|
|
foreach ($data as $column => $value) {
|
|
|
|
$columns[] = $this->db->escapeIdentifier($column).'=?';
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
// Sum columns
|
|
|
|
foreach ($this->sumColumns as $column => $value) {
|
|
|
|
$columns[] = $this->db->escapeIdentifier($column).'='.$this->db->escapeIdentifier($column).' + ?';
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append condition values
|
|
|
|
foreach ($this->condition->getValues() as $value) {
|
2013-02-18 03:48:21 +01:00
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
// Build SQL query
|
2013-02-18 03:48:21 +01:00
|
|
|
$sql = sprintf(
|
|
|
|
'UPDATE %s SET %s %s',
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->db->escapeIdentifier($this->name),
|
2013-02-18 03:48:21 +01:00
|
|
|
implode(', ', $columns),
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->condition->build()
|
2013-02-18 03:48:21 +01:00
|
|
|
);
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
return $this->db->execute($sql, $values) !== false;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +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)',
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->db->escapeIdentifier($this->name),
|
2013-02-18 03:48:21 +01:00
|
|
|
implode(', ', $columns),
|
|
|
|
implode(', ', array_fill(0, count($data), '?'))
|
|
|
|
);
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
return $this->db->execute($sql, array_values($data)) !== false;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +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),
|
|
|
|
$this->condition->build()
|
2013-02-18 03:48:21 +01:00
|
|
|
);
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$result = $this->db->execute($sql, $this->condition->getValues());
|
2015-01-07 01:08:10 +01:00
|
|
|
return $result->rowCount() > 0;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Fetch all rows
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function findAll()
|
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->db->execute($this->buildSelectQuery(), $this->condition->getValues());
|
2015-01-20 02:00:16 +01:00
|
|
|
$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);
|
2015-01-20 02:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Find all with a single column
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $column
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return mixed
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
2013-07-13 02:26:47 +02:00
|
|
|
public function findAllByColumn($column)
|
|
|
|
{
|
2013-08-31 17:05:45 +02:00
|
|
|
$this->columns = array($column);
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->db->execute($this->buildSelectQuery(), $this->condition->getValues());
|
2013-07-13 02:26:47 +02:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
return $rq->fetchAll(PDO::FETCH_COLUMN, 0);
|
2013-07-13 02:26:47 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Fetch one row
|
|
|
|
*
|
|
|
|
* @access public
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return array|null
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function findOne()
|
|
|
|
{
|
|
|
|
$this->limit(1);
|
|
|
|
$result = $this->findAll();
|
|
|
|
|
|
|
|
return isset($result[0]) ? $result[0] : null;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
return $this->db->execute($this->buildSelectQuery(), $this->condition->getValues())->fetchColumn();
|
2013-05-26 19:07:45 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
2015-08-15 03:33:39 +02:00
|
|
|
* Build a subquery with an alias
|
2015-01-07 01:08:10 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2015-08-15 03:33:39 +02:00
|
|
|
* @param string $sql
|
|
|
|
* @param string $alias
|
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
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(
|
|
|
|
'SELECT 1 FROM %s '.implode(' ', $this->joins).$this->condition->build(),
|
|
|
|
$this->db->escapeIdentifier($this->name)
|
2013-05-26 19:07:45 +02:00
|
|
|
);
|
2015-08-15 03:33:39 +02:00
|
|
|
|
|
|
|
$rq = $this->db->execute($sql, $this->condition->getValues());
|
|
|
|
$result = $rq->fetchColumn();
|
|
|
|
|
|
|
|
return $result ? true : false;
|
2013-05-26 19:07:45 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Count
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return integer
|
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
$sql = sprintf(
|
2015-08-15 03:33:39 +02:00
|
|
|
'SELECT COUNT(*) FROM %s '.implode(' ', $this->joins).$this->condition->build().$this->sqlOrder.$this->sqlLimit.$this->sqlOffset,
|
|
|
|
$this->db->escapeIdentifier($this->name)
|
2013-02-18 03:48:21 +01:00
|
|
|
);
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->db->execute($sql, $this->condition->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(
|
2015-08-15 03:33:39 +02:00
|
|
|
'SELECT SUM(%s) FROM %s '.implode(' ', $this->joins).$this->condition->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
|
|
|
);
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
$rq = $this->db->execute($sql, $this->condition->getValues());
|
2015-06-21 15:56:36 +02:00
|
|
|
$result = $rq->fetchColumn();
|
|
|
|
|
|
|
|
return $result ? (float) $result : 0;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +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
|
2015-06-21 15:56:36 +02:00
|
|
|
* @param string $alias Join table alias
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
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-01-07 01:08:10 +01:00
|
|
|
/**
|
2015-06-21 15:56:36 +02:00
|
|
|
* Left join
|
2015-01-07 01:08:10 +01:00
|
|
|
*
|
|
|
|
* @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
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Order by
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $column Column name
|
|
|
|
* @param string $order Direction ASC or DESC
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
2014-09-15 13:23:55 +02:00
|
|
|
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);
|
2014-09-15 13:23:55 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Ascending sort
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $column
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Descending sort
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $column
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Limit
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $value
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function limit($value)
|
|
|
|
{
|
2015-01-07 01:08:10 +01:00
|
|
|
if (! is_null($value)) {
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->sqlLimit = ' LIMIT '.(int) $value;
|
2015-01-07 01:08:10 +01:00
|
|
|
}
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Offset
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $value
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function offset($value)
|
|
|
|
{
|
2015-01-07 01:08:10 +01:00
|
|
|
if (! is_null($value)) {
|
2015-08-15 03:33:39 +02:00
|
|
|
$this->sqlOffset = ' OFFSET '.(int) $value;
|
2015-01-07 01:08:10 +01:00
|
|
|
}
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Group by
|
|
|
|
*
|
|
|
|
* @access public
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
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
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function select($select)
|
|
|
|
{
|
|
|
|
$this->sqlSelect = $select;
|
2013-07-13 02:26:47 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Define the columns for the select
|
|
|
|
*
|
|
|
|
* @access public
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
2013-02-18 03:48:21 +01:00
|
|
|
public function columns()
|
|
|
|
{
|
2015-01-07 01:08:10 +01:00
|
|
|
$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
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function sumColumn($column, $value)
|
|
|
|
{
|
|
|
|
$this->sumColumns[$column] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
/**
|
|
|
|
* Distinct
|
|
|
|
*
|
|
|
|
* @access public
|
2015-08-15 03:33:39 +02:00
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
2013-07-13 02:26:47 +02:00
|
|
|
public function distinct()
|
2013-02-18 03:48:21 +01:00
|
|
|
{
|
2015-01-07 01:08:10 +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-01-07 01:08:10 +01:00
|
|
|
/**
|
2015-08-15 03:33:39 +02:00
|
|
|
* Add callback to alter the resultset
|
2015-01-07 01:08:10 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2015-08-15 03:33:39 +02:00
|
|
|
* @param Closure|array $callback
|
|
|
|
* @return Table
|
2015-01-07 01:08:10 +01:00
|
|
|
*/
|
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),
|
|
|
|
$this->condition->build(),
|
|
|
|
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
|
|
|
|
* @return Table
|
|
|
|
*/
|
|
|
|
public function __call($name, array $arguments)
|
|
|
|
{
|
|
|
|
call_user_func_array(array($this->condition, $name), $arguments);
|
2013-02-18 03:48:21 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2013-07-17 01:54:44 +02:00
|
|
|
}
|