File: /var/www/limestate-api/lib/dblib/DbPgsqlSelect.php
<?php
class Db_Pgsql_Select implements iDb_Select
{
/** @var array массив параметров запроса */
public $string = array();
function __construct()
{
$columns = func_get_args();
if (!empty($columns[0])) {
$this->addColumns(is_array($columns[0][0]) ? $columns[0][0] : $columns[0]);
}
}
public function __toString()
{
$result = 'SELECT '
. (isset($this->string['distinct']) ? $this->string['distinct'] : '')
. (isset($this->string['columns']) && is_array($this->string['columns']) && !empty($this->string['columns'])
? implode(', ', $this->string['columns']) : '*')
. (isset($this->string['from'])?' FROM ' . implode(', ', $this->string['from']):'');
if (isset($this->string['join'])) {
$result .= implode("\n ", $this->string['join']);
}
if (isset($this->string['where'])) {
$result .= ' WHERE (' . implode(') AND (', $this->string['where']) . ')';
}
if (isset($this->string['group'])) {
$result .= ' ' . $this->string['group'];
}
if (isset($this->string['order'])) {
$result .= ' ORDER BY ' . implode(', ', $this->string['order']);
}
if (isset($this->string['having'])) {
$result .= ' HAVING (' . implode(') AND (', $this->string['having']) . ')';
}
if (isset($this->string['limit'])) {
$result .= ' ' . $this->string['limit'];
}
return $result;
}
public function distinct()
{
$this->string['distinct'] = 'DISTINCT ';
return $this;
}
public function from($tableName)
{
if (is_string($tableName)) {
$this->string['from'][] = $tableName;
} else if (is_array($tableName) && !empty($tableName)) {
foreach ($tableName as $name => $alias) {
$this->string['from'][] = '"' . $name . '" AS ' . $alias;
}
}
return $this;
}
private function conditionReplace($condition, $value = null)
{
if ($value !== null) {
if (is_array($value)) {
foreach ($value as &$val) {
if (is_array($val)) {
foreach ($val as &$subval) {
if (!(is_float($subval) || is_int($subval))){
$subval = $this->quote($subval);
}
}
$val = implode(', ', $val);
} else {
if (!(is_float($val) || is_int($val))) {
$val = $this->quote($val);
}
}
$spos = mb_strpos($condition, '?');
if ($spos !== false) $condition = mb_substr($condition, 0, $spos).$val.mb_substr($condition, $spos+1);
}
} else {
if (!(is_float($value) || is_int($value))) {
$value = $this->quote($value);
}
$condition = str_replace('?', $value, $condition);
}
}
return $condition;
}
public function where($condition, $value = null)
{
$this->string['where'][] = $this->conditionReplace($condition, $value);
return $this;
}
public function having($condition, $value = null)
{
$this->string['having'][] = $this->conditionReplace($condition, $value);
return $this;
}
public function order($field, $direction = null)
{
$this->string['order'][] = $field . ($direction !== null && is_string($direction) ? ' ' . $direction : '');
return $this;
}
public function group($value)
{
if (is_array($value)) {
$value = implode(', ', $value);
}
$this->string['group'] = 'GROUP BY ' . $value;
return $this;
}
public function limit($count, $offset = 0)
{
$this->string['limit'] = "LIMIT $count" . ($offset ? ' OFFSET ' . $offset : '');
return $this;
}
/**
* Присоединение таблицы
*
* @param string $type тип соединение таблиц
* @param string $tableName таблица БД
* @param string $condition условие присоединения
* @return Db_Pgsql_Select
*/
private function _join($type, $tableName, $condition)
{
if ($type == 'left') {
$join = 'LEFT JOIN';
} else if ($type == 'right') {
$join = 'RIGHT JOIN';
} else if ($type == 'inner') {
$join = 'INNER JOIN';
} else {
$join = 'JOIN';
}
$this->string['join'][] = " $join $tableName ON $condition ";
return $this;
}
public function quote($str)
{
return Db_Driver::getInstance()->quote($str);
}
public function join($tableName, $condition)
{
return $this->_join('inner', $tableName, $condition);
}
public function joinLeft($tableName, $condition)
{
return $this->_join('left', $tableName, $condition);
}
public function addColumns(array $columns)
{
if (empty($columns)) return $this;
foreach ($columns as $column) {
if (is_string($column)) {
$this->string['columns'][] = $column;
// todo: may be don't need this :)
} else if (is_array($column)) {
$this->string['columns'][] = $column[1] . '."' . $column[0] . '"';
}
}
return $this;
}
public function fetchCol($server = null)
{
return Db_Driver::getInstance()->fetchCol($this, $server);
}
public function fetchAll($server = null)
{
return Db_Driver::getInstance()->fetchAll($this, $server);
}
public function fetchArray($server = null)
{
return Db_Driver::getInstance()->fetchArray($this, $server);
}
public function fetchAssoc($server = null)
{
return Db_Driver::getInstance()->fetchAssoc($this, $server);
}
public function fetchOne($server = null)
{
return Db_Driver::getInstance()->fetchOne($this, $server);
}
public function fetchRow($server = null)
{
return Db_Driver::getInstance()->fetchRow($this, $server);
}
}