2021-12-08 16:53:43 +01:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\Driver\SQL\Expression;
|
2021-12-08 16:53:43 +01:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\Driver\SQL\Column\Column;
|
|
|
|
use Core\Driver\SQL\MySQL;
|
|
|
|
use Core\Driver\SQL\PostgreSQL;
|
|
|
|
use Core\Driver\SQL\SQL;
|
|
|
|
use Exception;
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
class JsonArrayAgg extends Expression {
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
private mixed $value;
|
2021-12-08 16:53:43 +01:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
public function __construct(mixed $value) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
public function getExpression(SQL $sql, array &$params): string {
|
|
|
|
$value = is_string($this->value) ? new Column($this->value) : $this->value;
|
|
|
|
$value = $sql->addValue($value, $params);
|
|
|
|
if ($sql instanceof MySQL) {
|
|
|
|
return "JSON_ARRAYAGG($value)";
|
|
|
|
} else if ($sql instanceof PostgreSQL) {
|
|
|
|
return "JSON_AGG($value)";
|
|
|
|
} else {
|
|
|
|
throw new Exception("JsonArrayAgg not implemented for driver type: " . get_class($sql));
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 16:53:43 +01:00
|
|
|
}
|