Skip to content

EpiDatabase #44

@rrasto

Description

@rrasto

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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions