Skip to content

Commit eece5b4

Browse files
authored
Merge pull request #31 from tyrsson/patch-driver-and-driver-feature
Aligns code with phpdb 0.6.0
2 parents ba622f1 + 8ccf969 commit eece5b4

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

phpunit.xml.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
failOnDeprecation="true"
1111
failOnWarning="true">
1212

13-
<coverage pathCoverage="true">
14-
<report>
15-
<html outputDirectory="test/coverage/html"/>
16-
</report>
17-
</coverage>
1813
<testsuites>
1914
<testsuite name="unit test">
2015
<directory>./test/unit</directory>

src/Pdo/Driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function createResult($resource, Statement|string|null $context = null):
2929
$result = clone $this->resultPrototype;
3030
/** @var Feature\SqliteRowCounter $sqliteRowCounter */
3131
$sqliteRowCounter = $this->getFeature(Feature\SqliteRowCounter::class);
32-
$rowCount = null;
32+
$rowCount = 0;
3333

3434
if ($sqliteRowCounter && $resource->columnCount() > 0) {
3535
$rowCount = $sqliteRowCounter->getRowCountClosure($context);

src/Pdo/Feature/SqliteRowCounter.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,47 @@
99
use PhpDb\Adapter\Driver\Pdo;
1010
use PhpDb\Adapter\Driver\Pdo\Statement;
1111

12-
use function stripos;
12+
use function str_contains;
13+
use function strtolower;
1314

1415
/**
1516
* SqliteRowCounter
1617
*/
1718
class SqliteRowCounter extends AbstractFeature
1819
{
19-
public function getCountForStatement(Pdo\Statement $statement): ?int
20+
public function getCountForStatement(Pdo\Statement $statement): int
2021
{
2122
$countStmt = clone $statement;
2223
$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;
2526
}
2627
$countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')';
2728
$countStmt->prepare($countSql);
2829
$result = $countStmt->execute();
2930
$countRow = $result->getResource()->fetch(\PDO::FETCH_ASSOC);
3031
unset($statement, $result);
3132

32-
return $countRow['count'];
33+
return (int) $countRow['count'];
3334
}
3435

35-
public function getCountForSql(string $sql): ?int
36+
public function getCountForSql(string $sql): int
3637
{
37-
if (stripos($sql, 'select') === false) {
38-
return null;
38+
if (empty($sql) || ! str_contains(strtolower($sql), 'select')) {
39+
return 0;
3940
}
4041
$countSql = 'SELECT COUNT(*) as count FROM (' . $sql . ')';
4142
/** @var \PDO $pdo */
4243
$pdo = $this->driver->getConnection()->getResource();
4344
$result = $pdo->query($countSql);
4445
$countRow = $result->fetch(\PDO::FETCH_ASSOC);
4546

46-
return $countRow['count'];
47+
return (int) $countRow['count'];
4748
}
4849

4950
public function getRowCountClosure(Statement|string|null $context): Closure
5051
{
51-
return function () use ($context) {
52+
return function () use ($context): int {
5253
return $context instanceof Pdo\Statement
5354
? $this->getCountForStatement($context)
5455
: $this->getCountForSql($context);

0 commit comments

Comments
 (0)