@@ -144,14 +144,16 @@ private function processParsedUnionQuery(array $parsedSql, ?string $sqlCount): a
144
144
private function processParsedSelectQuery (array $ parsedSql , ?string $ sqlCount ): array
145
145
{
146
146
// 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 ' ]);
148
148
$ generator = new PHPSQLCreator ();
149
149
$ parsedSql ['SELECT ' ] = $ select ;
150
150
$ processedSql = $ generator ->create ($ parsedSql );
151
151
152
152
// 2: let's compute the count query if needed
153
153
if ($ sqlCount === null ) {
154
- $ parsedSqlCount = $ this ->generateParsedSqlCount ($ parsedSql );
154
+ $ parsedCountSql = $ parsedSql ;
155
+ $ parsedCountSql ['SELECT ' ] = $ countSelect ;
156
+ $ parsedSqlCount = $ this ->generateParsedSqlCount ($ parsedCountSql );
155
157
$ processedSqlCount = $ generator ->create ($ parsedSqlCount );
156
158
} else {
157
159
$ processedSqlCount = $ sqlCount ;
@@ -173,40 +175,46 @@ private function formatSelect(array $baseSelect): array
173
175
174
176
$ connection = $ this ->tdbmService ->getConnection ();
175
177
$ formattedSelect = [];
178
+ $ formattedCountSelect = [];
176
179
$ columnDescriptors = [];
177
180
$ fetchedTables = [];
178
181
179
182
foreach ($ baseSelect as $ entry ) {
180
183
if ($ entry ['expr_type ' ] !== 'colref ' ) {
181
184
$ formattedSelect [] = $ entry ;
185
+ $ formattedCountSelect [] = $ entry ;
182
186
continue ;
183
187
}
184
188
185
189
$ noQuotes = $ entry ['no_quotes ' ];
186
190
if ($ noQuotes ['delim ' ] !== '. ' || count ($ noQuotes ['parts ' ]) !== 2 ) {
187
191
$ formattedSelect [] = $ entry ;
192
+ $ formattedCountSelect [] = $ entry ;
188
193
continue ;
189
194
}
190
195
191
196
$ tableName = $ noQuotes ['parts ' ][0 ];
192
197
if (!in_array ($ tableName , $ relatedTables )) {
193
198
$ formattedSelect [] = $ entry ;
199
+ $ formattedCountSelect [] = $ entry ;
194
200
continue ;
195
201
}
196
202
197
203
$ columnName = $ noQuotes ['parts ' ][1 ];
198
204
if ($ columnName !== '* ' ) {
199
205
$ formattedSelect [] = $ entry ;
206
+ $ formattedCountSelect [] = $ entry ;
200
207
continue ;
201
208
}
202
209
203
210
$ table = $ this ->schema ->getTable ($ tableName );
211
+ $ pkColumns = $ table ->getPrimaryKeyColumns ();
204
212
foreach ($ table ->getColumns () as $ column ) {
205
213
$ columnName = $ column ->getName ();
206
214
$ alias = "{$ tableName }____ {$ columnName }" ;
207
- $ formattedSelect [] = [
215
+ $ astColumn = [
208
216
'expr_type ' => 'colref ' ,
209
- 'base_expr ' => $ connection ->quoteIdentifier ($ tableName ). '. ' . $ connection ->quoteIdentifier ($ columnName ),
217
+ 'base_expr ' => $ connection ->quoteIdentifier ($ tableName ) . '. ' . $ connection ->quoteIdentifier ($ columnName ),
210
218
'no_quotes ' => [
211
219
'delim ' => '. ' ,
212
220
'parts ' => [
@@ -219,7 +227,10 @@ private function formatSelect(array $baseSelect): array
219
227
'name ' => $ alias ,
220
228
]
221
229
];
222
-
230
+ $ formattedSelect [] = $ astColumn ;
231
+ if (in_array ($ columnName , $ pkColumns )) {
232
+ $ formattedCountSelect [] = $ astColumn ;
233
+ }
223
234
$ columnDescriptors [$ alias ] = [
224
235
'as ' => $ alias ,
225
236
'table ' => $ tableName ,
@@ -241,7 +252,13 @@ private function formatSelect(array $baseSelect): array
241
252
$ formattedSelect [$ i ]['delim ' ] = ', ' ;
242
253
}
243
254
}
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 ];
245
262
}
246
263
247
264
/**
0 commit comments