public function prepare() { $sql = ['SELECT']; $fields = []; foreach ($this->fields as $key => $field) { if (is_int($key)) { $fields[] = $this->escapeName($field); } else { $fields[] = $this->escapeName($key) . ' AS ' . $this->escapeName($field); } } $sql[] = implode(', ', $fields); $sql[] = 'FROM'; $sql[] = $this->escapeName($this->table); $args = []; if ($this->conditions) { $sql[] = 'WHERE'; foreach ($this->conditions as $condition) { if (is_string($condition)) { $sql[] = $condition; } elseif (is_array($condition)) { $argName = ':' . $condition[0]; $sql[] = '('; $sql[] = $this->escapeName($condition[0]); $sql[] = $condition[1]; $sql[] = $argName; $sql[] = ')'; $args[$argName] = $condition[2]; } } } if ($this->limit) { $sql[] = 'LIMIT'; $sql[] = $this->limit; } $this->query = $this->db->prepare(implode(' ', $sql) . ';'); $this->args = $args; return $this; }