-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL: Custom SqlProcessorFactory, modifiers support, ArrayExpressionMo…
…difier
- Loading branch information
Showing
6 changed files
with
155 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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,86 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace OriCMF\Core\SQL\Modifier; | ||
|
||
use Nextras\Dbal\Connection; | ||
use Orisai\Exceptions\Logic\InvalidArgument; | ||
use function array_key_last; | ||
use function assert; | ||
use function get_debug_type; | ||
use function is_array; | ||
use function is_bool; | ||
use function is_finite; | ||
use function is_float; | ||
use function is_int; | ||
use function is_object; | ||
use function is_string; | ||
use function json_encode; | ||
use function method_exists; | ||
use function str_contains; | ||
use function strlen; | ||
use function substr; | ||
|
||
final class ArrayExpressionModifier implements ExpressionModifier | ||
{ | ||
|
||
/** | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public function valueToExpression($value, Connection $connection) | ||
{ | ||
assert(is_array($value)); | ||
|
||
return "array [{$this->process($connection, $value)}]"; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $value | ||
*/ | ||
protected function process(Connection $connection, array $value): string | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mabar
Author
Member
|
||
{ | ||
$driver = $connection->getDriver(); | ||
$valueSeparator = ', '; | ||
$processed = ''; | ||
$last = array_key_last($value); | ||
foreach ($value as $key => $item) { | ||
if (is_array($item)) { | ||
$processed .= $this->valueToExpression($item, $connection); | ||
} elseif (is_string($item)) { | ||
$processed .= $driver->convertStringToSql($item); | ||
} elseif (is_int($item)) { | ||
$processed .= (string) $item; | ||
} elseif (is_float($item)) { | ||
if (!is_finite($item)) { | ||
if ($key === $last) { | ||
$processed = substr($processed, 0, -strlen($valueSeparator)); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
$tmp = json_encode($item); | ||
assert(is_string($tmp)); | ||
$processed .= $tmp . (!str_contains($tmp, '.') ? '.0' : ''); | ||
} elseif (is_bool($item)) { | ||
$processed .= $driver->convertBoolToSql($item); | ||
} elseif ($item === null) { | ||
$processed .= 'NULL'; | ||
} elseif (is_object($item) && method_exists($item, '__toString')) { | ||
$processed .= $driver->convertStringToSql((string) $item); | ||
} else { | ||
$itemType = get_debug_type($item); | ||
|
||
throw InvalidArgument::create() | ||
->withMessage("Value of type {$itemType} is not supported."); | ||
} | ||
|
||
if ($key !== $last) { | ||
$processed .= $valueSeparator; | ||
} | ||
} | ||
|
||
return $processed; | ||
} | ||
|
||
} |
This file contains 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,16 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace OriCMF\Core\SQL\Modifier; | ||
|
||
use Nextras\Dbal\Connection; | ||
|
||
interface ExpressionModifier | ||
{ | ||
|
||
/** | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public function valueToExpression($value, Connection $connection); | ||
|
||
} |
This file contains 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,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace OriCMF\Core\SQL; | ||
|
||
use Nextras\Dbal\Connection; | ||
use Nextras\Dbal\ISqlProcessorFactory; | ||
use Nextras\Dbal\SqlProcessor; | ||
use OriCMF\Core\SQL\Modifier\ExpressionModifier; | ||
|
||
final class SqlProcessorFactory implements ISqlProcessorFactory | ||
{ | ||
|
||
/** @var array<ExpressionModifier> */ | ||
private array $modifiers = []; | ||
|
||
public function addModifier(string $modifier, ExpressionModifier $expressionModifier): void | ||
{ | ||
$this->modifiers[$modifier] = $expressionModifier; | ||
} | ||
|
||
public function create(Connection $connection): SqlProcessor | ||
{ | ||
$sqlProcessor = new SqlProcessor($connection->getDriver(), $connection->getPlatform()); | ||
|
||
foreach ($this->modifiers as $name => $expressionModifier) { | ||
$sqlProcessor->setCustomModifier( | ||
$name, | ||
static fn ($value, string $modifier) => $expressionModifier->valueToExpression($value, $connection), | ||
); | ||
} | ||
|
||
return $sqlProcessor; | ||
} | ||
|
||
} |
This file contains 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
Hey @mabar. I've reviewed the api and added SqlProcessor, otherwise it should be pretty easy to use. Tried solve your issue and i would go with more simple implementation: nextras/dbal@92d28a9#diff-ab562a723d045317f8d22acacebfd1fe785d271834414bc782fe72a0a3781c35R62-R73