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,33 +148,11 @@ 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
- }
155
+ [$ queryParameters , $ queryTypes ] = $ this ->addWheres ($ query , $ qb );
177
156
178
157
switch ($ query ->sort ()) {
179
158
case Query::SORT_BY_START_ASC :
@@ -196,6 +175,18 @@ public function query(Query $query): iterable
196
175
yield from $ this ->queryList ($ qb ->getSQL (), $ queryParameters , $ queryTypes );
197
176
}
198
177
178
+ public function count (Query $ query ): int
179
+ {
180
+ $ qb = $ this ->connection ->createQueryBuilder ();
181
+ $ qb ->select ('count(*) ' )
182
+ ->from ($ this ->table );
183
+
184
+ /** @var int $result */
185
+ $ result = $ this ->connection ->executeQuery ($ qb ->getSQL (), ...$ this ->addWheres ($ query , $ qb ))->fetchOne ();
186
+
187
+ return $ result ;
188
+ }
189
+
199
190
private function getSchema (): Schema
200
191
{
201
192
$ schema = new Schema ();
@@ -325,4 +316,36 @@ private function getNormalizer(): JobExecutionRowNormalizer
325
316
326
317
return $ this ->normalizer ;
327
318
}
319
+
320
+ /**
321
+ * @return array<int, array<string, array<int|string>|int>>
322
+ */
323
+ private function addWheres (Query $ query , QueryBuilder $ qb ): array
324
+ {
325
+ $ queryParameters = [];
326
+ $ queryTypes = [];
327
+
328
+ $ names = $ query ->jobs ();
329
+ if (\count ($ names ) > 0 ) {
330
+ $ qb ->andWhere ($ qb ->expr ()->in ('job_name ' , ':jobNames ' ));
331
+ $ queryParameters ['jobNames ' ] = $ names ;
332
+ $ queryTypes ['jobNames ' ] = Connection::PARAM_STR_ARRAY ;
333
+ }
334
+
335
+ $ ids = $ query ->ids ();
336
+ if (\count ($ ids ) > 0 ) {
337
+ $ qb ->andWhere ($ qb ->expr ()->in ('id ' , ':ids ' ));
338
+ $ queryParameters ['ids ' ] = $ ids ;
339
+ $ queryTypes ['ids ' ] = Connection::PARAM_STR_ARRAY ;
340
+ }
341
+
342
+ $ statuses = $ query ->statuses ();
343
+ if (\count ($ statuses ) > 0 ) {
344
+ $ qb ->andWhere ($ qb ->expr ()->in ('status ' , ':statuses ' ));
345
+ $ queryParameters ['statuses ' ] = $ statuses ;
346
+ $ queryTypes ['statuses ' ] = Connection::PARAM_INT_ARRAY ;
347
+ }
348
+
349
+ return [$ queryParameters , $ queryTypes ];
350
+ }
328
351
}
0 commit comments