-
-
Notifications
You must be signed in to change notification settings - Fork 43
Add functions as expressions #1029
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
15 commits
Select commit
Hold shift + click to select a range
096308d
Add functions as expressions
Tigrov f71c6bc
Apply fixes from StyleCI
StyleCIBot b2a8dc6
Fix tests
Tigrov 8b7d117
Fix psalm
Tigrov b941470
Merge branch 'master' into add-functions
Tigrov b5dea5f
Fix tests
Tigrov aaee6ec
Merge branch 'master' into add-functions
samdark 727f2d6
Fix psalm
Tigrov 4d8ca26
Add line to CHANGELOG.md [skip ci]
Tigrov 4b8e551
Apply suggestions from code review [skip ci]
Tigrov 7231945
Update doc
Tigrov 32907c2
Rename `addOperand()` to `add()`
Tigrov 6d173fc
Move `type` to `ArrayMerge`
Tigrov d82ad8e
Remove `buildSelect()`
Tigrov 27abb38
Update template annotation
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,44 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Function; | ||
|
|
||
| use Yiisoft\Db\Schema\Column\ColumnInterface; | ||
|
|
||
| /** | ||
| * Represents an SQL expression that returns the merged array from a list of operands. | ||
| * | ||
| * Example usage: | ||
| * | ||
| * ```php | ||
| * $arrayMerge = new ArrayMerge('operand1', 'operand2'); | ||
| * ``` | ||
| * | ||
| * For example, it will be generated into the following SQL expression in PostgreSQL: | ||
| * | ||
| * ```sql | ||
| * ARRAY(SELECT DISTINCT UNNEST(operand1 || operand2)) | ||
| * ``` | ||
| */ | ||
| final class ArrayMerge extends MultiOperandFunction | ||
| { | ||
| private string|ColumnInterface $type = ''; | ||
|
|
||
| /** | ||
| * Returns the type of the operands. Empty string if not set. | ||
| */ | ||
| public function getType(): string|ColumnInterface | ||
| { | ||
| return $this->type; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the type of the operands. | ||
| */ | ||
| public function type(string|ColumnInterface $type): self | ||
| { | ||
| $this->type = $type; | ||
| return $this; | ||
| } | ||
| } |
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,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Function\Builder; | ||
|
|
||
| use Yiisoft\Db\Expression\Function\Greatest; | ||
| use Yiisoft\Db\Expression\Function\MultiOperandFunction; | ||
|
|
||
| use function implode; | ||
|
|
||
| /** | ||
| * Builds SQL `GREATEST()` function expressions for {@see Greatest} objects. | ||
| * | ||
| * @extends MultiOperandFunctionBuilder<Greatest> | ||
| */ | ||
| final class GreatestBuilder extends MultiOperandFunctionBuilder | ||
| { | ||
| /** | ||
| * Builds a SQL `GREATEST()` function expression from the given {@see Greatest} object. | ||
| * | ||
| * @param Greatest $expression The expression to build. | ||
| * @param array $params The parameters to bind. | ||
| * | ||
| * @return string The SQL `GREATEST()` function expression. | ||
| */ | ||
| protected function buildFromExpression(MultiOperandFunction $expression, array &$params): string | ||
| { | ||
| $builtOperands = []; | ||
|
|
||
| foreach ($expression->getOperands() as $operand) { | ||
| $builtOperands[] = $this->buildOperand($operand, $params); | ||
| } | ||
|
|
||
| return 'GREATEST(' . implode(', ', $builtOperands) . ')'; | ||
| } | ||
| } |
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,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Function\Builder; | ||
|
|
||
| use Yiisoft\Db\Expression\Function\Least; | ||
| use Yiisoft\Db\Expression\Function\MultiOperandFunction; | ||
|
|
||
| use function implode; | ||
|
|
||
| /** | ||
| * Builds SQL `LEAST()` function expressions for {@see Least} objects. | ||
| * | ||
| * @extends MultiOperandFunctionBuilder<Least> | ||
| */ | ||
| final class LeastBuilder extends MultiOperandFunctionBuilder | ||
| { | ||
| /** | ||
| * Builds a SQL `LEAST()` function expression from the given {@see Least} object. | ||
| * | ||
| * @param Least $expression The expression to build. | ||
| * @param array $params The parameters to bind. | ||
| * | ||
| * @return string The SQL `LEAST()` function expression. | ||
| */ | ||
| protected function buildFromExpression(MultiOperandFunction $expression, array &$params): string | ||
| { | ||
| $builtOperands = []; | ||
|
|
||
| foreach ($expression->getOperands() as $operand) { | ||
| $builtOperands[] = $this->buildOperand($operand, $params); | ||
| } | ||
|
|
||
| return 'LEAST(' . implode(', ', $builtOperands) . ')'; | ||
| } | ||
| } |
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,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Function\Builder; | ||
|
|
||
| use Yiisoft\Db\Expression\Builder\ExpressionBuilderInterface; | ||
| use Yiisoft\Db\Expression\ExpressionInterface; | ||
| use Yiisoft\Db\Expression\Function\Length; | ||
| use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; | ||
|
|
||
| use function is_string; | ||
|
|
||
| /** | ||
| * Builds SQL `LENGTH()` function expressions for {@see Length} objects. | ||
| * | ||
| * @implements ExpressionBuilderInterface<Length> | ||
| */ | ||
| final class LengthBuilder implements ExpressionBuilderInterface | ||
| { | ||
| public function __construct(private readonly QueryBuilderInterface $queryBuilder) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Builds a SQL `LENGTH()` function expression from the given {@see Length} object. | ||
| * | ||
| * @param Length $expression The expression to build. | ||
| * @param array $params The parameters to be bound to the query. | ||
| * | ||
| * @return string The SQL `LENGTH()` function expression. | ||
| */ | ||
| public function build(ExpressionInterface $expression, array &$params = []): string | ||
| { | ||
| $operand = $expression->operand; | ||
|
|
||
| if (is_string($operand)) { | ||
| return "LENGTH($operand)"; | ||
| } | ||
|
|
||
| return 'LENGTH(' . $this->queryBuilder->buildExpression($operand, $params) . ')'; | ||
| } | ||
| } | ||
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\Expression\Function\Builder; | ||
|
|
||
| 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 | ||
| * UNION | ||
| * SELECT operand2 AS value | ||
| * ) AS t ORDER BY LENGTH(value) DESC LIMIT 1) | ||
| * ``` | ||
| * | ||
| * @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'; | ||
| } | ||
|
|
||
| $unions = implode(' UNION ', $selects); | ||
|
|
||
| return "(SELECT value FROM ($unions) AS t ORDER BY LENGTH(value) DESC LIMIT 1)"; | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
src/Expression/Function/Builder/MultiOperandFunctionBuilder.php
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,78 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Function\Builder; | ||
|
|
||
| use InvalidArgumentException; | ||
| use Yiisoft\Db\Expression\Builder\ExpressionBuilderInterface; | ||
| use Yiisoft\Db\Expression\ExpressionInterface; | ||
| use Yiisoft\Db\Expression\Function\MultiOperandFunction; | ||
| use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; | ||
|
|
||
| use function count; | ||
| use function is_string; | ||
|
|
||
| /** | ||
| * Base class for building SQL representation of multi-operand function expressions. | ||
| * | ||
| * @template T as MultiOperandFunction | ||
| * @implements ExpressionBuilderInterface<T> | ||
| */ | ||
| abstract class MultiOperandFunctionBuilder implements ExpressionBuilderInterface | ||
| { | ||
| /** | ||
| * Builds a SQL multi-operand function expression from the given {@see MultiOperandFunction} instance. | ||
| * | ||
| * @param MultiOperandFunction $expression The expression to build from. | ||
| * @param array $params The parameters to be bound to the query. | ||
| * | ||
| * @psalm-param T $expression | ||
| * | ||
| * @return string SQL multi-operand function expression. | ||
| */ | ||
| abstract protected function buildFromExpression(MultiOperandFunction $expression, array &$params): string; | ||
|
|
||
| public function __construct(protected readonly QueryBuilderInterface $queryBuilder) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Builds a SQL multi-operand function expression from the given {@see MultiOperandFunction} instance. | ||
| * | ||
| * @param MultiOperandFunction $expression The expression to build. | ||
| * @param array $params The parameters to be bound to the query. | ||
| * | ||
| * @psalm-param T $expression | ||
| * | ||
| * @return string SQL multi-operand function expression. | ||
| */ | ||
| public function build(ExpressionInterface $expression, array &$params = []): string | ||
| { | ||
| $operands = $expression->getOperands(); | ||
|
|
||
| if (empty($operands)) { | ||
| throw new InvalidArgumentException( | ||
| 'The ' . $expression::class . ' expression must have at least one operand.' | ||
| ); | ||
| } | ||
|
|
||
| if (count($operands) === 1) { | ||
| return '(' . $this->buildOperand($operands[0], $params) . ')'; | ||
| } | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return $this->buildFromExpression($expression, $params); | ||
| } | ||
|
|
||
| /** | ||
| * Builds an operand expression of the multi-operand function. | ||
| */ | ||
| protected function buildOperand(mixed $operand, array &$params): string | ||
| { | ||
| if (is_string($operand)) { | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return $operand; | ||
| } | ||
|
|
||
| return $this->queryBuilder->buildValue($operand, $params); | ||
| } | ||
| } | ||
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,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Function\Builder; | ||
|
|
||
| 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 | ||
| * UNION | ||
| * SELECT operand2 AS value | ||
| * ) AS t ORDER BY LENGTH(value) ASC LIMIT 1) | ||
| * ``` | ||
| * | ||
| * @extends MultiOperandFunctionBuilder<Shortest> | ||
| */ | ||
| final class ShortestBuilder extends MultiOperandFunctionBuilder | ||
| { | ||
| /** | ||
| * 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'; | ||
| } | ||
|
|
||
| $unions = implode(' UNION ', $selects); | ||
|
|
||
| return "(SELECT value FROM ($unions) AS t ORDER BY LENGTH(value) ASC LIMIT 1)"; | ||
| } | ||
| } |
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,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Expression\Function; | ||
|
|
||
| use Yiisoft\Db\Expression\Function\Builder\GreatestBuilder; | ||
|
|
||
| /** | ||
| * Represents a SQL `GREATEST()` function that returns the greatest value from a list of values or expressions. | ||
| * | ||
| * Example usage: | ||
| * | ||
| * ```php | ||
| * $greatest = new Greatest(1, 'a + b', $db->select('column')->from('table')->where(['id' => 1])); | ||
| * ``` | ||
| * | ||
| * ```sql | ||
| * GREATEST(1, a + b, (SELECT "column" FROM "table" WHERE "id" = 1)) | ||
| * ``` | ||
| * | ||
| * @see GreatestBuilder for building SQL representations of this function expression. | ||
| */ | ||
| final class Greatest extends MultiOperandFunction | ||
| { | ||
| } |
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.