SQLiteConnection.php
TLDR
This file is a part of the Demo Projects project and is located at src/Illuminate/Database/SQLiteConnection.php
. It contains the SQLiteConnection
class which extends the Connection
class. The SQLiteConnection
class is responsible for creating a new database connection instance, escaping binary values for safe SQL embedding, determining if a given database exception was caused by a unique constraint violation, and getting the default query grammar instance, schema builder instance, schema grammar instance, schema state instance, post processor instance, and Doctrine DBAL driver.
Methods
__construct($pdo, $database = '', $tablePrefix = '', array $config = [])
This method is the constructor of the class. It creates a new database connection instance. It accepts the PDO object or closure, database name, table prefix, and an array of configurations as parameters.
escapeBinary($value)
This method escapes a binary value for safe SQL embedding. It accepts a string value as a parameter and returns the escaped value as a string.
isUniqueConstraintError(Exception $exception)
This method determines if the given database exception was caused by a unique constraint violation. It accepts an Exception object as a parameter and returns a boolean value.
getDefaultQueryGrammar()
This method returns the default query grammar instance for the connection. It returns an instance of the SQLiteGrammar
class.
getSchemaBuilder()
This method returns a schema builder instance for the connection. It returns an instance of the SQLiteBuilder
class.
getDefaultSchemaGrammar()
This method returns the default schema grammar instance for the connection. It returns an instance of the SQLiteGrammar
class.
getSchemaState(Filesystem $files = null, callable $processFactory = null)
This method returns the schema state for the connection. It accepts optional Filesystem
and callable
parameters. It returns an instance of the SqliteSchemaState
class.
getDefaultPostProcessor()
This method returns the default post processor instance for the connection. It returns an instance of the SQLiteProcessor
class.
getDoctrineDriver()
This method returns the Doctrine DBAL driver for the connection. It returns an instance of the SQLiteDriver
class.
getForeignKeyConstraintsConfigurationValue()
This method returns the database connection foreign key constraints configuration option. It returns a boolean or null value.
<?php
namespace Illuminate\Database;
use Exception;
use Illuminate\Database\PDO\SQLiteDriver;
use Illuminate\Database\Query\Grammars\SQLiteGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\SQLiteProcessor;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SchemaGrammar;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Database\Schema\SqliteSchemaState;
use Illuminate\Filesystem\Filesystem;
class SQLiteConnection extends Connection
{
/**
* Create a new database connection instance.
*
* @param \PDO|\Closure $pdo
* @param string $database
* @param string $tablePrefix
* @param array $config
* @return void
*/
public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
{
parent::__construct($pdo, $database, $tablePrefix, $config);
$enableForeignKeyConstraints = $this->getForeignKeyConstraintsConfigurationValue();
if ($enableForeignKeyConstraints === null) {
return;
}
$enableForeignKeyConstraints
? $this->getSchemaBuilder()->enableForeignKeyConstraints()
: $this->getSchemaBuilder()->disableForeignKeyConstraints();
}
/**
* 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('#(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)#i', $exception->getMessage()));
}
/**
* Get the default query grammar instance.
*
* @return \Illuminate\Database\Query\Grammars\SQLiteGrammar
*/
protected function getDefaultQueryGrammar()
{
($grammar = new QueryGrammar)->setConnection($this);
return $this->withTablePrefix($grammar);
}
/**
* Get a schema builder instance for the connection.
*
* @return \Illuminate\Database\Schema\SQLiteBuilder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new SQLiteBuilder($this);
}
/**
* Get the default schema grammar instance.
*
* @return \Illuminate\Database\Schema\Grammars\SQLiteGrammar
*/
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
*
* @throws \RuntimeException
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
return new SqliteSchemaState($this, $files, $processFactory);
}
/**
* Get the default post processor instance.
*
* @return \Illuminate\Database\Query\Processors\SQLiteProcessor
*/
protected function getDefaultPostProcessor()
{
return new SQLiteProcessor;
}
/**
* Get the Doctrine DBAL driver.
*
* @return \Illuminate\Database\PDO\SQLiteDriver
*/
protected function getDoctrineDriver()
{
return new SQLiteDriver;
}
/**
* Get the database connection foreign key constraints configuration option.
*
* @return bool|null
*/
protected function getForeignKeyConstraintsConfigurationValue()
{
return $this->getConfig('foreign_key_constraints');
}
}