Adding support for MySQL REPLACE operator

Notice: This thread is very old.
ivanscm
Member | 16
+
0
-

In one of the projects on the Nette Framework I needed support for MySQL REPLACE operator level framework.

For this, I added the following:

“Nette\Database\Table\Selection.php”

public function replace($data)
	{
		if ($data instanceof Selection) {
			$data = $data->getSql();

		} elseif ($data instanceof \Traversable) {
			$data = iterator_to_array($data);
		}

		$return = $this->connection->query($this->sqlBuilder->buildReplaceQuery(), $data);
		$this->checkReferenced = TRUE;

		if (!is_array($data)) {
			return $return->rowCount();
		}

		if (!is_array($this->primary) && !isset($data[$this->primary]) && ($id = $this->connection->lastInsertId($this->getPrimarySequence()))) {
			$data[$this->primary] = $id;
		}

		$row = $this->createRow($data);
		if ($signature = $row->getSignature(FALSE)) {
			$this->rows[$signature] = $row;
		}

		return $row;
	}

“Nette\Database\Table\SqlBuilder.php”

public function buildReplaceQuery()
	{
		return "REPLACE INTO {$this->delimitedTable}";
	}

I want to say this is not a complete solution. It can be simplified and improved. Later publish a good option.

Last edited by ivanscm (2013-02-18 10:33)

hrach
Member | 1838
+
0
-

Well, Nette\Database must be driver(database) independent so using replace is not the right way. However, we are planning other way to do it. But not yet done.