diff --git a/src/Drivers/MySQL.php b/src/Drivers/MySQL.php index ab5d81e..d1776c6 100755 --- a/src/Drivers/MySQL.php +++ b/src/Drivers/MySQL.php @@ -16,9 +16,10 @@ class MySQL implements SqlDialectInterface { #[\Override] - public function groupConcat(string $fieldName, string $alias, ?string $orderBy): string + public function groupConcat(string $fieldName, string $alias, ?string $orderBy, bool $distinct = true): string { - return ", GROUP_CONCAT($fieldName ORDER BY $orderBy SEPARATOR ';') as $alias "; + $distinctStr = $distinct ? 'DISTINCT ' : ''; + return ", GROUP_CONCAT({$distinctStr}$fieldName ORDER BY $orderBy SEPARATOR ';') as $alias "; } #[\Override] diff --git a/src/Drivers/PgSQL.php b/src/Drivers/PgSQL.php index 2b2cd47..7e8e849 100755 --- a/src/Drivers/PgSQL.php +++ b/src/Drivers/PgSQL.php @@ -16,9 +16,11 @@ class PgSQL implements SqlDialectInterface { #[\Override] - public function groupConcat(string $fieldName, string $alias, ?string $orderBy): string + public function groupConcat(string $fieldName, string $alias, ?string $orderBy, bool $distinct = true): string { - return ", array_to_string(array_agg($fieldName ORDER BY $orderBy), ';') $alias "; + $distinctStr = $distinct ? 'DISTINCT ' : ''; + $orderField = $distinct ? $fieldName : ($orderBy ?? $fieldName); + return ", array_to_string(array_agg({$distinctStr}$fieldName ORDER BY $orderField), ';') $alias"; } #[\Override] diff --git a/src/Drivers/SQLite.php b/src/Drivers/SQLite.php index 02c1cba..f993295 100755 --- a/src/Drivers/SQLite.php +++ b/src/Drivers/SQLite.php @@ -16,9 +16,10 @@ class SQLite implements SqlDialectInterface { #[\Override] - public function groupConcat(string $fieldName, string $alias, ?string $orderBy): string + public function groupConcat(string $fieldName, string $alias, ?string $orderBy, bool $distinct = true): string { - return ", GROUP_CONCAT($fieldName,';') $alias "; + $distinctStr = $distinct ? 'DISTINCT ' : ''; + return ", GROUP_CONCAT({$distinctStr}$fieldName, ';') $alias"; } #[\Override] diff --git a/src/Interfaces/SqlDialectInterface.php b/src/Interfaces/SqlDialectInterface.php index 48d86ea..4dbb486 100644 --- a/src/Interfaces/SqlDialectInterface.php +++ b/src/Interfaces/SqlDialectInterface.php @@ -13,7 +13,7 @@ interface SqlDialectInterface { - public function groupConcat(string $fieldName, string $alias, ?string $orderBy): string; + public function groupConcat(string $fieldName, string $alias, ?string $orderBy, bool $distinct = true): string; public function close(): string; public function integer(string $field, string $default = "", bool $autoincrement = false, string $null = "NOT NULL"): string; public function string(string $field, string $default = "", string $null = "NOT NULL"): string;