forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexStatement.php
More file actions
33 lines (24 loc) · 925 Bytes
/
IndexStatement.php
File metadata and controls
33 lines (24 loc) · 925 Bytes
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
<?php
declare(strict_types=1);
namespace Tempest\Database\QueryStatements;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\QueryStatement;
use function Tempest\Support\arr;
use function Tempest\Support\str;
final readonly class IndexStatement implements QueryStatement
{
public function __construct(
private string $tableName,
private array $columns,
) {}
public function compile(DatabaseDialect $dialect): string
{
$columns = arr($this->columns)
->map($dialect->quoteIdentifier(...))
->implode(', ');
$rawColumns = arr($this->columns)->implode('_');
$indexName = str($this->tableName . '_' . $rawColumns)->snake()->toString();
$on = sprintf('%s (%s)', $dialect->quoteIdentifier($this->tableName), $columns);
return sprintf('CREATE INDEX %s ON %s', $dialect->quoteIdentifier($indexName), $on);
}
}