yiisoft /
db-mysql
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Yiisoft\Db\Mysql\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 | use function count; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Builds expressions for {@see `Yiisoft\Db\Expression\JsonExpression`} for MySQL, MariaDB. |
||
| 23 | */ |
||
| 24 | final class JsonExpressionBuilder implements ExpressionBuilderInterface |
||
| 25 | { |
||
| 26 | public const PARAM_PREFIX = ':qp'; |
||
| 27 | |||
| 28 | 16 | public function __construct(private QueryBuilderInterface $queryBuilder) |
|
| 29 | { |
||
| 30 | 16 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @throws Exception |
||
| 34 | * @throws InvalidArgumentException |
||
| 35 | * @throws InvalidConfigException |
||
| 36 | * @throws JsonException |
||
| 37 | * @throws NotSupportedException |
||
| 38 | * |
||
| 39 | * @psalm-param JsonExpression $expression |
||
| 40 | */ |
||
| 41 | 16 | public function build(ExpressionInterface $expression, array &$params = []): string |
|
| 42 | { |
||
| 43 | /** @psalm-var mixed|QueryInterface $value */ |
||
| 44 | 16 | $value = $expression->getValue(); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 45 | |||
| 46 | 16 | if ($value instanceof QueryInterface) { |
|
| 47 | 2 | [$sql, $params] = $this->queryBuilder->build($value, $params); |
|
| 48 | |||
| 49 | 2 | return "($sql)"; |
|
| 50 | } |
||
| 51 | |||
| 52 | 14 | $placeholder = self::PARAM_PREFIX . count($params); |
|
| 53 | 14 | $params[$placeholder] = Json::encode($value); |
|
| 54 | |||
| 55 | 14 | return $placeholder; |
|
| 56 | } |
||
| 57 | } |
||
| 58 |