master

laravel/framework

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

MySqlConnection.php

TLDR

The provided file is MySqlConnection.php, which is located at src/Illuminate/Database/MySqlConnection.php. This file contains the definition of the MySqlConnection class, which extends the Connection class. The MySqlConnection class contains various methods that handle the functionality related to the MySQL database connection.

Methods

escapeBinary($value)

This method is used to escape a binary value for safe SQL embedding. It takes a string value and returns the escaped version of the value.

isUniqueConstraintError(Exception $exception)

This method is used to determine if a given database exception was caused by a unique constraint violation. It takes an Exception object as a parameter and returns a boolean indicating whether the exception is a unique constraint violation.

isMaria()

This method is used to determine if the connected database is a MariaDB database. It checks the server version attribute of the PDO instance and returns a boolean indicating whether it contains the string "MariaDB".

getDefaultQueryGrammar()

This method returns the default query grammar instance for the connection. It creates a new instance of the QueryGrammar class and sets the connection, and then returns the grammar instance.

getSchemaBuilder()

This method returns a schema builder instance for the connection. If the schema grammar is not set, it sets the default schema grammar. It creates a new instance of the MySqlBuilder class, passing the connection, and then returns the builder instance.

getDefaultSchemaGrammar()

This method returns the default schema grammar instance for the connection. It creates a new instance of the SchemaGrammar class and sets the connection, and then returns the grammar instance.

getSchemaState(Filesystem $files = null, callable $processFactory = null)

This method returns the schema state for the connection. It creates a new instance of the MySqlSchemaState class, passing the connection, the filesystem (optional), and the process factory (optional), and then returns the schema state instance.

getDefaultPostProcessor()

This method returns the default post processor instance for the connection. It creates a new instance of the MySqlProcessor class and returns the processor instance.

getDoctrineDriver()

This method returns the Doctrine DBAL driver for the connection. It creates a new instance of the MySqlDriver class and returns the driver instance.

Classes

No classes in this file

<?php

namespace Illuminate\Database;

use Exception;
use Illuminate\Database\PDO\MySqlDriver;
use Illuminate\Database\Query\Grammars\MySqlGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Database\Schema\MySqlSchemaState;
use Illuminate\Filesystem\Filesystem;
use PDO;

class MySqlConnection 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}'";
    }

    /**
     * Determine if the given database exception was caused by a unique constraint violation.
     *
     * @param  \Exception  $exception
     * @return bool
     */
    protected function isUniqueConstraintError(Exception $exception)
    {
        return boolval(preg_match('#Integrity constraint violation: 1062#i', $exception->getMessage()));
    }

    /**
     * Determine if the connected database is a MariaDB database.
     *
     * @return bool
     */
    public function isMaria()
    {
        return str_contains($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB');
    }

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

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

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

        return new MySqlBuilder($this);
    }

    /**
     * Get the default schema grammar instance.
     *
     * @return \Illuminate\Database\Schema\Grammars\MySqlGrammar
     */
    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\MySqlSchemaState
     */
    public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
    {
        return new MySqlSchemaState($this, $files, $processFactory);
    }

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

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