Skip to content
Draft
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
12 changes: 7 additions & 5 deletions src/AdapterPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Exception;
use PhpDb\Adapter\Platform\AbstractPlatform;
use PhpDb\Sql\Platform\StatementFactoryInterface;
use PhpDb\Sql\Dialect\DialectInterface;

use function implode;
use function str_replace;
Expand All @@ -34,6 +34,8 @@ final class AdapterPlatform extends AbstractPlatform
*/
protected string $quoteIdentifierFragmentPattern = '/([^0-9,a-z,A-Z$_\-:])/i';

private ?Sql\Dialect $dialect = null;

public function __construct(
protected readonly DriverInterface|mysqli|PDO $driver,
) {}
Expand All @@ -42,18 +44,18 @@ public function __construct(
* {@inheritDoc}
*/
#[Override]
public function getName(): string
public function getDialect(): DialectInterface
{
return self::PLATFORM_NAME;
return $this->dialect ??= new Sql\Dialect();
}

/**
* {@inheritDoc}
*/
#[Override]
public function getStatementFactory(): StatementFactoryInterface
public function getName(): string
{
return new Sql\Platform();
return self::PLATFORM_NAME;
}

/**
Expand Down
85 changes: 0 additions & 85 deletions src/Sql/Ddl/AlterTable.php

This file was deleted.

55 changes: 55 additions & 0 deletions src/Sql/Ddl/ColumnOptionRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace PhpDb\Mysql\Sql\Ddl;

use Closure;
use Override;
use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Sql\Ddl\AlterTable;
use PhpDb\Sql\Ddl\Column\ColumnInterface;
use PhpDb\Sql\Dialect\ColumnRendererInterface;
use PhpDb\Sql\Dialect\RenderContext;

/**
* MySQL renders each column with the storage and format options it accepts after the type, and
* places an added column with AFTER.
*/
final class ColumnOptionRenderer implements ColumnRendererInterface
{
use ColumnOptionTrait;

/** @var (Closure(string, mixed, PlatformInterface): ?string)|null */
private ?Closure $afterResolver = null;

private static function resolveAfterOption(string $option, mixed $value, PlatformInterface $platform): ?string
{
if ('after' !== $option) {
return null;
}

return $platform->quoteIdentifier((string) $value);
}

#[Override]
public function renderColumn(
ColumnInterface $column,
string $standard,
string $clause,
RenderContext $context,
): string {
$options = $column->getOptions();

if ([] === $options) {
return $standard;
}

return $this->processColumnOptions(
$standard,
$options,
$context->platform,
AlterTable::ADD_COLUMNS === $clause ? ($this->afterResolver ??= self::resolveAfterOption(...)) : null,
);
}
}
48 changes: 0 additions & 48 deletions src/Sql/Ddl/CreateTable.php

This file was deleted.

45 changes: 45 additions & 0 deletions src/Sql/Dialect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace PhpDb\Mysql\Sql;

use Override;
use PhpDb\Sql\Dialect\ColumnRendererInterface;
use PhpDb\Sql\Dialect\DialectInterface;
use PhpDb\Sql\Dialect\FixedClause;
use PhpDb\Sql\Select;
use PhpDb\Sql\SqlInterface;

/**
* What MySQL renders differently from standard SQL: the limit of a select that has only an
* offset, and the options of a column definition.
*/
final class Dialect implements DialectInterface
{
/**
* MySQL has no OFFSET clause of its own, so an offset without a limit names the maximum
* unsigned bigint as the limit.
*
* @var array<string, FixedClause>|null
*/
private ?array $offsetWithoutLimit = null;

private ?ColumnRendererInterface $columnRenderer = null;

#[Override]
public function clauseRenderers(SqlInterface $statement): array
{
if ($statement instanceof Select && null === $statement->getLimit() && null !== $statement->getOffset()) {
return $this->offsetWithoutLimit ??= [Select::LIMIT => new FixedClause('LIMIT 18446744073709551615')];
}

return [];
}

#[Override]
public function columnRenderer(): ColumnRendererInterface
{
return $this->columnRenderer ??= new Ddl\ColumnOptionRenderer();
}
}
31 changes: 0 additions & 31 deletions src/Sql/Platform.php

This file was deleted.

67 changes: 0 additions & 67 deletions src/Sql/Select.php

This file was deleted.

Loading