Welcome Guest, Not a member yet? Register   Sign In
Extend MySQL DB Driver to issue REPLACE INTO queries
#1

[eluser]SeasonedCoder[/eluser]
MySQL has a very convenient statement - REPLACE INTO - that I'd like to use in my code. Unfortunately, I haven't found appropriate method in Active Record. So, I'm thinking about extending it's functionality to include REPLACE method, but not sure where to start.

Has anybody tried to accomplish this? If yes, could you share your experience?
#2

[eluser]Dam1an[/eluser]
Based on the definition given on the MySQL site
Quote:REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted

A very inefficient way to do it which springs to mine, is to have a replace function in the DB class, which when called check if the PK/unique fields exist, if so, create a delete clause and exacute it, and then call the insert method, else, just call insert

Pseudo code:
Code:
function replace() {
  if primary_key || unique_key exists
    $this->delete where pk = $pk etc
  
  // Now we've removed any conflicting rows, so insert as normal
  $this->insert();
}
#3

[eluser]SeasonedCoder[/eluser]
Well, I was thinking about adding the REPLACE INTO directly.
#4

[eluser]Dam1an[/eluser]
Have you looked at the source code for DB_active record, and the mysql_driver?
It actually looks to be pretty simple (assuming you know the syntax for the statement) is should mostly a copy and paste job with a few changes

If you get stuck with it, I might have a go later on




Theme © iAndrew 2016 - Forum software by © MyBB