遠藤フレームワーク内での使用を想定
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;
}
}
?>