0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自作SQLcontrolクラス

Last updated at Posted at 2013-06-24

遠藤フレームワーク内での使用を想定

SQLcontrol.class.php

<?php
class SQLcontrol {
    private $field = null;
    private $table = null;
    private $where = null;
    private $order = "updated DESC";
    private $limit = null;
    private $data = null;
    public function addField($field){   //フィールドの追加
        //空の場合
        if(is_null($this->field)){ $this->field = $field; }
        //空でない場合
        else{ $this->field .= ",".$field; }
    }
    public function addTable($table){   //テーブルの追加
        //空の場合
        if(is_null($this->table)){ $this->table = $table; }
        //空でない場合
        else{ $this->table .= ",".$table; }
    }
    public function addWhere($where,$data=null){   //WHERE句の追加(AND)
        //空の場合
        if(is_null($this->where)){ $this->where = " WHERE ".$where; }
        //空でない場合
        else{ $this->where .= " AND ".$where; }
        if(!is_null($data)){ $this->data[] = $data; }
    }
    public function addWhereOR($where,$data=null){   //WHERE句の追加(OR)
        //空の場合
        if(is_null($this->where)){ $this->where = " WHERE ".$where; }
        //空でない場合
        else{ $this->where .= " OR ".$where; }
        if(!is_null($data)){ $this->data[] = $data; }
    }
    public function addOrder($order){   //ORDER句の追加
        $this->order = $order;
    }
    public function addLimit($limit){   //LIMIT句の追加
        $this->limit = " LIMIT ".$limit;
    }
    public function addData($data){   //Dataだけ追加
        $this->data[] = $data;
    }
    public function addString($item,$string){   //それぞれのitemに文字列を追加する関数
        switch($item){
            case "field":
                $this->field .= $string;
                break;
            case "table":
                $this->table .= $string;
                break;
            case "where":
                $this->where .= $string;
                break;
            case "order":
                $this->order .= $string;
                break;
            case "limit":
                $this->limit .= $string;
                break;
        }
    }
    public function getSQL(){
        if(is_null($this->field)){ $this->field = "*"; }
        return "SELECT ".$this->field." FROM ".$this->table.$this->where." ORDER BY ".$this->order.$this->limit;
    }
    public function getData(){
        return $this->data;
    }
}
?>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?