88 * @phpstan-type QueryParams array{
99 * select: array<int|string, string|int|\BackedEnum|Query|Db\Sql>,
1010 * distinct: bool,
11- * distinctOn: list<string>,
11+ * distinctOn: list<string|Query|Db\Sql >,
1212 * tables: array<string, array{0: string, 1: string}>,
1313 * table-types: array{main: string|NULL, from: list<string>, joins: list<string>, using: string|NULL},
1414 * on-conditions: array<string, Complex>,
@@ -95,7 +95,7 @@ private function createSelect(
9595 ): string
9696 {
9797 $ selectSql = 'SELECT ' .
98- $ this ->getSelectDistinct ($ queryParams ) .
98+ $ this ->getSelectDistinct ($ queryParams, $ params ) .
9999 $ this ->getSelectColumns ($ queryParams , $ params , $ insertSelectColumnNames ) .
100100 $ this ->getFrom ($ queryParams , $ params , $ insertSelectColumnNames === NULL ) .
101101 $ this ->getJoins ($ queryParams , $ params ) .
@@ -429,9 +429,10 @@ private function createWith(array $queryParams, array &$params): string
429429
430430 /**
431431 * @param array<string, mixed> $queryParams
432+ * @param list<mixed> $params
432433 * @phpstan-param QueryParams $queryParams
433434 */
434- private function getSelectDistinct (array $ queryParams ): string
435+ private function getSelectDistinct (array $ queryParams, array & $ params ): string
435436 {
436437 if (($ queryParams [Query::PARAM_DISTINCT ] === TRUE ) && ($ queryParams [Query::PARAM_DISTINCTON ] !== [])) {
437438 throw Exceptions \QueryBuilderException::cantCombineDistinctAndDistinctOn ();
@@ -440,7 +441,7 @@ private function getSelectDistinct(array $queryParams): string
440441 if ($ queryParams [Query::PARAM_DISTINCT ] === TRUE ) {
441442 return 'DISTINCT ' ;
442443 } else if ($ queryParams [Query::PARAM_DISTINCTON ] !== []) {
443- return 'DISTINCT ON ( ' . implode ( ' , ' , $ queryParams [Query::PARAM_DISTINCTON ]) . ') ' ;
444+ return 'DISTINCT ON ( ' . $ this -> processElements ( $ queryParams [Query::PARAM_DISTINCTON ], $ params ) . ') ' ;
444445 }
445446
446447 return '' ;
@@ -631,19 +632,7 @@ private function getOrderBy(array $queryParams, array &$params): string
631632 return '' ;
632633 }
633634
634- $ columns = [];
635- foreach ($ orderBy as $ value ) {
636- if ($ value instanceof Db \Sql) {
637- $ params [] = $ value ;
638- $ value = '? ' ;
639- } else if ($ value instanceof Query) {
640- $ params [] = $ value ->createSqlQuery ();
641- $ value = '(?) ' ;
642- }
643- $ columns [] = $ value ;
644- }
645-
646- return ' ORDER BY ' . \implode (', ' , $ columns );
635+ return ' ORDER BY ' . $ this ->processElements ($ orderBy , $ params );
647636 }
648637
649638
@@ -882,4 +871,27 @@ private function processComplex(Complex $complex, array &$params): string
882871 return \implode (' ' . $ complex ->getType () . ' ' , $ processedConditions );
883872 }
884873
874+
875+ /**
876+ * @param list<string|Query|Db\Sql> $elements
877+ * @param list<mixed> $params
878+ */
879+ private function processElements (array $ elements , array &$ params ): string
880+ {
881+ $ return = [];
882+
883+ foreach ($ elements as $ element ) {
884+ if ($ element instanceof Db \Sql) {
885+ $ params [] = $ element ;
886+ $ element = '? ' ;
887+ } else if ($ element instanceof Query) {
888+ $ params [] = $ element ->createSqlQuery ();
889+ $ element = '(?) ' ;
890+ }
891+ $ return [] = $ element ;
892+ }
893+
894+ return \implode (', ' , $ return );
895+ }
896+
885897}
0 commit comments