Repository navigation
Expand file tree
/
Copy pathDatabaseDialect.php
More file actions
50 lines (41 loc) · 1.5 KB
/
Copy pathDatabaseDialect.php
File metadata and controls
50 lines (41 loc) · 1.5 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
<?php
declare(strict_types=1);
namespace Tempest\Database\Config;
use Tempest\Database\Exceptions\QueryWasInvalid;
enum DatabaseDialect: string
{
case SQLITE = 'sqlite';
case MYSQL = 'mysql';
case POSTGRESQL = 'pgsql';
public function tableNotFoundCode(): string
{
return match ($this) {
self::MYSQL => '42S02',
self::POSTGRESQL => '42P01',
self::SQLITE => 'HY000',
};
}
public function quoteIdentifier(string $identifier): string
{
return match ($this) {
self::MYSQL, self::SQLITE => sprintf('`%s`', str_replace('`', '``', $identifier)),
self::POSTGRESQL => sprintf('"%s"', str_replace('"', '""', $identifier)),
};
}
public function encloseExpressionDefault(string $expression): string
{
return match ($this) {
self::MYSQL => sprintf('(%s)', $expression),
default => $expression,
};
}
public function isTableNotFoundError(QueryWasInvalid $queryWasInvalid): bool
{
$pdoException = $queryWasInvalid->pdoException;
return match ($this) {
self::MYSQL => $pdoException->getCode() === '42S02' && str_contains($pdoException->getMessage(), 'table'),
self::SQLITE => $pdoException->getCode() === 'HY000' && str_contains($pdoException->getMessage(), 'table'),
self::POSTGRESQL => $pdoException->getCode() === '42P01' && str_contains($pdoException->getMessage(), 'relation'),
};
}
}