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

.
ramzes
поправка (+/-)

/**
 * Class SqlBuilder
 * @package microapp
 */
class SqlBuilder {

    /**
     * @var array
     */
    private $query = array();
    private $compilate = '';

    public function __construct(){
        return $this;
    }

    public function select($query){
        $this->query['select'] = is_array($query) ? implode(', ', array_map(array(get_class($this), 'article'), $query)) : $this->article($query);
        return $this;
    }

    public function insert($query){
        $this->query['insert'] = false;
        return $this;
    }

    public function update(){
        $this->query['update'] = false;
        return $this;
    }

    public function delete(){
        $this->query['delete'] = false;
        return $this;
    }

    public function from($table){
        $this->query['from'] = $this->article($table);
        return $this;
    }

    public function left($table){
        $this->query['left'] = $this->article($table);
        return $this;
    }

    public function right($table){
        $this->query['right'] = $this->article($table);
        return $this;
    }

    public function on($query){
        $this->query['on'] = $query;
        return $this;
    }

    public function where($where){
        if(preg_match('#([^=<>\!]+)#', $where, $row)){
            $this->query['where'] = str_replace($row[1], $this->article($row[1]), $where);
        }else{
            $this->query['where'] = $where;
        }
        return $this;
    }

    public function order($field, $type = 'ASC'){
        $this->query['order'][] = $this->article($field).' '.(strtoupper($type)=='ASC' ? 'ASC' : 'DESC');
        return $this;
    }

    public function limit($start, $len = false){
        $this->query['limit'] = $start;
        if($len!=false){
            $this->query['limit_len'] = $len;
        }
        return $this;
    }

    public function set(array $value){
        $this->query['set'] = $value;
        return $this;
    }



    public function run(){
        foreach ($this->query as $q => $v) {
            switch ($q) {
                case 'limit':
                    $this->compilate.= ' LIMIT';
                    break;
                case 'limit_len':
                    $this->compilate.= ' ,';
                    break;
                case 'order':
                    $this->compilate.= ' ORDER BY';
                    break;
                case 'sort':
                    $this->compilate.= '';
                    break;
                case 'right':
                    $this->compilate.= ' RIGHT JOIN';
                    break;
                case 'left':
                    $this->compilate.= ' LEFT JOIN';
                    break;
                case 'insert':
                    $this->compilate.= ' INSERT INTO';
                    break;
                default:
                    $this->compilate.= ' '.strtoupper($q);
                    break;
            }

            if($q=='on') {
                $split = explode('=', $v);
                $this->compilate .= ' ' . $this->query['from'] . '.' . $this->article($split[0]) . ' = ' . (isset($this->query['left']) ? $this->query['left'] : $this->query['right']) . '.' . $this->article($split[1]) . ' ';
            }else if($q=='order'){
                $this->compilate .= implode(', ', $v);
            }else if (is_array($v)) {
                $this->compilate .= ' ' . implode(', ', $v);
            }else{
                $this->compilate.= ($v != false) ? ' ' . $v . ' ' : '';
            }
        }
    }

    public function exec(){
        $this->run();
        return microapp::sql()->query($this->compilate);
    }

    public function view(){
        $this->run();
        return $this->compilate;
    }

    private function article($row){
        return preg_replace('|([a-z\d_\-]+)|is', '`\1`', str_replace('`', '', trim($row) ) );
    }

}

SELECT `forum_thread`.*, `users`.`login`, `users`.`ontime`, `users`.`name` 
FROM `forum_thread`  LEFT JOIN `users` 
ON `forum_thread`.`last_post_author` = `users`.`id` 
WHERE `forum_thread`.`parent_id`= 4 
ORDER BY`forum_thread`.`thread_fixed` DESC, `forum_thread`.`last_time` DESC
LIMIT 20