1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Db\Sqlite\Builder; |
||
6 | |||
7 | use JsonException; |
||
8 | use Yiisoft\Db\Exception\Exception; |
||
9 | use Yiisoft\Db\Exception\InvalidArgumentException; |
||
10 | use Yiisoft\Db\Exception\InvalidConfigException; |
||
11 | use Yiisoft\Db\Exception\NotSupportedException; |
||
12 | use Yiisoft\Db\Expression\ExpressionBuilderInterface; |
||
13 | use Yiisoft\Db\Expression\ExpressionInterface; |
||
14 | use Yiisoft\Db\Expression\JsonExpression; |
||
15 | use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
||
16 | use Yiisoft\Db\Query\QueryInterface; |
||
17 | use Yiisoft\Json\Json; |
||
18 | |||
19 | /** |
||
20 | * Builds expressions for {@see `Yiisoft\Db\Expression\JsonExpression`} for SQLite. |
||
21 | */ |
||
22 | final class JsonExpressionBuilder implements ExpressionBuilderInterface |
||
23 | { |
||
24 | 13 | public function __construct(private QueryBuilderInterface $queryBuilder) |
|
25 | { |
||
26 | 13 | } |
|
27 | |||
28 | /** |
||
29 | * The method builds the raw SQL from the `$expression` that won't be additionally escaped or quoted. |
||
30 | * |
||
31 | * @param JsonExpression $expression The expression to build. |
||
32 | * @param array $params The binding parameters. |
||
33 | * |
||
34 | * @throws Exception |
||
35 | * @throws InvalidArgumentException |
||
36 | * @throws InvalidConfigException |
||
37 | * @throws JsonException |
||
38 | * @throws NotSupportedException |
||
39 | * |
||
40 | * @return string The raw SQL that won't be additionally escaped or quoted. |
||
41 | */ |
||
42 | 13 | public function build(ExpressionInterface $expression, array &$params = []): string |
|
43 | { |
||
44 | 13 | $value = $expression->getValue(); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
45 | |||
46 | 13 | if ($value instanceof QueryInterface) { |
|
47 | 2 | [$sql, $params] = $this->queryBuilder->build($value, $params); |
|
48 | |||
49 | 2 | return "($sql)"; |
|
50 | } |
||
51 | |||
52 | 11 | return $this->queryBuilder->bindParam(Json::encode($value), $params); |
|
53 | } |
||
54 | } |
||
55 |