PHP Code:
protected function execute(string $sql)
{
while ($this->connID->more_results()) {
$this->connID->next_result();
if ($res = $this->connID->store_result()) {
$res->free();
}
}
try {
return $this->connID->query($this->prepQuery($sql), $this->resultMode);
} catch (mysqli_sql_exception $e) {
log_message('error', (string) $e);
if ($this->DBDebug) {
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
}
}
return false;
}
My issue is: when encountering a duplicate key error (1062), the exception is caught, but NOT re-thrown UNLESS using the DBDebug flag.
Using SQL's INSERT IGNORE, the exception is not thrown. But, there is no error code from $this->db->error(). The only way I can think to detect the duplicate to use a transaction, do a SELECT, if that fails then do an INSERT.
Looking at this code, to me the if ($this->DBDebug) condition looks plain wrong. It's an accident waiting to happen. Using DBDebug and an appropriate try/catch block in the user's code, all works fine and dandy. In production, the code aborts because there is no re-throw.
It would be interesting to know the rationale for the dual behaviour. execute() must be one of the most commonly used methods in the framework.
Apologies if this has already been covered elsewhere. If I get time I'd like to update the documentation, the duplicate key handling required does not appear to be covered.