forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextStatement.php
More file actions
46 lines (39 loc) · 1.33 KB
/
Copy pathTextStatement.php
File metadata and controls
46 lines (39 loc) · 1.33 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
<?php
declare(strict_types=1);
namespace Tempest\Database\QueryStatements;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\Enums\DatabaseTextLength;
use Tempest\Database\QueryStatement;
final readonly class TextStatement implements QueryStatement
{
public function __construct(
private string $name,
private bool $nullable = false,
private int|DatabaseTextLength $length = DatabaseTextLength::DEFAULT,
private ?string $default = null,
) {}
public function compile(DatabaseDialect $dialect): string
{
return match ($dialect) {
DatabaseDialect::MYSQL => sprintf(
'`%s` %s %s',
$this->name,
$this->getSQLTypeDeclaration($this->length),
$this->nullable ? '' : 'NOT NULL',
),
default => sprintf(
'`%s` TEXT %s %s',
$this->name,
$this->default !== null ? "DEFAULT '{$this->default}'" : '',
$this->nullable ? '' : 'NOT NULL',
),
};
}
private function getSQLTypeDeclaration(int|DatabaseTextLength $length): string
{
if ($length instanceof DatabaseTextLength) {
return $length->toString();
}
return DatabaseTextLength::fromLength($length)->toString();
}
}