phplibrary/core/query/query.php
2016-06-29 18:51:32 +03:00

206 lines
No EOL
4.8 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))) . ")";
}
}
?>