# Koenig (16.09.2016 / 23:05)
кину сюда http://medoo.in/doc
на фоне чистого синтаксиса sql это адская срань
по моему.
простое превращать в сложное
напишу сюда про наработки
класс лимит (+/-)
class KSQLBuilderLimit {
private $limit;
private $page;
private $offset;
public function __construct($limit = 0, $page = 1) {
$this->limit = $limit;
$this->page = $page;
$this->offset = 0;
}
public function setLimit($limit) {
$this->limit = $limit;
return $this;
}
protected function limit() {
return $this->limit;
}
public function setOffset($offset) {
$this->offset = $offset;
return $this;
}
protected function offset() {
return $this->offset;
}
public function setPage($page) {
$this->page = $page;
return $this;
}
protected function page() {
return $this->page;
}
protected function proc() {
if ($this->page() > 1 || !$this->offset() > 0) {
$this->setOffset(($this->page() - 1) * $this->limit());
}
}
public function get() {
$limit = '';
if ($this->limit()) {
$this->proc();
$limit = ' LIMIT ' . $this->limit() . ' OFFSET ' . $this->offset();
}
return $limit;
}
}
$obj = new KSQLBuilderLimit;
#$obj->setLimit(20)->setOffset(200);
#$obj->setLimit(20)->setPage(200);
#$obj->setLimit(20);
echo $obj->get();
и еще
класс ордер бай (+/-)
class KSQLBuilderOrder {
public function test() {
#print_r(array_keys($this->orders));
echo '<pre>' . print_r($this, 1) . '</pre>';
}
private $orders = array(
'asc' => array(),
'desc' => array()
);
public function add($field, $type = 'asc') {
if (!in_array($type, array_keys($this->orders))) {
throw new InvalidArgumentException('Undefined type by order');
}
$this->orders[$type][] = $field;
return $this;
}
protected function concat($type = 'asc') {
$result = '';
if (count($this->orders[$type]) > 0) {
$result = implode(', ', $this->escapeField(array_values($this->orders[$type]))) . ' ' . strtoupper($type);
}
return $result;
}
protected function escapeField($field) {
if (!is_array($field)) {
$field = '`' . $field . '`';
} else {
$field = array_map(array($this, 'escapeField'), $field);
}
return $field;
}
protected function proc() {
$order = '';
$asc = $this->concat();
$desc = $this->concat('desc');
if ($asc || $desc) {
$order = ' ORDER BY ' . $asc . ($asc && $desc ? ', ' : '') . $desc;
}
return $order;
}
public function get() {
return $this->proc();
}
}
$obj = new KSQLBuilderOrder;
$obj->add('test', 'desc');
$obj->add('test2', 'asc');
#$obj->add('test3', 'asc');
#$obj->add('test4', 'desc');
echo $obj->get();
$obj->test();