phplibrary/src/query/query.php
2017-02-09 14:57:40 +03:00

205 lines
5.1 KiB
PHP

<?php
require_once 'table.php';
require_once 'meta.php';
/**
* Класс для составления запроса
*/
class Query
{
static $alias = 0;
public $table;
public $filter = array ();
public $calc = array ();
public $order = array ();
/**
* Создает пустой зарос
*/
static function create()
{
return new Query();
}
/**
* Метаданные таблицы
* @param string $name Имя таблицы
*/
function getTableMeta($name)
{
}
/**
* Создает новый запрос
* @param string $name Имя таблицы
*
* @return Query
*/
public function from($name)
{
$result = clone $this;
$table = new Table();
$table->type = 'simple';
$table->name = $name;
$table->alias = "t" . self::$alias++;
$result->table = $table;
return $result;
}
/**
* Добавляет к запросу условие
* @param string $field Имя поля таблицы
* @param string $op Имя оператора
* @param $value Значение поля
*
* @return Query
*/
public function filter($field, $op, $value)
{
$result = clone $this;
$result->filter [] = $result->table->alias ."." . $field . $op . "'" . $value . "'";
return $result;
}
/**
* @param string $e Выражение
*/
public function calc($e)
{
$result = clone $this;
$result->calc [] = $e;
return $result;
}
public function joinOn($first, $e)
{
$result = $this->compose($first, array ('filter', 'calc', 'order'));
$e = strtr($e, array ("a." => $this->table->alias . ".",
"b." => $first->table->alias . "."));
$table = new Table ();
$table->type = 'join';
$table->name = $this->table->name;
$table->alias = $this->table->alias;
$table->join = $first->table;
$table->condition = $e;
$result->table = $table;
return $result;
}
/**
* Обьединяет параметры двух заросов
* @param Query $first
* @pram array $list Список параметров
*
* @return Query
*/
private function compose(Query $first, array $list)
{
$result = new Query();
foreach ($list as $name) {
$result->$name = array_merge($this->$name, $first->$name);
}
return $result;
}
/**
* Заковычивает значение
*/
public function quote ($value)
{
return "'" . $value . "'";
}
/**
* Компиляция таблицы
* @param Table $table
*
* @return string Часть строки SQL
*/
private function table(Table $table)
{
if ($table->type == 'simple') {
return $table->name ." AS ".$table->alias;
}
if ($table->type == 'join') {
return $table->name ." AS ".$table->alias. " JOIN " . $this->table ($table->join) . " ON " . $table->condition;
}
return "";
}
/**
* Компиляция WHERE
*
* @return string Часть строки SQL
*/
private function where ()
{
return implode(" AND ", $this->filter);
}
/**
* Компиляция запроса в SELECT SQL
*
* @return string SQL выражение
*/
public function select()
{
return "SELECT "
. ((!empty($this->calc))? implode (",", $this->calc): "*")
. " FROM " . $this->table($this->table)
. ((!empty ($this->filter))? " WHERE " . $this->where() : "");
}
/**
* Компиляция в DELETE
*/
public function delete()
{
return "DELETE FROM " . $this->table->name
. ((!empty ($this->filter))? " WHERE " . $this->where() : "");
}
private function add_prefix($prefix, $array) {
$result = array();
foreach ($array as $value) {
$result [] = $prefix . $value;
}
return $result;
}
/**
* Компиляция в UPDATE
* TODO: Разные типы запросов Query->update(); Query->select(), Query->insert();!!
* Возвраащают statement
*/
public function update($values)
{
$pairs = array ();
foreach ($values as $key => $value) { //
$pairs [] = $key . "=" . $this->quote ($value);
}
return "UPDATE " . $this->table->name . " SET "
. implode(",", $pairs)
. ((!empty($this->filter))? " WHERE " . $this->where() : "");
}
/**
* Компиляция в INSERT
*/
public function insert($values)
{
return "INSERT INTO ". $this->table->name . "("
. implode(",", array_keys($values))
. ") VALUES (" . implode(",", array_map(array($this, 'quote'), array_values($values))) . ")";
}
}