-
-
Notifications
You must be signed in to change notification settings - Fork 12
Implement ArrayMergeBuilder
, LongestBuilder
and ShortestBuilder
#357
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
34aa2c2
Implement `ArrayMergeBuilder`, `LongestBuilder` and `ShortestBuilder`
Tigrov c492919
Apply fixes from StyleCI
StyleCIBot 559dc74
Fix tests
Tigrov a733212
Fix tests
Tigrov 502a801
Apply fixes from StyleCI
StyleCIBot 5971381
Fix tests
Tigrov 75728b9
Merge remote-tracking branch 'origin/add-functions' into add-functions
Tigrov ec23ec4
Merge branch 'master' into add-functions
Tigrov 2ad7918
Fix tests
Tigrov b050395
Fix tests
Tigrov 44ec3e2
Fix psalm
Tigrov 3c4882f
Add line to CHANGELOG.md [skip ci]
Tigrov f6898b8
Remove `buildSelect()`
Tigrov 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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Builder; | ||
|
||
use Yiisoft\Db\Expression\Function\ArrayMerge; | ||
use Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder; | ||
use Yiisoft\Db\Expression\Function\MultiOperandFunction; | ||
use Yiisoft\Db\Schema\Column\AbstractArrayColumn; | ||
use Yiisoft\Db\Schema\Column\ColumnInterface; | ||
|
||
use function implode; | ||
use function is_string; | ||
use function rtrim; | ||
|
||
/** | ||
* Builds SQL expressions which merge arrays for {@see ArrayMerge} objects. | ||
* | ||
* ```sql | ||
* (SELECT JSON_ARRAYAGG(value) AS value FROM ( | ||
* SELECT value FROM JSON_TABLE(operand1, '$[*]' COLUMNS(value int PATH '$')) | ||
* UNION | ||
* SELECT value FROM JSON_TABLE(operand2, '$[*]' COLUMNS(value int PATH '$')) | ||
* )) | ||
* ``` | ||
* | ||
* @extends MultiOperandFunctionBuilder<ArrayMerge> | ||
*/ | ||
final class ArrayMergeBuilder extends MultiOperandFunctionBuilder | ||
{ | ||
private const DEFAULT_OPERAND_TYPE = ''; | ||
|
||
/** | ||
* Builds a SQL expression which merges arrays from the given {@see ArrayMerge} object. | ||
* | ||
* @param ArrayMerge $expression The expression to build. | ||
* @param array $params The parameters to bind. | ||
* | ||
* @return string The SQL expression. | ||
*/ | ||
protected function buildFromExpression(MultiOperandFunction $expression, array &$params): string | ||
{ | ||
$selects = []; | ||
$operandType = $this->buildOperandType($expression->getType()); | ||
|
||
foreach ($expression->getOperands() as $operand) { | ||
$builtOperand = $this->buildOperand($operand, $params); | ||
$selects[] = "SELECT value FROM JSON_TABLE($builtOperand, '$[*]' COLUMNS(value $operandType PATH '$'))"; | ||
} | ||
|
||
return '(SELECT JSON_ARRAYAGG(value) AS value FROM (' . implode(' UNION ', $selects) . '))'; | ||
} | ||
|
||
private function buildOperandType(string|ColumnInterface $type): string | ||
{ | ||
if (is_string($type)) { | ||
return $type === '' ? self::DEFAULT_OPERAND_TYPE : rtrim($type, '[]'); | ||
} | ||
|
||
if ($type instanceof AbstractArrayColumn) { | ||
if ($type->getDimension() > 1) { | ||
return self::DEFAULT_OPERAND_TYPE; | ||
} | ||
|
||
$type = $type->getColumn(); | ||
|
||
if ($type === null) { | ||
return self::DEFAULT_OPERAND_TYPE; | ||
} | ||
} | ||
|
||
return $this->queryBuilder->getColumnDefinitionBuilder()->buildType($type); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Builder; | ||
|
||
use Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder; | ||
use Yiisoft\Db\Expression\Function\Greatest; | ||
use Yiisoft\Db\Expression\Function\Longest; | ||
use Yiisoft\Db\Expression\Function\MultiOperandFunction; | ||
|
||
/** | ||
* Builds SQL representation of function expressions which returns the longest string from a set of operands. | ||
* | ||
* ```SQL | ||
* (SELECT value FROM ( | ||
* SELECT operand1 AS value FROM DUAL | ||
* UNION | ||
* SELECT operand2 AS value FROM DUAL | ||
* ) ORDER BY LENGTH(value) DESC FETCH FIRST 1 ROWS ONLY) | ||
* ``` | ||
* | ||
* @extends MultiOperandFunctionBuilder<Longest> | ||
*/ | ||
final class LongestBuilder extends MultiOperandFunctionBuilder | ||
{ | ||
/** | ||
* Builds a SQL expression to represent the function which returns the longest string. | ||
* | ||
* @param Greatest $expression The expression to build. | ||
* @param array $params The parameters to bind. | ||
* | ||
* @return string The SQL expression. | ||
*/ | ||
protected function buildFromExpression(MultiOperandFunction $expression, array &$params): string | ||
{ | ||
$selects = []; | ||
|
||
foreach ($expression->getOperands() as $operand) { | ||
$selects[] = 'SELECT ' . $this->buildOperand($operand, $params) . ' AS value FROM DUAL'; | ||
} | ||
|
||
$unions = implode(' UNION ', $selects); | ||
|
||
return "(SELECT value FROM ($unions) ORDER BY LENGTH(value) DESC FETCH FIRST 1 ROWS ONLY)"; | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Builder; | ||
|
||
use Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder; | ||
use Yiisoft\Db\Expression\Function\MultiOperandFunction; | ||
use Yiisoft\Db\Expression\Function\Shortest; | ||
|
||
/** | ||
* Builds SQL representation of function expressions which return the shortest string from a set of operands. | ||
* | ||
* ```SQL | ||
* (SELECT value FROM ( | ||
* SELECT operand1 AS value FROM DUAL | ||
* UNION | ||
* SELECT operand2 AS value FROM DUAL | ||
* ) ORDER BY LENGTH(value) ASC FETCH FIRST 1 ROWS ONLY) | ||
* ``` | ||
* | ||
* @extends MultiOperandFunctionBuilder<Shortest> | ||
*/ | ||
final class ShortestBuilder extends MultiOperandFunctionBuilder | ||
Tigrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* Builds a SQL expression to represent the function which returns the shortest string. | ||
* | ||
* @param Shortest $expression The expression to build. | ||
* @param array $params The parameters to bind. | ||
* | ||
* @return string The SQL expression. | ||
*/ | ||
protected function buildFromExpression(MultiOperandFunction $expression, array &$params): string | ||
{ | ||
$selects = []; | ||
|
||
foreach ($expression->getOperands() as $operand) { | ||
$selects[] = 'SELECT ' . $this->buildOperand($operand, $params) . ' AS value FROM DUAL'; | ||
} | ||
|
||
$unions = implode(' UNION ', $selects); | ||
|
||
return "(SELECT value FROM ($unions) ORDER BY LENGTH(value) ASC FETCH FIRST 1 ROWS ONLY)"; | ||
} | ||
} |
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
Oops, something went wrong.
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.