epiphany icon indicating copy to clipboard operation
epiphany copied to clipboard

EpiDatabase

Open rrasto opened this issue 12 years ago • 1 comments

If you execute an SQL statement like this: INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; Your original solution contains the following error. If you try to insert a duplicate key, the INSERT statement is not executed and the SQL statement returns 0. But the subsequent UPDATE changed some rows, so one should expect that the function returns the number of updated rows.

public function execute($sql = false, $params = array())
{
  $this->init();
  try
  {
    $sth = $this->prepare($sql, $params);

    /* original solution
    if(preg_match('/insert/i', $sql))
      return $this->dbh->lastInsertId();
    else
      return $sth->rowCount();*/

    /* my quick fix */
    return ($this->dbh->lastInsertId() > $sth->rowCount()) ? $this->dbh->lastInsertId() : $sth->rowCount();
  }
  catch(PDOException $e)
  {
    EpiException::raise(new EpiDatabaseQueryException("Query error: {$e->getMessage()} - {$sql}"));
    return false;
  }
}

rrasto avatar Sep 22 '12 17:09 rrasto

Thanks for this. I will review and merge this but it may take me a little bit :)

jmathai avatar Oct 09 '12 17:10 jmathai