DatabaseExceptionWrapper example


      }
      else {
        // Inserting multiple rows requires a transaction to be atomic, and         // executes faster as a single transaction.         try {
          $transaction = $this->connection->startTransaction();
        }
        catch (\PDOException $e) {
          // $this->connection->exceptionHandler()->handleExecutionException()           // requires a $statement argument, so we cannot use that.           throw new DatabaseExceptionWrapper($e->getMessage(), 0, $e);
        }
        foreach ($this->insertValues as $insert_values) {
          try {
            $stmt->execute($insert_values$this->queryOptions);
          }
          catch (\Exception $e) {
            // One of the INSERTs failed, rollback the whole batch.             $transaction->rollBack();
            $this->connection->exceptionHandler()->handleExecutionException($e$stmt$insert_values$this->queryOptions);
          }
        }
      }

    return $statement;
  }

  public function nextId($existing_id = 0) {
    try {
      $this->startTransaction();
    }
    catch (\PDOException $e) {
      // $this->exceptionHandler()->handleExecutionException()       // requires a $statement argument, so we cannot use that.       throw new DatabaseExceptionWrapper($e->getMessage(), 0, $e);
    }

    // We can safely use literal queries here instead of the slower query     // builder because if a given database breaks here then it can simply     // override nextId. However, this is unlikely as we deal with short strings     // and integers and no known databases require special handling for those     // simple cases. If another transaction wants to write the same row, it will     // wait until this transaction commits.     $stmt = $this->prepareStatement('UPDATE {sequences} SET [value] = GREATEST([value], :existing_id) + 1', [], TRUE);
    $args = [':existing_id' => $existing_id];
    try {
      
$view->total_rows = $view->pager->getTotalItems();

        // Load all entities contained in the results.         $this->loadEntities($view->result);
      }
      catch (DatabaseExceptionWrapper $e) {
        $view->result = [];
        if (!empty($view->live_preview)) {
          $this->messenger->addError($e->getMessage());
        }
        else {
          throw new DatabaseExceptionWrapper("Exception in {$view->storage->label()}[{$view->storage->id()}]: {$e->getMessage()}");
        }
      }

    }
    else {
      $start = microtime(TRUE);
    }
    $view->execute_time = microtime(TRUE) - $start;
  }

  /** * Loads all entities contained in the passed-in $results. * * If the entity belongs to the base table, then it gets stored in * $result->_entity. Otherwise, it gets stored in * $result->_relationship_entities[$relationship_id]; * * @param \Drupal\views\ResultRow[] $results * The result of the SQL query. */
if ($exception instanceof \PDOException) {
      // Wrap the exception in another exception, because PHP does not allow       // overriding Exception::getMessage(). Its message is the extra database       // debug information.       $code = is_int($exception->getCode()) ? $exception->getCode() : 0;

      // If a max_allowed_packet error occurs the message length is truncated.       // This should prevent the error from recurring if the exception is logged       // to the database using dblog or the like.       if (($exception->errorInfo[1] ?? NULL) === 1153) {
        $message = Unicode::truncateBytes($exception->getMessage(), Connection::MIN_MAX_ALLOWED_PACKET);
        throw new DatabaseExceptionWrapper($message$code$exception);
      }

      $message = $exception->getMessage() . ": " . $statement->getQueryString() . "; " . print_r($arguments, TRUE);

      // SQLSTATE 23xxx errors indicate an integrity constraint violation. Also,       // in case of attempted INSERT of a record with an undefined column and no       // default value indicated in schema, MySql returns a 1364 error code.       if (
        substr($exception->getCode(), -6, -3) == '23' ||
        ($exception->errorInfo[1] ?? NULL) === 1364
      ) {
        

  public function lastInsertId(?string $name = NULL): string {
    if (($last_insert_id = $this->connection->lastInsertId($name)) === FALSE) {
      throw new DatabaseExceptionWrapper("Could not determine last insert id" . $name === NULL ? '' : " for sequence $name");
    }
    return $last_insert_id;
  }

  /** * Prepares and returns a MERGE query object. * * @param string $table * The table to use for the merge statement. * @param array $options * (optional) An array of options on the query. * * @return \Drupal\Core\Database\Query\Merge * A new Merge query object. * * @see \Drupal\Core\Database\Query\Merge */

  public function handleStatementException(\Exception $exception, string $sql, array $options = []): void {
    if ($exception instanceof \PDOException) {
      // Wrap the exception in another exception, because PHP does not allow       // overriding Exception::getMessage(). Its message is the extra database       // debug information.       $message = $exception->getMessage() . ": " . $sql . "; ";
      throw new DatabaseExceptionWrapper($message, 0, $exception);
    }

    throw $exception;
  }

  /** * Handles exceptions thrown during execution of statement objects. * * @param \Exception $exception * The exception to be handled. * @param \Drupal\Core\Database\StatementInterface $statement * The statement object requested to be executed. * @param array $arguments * An array of arguments for the prepared statement. * @param array $options * An associative array of options to control how the database operation is * run. * * @throws \Drupal\Core\Database\DatabaseExceptionWrapper * @throws \Drupal\Core\Database\IntegrityConstraintViolationException */
'expected_version_string' => FALSE,
        'schema_version' => '123',
        'exception' => NULL,
        'table_map' => [
          ['system', TRUE],
          ['key_value', FALSE],
        ],
      ],
      'D5/6/7 Exception' => [
        'expected_version_string' => FALSE,
        'schema_version' => NULL,
        'exception' => new DatabaseExceptionWrapper(),
        'table_map' => [
          ['system', TRUE],
          ['key_value', FALSE],
        ],
      ],
      'D8/9 Exception' => [
        'expected_version_string' => FALSE,
        'schema_version' => NULL,
        'exception' => new DatabaseExceptionWrapper(),
        'table_map' => [
          ['system', FALSE],
          [
Home | Imprint | This part of the site doesn't use cookies.