epiphany
epiphany copied to clipboard
EpiDatabase
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;
}
}
Thanks for this. I will review and merge this but it may take me a little bit :)