Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Dec 13, 2024
1 parent 7e9d7db commit 37e8b4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 15 additions & 3 deletions src/Tempest/Database/src/QueryStatements/AlterTableStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class AlterTableStatement implements QueryStatement
public function __construct(
private readonly string $tableName,
private array $statements = [],
private array $createIndexStatements = [],
) {}

/** @param class-string<\Tempest\Database\DatabaseModel> $modelClass */
Expand Down Expand Up @@ -61,7 +62,7 @@ public function constraint(string $constraintName, ?QueryStatement $statement =

public function unique(string ...$columns): self
{
$this->statements[] = new UniqueStatement(
$this->createIndexStatements[] = new UniqueStatement(
tableName: $this->tableName,
columns: $columns,
);
Expand All @@ -71,7 +72,7 @@ public function unique(string ...$columns): self

public function index(string ...$columns): self
{
$this->statements[] = new IndexStatement(
$this->createIndexStatements[] = new IndexStatement(
tableName: $this->tableName,
columns: $columns,
);
Expand All @@ -88,7 +89,7 @@ public function drop(QueryStatement $statement): self

public function compile(DatabaseDialect $dialect): string
{
return sprintf(
$alterTable = sprintf(
'ALTER TABLE %s %s;',
new TableName($this->tableName),
arr($this->statements)
Expand All @@ -98,5 +99,16 @@ public function compile(DatabaseDialect $dialect): string
->wrap(before: PHP_EOL . ' ', after: PHP_EOL)
->toString(),
);

if ($this->createIndexStatements !== []) {
$createIndices = PHP_EOL . arr($this->createIndexStatements)
->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' '))
->implode(';' . PHP_EOL)
->append(';');
} else {
$createIndices = '';
}

return $alterTable . $createIndices;
}
}
16 changes: 10 additions & 6 deletions src/Tempest/Database/src/QueryStatements/CreateTableStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ public function compile(DatabaseDialect $dialect): string
->toString(),
);

$createIndices = arr($this->indexStatements)
->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' '))
->implode(';' . PHP_EOL)
->append(';');
var_dump($createTable . PHP_EOL . $createIndices);
return $createTable . PHP_EOL . $createIndices;
if ($this->indexStatements !== []) {
$createIndices = PHP_EOL . arr($this->indexStatements)
->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' '))
->implode(';' . PHP_EOL)
->append(';');
} else {
$createIndices = '';
}

return $createTable . $createIndices;
}
}

0 comments on commit 37e8b4a

Please sign in to comment.