szurubooru/src/SqlQuery.php
Marcin Kurczewski 5607cfc353 Models rewrite; removed RedBeanPHP; misc changes
Pages load 1.5-2x faster
Exception trace in JSON is now represented as an array
Fixed pagination of default favorites page in user pages
Fixed thumbnail size validation for non-square thumbnails
2013-12-18 15:17:49 +01:00

99 lines
1.4 KiB
PHP

<?php
class SqlQuery
{
protected $sql;
protected $bindings;
public function __construct()
{
$this->sql = '';
$this->bindings = [];
}
public function __call($name, array $arguments)
{
$name = TextHelper::camelCaseToKebabCase($name);
$name = str_replace('-', ' ', $name);
$this->sql .= $name . ' ';
if (!empty($arguments))
{
$arg = array_shift($arguments);
assert(empty($arguments));
if (is_object($arg))
{
throw new Exception('Not implemented');
}
else
{
$this->sql .= $arg . ' ';
}
}
return $this;
}
public function put($arg)
{
if (is_array($arg))
{
foreach ($arg as $key => $val)
{
if (is_numeric($key))
$this->bindings []= $val;
else
$this->bindings[$key] = $val;
}
}
else
{
$this->bindings []= $arg;
}
return $this;
}
public function raw($raw)
{
$this->sql .= $raw . ' ';
return $this;
}
public function open()
{
$this->sql .= '(';
return $this;
}
public function close()
{
$this->sql .= ') ';
return $this;
}
public function surround($raw)
{
$this->sql .= '(' . $raw . ') ';
return $this;
}
public function genSlots($bindings)
{
if (empty($bindings))
return $this;
$this->sql .= '(';
$this->sql .= join(',', array_fill(0, count($bindings), '?'));
$this->sql .= ') ';
return $this;
}
public function getBindings()
{
return $this->bindings;
}
public function getSql()
{
return trim($this->sql);
}
}