statement = $statement; $this->logQueryCallable = $logQueryCallable; } public function execute(array $input_parameters = []) { // Run the query and benchmark the time $benchmarkStart = microtime(true); $result = $this->statement->execute($input_parameters); $benchmarkEnd = microtime(true) - $benchmarkStart; $errInfo = $this->error(); call_user_func_array($this->logQueryCallable, [$this->statement->queryString, $this->statement->rowCount(), $benchmarkEnd, $errInfo]); // If the query failed, throw an error if ($result === false) { // And throw an exception throw new DatabaseException("Could not run query. Database returned an error. Error code: " . $errInfo['code']); } return $result; } /** * Generates an error message for the last failure in PDO * * @return array */ private function error(): array { $error = []; $pdoError = $this->statement->errorInfo(); if (empty($pdoError[0]) || $pdoError[0] == '00000') return $error; $error['code'] = isset($pdoError[1]) ? $pdoError[0] . '/' . $pdoError[1] : $pdoError[0]; if (isset($pdoError[2])) $error['message'] = $pdoError[2]; return $error; } public function __call($method, $parameters) { return call_user_func_array([$this->statement, $method], $parameters); } public function __get($name) { return $this->statement->$name; } }