1313use mako \database \query \Query ;
1414use mako \database \query \Raw ;
1515use mako \database \query \Subquery ;
16+ use mako \database \query \values \out \ValueWithAliasInterface ;
17+ use mako \database \query \values \ValueInterface ;
1618
1719use function array_keys ;
1820use function array_shift ;
@@ -83,6 +85,28 @@ protected function raw(Raw $raw): string
8385 return $ raw ->getSql ();
8486 }
8587
88+ /**
89+ * Compiles a value.
90+ */
91+ protected function value (ValueInterface $ value ): string
92+ {
93+ $ parameters = $ value ->getParameters ();
94+
95+ if (!empty ($ parameters )) {
96+ $ this ->params = [...$ this ->params , ...$ parameters ];
97+ }
98+
99+ $ sql = $ value ->getSql ($ this );
100+
101+ if ($ value instanceof ValueWithAliasInterface) {
102+ if (($ alias = $ value ->getAlias ()) !== null ) {
103+ $ sql .= " AS {$ this ->escapeIdentifier ($ alias )}" ;
104+ }
105+ }
106+
107+ return $ sql ;
108+ }
109+
86110 /**
87111 * Compiles a subselect and merges the parameters.
88112 */
@@ -210,11 +234,13 @@ public function escapeTableNames(array $tables): string
210234 */
211235 public function table (Raw |string |Subquery $ table ): string
212236 {
213- if ($ table instanceof Raw) {
214- return $ this ->raw ($ table );
215- }
216- elseif ($ table instanceof Subquery) {
217- return $ this ->subquery ($ table );
237+ if (is_object ($ table )) {
238+ if ($ table instanceof Raw) {
239+ return $ this ->raw ($ table );
240+ }
241+ elseif ($ table instanceof Subquery) {
242+ return $ this ->subquery ($ table );
243+ }
218244 }
219245
220246 return $ this ->escapeTableNameWithAlias ($ table );
@@ -286,15 +312,21 @@ public function columnNames(array $columns): string
286312 /**
287313 * Compiles a column.
288314 */
289- public function column (Raw |string |Subquery $ column , bool $ allowAlias = false ): string
315+ public function column (Raw |string |Subquery | ValueInterface $ column , bool $ allowAlias = false ): string
290316 {
291- if ($ column instanceof Raw) {
292- return $ this ->raw ($ column );
293- }
294- elseif ($ column instanceof Subquery) {
295- return $ this ->subquery ($ column );
317+ if (is_object ($ column )) {
318+ if ($ column instanceof Raw) {
319+ return $ this ->raw ($ column );
320+ }
321+ elseif ($ column instanceof Subquery) {
322+ return $ this ->subquery ($ column );
323+ }
324+ elseif ($ column instanceof ValueInterface) {
325+ return $ this ->value ($ column );
326+ }
296327 }
297- elseif ($ allowAlias && stripos ($ column , ' AS ' ) !== false ) {
328+
329+ if ($ allowAlias && stripos ($ column , ' AS ' ) !== false ) {
298330 [$ column , , $ alias ] = explode (' ' , $ column , 3 );
299331
300332 return "{$ this ->columnName ($ column )} AS {$ this ->columnName ($ alias )}" ;
@@ -378,6 +410,9 @@ protected function param(mixed $param, bool $enclose = true): string
378410
379411 return '? ' ;
380412 }
413+ elseif ($ param instanceof ValueInterface) {
414+ return $ this ->value ($ param );
415+ }
381416 }
382417
383418 $ this ->params [] = $ param ;
@@ -483,6 +518,14 @@ protected function whereColumn(array $where): string
483518 return "{$ this ->columnName ($ where ['column1 ' ])} {$ where ['operator ' ]} {$ this ->columnName ($ where ['column2 ' ])}" ;
484519 }
485520
521+ /**
522+ * Compiles vector distance clauses.
523+ */
524+ protected function whereVectorDistance (array $ where ): string
525+ {
526+ throw new DatabaseException (sprintf ('The [ %s ] query compiler does not support vector distance calculations. ' , static ::class));
527+ }
528+
486529 /**
487530 * Compiles BETWEEN clauses.
488531 */
@@ -631,6 +674,22 @@ protected function groupings(array $groupings): string
631674 return empty ($ groupings ) ? '' : " GROUP BY {$ this ->columns ($ groupings )}" ;
632675 }
633676
677+ /**
678+ * Compiles a basic ordering clause.
679+ */
680+ protected function basicOrdering (array $ order ): string
681+ {
682+ return "{$ this ->columns ($ order ['column ' ])} {$ order ['order ' ]}" ;
683+ }
684+
685+ /**
686+ * Compiles vector distance ordering clause.
687+ */
688+ protected function vectorDistanceOrdering (array $ order ): string
689+ {
690+ throw new DatabaseException (sprintf ('The [ %s ] query compiler does not support vector distance calculations. ' , static ::class));
691+ }
692+
634693 /**
635694 * Compiles ORDER BY clauses.
636695 */
@@ -643,7 +702,7 @@ protected function orderings(array $orderings): string
643702 $ sql = [];
644703
645704 foreach ($ orderings as $ order ) {
646- $ sql [] = "{ $ this ->columns ( $ order ['column ' ])} { $ order[ ' order ' ]}" ;
705+ $ sql [] = $ this ->{ $ order ['type ' ]}( $ order) ;
647706 }
648707
649708 return ' ORDER BY ' . implode (', ' , $ sql );
0 commit comments