db = $db; } public function from($table, $columns = null) { $this->table = (string)$table; $this->columns = (array)$columns; return $this; } public function where($condition, $values = null, $conditionName = null) { $condition = (string)$condition; if ($values !== null) $condition = $this->db->quoteInto($condition, $values); if ($conditionName !== null && !is_numeric($conditionName)) $this->where[$conditionName] = $condition; else $this->where[] = $condition; return $this; } public function combineWhere($conditionName1, $conditionName2, $combineType = self::COMBINE_TYPE_AND, $conditionName = null) { $condition1 = isset($this->where[$conditionName1]) ? $this->where[$conditionName1] : null; $condition2 = isset($this->where[$conditionName2]) ? $this->where[$conditionName2] : null; if ($condition1 !== null && $condition2 !== null) { unset($this->where[$conditionName1], $this->where[$conditionName2]); $this->where($condition1 . ' ' . $combineType . ' ' . $condition2, null, $conditionName); } return $this; } public function resetWhere() { $this->where = array(); return $this; } public function order($column) { $this->order[] = (string)$column; return $this; } public function resetOrder() { $this->order = array(); return $this; } public function limit($limit, $offset = null) { $this->limit = ($limit === null) ? null : (int)$limit; $this->offset = ($offset === null) ? null : (int)$offset; return $this; } public function innerJoin($tableName, $on, array $columns = array()) { $this->join(self::JOIN_TYPE_INNER, $tableName, $on, $columns); return $this; } public function leftJoin($tableName, $on, array $columns = array()) { $this->join(self::JOIN_TYPE_LEFT, $tableName, $on, $columns); return $this; } private function join($type, $tableName, $on, $columns) { $this->columns = array_merge($this->columns, (array)$columns); $this->join[] = array('type' => $type, 'tableName' => $tableName, 'on' => $on); } private function renderWhereClause() { if ($this->where) return ' WHERE ' . implode(self::COMBINE_TYPE_AND, $this->where); return ''; } private function renderOrderClause() { if ($this->order) return ' ORDER BY ' . implode(', ', $this->order); return ''; } private function renderLimitClause($useOffset = true) { if ($this->limit !== null) { $sql = ' LIMIT '; if ($useOffset && ($this->offset !== null)) $sql .= $this->offset . ', '; $sql .= $this->limit; return $sql; } return ''; } private function renderJoinClause() { if ($this->join) { $sql = ''; foreach ($this->join as $join) $sql .= " {$join['type']} JOIN {$join['tableName']} ON ({$join['on']}) "; return $sql; } return ''; } public function renderSelectSql() { $sql = 'SELECT ' . implode(', ', $this->columns) . ' FROM ' . $this->table . $this->renderJoinClause() . $this->renderWhereClause() . $this->renderOrderClause() . $this->renderLimitClause(); return $sql; } /* public function update(array $values = array()) { $sql = 'UPDATE ' . $this->table; // Not Working $sql .= $this->renderWhereClause(); $sql .= $this->renderOrderClause(); $sql .= $this->renderLimitClause(false); } public function delete() { $sql = 'DELETE FROM ' . $this->table; $sql .= $this->renderWhereClause(); $sql .= $this->renderOrderClause(); $sql .= $this->renderLimitClause(false); return $sql; } */ }