Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Sql/Ddl/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use PhpDb\Sql\Argument\Identifier;
use PhpDb\Sql\Argument\Literal;
use PhpDb\Sql\Argument\Value;
use PhpDb\Sql\ArgumentInterface;
use PhpDb\Sql\Ddl\Constraint\ConstraintInterface;

use function implode;

class Column implements ColumnInterface
{
protected string|int|null $default;
protected string|int|float|bool|Literal|Value|null $default;

protected bool $isNullable = false;

Expand Down Expand Up @@ -65,14 +66,14 @@ public function isNullable(): bool
return $this->isNullable;
}

public function setDefault(string|int|null $default): static
public function setDefault(string|int|float|bool|Literal|Value|null $default): static
{
$this->default = $default;
return $this;
}

#[Override]
public function getDefault(): string|int|null
public function getDefault(): string|int|float|bool|Literal|Value|null
{
return $this->default;
}
Expand Down Expand Up @@ -118,7 +119,9 @@ public function getExpressionData(): array

if ($this->default !== null) {
$specParts[] = 'DEFAULT %s';
$values[] = new Value($this->default);
$values[] = $this->default instanceof ArgumentInterface
? $this->default
: new Value($this->default);
}

foreach ($this->constraints as $constraint) {
Expand Down
4 changes: 3 additions & 1 deletion src/Sql/Ddl/Column/ColumnInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpDb\Sql\Ddl\Column;

use PhpDb\Sql\Argument\Literal;
use PhpDb\Sql\Argument\Value;
use PhpDb\Sql\ExpressionInterface;

/**
Expand All @@ -15,7 +17,7 @@ public function getName(): string;

public function isNullable(): bool;

public function getDefault(): string|int|null;
public function getDefault(): string|int|float|bool|Literal|Value|null;

public function getOptions(): array;
}
10 changes: 10 additions & 0 deletions src/Sql/Ddl/Column/Double.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpDb\Sql\Ddl\Column;

class Double extends AbstractPrecisionColumn
{
protected string $type = 'DOUBLE';
}
10 changes: 10 additions & 0 deletions src/Sql/Ddl/Column/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpDb\Sql\Ddl\Column;

class Json extends Column
{
protected string $type = 'JSON';
}
10 changes: 10 additions & 0 deletions src/Sql/Ddl/Column/SmallInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpDb\Sql\Ddl\Column;

class SmallInteger extends Integer
{
protected string $type = 'SMALLINT';
}
16 changes: 15 additions & 1 deletion src/Sql/Ddl/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class CreateTable extends AbstractSql

protected array $constraints = [];

protected bool $ifNotExists = false;

protected bool $isTemporary = false;

/**
* {@inheritDoc}
*/
protected array $specifications = [
self::TABLE => 'CREATE %1$sTABLE %2$s (',
self::TABLE => 'CREATE %1$sTABLE %2$s%3$s (',
self::COLUMNS => [
"\n %1\$s" => [
[1 => '%1$s', 'combinedby' => ",\n "],
Expand All @@ -51,6 +53,17 @@ public function __construct(string|TableIdentifier $table = '', bool $isTemporar
$this->setTemporary($isTemporary);
}

public function ifNotExists(bool $ifNotExists = true): static
{
$this->ifNotExists = $ifNotExists;
return $this;
}

public function getIfNotExists(): bool
{
return $this->ifNotExists;
}

public function setTemporary(string|int|bool $temporary): static
{
$this->isTemporary = (bool) $temporary;
Expand Down Expand Up @@ -102,6 +115,7 @@ protected function processTable(?PlatformInterface $adapterPlatform = null): arr
{
return [
$this->isTemporary ? 'TEMPORARY ' : '',
$this->ifNotExists ? 'IF NOT EXISTS ' : '',
$this->resolveTable($this->table, $adapterPlatform),
];
}
Expand Down
20 changes: 18 additions & 2 deletions src/Sql/Ddl/DropTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class DropTable extends AbstractSql
{
final public const TABLE = 'table';

protected bool $ifExists = false;

protected array $specifications = [
self::TABLE => 'DROP TABLE %1$s',
self::TABLE => 'DROP TABLE %1$s%2$s',
];

protected string|TableIdentifier $table = '';
Expand All @@ -23,9 +25,23 @@ public function __construct(string|TableIdentifier $table = '')
$this->table = $table;
}

public function ifExists(bool $ifExists = true): static
{
$this->ifExists = $ifExists;
return $this;
}

public function getIfExists(): bool
{
return $this->ifExists;
}

/** @return string[] */
protected function processTable(?PlatformInterface $adapterPlatform = null): array
{
return [$this->resolveTable($this->table, $adapterPlatform)];
return [
$this->ifExists ? 'IF EXISTS ' : '',
$this->resolveTable($this->table, $adapterPlatform),
];
}
}
23 changes: 22 additions & 1 deletion src/Sql/Ddl/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Override;
use PhpDb\Sql\Argument\Identifier;
use PhpDb\Sql\Argument\Literal;

use function count;
use function implode;
Expand All @@ -17,13 +18,26 @@ class Index extends AbstractIndex

protected array $lengths;

protected ?string $type = null;

public function __construct(null|array|string $columns, ?string $name = null, array $lengths = [])
{
parent::__construct($columns, $name);

$this->lengths = $lengths;
}

public function setType(string $type): static
{
$this->type = $type;
return $this;
}

public function getType(): ?string
{
return $this->type;
}

/** @inheritDoc */
#[Override]
public function getExpressionData(): array
Expand All @@ -43,8 +57,15 @@ public function getExpressionData(): array
$specParts[] = $specPart;
}

$spec = str_replace('...', implode(', ', $specParts), $this->specification);

if ($this->type !== null) {
$spec .= ' USING %s';
$values[] = new Literal($this->type);
}

return [
'spec' => str_replace('...', implode(', ', $specParts), $this->specification),
'spec' => $spec,
'values' => $values,
];
}
Expand Down
112 changes: 112 additions & 0 deletions test/unit/Sql/Ddl/Column/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace PhpDbTest\Sql\Ddl\Column;

use PhpDb\Sql\Argument;
use PhpDb\Sql\Argument\Literal;
use PhpDb\Sql\Argument\Value;
use PhpDb\Sql\Ddl\Column\Column;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -166,4 +168,114 @@ public function testGetExpressionData(): void
Argument::value('bar'),
], $expressionData['values']);
}

public function testSetDefaultWithLiteral(): void
{
$column = new Column();
$column->setName('created_at');

$literal = new Literal('CURRENT_TIMESTAMP');
$result = $column->setDefault($literal);

self::assertSame($column, $result);
self::assertSame($literal, $column->getDefault());
}

public function testGetExpressionDataWithLiteralDefault(): void
{
$column = new Column();
$column->setName('created_at');
$column->setDefault(new Literal('CURRENT_TIMESTAMP'));

$expressionData = $column->getExpressionData();

self::assertEquals('%s %s NOT NULL DEFAULT %s', $expressionData['spec']);
self::assertEquals([
Argument::identifier('created_at'),
Argument::literal('INTEGER'),
Argument::literal('CURRENT_TIMESTAMP'),
], $expressionData['values']);
}

public function testSetDefaultWithValue(): void
{
$column = new Column();
$column->setName('score');

$value = new Value(99);
$result = $column->setDefault($value);

self::assertSame($column, $result);
self::assertSame($value, $column->getDefault());
}

public function testGetExpressionDataWithValueDefault(): void
{
$column = new Column();
$column->setName('score');
$column->setDefault(new Value(42));

$expressionData = $column->getExpressionData();

self::assertEquals('%s %s NOT NULL DEFAULT %s', $expressionData['spec']);
self::assertEquals([
Argument::identifier('score'),
Argument::literal('INTEGER'),
Argument::value(42),
], $expressionData['values']);
}

public function testSetDefaultWithFloat(): void
{
$column = new Column();
$column->setName('rate');

$result = $column->setDefault(3.14);

self::assertSame($column, $result);
self::assertSame(3.14, $column->getDefault());
}

public function testGetExpressionDataWithFloatDefault(): void
{
$column = new Column();
$column->setName('rate');
$column->setDefault(9.99);

$expressionData = $column->getExpressionData();

self::assertEquals('%s %s NOT NULL DEFAULT %s', $expressionData['spec']);
self::assertEquals([
Argument::identifier('rate'),
Argument::literal('INTEGER'),
Argument::value(9.99),
], $expressionData['values']);
}

public function testSetDefaultWithBool(): void
{
$column = new Column();
$column->setName('is_active');

$result = $column->setDefault(true);

self::assertSame($column, $result);
self::assertTrue($column->getDefault());
}

public function testGetExpressionDataWithBoolDefault(): void
{
$column = new Column();
$column->setName('is_active');
$column->setDefault(false);

$expressionData = $column->getExpressionData();

self::assertEquals('%s %s NOT NULL DEFAULT %s', $expressionData['spec']);
self::assertEquals([
Argument::identifier('is_active'),
Argument::literal('INTEGER'),
Argument::value(false),
], $expressionData['values']);
}
}
28 changes: 28 additions & 0 deletions test/unit/Sql/Ddl/Column/DoubleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PhpDbTest\Sql\Ddl\Column;

use PhpDb\Sql\Argument;
use PhpDb\Sql\Ddl\Column\Double;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;

#[CoversMethod(Double::class, 'getExpressionData')]
final class DoubleTest extends TestCase
{
public function testGetExpressionData(): void
{
$column = new Double('foo', 10, 5);

$expressionData = $column->getExpressionData();

self::assertEquals('%s %s(%s) NOT NULL', $expressionData['spec']);
self::assertEquals([
Argument::identifier('foo'),
Argument::literal('DOUBLE'),
Argument::literal('10,5'),
], $expressionData['values']);
}
}
Loading
Loading