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
|
|
|
/** @psalm-var mixed $value */ |
45
|
13 |
|
$value = $expression->getValue(); |
|
|
|
|
46
|
|
|
|
47
|
13 |
|
if ($value instanceof QueryInterface) { |
48
|
2 |
|
[$sql, $params] = $this->queryBuilder->build($value, $params); |
49
|
|
|
|
50
|
2 |
|
return "($sql)"; |
51
|
|
|
} |
52
|
|
|
|
53
|
11 |
|
return $this->queryBuilder->bindParam(Json::encode($value), $params); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|