. */ require_once dirname(__FILE__) . "/IDB.php"; require_once dirname(__FILE__) . "/DBMethod.php"; require_once dirname(__FILE__) . "/DBWhere.php"; class QueryBuilder { private $db; private $table; private $fields = array(); private $joins = array(); private $where = array(); private $group = array(); private $order = array(); private $limit = -1; private $offset = -1; public function __construct(IDB $db, $table) { $this->db = $db; $this->table = $table; } public function addField($field, $as = NULL) { if( is_array($field) ) { foreach( $field as $singleField ) { $this->fields[] = $this->db->escapeField($singleField); } }else { $this->fields[] = $this->db->escapeField($field) . ($as == NULL ? "" : " AS " . $this->db->escapeField($as)); } } public function addLeftJoin($table, $field, $joinOn, $as = NULL) { $this->addJoin($table, $field, $joinOn, "LEFT", $as); } public function addInnerJoin($table, $field, $joinOn, $as = NULL) { $this->addJoin($table, $field, $joinOn, "INNER", $as); } private function addJoin($table, $field, $joinOn, $joinMethod, $as = NULL) { $join = $joinMethod . " JOIN " . $this->db->escapeField($table); if( $as == NULL ) { $baseTablename = $table; }else { $join .= " " . $this->db->escapeField($as); $baseTablename = $as; } $join .= " ON "; if( is_array($field) && is_array($joinOn) ) { $joinParts = array(); for( $i = 0; $i < count($field); $i++ ) { if( $field[$i] instanceof DBMethod ) { $joinParts[] = $this->db->escapeField($field[$i]) . " = " . $this->db->escapeField($joinOn[$i]); }else { $joinParts[] = $this->db->escapeField($baseTablename . "." . $field[$i]) . " = " . $this->db->escapeField($joinOn[$i]); } } $join .= implode(" AND ", $joinParts); }else { $join .= $this->db->escapeField($baseTablename . "." . $field) . " = " . $this->db->escapeField($joinOn); } $this->joins[] = $join; } public function addWhere(DBWhere $where) { $this->where[] = $where->toQuery($this->db); } public function addWhereEquals($field, $value) { $this->addWhere(new DBWhereEquals($field, $value)); } public function addWhereNotEquals($field, $value) { $this->addWhere(new DBWhereNotEquals($field, $value)); } public function addWhereGreaterEquals($field, $value) { $this->addWhere(new DBWhereGreaterEquals($field, $value)); } public function addWhereSmallerEquals($field, $value) { $this->addWhere(new DBWhereSmallerEquals($field, $value)); } public function addWhereGreater($field, $value) { $this->addWhere(new DBWhereGreater($field, $value)); } public function addWhereSmaller($field, $value) { $this->addWhere(new DBWhereSmaller($field, $value)); } public function addWhereIn($field, array $values) { $this->addWhere(new DBWhereIn($field, $values)); } public function addWhereIsNull($field) { $this->addWhere(new DBWhereIsNull($field)); } public function addWhereIsNotNull($field) { $this->addWhere(new DBWhereIsNotNull($field)); } public function addGroup($field) { $this->group[] = $this->db->escapeField($field); } public function addOrderAsc($field) { $this->order[] = $this->db->escapeField($field) . " ASC"; } public function addOrderDesc($field) { $this->order[] = $this->db->escapeField($field) . " DESC"; } public function setLimit($limit) { $this->limit = $limit; } public function setOffset($offset) { $this->offset = $offset; } public function toQuery() { $query = "SELECT "; if( count($this->fields) > 0 ) { $query .= implode(", ", $this->fields); }else { $query .= "*"; } $query .= " FROM " . $this->db->escapeField($this->table); if( count($this->joins) > 0 ) { $query .= " " . implode(" ", $this->joins); } if( count($this->where) > 0 ) { $query .= " WHERE " . implode(" AND ", $this->where); } if( count($this->group) ) { $query .= " GROUP BY " . implode(", ", $this->group); } if( count($this->order) ) { $query .= " ORDER BY " . implode(", ", $this->order); } if( $this->limit >= 0 ) { $query .= " LIMIT " . $this->db->escapeValue($this->limit); if( $this->offset >= 0 ) { $query .= "OFFSET " . $this->db->escapeValue($this->offset); } } return $query; } } ?>