master

laravel/framework

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

PostgresConnection.php

TLDR

The PostgresConnection.php file is a class that extends the Connection class. It provides methods for interacting with a PostgreSQL database, including escaping values, determining unique constraint violations, getting query and schema grammar instances, creating a schema builder, getting the schema state, getting the default post processor instance, and getting the Doctrine DBAL driver for PostgreSQL.

Methods

escapeBinary

This method escapes a binary value for safe SQL embedding by converting it to hexadecimal and appending the bytea type casting.

escapeBool

This method escapes a boolean value for safe SQL embedding by returning "true" if the value is true, and "false" if the value is false.

isUniqueConstraintError

This method determines if the given database exception was caused by a unique constraint violation. It checks if the exception code is equal to 23505.

getDefaultQueryGrammar

This method returns the default query grammar instance for the connection, specifically an instance of PostgresGrammar, which is a class that defines the grammar for constructing PostgreSQL queries.

getSchemaBuilder

This method returns a schema builder instance for the connection, specifically an instance of PostgresBuilder, which is a class for building schemas specific to PostgreSQL.

getDefaultSchemaGrammar

This method returns the default schema grammar instance for the connection, specifically an instance of PostgresGrammar, which is a class that defines the grammar for constructing PostgreSQL schema statements.

getSchemaState

This method returns the schema state for the connection, specifically an instance of PostgresSchemaState, which is a class that manages the state of the PostgreSQL schema.

getDefaultPostProcessor

This method returns the default post processor instance for the connection, specifically an instance of PostgresProcessor, which is a class that processes the results of queries executed against the PostgreSQL database.

getDoctrineDriver

This method returns the Doctrine DBAL (Database Abstraction Layer) driver for PostgreSQL, specifically an instance of PostgresDriver, which is a class that implements the Doctrine driver interface.

Classes

No additional classes are defined in the file.

<?php

namespace Illuminate\Database;

use Exception;
use Illuminate\Database\PDO\PostgresDriver;
use Illuminate\Database\Query\Grammars\PostgresGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Schema\Grammars\PostgresGrammar as SchemaGrammar;
use Illuminate\Database\Schema\PostgresBuilder;
use Illuminate\Database\Schema\PostgresSchemaState;
use Illuminate\Filesystem\Filesystem;

class PostgresConnection extends Connection
{
    /**
     * Escape a binary value for safe SQL embedding.
     *
     * @param  string  $value
     * @return string
     */
    protected function escapeBinary($value)
    {
        $hex = bin2hex($value);

        return "'\x{$hex}'::bytea";
    }

    /**
     * Escape a bool value for safe SQL embedding.
     *
     * @param  bool  $value
     * @return string
     */
    protected function escapeBool($value)
    {
        return $value ? 'true' : 'false';
    }

    /**
     * Determine if the given database exception was caused by a unique constraint violation.
     *
     * @param  \Exception  $exception
     * @return bool
     */
    protected function isUniqueConstraintError(Exception $exception)
    {
        return '23505' === $exception->getCode();
    }

    /**
     * Get the default query grammar instance.
     *
     * @return \Illuminate\Database\Query\Grammars\PostgresGrammar
     */
    protected function getDefaultQueryGrammar()
    {
        ($grammar = new QueryGrammar)->setConnection($this);

        return $this->withTablePrefix($grammar);
    }

    /**
     * Get a schema builder instance for the connection.
     *
     * @return \Illuminate\Database\Schema\PostgresBuilder
     */
    public function getSchemaBuilder()
    {
        if (is_null($this->schemaGrammar)) {
            $this->useDefaultSchemaGrammar();
        }

        return new PostgresBuilder($this);
    }

    /**
     * Get the default schema grammar instance.
     *
     * @return \Illuminate\Database\Schema\Grammars\PostgresGrammar
     */
    protected function getDefaultSchemaGrammar()
    {
        ($grammar = new SchemaGrammar)->setConnection($this);

        return $this->withTablePrefix($grammar);
    }

    /**
     * Get the schema state for the connection.
     *
     * @param  \Illuminate\Filesystem\Filesystem|null  $files
     * @param  callable|null  $processFactory
     * @return \Illuminate\Database\Schema\PostgresSchemaState
     */
    public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
    {
        return new PostgresSchemaState($this, $files, $processFactory);
    }

    /**
     * Get the default post processor instance.
     *
     * @return \Illuminate\Database\Query\Processors\PostgresProcessor
     */
    protected function getDefaultPostProcessor()
    {
        return new PostgresProcessor;
    }

    /**
     * Get the Doctrine DBAL driver.
     *
     * @return \Illuminate\Database\PDO\PostgresDriver
     */
    protected function getDoctrineDriver()
    {
        return new PostgresDriver;
    }
}