Repository navigation
Expand file tree
/
Copy pathSQLiteConnection.php
More file actions
130 lines (115 loc) · 3.55 KB
/
Copy pathSQLiteConnection.php
File metadata and controls
130 lines (115 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\SQLite;
use Closure;
use Doctrine\DBAL\Driver\PDO\SQLite\Driver;
use Doctrine\DBAL\Driver\PDO\SQLite\Driver as DoctrineDriver;
use Exception;
use Hyperf\Database\Connection;
use Hyperf\Database\Query\Grammars\Grammar as HyperfQueryGrammar;
use Hyperf\Database\Query\Processors\Processor;
use Hyperf\Database\Schema\Builder as SchemaBuilder;
use Hyperf\Database\Schema\Grammars\Grammar as HyperfSchemaGrammar;
use Hyperf\Database\SQLite\Query\Grammars\SQLiteGrammar;
use Hyperf\Database\SQLite\Query\Grammars\SQLiteGrammar as QueryGrammar;
use Hyperf\Database\SQLite\Query\Processors\SQLiteProcessor;
use Hyperf\Database\SQLite\Schema\Grammars\SQLiteGrammar as SchemaGrammar;
use Hyperf\Database\SQLite\Schema\SQLiteBuilder;
use PDO;
class SQLiteConnection extends Connection
{
/**
* Create a new database connection instance.
*
* @param Closure|PDO $pdo
* @param string $database
* @param string $tablePrefix
*/
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();
}
/**
* Get a schema builder instance for the connection.
*
* @return SQLiteBuilder
*/
public function getSchemaBuilder(): SchemaBuilder
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new SQLiteBuilder($this);
}
/**
* Determine if the given database exception was caused by a unique constraint violation.
*
* @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 SQLiteGrammar
*/
protected function getDefaultQueryGrammar(): HyperfQueryGrammar
{
/* @phpstan-ignore-next-line */
return $this->withTablePrefix(new QueryGrammar());
}
/**
* Get the default schema grammar instance.
*
* @return SchemaGrammar
*/
protected function getDefaultSchemaGrammar(): HyperfSchemaGrammar
{
/* @phpstan-ignore-next-line */
return $this->withTablePrefix(new SchemaGrammar());
}
/**
* Get the default post processor instance.
*
* @return SQLiteProcessor
*/
protected function getDefaultPostProcessor(): Processor
{
return new SQLiteProcessor();
}
/**
* Get the Doctrine DBAL driver.
*
* @return Driver
*/
protected function getDoctrineDriver()
{
return new DoctrineDriver();
}
/**
* Get the database connection foreign key constraints configuration option.
*
* @return null|bool
*/
protected function getForeignKeyConstraintsConfigurationValue()
{
return $this->getConfig('foreign_key_constraints');
}
}