Adding support for MySQL REPLACE operator

1. about a year ago

ivanscm
Member
Registered: 2012-03-06
Location: Kazahstan
Posts: 16

Adding support for MySQL REPLACE operator

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)

 

2. about a year ago

hrach
Moderator
Registered: 2007-03-11
Location: Brno
Posts: 1302

Re: Adding support for MySQL REPLACE operator

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.

 

Board footer

Switch to desktop