-
Notifications
You must be signed in to change notification settings - Fork 8
Add table prefix support to TableIdentifier with configurable factory #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
simon-mundy
wants to merge
5
commits into
php-db:0.6.x
Choose a base branch
from
simon-mundy:issue-147
base: 0.6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c7962a
Add table prefix support to TableIdentifier with configurable factory
simon-mundy d1f9228
- Improvements to testing
simon-mundy 8f3207a
Added separator CONST to TableIdentifier
simon-mundy 6c8b87c
Updated testing to ensure a non-empty string for separator
simon-mundy 642f5bd
Fixes as per code review
simon-mundy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpDb\Container; | ||
|
|
||
| use PhpDb\Sql\TableIdentifier; | ||
| use PhpDb\Sql\TableIdentifierFactory; | ||
| use Psr\Container\ContainerInterface; | ||
|
|
||
| /** | ||
| * Creates a {@see TableIdentifierFactory} configured from the application | ||
| * config service. | ||
| * | ||
| * Expected configuration: | ||
| * | ||
| * <code> | ||
| * return [ | ||
| * TableIdentifierFactory::class => [ | ||
| * 'prefix' => 'backup', | ||
| * 'separator' => '_', | ||
| * ], | ||
| * ]; | ||
| * </code> | ||
| * | ||
| * When no configuration is present the factory is created without a prefix. | ||
| * The separator defaults to '_' when not configured. | ||
| */ | ||
| final class TableIdentifierFactoryFactory | ||
| { | ||
| public function __invoke(ContainerInterface $container): TableIdentifierFactory | ||
| { | ||
| /** @var array<string, mixed> $config */ | ||
| $config = $container->has('config') ? $container->get('config') ?? [] : []; | ||
|
|
||
| /** @var array{prefix?: string|null, separator?: string} $factoryConfig */ | ||
| $factoryConfig = $config[TableIdentifierFactory::class] ?? []; | ||
|
|
||
| return new TableIdentifierFactory( | ||
| $factoryConfig['prefix'] ?? null, | ||
| $factoryConfig['separator'] ?? TableIdentifier::SEPARATOR, | ||
| ); | ||
| } | ||
| } |
|
simon-mundy marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpDb\Sql; | ||
|
|
||
| /** | ||
| * Callable factory producing {@see TableIdentifier} instances with a | ||
| * preconfigured table prefix and separator. | ||
| * | ||
| * Register via {@see \PhpDb\ConfigProvider} and configure the prefix once | ||
| * (e.g. in application config) to have every identifier created through the | ||
| * factory share the same prefix — convenient for creating backup_* tables | ||
| * during a migration. | ||
| */ | ||
| final readonly class TableIdentifierFactory | ||
| { | ||
| /** | ||
| * @param null|string $separator Null falls back to {@see TableIdentifier::SEPARATOR}. | ||
| * @throws Exception\InvalidArgumentException If $prefix or $separator is an empty string. | ||
| */ | ||
| public function __construct( | ||
| private ?string $prefix = null, | ||
| private ?string $separator = TableIdentifier::SEPARATOR, | ||
| ) { | ||
| if ('' === $prefix) { | ||
| throw new Exception\InvalidArgumentException( | ||
| '$prefix must be a valid table prefix or null, empty string given' | ||
| ); | ||
| } | ||
|
|
||
| if ('' === $separator) { | ||
| throw new Exception\InvalidArgumentException( | ||
| '$separator must be a valid table separator, empty string given' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function getPrefix(): ?string | ||
| { | ||
| return $this->prefix; | ||
| } | ||
|
|
||
| public function getSeparator(): string | ||
| { | ||
| return $this->separator ?? TableIdentifier::SEPARATOR; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a TableIdentifier carrying the configured prefix and separator. | ||
| * | ||
| * A prefix or separator passed at call time takes precedence over the | ||
| * configured one. | ||
| * | ||
| * @throws Exception\InvalidArgumentException If $prefix or $separator is an empty string. | ||
| */ | ||
| public function __invoke( | ||
| string $table, | ||
| ?string $schema = null, | ||
| ?string $prefix = null, | ||
| ?string $separator = null, | ||
| ): TableIdentifier { | ||
| return new TableIdentifier($table, $schema, $prefix ?? $this->prefix, $separator ?? $this->getSeparator()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.