forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseDialect.php
More file actions
42 lines (34 loc) · 1.24 KB
/
DatabaseDialect.php
File metadata and controls
42 lines (34 loc) · 1.24 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
<?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`', $identifier),
self::POSTGRESQL => sprintf('"%s"', $identifier),
};
}
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'),
};
}
}