|
9 | 9 | use PhpDb\Adapter\Driver\Pdo; |
10 | 10 | use PhpDb\Adapter\Driver\Pdo\Statement; |
11 | 11 |
|
12 | | -use function stripos; |
| 12 | +use function str_contains; |
| 13 | +use function strtolower; |
13 | 14 |
|
14 | 15 | /** |
15 | 16 | * SqliteRowCounter |
16 | 17 | */ |
17 | 18 | class SqliteRowCounter extends AbstractFeature |
18 | 19 | { |
19 | | - public function getCountForStatement(Pdo\Statement $statement): ?int |
| 20 | + public function getCountForStatement(Pdo\Statement $statement): int |
20 | 21 | { |
21 | 22 | $countStmt = clone $statement; |
22 | 23 | $sql = $statement->getSql(); |
23 | | - if ($sql === '' || $sql === null || stripos($sql, 'select') === false) { |
24 | | - return null; |
| 24 | + if (empty($sql) || ! str_contains(strtolower($sql), 'select')) { |
| 25 | + return 0; |
25 | 26 | } |
26 | 27 | $countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')'; |
27 | 28 | $countStmt->prepare($countSql); |
28 | 29 | $result = $countStmt->execute(); |
29 | 30 | $countRow = $result->getResource()->fetch(\PDO::FETCH_ASSOC); |
30 | 31 | unset($statement, $result); |
31 | 32 |
|
32 | | - return $countRow['count']; |
| 33 | + return (int) $countRow['count']; |
33 | 34 | } |
34 | 35 |
|
35 | | - public function getCountForSql(string $sql): ?int |
| 36 | + public function getCountForSql(string $sql): int |
36 | 37 | { |
37 | | - if (stripos($sql, 'select') === false) { |
38 | | - return null; |
| 38 | + if (empty($sql) || ! str_contains(strtolower($sql), 'select')) { |
| 39 | + return 0; |
39 | 40 | } |
40 | 41 | $countSql = 'SELECT COUNT(*) as count FROM (' . $sql . ')'; |
41 | 42 | /** @var \PDO $pdo */ |
42 | 43 | $pdo = $this->driver->getConnection()->getResource(); |
43 | 44 | $result = $pdo->query($countSql); |
44 | 45 | $countRow = $result->fetch(\PDO::FETCH_ASSOC); |
45 | 46 |
|
46 | | - return $countRow['count']; |
| 47 | + return (int) $countRow['count']; |
47 | 48 | } |
48 | 49 |
|
49 | 50 | public function getRowCountClosure(Statement|string|null $context): Closure |
50 | 51 | { |
51 | | - return function () use ($context) { |
| 52 | + return function () use ($context): int { |
52 | 53 | return $context instanceof Pdo\Statement |
53 | 54 | ? $this->getCountForStatement($context) |
54 | 55 | : $this->getCountForSql($context); |
|
0 commit comments