protected function prepareSqlAndArgs() { $sql = 'UPDATE ' . $this->db->quoteIdentifier($this->table->getTableName()) . ' SET '; $args = []; foreach ($this->fields as $name => $value) { $sql .= '' . $this->db->quoteIdentifier($name) . ' = ?, '; $args[] = $value; } $sql = substr($sql, 0, -2); list($whereSql, $whereArgs) = $this->getWhereSqlAndArgs(); $sql .= $whereSql; $args = array_merge($args, $whereArgs); return [$sql, $args]; }
protected function getWhereSqlAndArgs() { $whereParts = []; $args = []; foreach ($this->filter as $field => $value) { $whereParts[] = $this->db->quoteIdentifier($field) . ' = ?'; $args[] = $value; } foreach ($this->filterIn as $field => $value) { $whereParts[] = $this->db->quoteIdentifier($field) . ' = ?l'; $args[] = $value; } foreach ($this->exclude as $field => $value) { $whereParts[] = $this->db->quoteIdentifier($field) . ' != ?'; $args[] = $value; } return [join(' AND ', $whereParts), $args]; }
public function one() { $quotedFields = array_map(function ($field) { return $this->db->quoteIdentifier($field); }, array_keys($this->fields)); $placeholders = array_map(function ($field) { return '?'; }, array_values($this->fields)); $fieldsSql = join(', ', $quotedFields); $placeholdersSql = join(', ', $placeholders); $sql = 'INSERT INTO ' . $this->db->quoteIdentifier($this->table->getTableName()) . ' (' . $fieldsSql . ') VALUES (' . $placeholdersSql . ')'; if ($this->db instanceof AdapterPgsqlConnection) { $row = $this->table->sqlOne($sql . ' RETURNING *', array_values($this->fields)); } elseif ($this->db instanceof AdapterMysqliConnection) { $this->db->execute($sql, array_values($this->fields)); $row = $this->table->id($this->db->lastInsertId()); } else { throw new \Exception('Unreacheable statement'); } return $row; }
protected function quoteIdentifier($identifer) { return $this->db->quoteIdentifier($identifer); }