@@ -144,14 +144,16 @@ private function processParsedUnionQuery(array $parsedSql, ?string $sqlCount): a
144144 private function processParsedSelectQuery (array $ parsedSql , ?string $ sqlCount ): array
145145 {
146146 // 1: let's reformat the SELECT and construct our columns
147- list ($ select , $ columnDescriptors ) = $ this ->formatSelect ($ parsedSql ['SELECT ' ]);
147+ list ($ select , $ countSelect , $ columnDescriptors ) = $ this ->formatSelect ($ parsedSql ['SELECT ' ]);
148148 $ generator = new PHPSQLCreator ();
149149 $ parsedSql ['SELECT ' ] = $ select ;
150150 $ processedSql = $ generator ->create ($ parsedSql );
151151
152152 // 2: let's compute the count query if needed
153153 if ($ sqlCount === null ) {
154- $ parsedSqlCount = $ this ->generateParsedSqlCount ($ parsedSql );
154+ $ parsedCountSql = $ parsedSql ;
155+ $ parsedCountSql ['SELECT ' ] = $ countSelect ;
156+ $ parsedSqlCount = $ this ->generateParsedSqlCount ($ parsedCountSql );
155157 $ processedSqlCount = $ generator ->create ($ parsedSqlCount );
156158 } else {
157159 $ processedSqlCount = $ sqlCount ;
@@ -173,40 +175,46 @@ private function formatSelect(array $baseSelect): array
173175
174176 $ connection = $ this ->tdbmService ->getConnection ();
175177 $ formattedSelect = [];
178+ $ formattedCountSelect = [];
176179 $ columnDescriptors = [];
177180 $ fetchedTables = [];
178181
179182 foreach ($ baseSelect as $ entry ) {
180183 if ($ entry ['expr_type ' ] !== 'colref ' ) {
181184 $ formattedSelect [] = $ entry ;
185+ $ formattedCountSelect [] = $ entry ;
182186 continue ;
183187 }
184188
185189 $ noQuotes = $ entry ['no_quotes ' ];
186190 if ($ noQuotes ['delim ' ] !== '. ' || count ($ noQuotes ['parts ' ]) !== 2 ) {
187191 $ formattedSelect [] = $ entry ;
192+ $ formattedCountSelect [] = $ entry ;
188193 continue ;
189194 }
190195
191196 $ tableName = $ noQuotes ['parts ' ][0 ];
192197 if (!in_array ($ tableName , $ relatedTables )) {
193198 $ formattedSelect [] = $ entry ;
199+ $ formattedCountSelect [] = $ entry ;
194200 continue ;
195201 }
196202
197203 $ columnName = $ noQuotes ['parts ' ][1 ];
198204 if ($ columnName !== '* ' ) {
199205 $ formattedSelect [] = $ entry ;
206+ $ formattedCountSelect [] = $ entry ;
200207 continue ;
201208 }
202209
203210 $ table = $ this ->schema ->getTable ($ tableName );
211+ $ pkColumns = $ table ->getPrimaryKeyColumns ();
204212 foreach ($ table ->getColumns () as $ column ) {
205213 $ columnName = $ column ->getName ();
206214 $ alias = "{$ tableName }____ {$ columnName }" ;
207- $ formattedSelect [] = [
215+ $ astColumn = [
208216 'expr_type ' => 'colref ' ,
209- 'base_expr ' => $ connection ->quoteIdentifier ($ tableName ). '. ' . $ connection ->quoteIdentifier ($ columnName ),
217+ 'base_expr ' => $ connection ->quoteIdentifier ($ tableName ) . '. ' . $ connection ->quoteIdentifier ($ columnName ),
210218 'no_quotes ' => [
211219 'delim ' => '. ' ,
212220 'parts ' => [
@@ -219,7 +227,10 @@ private function formatSelect(array $baseSelect): array
219227 'name ' => $ alias ,
220228 ]
221229 ];
222-
230+ $ formattedSelect [] = $ astColumn ;
231+ if (in_array ($ columnName , $ pkColumns )) {
232+ $ formattedCountSelect [] = $ astColumn ;
233+ }
223234 $ columnDescriptors [$ alias ] = [
224235 'as ' => $ alias ,
225236 'table ' => $ tableName ,
@@ -241,7 +252,13 @@ private function formatSelect(array $baseSelect): array
241252 $ formattedSelect [$ i ]['delim ' ] = ', ' ;
242253 }
243254 }
244- return [$ formattedSelect , $ columnDescriptors ];
255+
256+ for ($ i = 0 ; $ i < count ($ formattedCountSelect ) - 1 ; $ i ++) {
257+ if (!isset ($ formattedCountSelect [$ i ]['delim ' ])) {
258+ $ formattedCountSelect [$ i ]['delim ' ] = ', ' ;
259+ }
260+ }
261+ return [$ formattedSelect , $ formattedCountSelect , $ columnDescriptors ];
245262 }
246263
247264 /**
0 commit comments