Skip to content

Commit 4cfcbed

Browse files
samdarkvjik
andauthored
Fix #730: Add ExpressionBuilderInterface::build() (#946)
Co-authored-by: Sergei Predvoditelev <[email protected]>
1 parent 87b29b1 commit 4cfcbed

23 files changed

+123
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
- New #939: Add `caseSensitive` option to like condition (@vjik)
7878
- New #942: Allow PHP backed enums as values (@Tigrov)
7979
- Enh #943: Add `getCacheKey()` and `getCacheTag()` methods to `AbstractPdoSchema` class (@Tigrov)
80+
- Enh #730: Add `ExpressionBuilderInterface::build()` (@samdark)
8081
- Enh #944: Added `setWhere()` as a forced method to overwrite `where()` (@lav45)
8182
- Enh #925, #951: Add callback to `Query::all()` and `Query::one()` methods (@Tigrov, @vjik)
8283
- New #954: Add `DbArrayHelper::arrange()` method (@Tigrov)

src/Command/ParamBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/**
1313
* Implements the {@see ExpressionBuilderInterface} interface, is used to build {@see ParamInterface} objects.
14+
*
15+
* @implements ExpressionBuilderInterface<ExpressionInterface>
1416
*/
1517
final class ParamBuilder implements ExpressionBuilderInterface
1618
{

src/Expression/AbstractArrayExpressionBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
/**
1414
* Abstract expression builder for {@see ArrayExpression}.
15+
*
16+
* @implements ExpressionBuilderInterface<ArrayExpression>
1517
*/
1618
abstract class AbstractArrayExpressionBuilder implements ExpressionBuilderInterface
1719
{
1820
/**
19-
* Builds a SQL expression for a string value.
21+
* Builds an SQL expression for a string value.
2022
*
2123
* @param string $value The valid SQL string representation of the array value.
2224
* @param ArrayExpression $expression The array expression.

src/Expression/AbstractStructuredExpressionBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
/**
2222
* Abstract expression builder for {@see StructuredExpression}.
23+
*
24+
* @implements ExpressionBuilderInterface<StructuredExpression>
2325
*/
2426
abstract class AbstractStructuredExpressionBuilder implements ExpressionBuilderInterface
2527
{
2628
/**
27-
* Builds a SQL expression for a string value.
29+
* Builds an SQL expression for a string value.
2830
*
2931
* @param string $value The valid SQL string representation of the structured value.
3032
* @param StructuredExpression $expression The structured expression.
@@ -54,7 +56,7 @@ abstract protected function buildSubquery(
5456
): string;
5557

5658
/**
57-
* Builds a SQL expression for a structured value.
59+
* Builds an SQL expression for a structured value.
5860
*
5961
* @param array|object $value The structured value.
6062
* @param StructuredExpression $expression The structured expression.

src/Expression/CaseExpressionBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* Builds expressions for {@see CaseExpression}.
16+
*
17+
* @implements ExpressionBuilderInterface<CaseExpression>
1618
*/
1719
class CaseExpressionBuilder implements ExpressionBuilderInterface
1820
{

src/Expression/Expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class Expression implements ExpressionInterface, Stringable
3131
/**
3232
* @psalm-param ParamsType $params
3333
*/
34-
public function __construct(private string $expression, private array $params = [])
34+
public function __construct(private readonly string $expression, private readonly array $params = [])
3535
{
3636
}
3737

src/Expression/ExpressionBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* These expressions can be used with the query builder to build complex and customizable database queries
2121
* {@see Expression} class.
2222
*
23+
* @implements ExpressionBuilderInterface<Expression>
24+
*
2325
* @psalm-import-type ParamsType from ConnectionInterface
2426
*/
2527
final class ExpressionBuilder implements ExpressionBuilderInterface

src/Expression/ExpressionBuilderInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
1515
* database queries, without having to worry about the specific syntax of the underlying database.
1616
*
1717
* @see ExpressionInterface
18+
*
19+
* @template T as ExpressionInterface
1820
*/
1921
interface ExpressionBuilderInterface
2022
{
23+
/**
24+
* Method builds the raw SQL from the expression that will not be additionally
25+
* escaped or quoted.
26+
*
27+
* @param ExpressionInterface $expression The expression to be built.
28+
* @param array $params The binding parameters.
29+
* @throws \InvalidArgumentException If builder can't handle expression passed.
30+
* @return string The raw SQL that will not be additionally escaped or quoted.
31+
*
32+
* @psalm-param T $expression
33+
*/
34+
public function build(ExpressionInterface $expression, array &$params = []): string;
2135
}

src/Expression/JsonExpressionBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
/**
3030
* Builds expressions for {@see JsonExpression}.
31+
*
32+
* @implements ExpressionBuilderInterface<JsonExpression>
3133
*/
3234
final class JsonExpressionBuilder implements ExpressionBuilderInterface
3335
{

src/Query/QueryExpressionBuilder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
use Yiisoft\Db\Exception\InvalidConfigException;
1010
use Yiisoft\Db\Exception\NotSupportedException;
1111
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
12+
use Yiisoft\Db\Expression\ExpressionInterface;
1213
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
1314

1415
/**
15-
* Used internally to build {@see Query} object using unified {@see \Yiisoft\Db\QueryBuilder\AbstractQueryBuilder}
16+
* Used internally to build a {@see Query} object using unified {@see \Yiisoft\Db\QueryBuilder\AbstractQueryBuilder}
1617
* expression building interface.
18+
*
19+
* @implements ExpressionBuilderInterface<QueryInterface>
1720
*/
1821
final class QueryExpressionBuilder implements ExpressionBuilderInterface
1922
{
@@ -22,12 +25,14 @@ public function __construct(private QueryBuilderInterface $queryBuilder)
2225
}
2326

2427
/**
28+
* @param QueryInterface $expression
29+
*
2530
* @throws Exception
2631
* @throws InvalidArgumentException
2732
* @throws InvalidConfigException
2833
* @throws NotSupportedException
2934
*/
30-
public function build(QueryInterface $expression, array &$params = []): string
35+
public function build(ExpressionInterface $expression, array &$params = []): string
3136
{
3237
[$sql, $params] = $this->queryBuilder->build($expression, $params);
3338
return "($sql)";

0 commit comments

Comments
 (0)