Skip to content

Commit 4d73980

Browse files
[12.x] Configure connection on SQLite connector (#54588)
* fix SQLite connector * formatting
1 parent 3f801fa commit 4d73980

File tree

7 files changed

+206
-249
lines changed

7 files changed

+206
-249
lines changed

src/Illuminate/Database/Connectors/SQLiteConnector.php

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,118 @@ class SQLiteConnector extends Connector implements ConnectorInterface
1111
*
1212
* @param array $config
1313
* @return \PDO
14-
*
15-
* @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException
1614
*/
1715
public function connect(array $config)
1816
{
1917
$options = $this->getOptions($config);
2018

19+
$path = $this->parseDatabasePath($config['database']);
20+
21+
$connection = $this->createConnection("sqlite:{$path}", $config, $options);
22+
23+
$this->configureForeignKeyConstraints($connection, $config);
24+
$this->configureBusyTimeout($connection, $config);
25+
$this->configureJournalMode($connection, $config);
26+
$this->configureSynchronous($connection, $config);
27+
28+
return $connection;
29+
}
30+
31+
/**
32+
* Get the absolute database path.
33+
*
34+
* @param string $path
35+
* @return string
36+
*
37+
* @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException
38+
*/
39+
protected function parseDatabasePath(string $path): string
40+
{
2141
// SQLite supports "in-memory" databases that only last as long as the owning
2242
// connection does. These are useful for tests or for short lifetime store
2343
// querying. In-memory databases shall be anonymous (:memory:) or named.
24-
if ($config['database'] === ':memory:' ||
25-
str_contains($config['database'], '?mode=memory') ||
26-
str_contains($config['database'], '&mode=memory')
44+
if ($path === ':memory:' ||
45+
str_contains($path, '?mode=memory') ||
46+
str_contains($path, '&mode=memory')
2747
) {
28-
return $this->createConnection('sqlite:'.$config['database'], $config, $options);
48+
return $path;
2949
}
3050

31-
$path = realpath($config['database']) ?: realpath(base_path($config['database']));
51+
$path = realpath($path) ?: realpath(base_path($path));
3252

3353
// Here we'll verify that the SQLite database exists before going any further
3454
// as the developer probably wants to know if the database exists and this
3555
// SQLite driver will not throw any exception if it does not by default.
3656
if ($path === false) {
37-
throw new SQLiteDatabaseDoesNotExistException($config['database']);
57+
throw new SQLiteDatabaseDoesNotExistException($path);
58+
}
59+
60+
return $path;
61+
}
62+
63+
/**
64+
* Enable or disable foreign key constraints if configured.
65+
*
66+
* @param \PDO $connection
67+
* @param array $config
68+
* @return void
69+
*/
70+
protected function configureForeignKeyConstraints($connection, array $config): void
71+
{
72+
if (! isset($config['foreign_key_constraints'])) {
73+
return;
74+
}
75+
76+
$foreignKeys = $config['foreign_key_constraints'] ? 1 : 0;
77+
78+
$connection->prepare("pragma foreign_keys = {$foreignKeys}")->execute();
79+
}
80+
81+
/**
82+
* Set the busy timeout if configured.
83+
*
84+
* @param \PDO $connection
85+
* @param array $config
86+
* @return void
87+
*/
88+
protected function configureBusyTimeout($connection, array $config): void
89+
{
90+
if (! isset($config['busy_timeout'])) {
91+
return;
92+
}
93+
94+
$connection->prepare("pragma busy_timeout = {$config['busy_timeout']}")->execute();
95+
}
96+
97+
/**
98+
* Set the journal mode if configured.
99+
*
100+
* @param \PDO $connection
101+
* @param array $config
102+
* @return void
103+
*/
104+
protected function configureJournalMode($connection, array $config): void
105+
{
106+
if (! isset($config['journal_mode'])) {
107+
return;
108+
}
109+
110+
$connection->prepare("pragma journal_mode = {$config['journal_mode']}")->execute();
111+
}
112+
113+
/**
114+
* Set the synchronous mode if configured.
115+
*
116+
* @param \PDO $connection
117+
* @param array $config
118+
* @return void
119+
*/
120+
protected function configureSynchronous($connection, array $config): void
121+
{
122+
if (! isset($config['synchronous'])) {
123+
return;
38124
}
39125

40-
return $this->createConnection("sqlite:{$path}", $config, $options);
126+
$connection->prepare("pragma synchronous = {$config['synchronous']}")->execute();
41127
}
42128
}

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@
1212

1313
class SQLiteConnection extends Connection
1414
{
15-
/**
16-
* Create a new database connection instance.
17-
*
18-
* @param \PDO|\Closure $pdo
19-
* @param string $database
20-
* @param string $tablePrefix
21-
* @param array $config
22-
* @return void
23-
*/
24-
public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
25-
{
26-
parent::__construct($pdo, $database, $tablePrefix, $config);
27-
28-
$this->configureForeignKeyConstraints();
29-
$this->configureBusyTimeout();
30-
$this->configureJournalMode();
31-
$this->configureSynchronous();
32-
}
33-
3415
/**
3516
* {@inheritdoc}
3617
*/
@@ -39,98 +20,6 @@ public function getDriverTitle()
3920
return 'SQLite';
4021
}
4122

42-
/**
43-
* Enable or disable foreign key constraints if configured.
44-
*
45-
* @return void
46-
*/
47-
protected function configureForeignKeyConstraints(): void
48-
{
49-
$enableForeignKeyConstraints = $this->getConfig('foreign_key_constraints');
50-
51-
if ($enableForeignKeyConstraints === null) {
52-
return;
53-
}
54-
55-
$schemaBuilder = $this->getSchemaBuilder();
56-
57-
try {
58-
$enableForeignKeyConstraints
59-
? $schemaBuilder->enableForeignKeyConstraints()
60-
: $schemaBuilder->disableForeignKeyConstraints();
61-
} catch (QueryException $e) {
62-
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
63-
throw $e;
64-
}
65-
}
66-
}
67-
68-
/**
69-
* Set the busy timeout if configured.
70-
*
71-
* @return void
72-
*/
73-
protected function configureBusyTimeout(): void
74-
{
75-
$milliseconds = $this->getConfig('busy_timeout');
76-
77-
if ($milliseconds === null) {
78-
return;
79-
}
80-
81-
try {
82-
$this->getSchemaBuilder()->setBusyTimeout($milliseconds);
83-
} catch (QueryException $e) {
84-
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
85-
throw $e;
86-
}
87-
}
88-
}
89-
90-
/**
91-
* Set the journal mode if configured.
92-
*
93-
* @return void
94-
*/
95-
protected function configureJournalMode(): void
96-
{
97-
$mode = $this->getConfig('journal_mode');
98-
99-
if ($mode === null) {
100-
return;
101-
}
102-
103-
try {
104-
$this->getSchemaBuilder()->setJournalMode($mode);
105-
} catch (QueryException $e) {
106-
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
107-
throw $e;
108-
}
109-
}
110-
}
111-
112-
/**
113-
* Set the synchronous mode if configured.
114-
*
115-
* @return void
116-
*/
117-
protected function configureSynchronous(): void
118-
{
119-
$mode = $this->getConfig('synchronous');
120-
121-
if ($mode === null) {
122-
return;
123-
}
124-
125-
try {
126-
$this->getSchemaBuilder()->setSynchronous($mode);
127-
} catch (QueryException $e) {
128-
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
129-
throw $e;
130-
}
131-
}
132-
}
133-
13423
/**
13524
* Escape a binary value for safe SQL embedding.
13625
*

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function compileAlter(Blueprint $blueprint, Fluent $command)
354354
$table = $this->wrapTable($blueprint);
355355
$columnNames = implode(', ', $columnNames);
356356

357-
$foreignKeyConstraintsEnabled = $this->connection->scalar('pragma foreign_keys');
357+
$foreignKeyConstraintsEnabled = $this->connection->scalar($this->pragma('foreign_keys'));
358358

359359
return array_filter(array_merge([
360360
$foreignKeyConstraintsEnabled ? $this->compileDisableForeignKeyConstraints() : null,
@@ -511,11 +511,14 @@ public function compileDropAllViews($schema = null)
511511
/**
512512
* Compile the SQL needed to rebuild the database.
513513
*
514+
* @param string|null $schema
514515
* @return string
515516
*/
516-
public function compileRebuild()
517+
public function compileRebuild($schema = null)
517518
{
518-
return 'vacuum';
519+
return sprintf('vacuum %s',
520+
$this->wrapValue($schema ?? 'main')
521+
);
519522
}
520523

521524
/**
@@ -672,7 +675,7 @@ public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
672675
*/
673676
public function compileEnableForeignKeyConstraints()
674677
{
675-
return $this->pragma('foreign_keys', 'ON');
678+
return $this->pragma('foreign_keys', 1);
676679
}
677680

678681
/**
@@ -682,72 +685,22 @@ public function compileEnableForeignKeyConstraints()
682685
*/
683686
public function compileDisableForeignKeyConstraints()
684687
{
685-
return $this->pragma('foreign_keys', 'OFF');
686-
}
687-
688-
/**
689-
* Compile the command to set the busy timeout.
690-
*
691-
* @param int $milliseconds
692-
* @return string
693-
*/
694-
public function compileSetBusyTimeout($milliseconds)
695-
{
696-
return $this->pragma('busy_timeout', $milliseconds);
697-
}
698-
699-
/**
700-
* Compile the command to set the journal mode.
701-
*
702-
* @param string $mode
703-
* @return string
704-
*/
705-
public function compileSetJournalMode($mode)
706-
{
707-
return $this->pragma('journal_mode', $mode);
708-
}
709-
710-
/**
711-
* Compile the command to set the synchronous mode.
712-
*
713-
* @param string $mode
714-
* @return string
715-
*/
716-
public function compileSetSynchronous($mode)
717-
{
718-
return $this->pragma('synchronous', $mode);
719-
}
720-
721-
/**
722-
* Compile the SQL needed to enable a writable schema.
723-
*
724-
* @return string
725-
*/
726-
public function compileEnableWriteableSchema()
727-
{
728-
return $this->pragma('writable_schema', 1);
729-
}
730-
731-
/**
732-
* Compile the SQL needed to disable a writable schema.
733-
*
734-
* @return string
735-
*/
736-
public function compileDisableWriteableSchema()
737-
{
738-
return $this->pragma('writable_schema', 0);
688+
return $this->pragma('foreign_keys', 0);
739689
}
740690

741691
/**
742-
* Get the SQL to set a PRAGMA value.
692+
* Get the SQL to get or set a PRAGMA value.
743693
*
744-
* @param string $name
694+
* @param string $key
745695
* @param mixed $value
746696
* @return string
747697
*/
748-
protected function pragma(string $name, mixed $value): string
698+
public function pragma(string $key, mixed $value = null): string
749699
{
750-
return sprintf('PRAGMA %s = %s;', $name, $value);
700+
return sprintf('pragma %s%s',
701+
$key,
702+
is_null($value) ? '' : ' = '.$value
703+
);
751704
}
752705

753706
/**

0 commit comments

Comments
 (0)