Skip to content

Commit e19437e

Browse files
authored
feat(database)!: allow setting char size (#2087)
1 parent 8e9c5ff commit e19437e

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

packages/database/src/QueryStatements/CharStatement.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
{
1212
public function __construct(
1313
private string $name,
14+
private int $size,
1415
private bool $nullable = false,
1516
private ?string $default = null,
1617
) {}
1718

1819
public function compile(DatabaseDialect $dialect): string
1920
{
2021
return sprintf(
21-
'%s CHAR %s %s',
22+
'%s CHAR(%s) %s %s',
2223
$dialect->quoteIdentifier($this->name),
24+
$this->size,
2325
$this->default !== null ? "DEFAULT '{$this->default}'" : '',
2426
$this->nullable ? '' : 'NOT NULL',
2527
);

packages/database/src/QueryStatements/CreateTableStatement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ public function string(string $name, int $length = 255, bool $nullable = false,
186186
/**
187187
* Adds a `CHAR` column to the table.
188188
*/
189-
public function char(string $name, bool $nullable = false, ?string $default = null): self
189+
public function char(string $name, bool $nullable = false, ?string $default = null, int $size = 1): self
190190
{
191191
$this->statements[] = new CharStatement(
192192
name: $name,
193+
size: $size,
193194
nullable: $nullable,
194195
default: $default,
195196
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tempest\Database\Tests\QueryStatements;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
use Tempest\Database\Config\DatabaseDialect;
8+
use Tempest\Database\QueryStatements\CharStatement;
9+
10+
final class CharStatementTest extends TestCase
11+
{
12+
#[Test]
13+
public function test_char(): void
14+
{
15+
$statement = new CharStatement(
16+
name: 'foo',
17+
size: 36,
18+
nullable: false,
19+
default: '019d38a9-5504-7a16-ab9d-520bbc289ecc',
20+
);
21+
22+
$expectedMysql = '`foo` CHAR(36) DEFAULT \'019d38a9-5504-7a16-ab9d-520bbc289ecc\' NOT NULL';
23+
$expectedPgsql = '"foo" CHAR(36) DEFAULT \'019d38a9-5504-7a16-ab9d-520bbc289ecc\' NOT NULL';
24+
25+
$this->assertSame($expectedMysql, $statement->compile(DatabaseDialect::MYSQL));
26+
$this->assertSame($expectedPgsql, $statement->compile(DatabaseDialect::POSTGRESQL));
27+
}
28+
}

tests/Integration/Database/QueryStatements/CreateTableStatementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function up(): QueryStatement
3434
{
3535
return new CreateTableStatement('test_table')
3636
->text('text', default: 'default')
37-
->char('char', default: 'd')
37+
->char('char', default: 'default', size: 7)
3838
->varchar('varchar', default: 'default')
3939
->float('float', default: 0.1)
4040
->integer('integer', default: 1)

0 commit comments

Comments
 (0)