Skip to content

Commit 5683082

Browse files
committed
fix limit for update and delete queries, added affected rows to query result
1 parent 8fba927 commit 5683082

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/Driver/Mysqli/Mysqli.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
Driver\Features\CRUDAbleInterface,
77
Driver\Features\CRUDQueryableInterface,
88
ModelInterface,
9+
Query\DeleteQuery,
910
Query\Generator\SQL,
1011
Query\Query,
11-
Query\QueryResult};
12+
Query\QueryResult,
13+
Query\UpdateQuery
14+
};
1215
use Exception;
1316
use mysqli_result;
1417

@@ -240,6 +243,11 @@ public function query(Query $query): QueryResult
240243

241244
$result = new QueryResult((bool)$rawQueryResult);
242245
$result->setQueryString($queryString);
246+
if ($query instanceof UpdateQuery || $query instanceof DeleteQuery) {
247+
$result->setAffectedRows(mysqli_affected_rows($this->connection));
248+
return $result;
249+
}
250+
243251
if (is_bool($rawQueryResult)) {
244252
return $result;
245253
}

src/Query/Generator/SQL.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ public function generate(Query $query): string
9595
}
9696

9797
if ($limit = $query->getLimit()) {
98-
$queryString .= " LIMIT " . $limit->start . ", " . $limit->length;
98+
if ($query instanceof UpdateQuery || $query instanceof DeleteQuery) {
99+
$queryString .= " LIMIT " . $limit->length;
100+
} else {
101+
$queryString .= " LIMIT " . $limit->start . ", " . $limit->length;
102+
}
99103
}
100104

101105
return $queryString;

src/Query/QueryResult.php

+27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class QueryResult extends ModelCollectionResult
2323
*/
2424
protected ?string $queryString = null;
2525

26+
/**
27+
* Number of affected rows in update or delete queries
28+
*
29+
* @var int|null
30+
*/
31+
protected ?int $affectedRows = null;
32+
2633
/**
2734
* QueryResult constructor.
2835
*
@@ -57,4 +64,24 @@ public function setQueryString(?string $queryString): QueryResult
5764
}
5865
return $this;
5966
}
67+
68+
/**
69+
* @return int|null
70+
*/
71+
public function getAffectedRows(): ?int
72+
{
73+
return $this->affectedRows;
74+
}
75+
76+
/**
77+
* @param int|null $affectedRows
78+
* @return $this
79+
*/
80+
public function setAffectedRows(?int $affectedRows): static
81+
{
82+
if ($this->affectedRows === null) {
83+
$this->affectedRows = $affectedRows;
84+
}
85+
return $this;
86+
}
6087
}

0 commit comments

Comments
 (0)