master

laravel/framework

Last updated at: 29/12/2023 09:22

QueryException.php

TLDR

The QueryException.php file in the Illuminate\Database namespace contains a class called QueryException that extends the PDOException class. It represents an exception that is thrown when a database query encounters an error. The class provides methods to get the connection name, the SQL query, and the bindings used in the query.

Classes

QueryException

The QueryException class extends the PDOException class and represents an exception that is thrown when a database query encounters an error. It contains the following properties:

  • $connectionName: The database connection name.
  • $sql: The SQL query.
  • $bindings: The bindings for the query.

The class provides the following methods:

  • __construct($connectionName, $sql, array $bindings, Throwable $previous): Creates a new QueryException instance. It sets the connection name, SQL query, bindings, error code, and error message.
  • formatMessage($connectionName, $sql, $bindings, Throwable $previous): Formats the SQL error message.
  • getConnectionName(): Returns the connection name for the query.
  • getSql(): Returns the SQL query.
  • getBindings(): Returns the bindings for the query.
<?php

namespace Illuminate\Database;

use Illuminate\Support\Str;
use PDOException;
use Throwable;

class QueryException extends PDOException
{
    /**
     * The database connection name.
     *
     * @var string
     */
    public $connectionName;

    /**
     * The SQL for the query.
     *
     * @var string
     */
    protected $sql;

    /**
     * The bindings for the query.
     *
     * @var array
     */
    protected $bindings;

    /**
     * Create a new query exception instance.
     *
     * @param  string  $connectionName
     * @param  string  $sql
     * @param  array  $bindings
     * @param  \Throwable  $previous
     * @return void
     */
    public function __construct($connectionName, $sql, array $bindings, Throwable $previous)
    {
        parent::__construct('', 0, $previous);

        $this->connectionName = $connectionName;
        $this->sql = $sql;
        $this->bindings = $bindings;
        $this->code = $previous->getCode();
        $this->message = $this->formatMessage($connectionName, $sql, $bindings, $previous);

        if ($previous instanceof PDOException) {
            $this->errorInfo = $previous->errorInfo;
        }
    }

    /**
     * Format the SQL error message.
     *
     * @param  string  $connectionName
     * @param  string  $sql
     * @param  array  $bindings
     * @param  \Throwable  $previous
     * @return string
     */
    protected function formatMessage($connectionName, $sql, $bindings, Throwable $previous)
    {
        return $previous->getMessage().' (Connection: '.$connectionName.', SQL: '.Str::replaceArray('?', $bindings, $sql).')';
    }

    /**
     * Get the connection name for the query.
     *
     * @return string
     */
    public function getConnectionName()
    {
        return $this->connectionName;
    }

    /**
     * Get the SQL for the query.
     *
     * @return string
     */
    public function getSql()
    {
        return $this->sql;
    }

    /**
     * Get the bindings for the query.
     *
     * @return array
     */
    public function getBindings()
    {
        return $this->bindings;
    }
}