Skip to content

Commit 4cad258

Browse files
committed
feat: DB Result can use DataConverter
1 parent 885396e commit 4cad258

File tree

10 files changed

+162
-30
lines changed

10 files changed

+162
-30
lines changed

system/Database/BaseResult.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Database;
1313

14+
use CodeIgniter\Database\DataConverter\DataConverter;
1415
use CodeIgniter\Entity\Entity;
1516
use stdClass;
1617

@@ -101,17 +102,17 @@ public function __construct(&$connID, &$resultID)
101102
*
102103
* @param string $type The row type. Either 'array', 'object', or a class name to use
103104
*/
104-
public function getResult(string $type = 'object'): array
105+
public function getResult(string $type = 'object', ?DataConverter $converter = null): array
105106
{
106107
if ($type === 'array') {
107-
return $this->getResultArray();
108+
return $this->getResultArray($converter);
108109
}
109110

110111
if ($type === 'object') {
111-
return $this->getResultObject();
112+
return $this->getResultObject($converter);
112113
}
113114

114-
return $this->getCustomResultObject($type);
115+
return $this->getCustomResultObject($type, $converter);
115116
}
116117

117118
/**
@@ -121,7 +122,7 @@ public function getResult(string $type = 'object'): array
121122
*
122123
* @return array
123124
*/
124-
public function getCustomResultObject(string $className)
125+
public function getCustomResultObject(string $className, ?DataConverter $converter = null)
125126
{
126127
if (isset($this->customResultObject[$className])) {
127128
return $this->customResultObject[$className];
@@ -156,7 +157,7 @@ public function getCustomResultObject(string $className)
156157
}
157158
$this->customResultObject[$className] = [];
158159

159-
while ($row = $this->fetchObject($className)) {
160+
while ($row = $this->fetchObject($className, $converter)) {
160161
if (! is_subclass_of($row, Entity::class) && method_exists($row, 'syncOriginal')) {
161162
$row->syncOriginal();
162163
}
@@ -172,7 +173,7 @@ public function getCustomResultObject(string $className)
172173
*
173174
* If no results, an empty array is returned.
174175
*/
175-
public function getResultArray(): array
176+
public function getResultArray(?DataConverter $converter = null): array
176177
{
177178
if (! empty($this->resultArray)) {
178179
return $this->resultArray;
@@ -198,6 +199,10 @@ public function getResultArray(): array
198199
}
199200

200201
while ($row = $this->fetchAssoc()) {
202+
if ($converter !== null) {
203+
$row = $converter->fromDatabase($row);
204+
}
205+
201206
$this->resultArray[] = $row;
202207
}
203208

@@ -209,10 +214,9 @@ public function getResultArray(): array
209214
*
210215
* If no results, an empty array is returned.
211216
*
212-
* @return array<int, stdClass>
213-
* @phpstan-return list<stdClass>
217+
* @return list<stdClass>
214218
*/
215-
public function getResultObject(): array
219+
public function getResultObject(?DataConverter $converter = null): array
216220
{
217221
if (! empty($this->resultObject)) {
218222
return $this->resultObject;
@@ -237,7 +241,7 @@ public function getResultObject(): array
237241
$this->dataSeek();
238242
}
239243

240-
while ($row = $this->fetchObject()) {
244+
while ($row = $this->fetchObject('stdClass', $converter)) {
241245
if (! is_subclass_of($row, Entity::class) && method_exists($row, 'syncOriginal')) {
242246
$row->syncOriginal();
243247
}
@@ -260,12 +264,12 @@ public function getResultObject(): array
260264
* @return array|object|stdClass|null
261265
* @phpstan-return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : object|null))
262266
*/
263-
public function getRow($n = 0, string $type = 'object')
267+
public function getRow($n = 0, string $type = 'object', ?DataConverter $converter = null)
264268
{
265269
if (! is_numeric($n)) {
266270
// We cache the row data for subsequent uses
267271
if (! is_array($this->rowData)) {
268-
$this->rowData = $this->getRowArray();
272+
$this->rowData = $this->getRowArray(0, $converter);
269273
}
270274

271275
// array_key_exists() instead of isset() to allow for NULL values
@@ -277,14 +281,14 @@ public function getRow($n = 0, string $type = 'object')
277281
}
278282

279283
if ($type === 'object') {
280-
return $this->getRowObject($n);
284+
return $this->getRowObject($n, $converter);
281285
}
282286

283287
if ($type === 'array') {
284-
return $this->getRowArray($n);
288+
return $this->getRowArray($n, $converter);
285289
}
286290

287-
return $this->getCustomRowObject($n, $type);
291+
return $this->getCustomRowObject($n, $type, $converter);
288292
}
289293

290294
/**
@@ -294,10 +298,10 @@ public function getRow($n = 0, string $type = 'object')
294298
*
295299
* @return array|null
296300
*/
297-
public function getCustomRowObject(int $n, string $className)
301+
public function getCustomRowObject(int $n, string $className, ?DataConverter $converter = null)
298302
{
299303
if (! isset($this->customResultObject[$className])) {
300-
$this->getCustomResultObject($className);
304+
$this->getCustomResultObject($className, $converter);
301305
}
302306

303307
if (empty($this->customResultObject[$className])) {
@@ -318,9 +322,9 @@ public function getCustomRowObject(int $n, string $className)
318322
*
319323
* @return array|null
320324
*/
321-
public function getRowArray(int $n = 0)
325+
public function getRowArray(int $n = 0, ?DataConverter $converter = null)
322326
{
323-
$result = $this->getResultArray();
327+
$result = $this->getResultArray($converter);
324328
if (empty($result)) {
325329
return null;
326330
}
@@ -339,9 +343,10 @@ public function getRowArray(int $n = 0)
339343
*
340344
* @return object|stdClass|null
341345
*/
342-
public function getRowObject(int $n = 0)
346+
public function getRowObject(int $n = 0, ?DataConverter $converter = null)
343347
{
344-
$result = $this->getResultObject();
348+
$result = $this->getResultObject($converter);
349+
345350
if (empty($result)) {
346351
return null;
347352
}
@@ -529,5 +534,5 @@ abstract protected function fetchAssoc();
529534
*
530535
* @return Entity|false|object|stdClass
531536
*/
532-
abstract protected function fetchObject(string $className = 'stdClass');
537+
abstract protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null);
533538
}

system/Database/DataConverter/DataConverter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
/**
2828
* PHP data <==> DB data converter
29+
*
30+
* @see \CodeIgniter\Database\DataConverter\DataConverterTest
2931
*/
3032
class DataConverter
3133
{

system/Database/MySQLi/Result.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Database\MySQLi;
1313

1414
use CodeIgniter\Database\BaseResult;
15+
use CodeIgniter\Database\DataConverter\DataConverter;
1516
use CodeIgniter\Entity\Entity;
1617
use mysqli;
1718
use mysqli_result;
@@ -145,7 +146,7 @@ protected function fetchAssoc()
145146
*
146147
* @return Entity|false|object|stdClass
147148
*/
148-
protected function fetchObject(string $className = 'stdClass')
149+
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
149150
{
150151
if (is_subclass_of($className, Entity::class)) {
151152
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);

system/Database/OCI8/Result.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Database\OCI8;
1313

1414
use CodeIgniter\Database\BaseResult;
15+
use CodeIgniter\Database\DataConverter\DataConverter;
1516
use CodeIgniter\Entity\Entity;
1617
use stdClass;
1718

@@ -95,7 +96,7 @@ protected function fetchAssoc()
9596
*
9697
* @return Entity|false|object|stdClass
9798
*/
98-
protected function fetchObject(string $className = 'stdClass')
99+
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
99100
{
100101
$row = oci_fetch_object($this->resultID);
101102

system/Database/Postgre/Result.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Database\Postgre;
1313

1414
use CodeIgniter\Database\BaseResult;
15+
use CodeIgniter\Database\DataConverter\DataConverter;
1516
use CodeIgniter\Entity\Entity;
1617
use PgSql\Connection as PgSqlConnection;
1718
use PgSql\Result as PgSqlResult;
@@ -111,7 +112,7 @@ protected function fetchAssoc()
111112
*
112113
* @return Entity|false|object|stdClass
113114
*/
114-
protected function fetchObject(string $className = 'stdClass')
115+
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
115116
{
116117
if (is_subclass_of($className, Entity::class)) {
117118
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);

system/Database/SQLSRV/Result.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Database\SQLSRV;
1313

1414
use CodeIgniter\Database\BaseResult;
15+
use CodeIgniter\Database\DataConverter\DataConverter;
1516
use CodeIgniter\Entity\Entity;
1617
use stdClass;
1718

@@ -151,7 +152,7 @@ protected function fetchAssoc()
151152
*
152153
* @return Entity|false|object|stdClass
153154
*/
154-
protected function fetchObject(string $className = 'stdClass')
155+
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
155156
{
156157
if (is_subclass_of($className, Entity::class)) {
157158
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);

system/Database/SQLite3/Result.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Closure;
1515
use CodeIgniter\Database\BaseResult;
16+
use CodeIgniter\Database\DataConverter\DataConverter;
1617
use CodeIgniter\Database\Exceptions\DatabaseException;
1718
use CodeIgniter\Entity\Entity;
1819
use SQLite3;
@@ -128,13 +129,17 @@ protected function fetchAssoc()
128129
*
129130
* @return Entity|false|object|stdClass
130131
*/
131-
protected function fetchObject(string $className = 'stdClass')
132+
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
132133
{
133134
// No native support for fetching rows as objects
134135
if (($row = $this->fetchAssoc()) === false) {
135136
return false;
136137
}
137138

139+
if ($converter !== null) {
140+
$row = $converter->fromDatabase($row);
141+
}
142+
138143
if ($className === 'stdClass') {
139144
return (object) $row;
140145
}

system/Test/Mock/MockResult.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Test\Mock;
1313

1414
use CodeIgniter\Database\BaseResult;
15+
use CodeIgniter\Database\DataConverter\DataConverter;
1516
use stdClass;
1617

1718
/**
@@ -86,7 +87,7 @@ protected function fetchAssoc()
8687
*
8788
* @return object|stdClass
8889
*/
89-
protected function fetchObject($className = 'stdClass')
90+
protected function fetchObject($className = 'stdClass', ?DataConverter $converter = null)
9091
{
9192
return new $className();
9293
}

0 commit comments

Comments
 (0)