Просмотр поста

.
Koenig
(\/)____o_O____(\/)

мыслей много, но требуются додумки

class KMysqli extends Mysqli {
    use KsqlUtils, KsqlArgs, KsqlQueries;
    
    //
    # пересмотреть старый класс
    //
    
    public function sqlBuild() {
        // тут сам конструктор из массивов
    }
}

замутки (+/-)

trait KsqlQueries {
    
    private $pre = false;
    private $post = false;
    
    public function select($fieldsListArray = '*') {
        $this->pre = 'SELECT ' . (is_array($fieldsListArray) ? $this->concat(array_values($fieldsListArray)) : $fieldsListArray);
        $this->post = ' FROM ';
        
        return $this;
    }
    
    public function count($field = '*') {
        $this->pre = 'SELECT COUNT(' . $this->art($field) . ')';
        $this->post = ' FROM ';
        
        return $this;
    }
    
    public function delete() {
        $this->pre = 'DELETE';
        $this->post = ' FROM ';
        
        return $this;
    }
    
    public function insert() {
        $this->pre = 'INSERT INTO';
        $this->post = ' ';
        
        return $this;
    }
    
    public function update() {
        $this->pre = 'UPDATE';
        $this->post = ' ';
        
        return $this;
    }
}

trait KsqlArgs {
    
    private $table = array();
    private $on = array();
    private $limit;
    private $offset;
    private $where = array();
    private $order = array();
    private $group = array();
    
    public function table($table) {
        $this->table[] = $table;
        
        return $this;
    }
    
    public function on($field1, $field2) {
        $this->on[] = $field1 . ' = ' . $field2;
        
        return $this;
    }
    
    public function where($field, $type, $value) {
        $this->where[] = $this->art($field) . ' ' . $type . ' ' . esc($value);
        
        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 offset($offset = 0) {
        $this->offset = $offset;
        
        return $this;
    }
    
    public function limit($limit) {
        $this->limit = $limit;
    }
}

trait KsqlUtils {
    
    public function concat($array, $type = 'comma') {
        $types = array('comma' => ',', 1 => 'and');
        return $this->art(implode(' ' . $types[$type], array_values($array)));
    }
    
    public function art($string) {
        return '`' . str_replace('.', '`.`', str_replace('`', '', $string)) . '`';
    }
    
    public function esc($data) {
        if (!is_array($data)) {
            $data = is_int($data) ? $data : '"' . mysql_real_escape_string($data) . '"';
        } else {
            $data = array_map(array($this, 'esc'), $data);
        }

        return $data;
    }

}


ну и то что пока получается по вызову
$db_host = '127.0.0.1'; 
$db_user = 'root'; 
$db_pass = ''; 
$db_name = 'test'; 
$db_charset = 'utf-8'; 

$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')->table('forum_themes')->table('users')
    ->on('forum_posts.author', 'users.id')->on('forum_themes.id', 'forum_posts.tid')
    ->limit(1);