<?php
trait KsqlQueries {
public $pre = false;
public $post = false;
public function select($fieldsListArray = '*') {
$this->pre = 'SELECT ' . (is_array($fieldsListArray) ? $this->concat(array_map(array($this, 'art'), $fieldsListArray)) : $fieldsListArray) . ' FROM ';
return $this;
}
public function count($field = '*') {
$this->pre = 'SELECT COUNT(' . $this->art($field) . ') FROM ';
return $this;
}
public function delete() {
$this->pre = 'DELETE FROM ';
return $this;
}
public function insert($fieldsListArray, $values) {
$this->pre = 'INSERT INTO ';
$this->post = '(' . $this->concat(array_map(array($this, 'art'), $fieldsListArray)) . ') VALUES (' . implode(', ', array_fill(0, count($values), '?')) . ')';
foreach($values as $value) {
$this->placeholders[] = $this->esc($value);
}
return $this;
}
public function update($data) {
$this->pre = 'UPDATE ';
$this->post = 'SET ';
$i = 0;
foreach($data as $field => $value) {
$this->post .= ($i++ > 0 ? ', ' : '') . $this->art($field) . ' = ?';
$this->placeholders[] = $this->esc($value);
}
return $this;
}
}
trait KsqlArgs {
public $table = array();
public $on = array();
public $limit;
public $offset = 0;
public $where = array();
public $order = array();
public $group = array();
public function table($table) {
$this->table[] = $table;
return $this;
}
public function join($table) {
return $this->table($table);
}
public function on() {
if (func_num_args() == 2) {
$this->on[] = $this->art(func_get_arg(0)) . ' = ' . $this->art(func_get_arg(1));
} elseif (is_array($array)) {
foreach($array as $k => $on) {
$this->on[] = $this->art($on[0]) . ' = ' . $this->art($on[1]);
}
}
return $this;
}
public function where() {
if (func_num_args() == 3) {
$this->where[] = $this->art(func_get_arg(0)) . ' ' . func_get_arg(1) . ' ?';
$this->placeholders[] = $this->esc(func_get_arg(2));
} elseif (is_array(func_get_arg(0))) {
foreach($array as $k => $where) {
$this->where[] = $this->art($where[0]) . ' ' . $where[1] . ' ?';
$this->placeholders[] = $this->esc($where[2]);
}
}
return $this;
}
//
# прочие Where
//
public function group($field) {
$this->group[] = $this->art($field);
return $this;
}
public function order($field, $type = 'asc') {
$type = in_array($type, array('asc', 'desc')) ? $type : 'asc';
$this->order[] = $this->art($field) . ' ' . strtoupper($type);
return $this;
}
public function limit() {
if (func_num_args() == 2) {
$this->offset = '?';
$this->placeholders[] = $this->esc(func_get_arg(0));
$this->limit = '?';
$this->placeholders[] = $this->esc(func_get_arg(1));
} else {
$this->limit = '?';
$this->placeholders[] = $this->esc(func_get_arg(0));
}
return $this;
}
}
trait KsqlUtils {
public function concat($array, $sep = 'comma') {
$types = array('comma' => ', ', 1 => ' AND ');
return implode($types[$sep], $array);
}
public function art($args) {
if (!is_array($args)) {
return '`' . str_replace(array('.', ' = '), array('`.`', '` = `'), str_replace('`', '', $args)) . '`';
} else {
return array_map(array($this, 'art'), $args);
}
}
public function esc($data) {
if (!is_array($data)) {
return is_int($data) ? $data : '"' . $this->real_escape_string($data) . '"';
} else {
return array_map(array($this, 'esc'), $data);
}
}
public function reset() {
$this->sql = false;
$this->table = array();
$this->on = array();
$this->limit = null;
$this->offset = 0;
$this->where = array();
$this->order = array();
$this->group = array();
$this->pre = false;
$this->post = false;
$this->placeholders = false;
}
}
class KMysqli extends Mysqli {
use KsqlUtils, KsqlArgs, KsqlQueries;
public $sql;
public function __construct($db_host, $db_user, $db_pass, $db_name, $db_charset, $port = 3306) {
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_STRICT;
try {
parent::__construct($db_host, $db_user, $db_pass, $db_name, $port);
$this->set_charset($db_charset);
}
catch(mysqli_sql_exception $e) {
throw $e;
}
}
//
# пересмотреть старый класс
//
public function sqlBuild() {
if (!$this->pre) {
return false;
} else {
$this->sql = $this->pre;
$this->sql .= ((sizeof($this->table) > 1) ? implode(' JOIN ', $this->art($this->table)) : $this->art(array_shift($this->table))) . ' ';
$this->sql .= ($this->post ? $this->post . ' ' : '');
$this->sql .= ((sizeof($this->on) > 0) ? $this->concat($this->on, 1) . ' ' : '');
$this->sql .= ((sizeof($this->where) > 0) ? 'WHERE ' . $this->concat($this->where, 1) . ' ' : '');
$this->sql .= ((sizeof($this->group) > 0) ? 'GROUP BY ' . $this->concat($this->group, 1) . ' ' : '');
$this->sql .= ((sizeof($this->order) > 0) ? 'ORDER BY ' . $this->concat($this->order, 1) . ' ' : '');
$this->sql .= ($this->limit ? 'LIMIT ?, ? ;' : ';');
return $this;
}
}
public function test() {
return '<pre>' . print_r($this->sql, 1) . '</pre>' . '<pre>' . print_r($this->placeholders, 1) . '</pre>';
}
}
$db_host = '127.0.0.1';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$db_charset = 'utf8';
$mysqli = new KMysqli($db_host, $db_user, $db_pass, $db_name, $db_charset);
$mysqli->select(array('users.name', 'users.id', 'forum_themes.name', 'forum_themes.id', 'forum_posts.text', 'forum_posts.time'))
->table('forum_posts')->join('forum_themes')->join('users')
# это так же будет работать
# ->on('forum_posts.author', 'users.id')->on('forum_themes.id', 'forum_posts.tid')
->on(array(array('forum_posts.author', 'users.id'), array('forum_themes.id', 'forum_posts.tid')))
->limit(1);
# SELECT `users`.`name`, `users`.`id`, `forum_themes`.`name`, `forum_themes`.`id`, `forum_posts`.`text`, `forum_posts`.`time` FROM `forum_posts` JOIN `forum_themes` JOIN `users` `forum_posts`.`author` = `users`.`id` AND `forum_themes`.`id` = `forum_posts`.`tid` LIMIT ?, ? ;
$mysqli->reset();
$mysqli->select()->table('users')
->where('id', '=', 1)
->order('id', 'desc')
->group('name')
->limit(20, 10);
$mysqli->reset();
$mysqli->insert(array('name', 'pass'), array('vasya', 'dsfsdfsdf'))->table('users');
$mysqli->reset();
$mysqli->update(array('name' => 'Vasya'))->table('users')->where('id', '=', 1);
echo $mysqli->sqlBuild()->test();
#$mysqli->reset();
echo '<pre>' . print_r($mysqli, 1) . '</pre>';