44
55namespace PhpDb \Sql \Part ;
66
7+ use PhpDb \Sql \ArgumentInterface ;
78use PhpDb \Sql \ExpressionInterface ;
89use PhpDb \Sql \Platform \AbstractSqlRenderer ;
910use PhpDb \Sql \Select ;
1011
12+ use function array_merge ;
1113use function count ;
1214use function current ;
1315use function implode ;
1416use function is_array ;
1517use function is_numeric ;
16- use function is_string ;
1718use function key ;
1819
1920class Columns extends AbstractPart
@@ -23,8 +24,8 @@ class Columns extends AbstractPart
2324 private array $ rawColumns = [Select::SQL_STAR ];
2425 private bool $ prefixColumnsWithTable = true ;
2526 private string $ fromTablePrefix = '' ;
26- /** @var JoinSpec [] */
27- private array $ joinSpecs = [];
27+ /** @var ColumnRef [] */
28+ private array $ joinRefs = [];
2829
2930 public function __construct ()
3031 {
@@ -36,24 +37,28 @@ public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', i
3637 $ refs = $ this ->columnRefs ;
3738 $ fromPrefix = $ this ->fromTablePrefix ;
3839
39- if (isset ($ refs [0 ]) && ! isset ($ refs [1 ]) && $ refs [0 ]->isStar && $ this ->joinSpecs === []) {
40+ if (isset ($ refs [0 ]) && ! isset ($ refs [1 ]) && $ refs [0 ]->isStar && $ this ->joinRefs === []) {
4041 return $ fromPrefix . '* ' ;
4142 }
4243
4344 $ fragments = [];
4445 $ exprCounter = 1 ;
46+ $ pi = 0 ;
4547 $ platform = $ renderer ->platform ;
48+ $ allRefs = $ this ->joinRefs !== [] ? array_merge ($ refs , $ this ->joinRefs ) : $ refs ;
49+
50+ foreach ($ allRefs as $ ref ) {
51+ $ prefix = $ ref ->table !== null ? $ renderer ->tablePrefix ($ ref ->table ) : $ fromPrefix ;
4652
47- foreach ($ refs as $ ref ) {
4853 if ($ ref ->isStar ) {
49- $ fragments [] = $ fromPrefix . '* ' ;
54+ $ fragments [] = $ prefix . '* ' ;
5055 continue ;
5156 }
5257
5358 $ column = $ ref ->column ;
5459
55- if (is_string ( $ column) ) {
56- $ columnSql = $ fromPrefix . $ platform -> quoteIdentifier ( $ column );
60+ if ($ column instanceof ArgumentInterface ) {
61+ $ columnSql = $ prefix . $ column -> render ( $ renderer , '' , $ pi );
5762 } else {
5863 $ columnSql = $ renderer ->render ($ column , $ ref ->alias ?? 'column ' );
5964 }
@@ -67,37 +72,6 @@ public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', i
6772 }
6873 }
6974
70- if ($ this ->joinSpecs !== []) {
71- foreach ($ this ->joinSpecs as $ spec ) {
72- if ($ spec ->columnRefs === []) {
73- continue ;
74- }
75- $ joinPrefix = $ renderer ->tablePrefix ($ spec ->table );
76- foreach ($ spec ->columnRefs as $ ref ) {
77- if ($ ref ->isStar ) {
78- $ fragments [] = $ joinPrefix . '* ' ;
79- continue ;
80- }
81-
82- $ column = $ ref ->column ;
83-
84- if (is_string ($ column )) {
85- $ columnSql = $ joinPrefix . $ platform ->quoteIdentifier ($ column );
86- } else {
87- $ columnSql = $ renderer ->render ($ column , $ ref ->alias ?? 'column ' );
88- }
89-
90- if ($ ref ->alias !== null ) {
91- $ fragments [] = $ columnSql . ' AS ' . $ platform ->quoteIdentifier ($ ref ->alias );
92- } elseif ($ ref ->containsAlias ) {
93- $ fragments [] = $ columnSql ;
94- } else {
95- $ fragments [] = $ columnSql . ' AS Expression ' . $ exprCounter ++;
96- }
97- }
98- }
99- }
100-
10175 return implode (', ' , $ fragments );
10276 }
10377
@@ -113,7 +87,7 @@ public function set(array $columns): static
11387 return $ this ;
11488 }
11589
116- public function add (array |ExpressionInterface |string $ column , ?string $ alias = null ): static
90+ public function add (array |ArgumentInterface | ExpressionInterface |string $ column , ?string $ alias = null ): static
11791 {
11892 if (is_array ($ column )) {
11993 $ key = key ($ column );
@@ -153,16 +127,19 @@ public function setFromTablePrefix(string $prefix): static
153127 return $ this ;
154128 }
155129
156- public function addJoinSpec (JoinSpec $ spec ): static
130+ /** @param ColumnRef[] $refs */
131+ public function addJoinRefs (array $ refs ): static
157132 {
158- $ this ->joinSpecs [] = $ spec ;
133+ foreach ($ refs as $ ref ) {
134+ $ this ->joinRefs [] = $ ref ;
135+ }
159136 return $ this ;
160137 }
161138
162- /** @param JoinSpec [] $specs */
163- public function setJoinSpecs (array $ specs ): static
139+ /** @param ColumnRef [] $refs */
140+ public function setJoinRefs (array $ refs ): static
164141 {
165- $ this ->joinSpecs = $ specs ;
142+ $ this ->joinRefs = $ refs ;
166143 return $ this ;
167144 }
168145
0 commit comments