. */ 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 addWhereEquals($field, $value) { $this->addWhereOperator($field, $value, "="); } public function addWhereNotEquals($field, $value) { $this->addWhereOperator($field, $value, "!="); } public function addWhereGreaterEquals($field, $value) { $this->addWhereOperator($field, $value, ">="); } public function addWhereSmallerEquals($field, $value) { $this->addWhereOperator($field, $value, "<="); } public function addWhereGreater($field, $value) { $this->addWhereOperator($field, $value, ">"); } public function addWhereSmaller($field, $value) { $this->addWhereOperator($field, $value, "<"); } private function addWhereOperator($field, $value, $operator) { $this->where[] = $this->db->escapeField($field) . " " . $operator . " " . $this->db->escapeValue($value); } public function addWhereIn($field, array $values) { $escapedValues = array(); foreach($values as $value) { $escapedValues[] = $this->db->escapeValue($value); } $this->where[] = $this->db->escapeField($field) . " IN (" . implode(", ", $escapedValues) . ")"; } public function addWhereIsNull($field) { $this->where[] = $this->db->escapeField($field) . " IS NULL"; } public function addWhereIsNotNull($field) { $this->where[] = $this->db->escapeField($field) . " IS NOT NULL"; } 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; } } ?>