Skip to content

Commit 8de7297

Browse files
committed
refactor: complete QueryInterface
1 parent 7f07b92 commit 8de7297

File tree

5 files changed

+27
-79
lines changed

5 files changed

+27
-79
lines changed

system/Database/Query.php

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,7 @@ public function __construct(ConnectionInterface $db)
100100
$this->db = $db;
101101
}
102102

103-
/**
104-
* Sets the raw query string to use for this statement.
105-
*
106-
* @param mixed $binds
107-
*
108-
* @return $this
109-
*/
110-
public function setQuery(string $sql, $binds = null, bool $setEscape = true)
103+
public function setQuery(string $sql, mixed $binds = null, bool $setEscape = true): self
111104
{
112105
$this->originalQueryString = $sql;
113106
unset($this->swappedQueryString);
@@ -153,10 +146,6 @@ public function setBinds(array $binds, bool $setEscape = true)
153146
return $this;
154147
}
155148

156-
/**
157-
* Returns the final, processed query string after binding, etal
158-
* has been performed.
159-
*/
160149
public function getQuery(): string
161150
{
162151
if (empty($this->finalQueryString)) {
@@ -166,14 +155,7 @@ public function getQuery(): string
166155
return $this->finalQueryString;
167156
}
168157

169-
/**
170-
* Records the execution time of the statement using microtime(true)
171-
* for it's start and end values. If no end value is present, will
172-
* use the current time to determine total duration.
173-
*
174-
* @return $this
175-
*/
176-
public function setDuration(float $start, ?float $end = null)
158+
public function setDuration(float $start, ?float $end = null): self
177159
{
178160
$this->startTime = $start;
179161

@@ -200,68 +182,40 @@ public function getStartTime(bool $returnRaw = false, int $decimals = 6)
200182
return number_format($this->startTime, $decimals);
201183
}
202184

203-
/**
204-
* Returns the duration of this query during execution, or null if
205-
* the query has not been executed yet.
206-
*
207-
* @param int $decimals The accuracy of the returned time.
208-
*/
209185
public function getDuration(int $decimals = 6): string
210186
{
211187
return number_format(($this->endTime - $this->startTime), $decimals);
212188
}
213189

214-
/**
215-
* Stores the error description that happened for this query.
216-
*
217-
* @return $this
218-
*/
219-
public function setError(int $code, string $error)
190+
public function setError(int $code, string $error): self
220191
{
221192
$this->errorCode = $code;
222193
$this->errorString = $error;
223194

224195
return $this;
225196
}
226197

227-
/**
228-
* Reports whether this statement created an error not.
229-
*/
230198
public function hasError(): bool
231199
{
232200
return ! empty($this->errorString);
233201
}
234202

235-
/**
236-
* Returns the error code created while executing this statement.
237-
*/
238203
public function getErrorCode(): int
239204
{
240205
return $this->errorCode;
241206
}
242207

243-
/**
244-
* Returns the error message created while executing this statement.
245-
*/
246208
public function getErrorMessage(): string
247209
{
248210
return $this->errorString;
249211
}
250212

251-
/**
252-
* Determines if the statement is a write-type query or not.
253-
*/
254213
public function isWriteType(): bool
255214
{
256215
return $this->db->isWriteType($this->originalQueryString);
257216
}
258217

259-
/**
260-
* Swaps out one table prefix for a new one.
261-
*
262-
* @return $this
263-
*/
264-
public function swapPrefix(string $orig, string $swap)
218+
public function swapPrefix(string $orig, string $swap): self
265219
{
266220
$sql = $this->swappedQueryString ?? $this->originalQueryString;
267221

@@ -275,9 +229,6 @@ public function swapPrefix(string $orig, string $swap)
275229
return $this;
276230
}
277231

278-
/**
279-
* Returns the original SQL that was passed into the system.
280-
*/
281232
public function getOriginalQuery(): string
282233
{
283234
return $this->originalQueryString;
@@ -315,9 +266,6 @@ protected function compileBinds()
315266
}
316267
}
317268

318-
/**
319-
* Match bindings
320-
*/
321269
protected function matchNamedBinds(string $sql, array $binds): string
322270
{
323271
$replacers = [];
@@ -339,9 +287,6 @@ protected function matchNamedBinds(string $sql, array $binds): string
339287
return strtr($sql, $replacers);
340288
}
341289

342-
/**
343-
* Match bindings
344-
*/
345290
protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, int $ml): string
346291
{
347292
if ($c = preg_match_all("/'[^']*'/", $sql, $matches) >= 1) {

system/Database/QueryInterface.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
namespace CodeIgniter\Database;
1515

1616
/**
17-
* Interface QueryInterface
18-
*
1917
* Represents a single statement that can be executed against the database.
2018
* Statements are platform-specific and can handle binding of binds.
2119
*/
@@ -24,19 +22,15 @@ interface QueryInterface
2422
/**
2523
* Sets the raw query string to use for this statement.
2624
*
27-
* @param mixed $binds
28-
*
2925
* @return $this
3026
*/
31-
public function setQuery(string $sql, $binds = null, bool $setEscape = true);
27+
public function setQuery(string $sql, mixed $binds = null, bool $setEscape = true): self;
3228

3329
/**
3430
* Returns the final, processed query string after binding, etal
3531
* has been performed.
36-
*
37-
* @return string
3832
*/
39-
public function getQuery();
33+
public function getQuery(): string;
4034

4135
/**
4236
* Records the execution time of the statement using microtime(true)
@@ -45,7 +39,7 @@ public function getQuery();
4539
*
4640
* @return $this
4741
*/
48-
public function setDuration(float $start, ?float $end = null);
42+
public function setDuration(float $start, ?float $end = null): self;
4943

5044
/**
5145
* Returns the duration of this query during execution, or null if
@@ -60,7 +54,7 @@ public function getDuration(int $decimals = 6): string;
6054
*
6155
* @return $this
6256
*/
63-
public function setError(int $code, string $error);
57+
public function setError(int $code, string $error): self;
6458

6559
/**
6660
* Reports whether this statement created an error not.
@@ -87,5 +81,10 @@ public function isWriteType(): bool;
8781
*
8882
* @return $this
8983
*/
90-
public function swapPrefix(string $orig, string $swap);
84+
public function swapPrefix(string $orig, string $swap): self;
85+
86+
/**
87+
* Returns the original SQL that was passed into the system.
88+
*/
89+
public function getOriginalQuery(): string;
9190
}

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,11 @@ To use a different encryption key permanently, pass a custom config when creatin
177177
Interface Changes
178178
=================
179179

180-
- **Cache:** The ``CacheInterface`` now includes the ``deleteMatching()`` method. If you've implemented your own caching driver from scratch, you will need to provide an implementation for this method to ensure compatibility.
181-
- **Images:** The ``ImageHandlerInterface`` now includes a new method: ``clearMetadata()``. If you've implemented your own handler from scratch, you will need to provide an implementation for this method to ensure compatibility.
180+
**NOTE:** If you've implemented your own classes that implement these interfaces from scratch, you will need to update your implementations to include the new methods to ensure compatibility.
181+
182+
- **Database:** The ``QueryInterface`` now includes the ``getOriginalQueryString()`` method.
183+
- **Cache:** The ``CacheInterface`` now includes the ``deleteMatching()`` method.
184+
- **Images:** The ``ImageHandlerInterface`` now includes a new method: ``clearMetadata()``.
182185

183186
Method Signature Changes
184187
========================
@@ -195,6 +198,12 @@ Method Signature Changes
195198
- ``CodeIgniter\HTTP\CURLRequest::setAuth()``
196199
- ``CodeIgniter\HTTP\URI::setUserInfo()``
197200
- ``CodeIgniter\Security\Security::derandomize()``
201+
- Added native parameter and return types to ``Database\QueryInterface`` methods that were missing them. The following methods were updated:
202+
- ``setQuery(string $sql, mixed $binds = null, bool $setEscape = true): self``
203+
- ``getQuery(): string``
204+
- ``setDuration(float $start, ?float $end = null): self``
205+
- ``setError(int $code, string $error): self``
206+
- ``swapPrefix(string $orig, string $swap): self``
198207
- Added native types to ``CacheInterface`` methods that were missing them. The following methods were updated:
199208
- ``initialize()``
200209
- ``save()``

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2168 errors
1+
# total 2167 errors
22

33
includes:
44
- argument.type.neon

utils/phpstan-baseline/method.notFound.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
# total 81 errors
1+
# total 80 errors
22

33
parameters:
44
ignoreErrors:
5-
-
6-
message: '#^Call to an undefined method CodeIgniter\\Database\\QueryInterface\:\:getOriginalQuery\(\)\.$#'
7-
count: 1
8-
path: ../../system/Database/BaseConnection.php
9-
105
-
116
message: '#^Call to an undefined method CodeIgniter\\View\\RendererInterface\:\:getData\(\)\.$#'
127
count: 1

0 commit comments

Comments
 (0)