7
7
use Doctrine \DBAL \Connection ;
8
8
use Doctrine \DBAL \Driver \Result ;
9
9
use Doctrine \DBAL \Exception as DBALException ;
10
+ use Doctrine \DBAL \Query \QueryBuilder ;
10
11
use Doctrine \DBAL \Schema \Comparator ;
11
12
use Doctrine \DBAL \Schema \Schema ;
12
13
use Doctrine \DBAL \Types \Types ;
@@ -147,65 +148,15 @@ public function list(string $jobName): iterable
147
148
148
149
public function query (Query $ query ): iterable
149
150
{
150
- $ queryParameters = [];
151
- $ queryTypes = [];
152
-
153
151
$ qb = $ this ->connection ->createQueryBuilder ();
154
152
$ qb ->select ('* ' )
155
153
->from ($ this ->table );
156
154
157
- $ names = $ query ->jobs ();
158
- if (\count ($ names ) > 0 ) {
159
- $ qb ->andWhere ($ qb ->expr ()->in ('job_name ' , ':jobNames ' ));
160
- $ queryParameters ['jobNames ' ] = $ names ;
161
- $ queryTypes ['jobNames ' ] = Connection::PARAM_STR_ARRAY ;
162
- }
163
-
164
- $ ids = $ query ->ids ();
165
- if (\count ($ ids ) > 0 ) {
166
- $ qb ->andWhere ($ qb ->expr ()->in ('id ' , ':ids ' ));
167
- $ queryParameters ['ids ' ] = $ ids ;
168
- $ queryTypes ['ids ' ] = Connection::PARAM_STR_ARRAY ;
169
- }
170
-
171
- $ statuses = $ query ->statuses ();
172
- if (\count ($ statuses ) > 0 ) {
173
- $ qb ->andWhere ($ qb ->expr ()->in ('status ' , ':statuses ' ));
174
- $ queryParameters ['statuses ' ] = $ statuses ;
175
- $ queryTypes ['statuses ' ] = Connection::PARAM_INT_ARRAY ;
176
- }
177
-
178
- if ($ query ->startTime ()) {
179
- $ qb ->andWhere ($ qb ->expr ()->isNotNull ('start_time ' ));
180
- }
181
- $ startDateFrom = $ query ->startTime ()?->getFrom();
182
- if ($ startDateFrom ) {
183
- $ qb ->andWhere ($ qb ->expr ()->gte ('start_time ' , ':startDateFrom ' ));
184
- $ queryParameters ['startDateFrom ' ] = $ startDateFrom ;
185
- $ queryTypes ['startDateFrom ' ] = Types::DATETIME_IMMUTABLE ;
186
- }
187
- $ startDateTo = $ query ->startTime ()?->getTo();
188
- if ($ startDateTo ) {
189
- $ qb ->andWhere ($ qb ->expr ()->lte ('start_time ' , ':startDateTo ' ));
190
- $ queryParameters ['startDateTo ' ] = $ startDateTo ;
191
- $ queryTypes ['startDateTo ' ] = Types::DATETIME_IMMUTABLE ;
192
- }
193
-
194
- if ($ query ->endTime ()) {
195
- $ qb ->andWhere ($ qb ->expr ()->isNotNull ('start_time ' ));
196
- }
197
- $ endDateFrom = $ query ->endTime ()?->getFrom();
198
- if ($ endDateFrom ) {
199
- $ qb ->andWhere ($ qb ->expr ()->gte ('end_time ' , ':endDateFrom ' ));
200
- $ queryParameters ['endDateFrom ' ] = $ endDateFrom ;
201
- $ queryTypes ['endDateFrom ' ] = Types::DATETIME_IMMUTABLE ;
202
- }
203
- $ endDateTo = $ query ->endTime ()?->getTo();
204
- if ($ endDateTo ) {
205
- $ qb ->andWhere ($ qb ->expr ()->lte ('end_time ' , ':endDateTo ' ));
206
- $ queryParameters ['endDateTo ' ] = $ endDateTo ;
207
- $ queryTypes ['endDateTo ' ] = Types::DATETIME_IMMUTABLE ;
208
- }
155
+ /**
156
+ * @phpstan-var array<string, mixed> $queryParameters
157
+ * @phpstan-var array<string, string|int> $queryTypes
158
+ */
159
+ [$ queryParameters , $ queryTypes ] = $ this ->addWheres ($ query , $ qb );
209
160
210
161
switch ($ query ->sort ()) {
211
162
case Query::SORT_BY_START_ASC :
@@ -228,6 +179,24 @@ public function query(Query $query): iterable
228
179
yield from $ this ->queryList ($ qb ->getSQL (), $ queryParameters , $ queryTypes );
229
180
}
230
181
182
+ public function count (Query $ query ): int
183
+ {
184
+ $ qb = $ this ->connection ->createQueryBuilder ();
185
+ $ qb ->select ('count(*) ' )
186
+ ->from ($ this ->table );
187
+
188
+ /**
189
+ * @phpstan-var array<string, mixed> $queryParameters
190
+ * @phpstan-var array<string, string|int> $queryTypes
191
+ */
192
+ [$ queryParameters , $ queryTypes ] = $ this ->addWheres ($ query , $ qb );
193
+
194
+ /** @var int $result */
195
+ $ result = $ this ->connection ->executeQuery ($ qb ->getSQL (), $ queryParameters , $ queryTypes )->fetchOne ();
196
+
197
+ return $ result ;
198
+ }
199
+
231
200
private function getSchema (): Schema
232
201
{
233
202
$ schema = new Schema ();
@@ -357,4 +326,68 @@ private function getNormalizer(): JobExecutionRowNormalizer
357
326
358
327
return $ this ->normalizer ;
359
328
}
329
+
330
+ /**
331
+ * @return array{array<string, mixed>, array<string, string|int>}
332
+ */
333
+ private function addWheres (Query $ query , QueryBuilder $ qb ): array
334
+ {
335
+ $ queryParameters = [];
336
+ $ queryTypes = [];
337
+
338
+ $ names = $ query ->jobs ();
339
+ if (\count ($ names ) > 0 ) {
340
+ $ qb ->andWhere ($ qb ->expr ()->in ('job_name ' , ':jobNames ' ));
341
+ $ queryParameters ['jobNames ' ] = $ names ;
342
+ $ queryTypes ['jobNames ' ] = Connection::PARAM_STR_ARRAY ;
343
+ }
344
+
345
+ $ ids = $ query ->ids ();
346
+ if (\count ($ ids ) > 0 ) {
347
+ $ qb ->andWhere ($ qb ->expr ()->in ('id ' , ':ids ' ));
348
+ $ queryParameters ['ids ' ] = $ ids ;
349
+ $ queryTypes ['ids ' ] = Connection::PARAM_STR_ARRAY ;
350
+ }
351
+
352
+ $ statuses = $ query ->statuses ();
353
+ if (\count ($ statuses ) > 0 ) {
354
+ $ qb ->andWhere ($ qb ->expr ()->in ('status ' , ':statuses ' ));
355
+ $ queryParameters ['statuses ' ] = $ statuses ;
356
+ $ queryTypes ['statuses ' ] = Connection::PARAM_INT_ARRAY ;
357
+ }
358
+
359
+ if ($ query ->startTime ()) {
360
+ $ qb ->andWhere ($ qb ->expr ()->isNotNull ('start_time ' ));
361
+ }
362
+ $ startDateFrom = $ query ->startTime ()?->getFrom();
363
+ if ($ startDateFrom ) {
364
+ $ qb ->andWhere ($ qb ->expr ()->gte ('start_time ' , ':startDateFrom ' ));
365
+ $ queryParameters ['startDateFrom ' ] = $ startDateFrom ;
366
+ $ queryTypes ['startDateFrom ' ] = Types::DATETIME_IMMUTABLE ;
367
+ }
368
+ $ startDateTo = $ query ->startTime ()?->getTo();
369
+ if ($ startDateTo ) {
370
+ $ qb ->andWhere ($ qb ->expr ()->lte ('start_time ' , ':startDateTo ' ));
371
+ $ queryParameters ['startDateTo ' ] = $ startDateTo ;
372
+ $ queryTypes ['startDateTo ' ] = Types::DATETIME_IMMUTABLE ;
373
+ }
374
+
375
+ if ($ query ->endTime ()) {
376
+ $ qb ->andWhere ($ qb ->expr ()->isNotNull ('start_time ' ));
377
+ }
378
+ $ endDateFrom = $ query ->endTime ()?->getFrom();
379
+ if ($ endDateFrom ) {
380
+ $ qb ->andWhere ($ qb ->expr ()->gte ('end_time ' , ':endDateFrom ' ));
381
+ $ queryParameters ['endDateFrom ' ] = $ endDateFrom ;
382
+ $ queryTypes ['endDateFrom ' ] = Types::DATETIME_IMMUTABLE ;
383
+ }
384
+ $ endDateTo = $ query ->endTime ()?->getTo();
385
+ if ($ endDateTo ) {
386
+ $ qb ->andWhere ($ qb ->expr ()->lte ('end_time ' , ':endDateTo ' ));
387
+ $ queryParameters ['endDateTo ' ] = $ endDateTo ;
388
+ $ queryTypes ['endDateTo ' ] = Types::DATETIME_IMMUTABLE ;
389
+ }
390
+
391
+ return [$ queryParameters , $ queryTypes ];
392
+ }
360
393
}
0 commit comments