Skip to content

Commit

Permalink
feat: DB Result can use DataConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 18, 2023
1 parent 885396e commit 4cad258
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 30 deletions.
51 changes: 28 additions & 23 deletions system/Database/BaseResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Database;

use CodeIgniter\Database\DataConverter\DataConverter;
use CodeIgniter\Entity\Entity;
use stdClass;

Expand Down Expand Up @@ -101,17 +102,17 @@ public function __construct(&$connID, &$resultID)
*
* @param string $type The row type. Either 'array', 'object', or a class name to use
*/
public function getResult(string $type = 'object'): array
public function getResult(string $type = 'object', ?DataConverter $converter = null): array
{
if ($type === 'array') {
return $this->getResultArray();
return $this->getResultArray($converter);
}

if ($type === 'object') {
return $this->getResultObject();
return $this->getResultObject($converter);
}

return $this->getCustomResultObject($type);
return $this->getCustomResultObject($type, $converter);
}

/**
Expand All @@ -121,7 +122,7 @@ public function getResult(string $type = 'object'): array
*
* @return array
*/
public function getCustomResultObject(string $className)
public function getCustomResultObject(string $className, ?DataConverter $converter = null)
{
if (isset($this->customResultObject[$className])) {
return $this->customResultObject[$className];
Expand Down Expand Up @@ -156,7 +157,7 @@ public function getCustomResultObject(string $className)
}
$this->customResultObject[$className] = [];

while ($row = $this->fetchObject($className)) {
while ($row = $this->fetchObject($className, $converter)) {
if (! is_subclass_of($row, Entity::class) && method_exists($row, 'syncOriginal')) {
$row->syncOriginal();
}
Expand All @@ -172,7 +173,7 @@ public function getCustomResultObject(string $className)
*
* If no results, an empty array is returned.
*/
public function getResultArray(): array
public function getResultArray(?DataConverter $converter = null): array
{
if (! empty($this->resultArray)) {
return $this->resultArray;
Expand All @@ -198,6 +199,10 @@ public function getResultArray(): array
}

while ($row = $this->fetchAssoc()) {
if ($converter !== null) {
$row = $converter->fromDatabase($row);
}

$this->resultArray[] = $row;
}

Expand All @@ -209,10 +214,9 @@ public function getResultArray(): array
*
* If no results, an empty array is returned.
*
* @return array<int, stdClass>
* @phpstan-return list<stdClass>
* @return list<stdClass>
*/
public function getResultObject(): array
public function getResultObject(?DataConverter $converter = null): array
{
if (! empty($this->resultObject)) {
return $this->resultObject;
Expand All @@ -237,7 +241,7 @@ public function getResultObject(): array
$this->dataSeek();
}

while ($row = $this->fetchObject()) {
while ($row = $this->fetchObject('stdClass', $converter)) {
if (! is_subclass_of($row, Entity::class) && method_exists($row, 'syncOriginal')) {
$row->syncOriginal();
}
Expand All @@ -260,12 +264,12 @@ public function getResultObject(): array
* @return array|object|stdClass|null
* @phpstan-return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : object|null))
*/
public function getRow($n = 0, string $type = 'object')
public function getRow($n = 0, string $type = 'object', ?DataConverter $converter = null)
{
if (! is_numeric($n)) {
// We cache the row data for subsequent uses
if (! is_array($this->rowData)) {
$this->rowData = $this->getRowArray();
$this->rowData = $this->getRowArray(0, $converter);
}

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

if ($type === 'object') {
return $this->getRowObject($n);
return $this->getRowObject($n, $converter);
}

if ($type === 'array') {
return $this->getRowArray($n);
return $this->getRowArray($n, $converter);
}

return $this->getCustomRowObject($n, $type);
return $this->getCustomRowObject($n, $type, $converter);
}

/**
Expand All @@ -294,10 +298,10 @@ public function getRow($n = 0, string $type = 'object')
*
* @return array|null
*/
public function getCustomRowObject(int $n, string $className)
public function getCustomRowObject(int $n, string $className, ?DataConverter $converter = null)
{
if (! isset($this->customResultObject[$className])) {
$this->getCustomResultObject($className);
$this->getCustomResultObject($className, $converter);
}

if (empty($this->customResultObject[$className])) {
Expand All @@ -318,9 +322,9 @@ public function getCustomRowObject(int $n, string $className)
*
* @return array|null
*/
public function getRowArray(int $n = 0)
public function getRowArray(int $n = 0, ?DataConverter $converter = null)
{
$result = $this->getResultArray();
$result = $this->getResultArray($converter);
if (empty($result)) {
return null;
}
Expand All @@ -339,9 +343,10 @@ public function getRowArray(int $n = 0)
*
* @return object|stdClass|null
*/
public function getRowObject(int $n = 0)
public function getRowObject(int $n = 0, ?DataConverter $converter = null)
{
$result = $this->getResultObject();
$result = $this->getResultObject($converter);

if (empty($result)) {
return null;
}
Expand Down Expand Up @@ -529,5 +534,5 @@ abstract protected function fetchAssoc();
*
* @return Entity|false|object|stdClass
*/
abstract protected function fetchObject(string $className = 'stdClass');
abstract protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null);
}
2 changes: 2 additions & 0 deletions system/Database/DataConverter/DataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

/**
* PHP data <==> DB data converter
*
* @see \CodeIgniter\Database\DataConverter\DataConverterTest
*/
class DataConverter
{
Expand Down
3 changes: 2 additions & 1 deletion system/Database/MySQLi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Database\MySQLi;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\DataConverter\DataConverter;
use CodeIgniter\Entity\Entity;
use mysqli;
use mysqli_result;
Expand Down Expand Up @@ -145,7 +146,7 @@ protected function fetchAssoc()
*
* @return Entity|false|object|stdClass
*/
protected function fetchObject(string $className = 'stdClass')
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
{
if (is_subclass_of($className, Entity::class)) {
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);
Expand Down
3 changes: 2 additions & 1 deletion system/Database/OCI8/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Database\OCI8;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\DataConverter\DataConverter;
use CodeIgniter\Entity\Entity;
use stdClass;

Expand Down Expand Up @@ -95,7 +96,7 @@ protected function fetchAssoc()
*
* @return Entity|false|object|stdClass
*/
protected function fetchObject(string $className = 'stdClass')
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
{
$row = oci_fetch_object($this->resultID);

Expand Down
3 changes: 2 additions & 1 deletion system/Database/Postgre/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Database\Postgre;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\DataConverter\DataConverter;
use CodeIgniter\Entity\Entity;
use PgSql\Connection as PgSqlConnection;
use PgSql\Result as PgSqlResult;
Expand Down Expand Up @@ -111,7 +112,7 @@ protected function fetchAssoc()
*
* @return Entity|false|object|stdClass
*/
protected function fetchObject(string $className = 'stdClass')
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
{
if (is_subclass_of($className, Entity::class)) {
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);
Expand Down
3 changes: 2 additions & 1 deletion system/Database/SQLSRV/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Database\SQLSRV;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\DataConverter\DataConverter;
use CodeIgniter\Entity\Entity;
use stdClass;

Expand Down Expand Up @@ -151,7 +152,7 @@ protected function fetchAssoc()
*
* @return Entity|false|object|stdClass
*/
protected function fetchObject(string $className = 'stdClass')
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
{
if (is_subclass_of($className, Entity::class)) {
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);
Expand Down
7 changes: 6 additions & 1 deletion system/Database/SQLite3/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Closure;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\DataConverter\DataConverter;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Entity\Entity;
use SQLite3;
Expand Down Expand Up @@ -128,13 +129,17 @@ protected function fetchAssoc()
*
* @return Entity|false|object|stdClass
*/
protected function fetchObject(string $className = 'stdClass')
protected function fetchObject(string $className = 'stdClass', ?DataConverter $converter = null)
{
// No native support for fetching rows as objects
if (($row = $this->fetchAssoc()) === false) {
return false;
}

if ($converter !== null) {
$row = $converter->fromDatabase($row);
}

if ($className === 'stdClass') {
return (object) $row;
}
Expand Down
3 changes: 2 additions & 1 deletion system/Test/Mock/MockResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Test\Mock;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\DataConverter\DataConverter;
use stdClass;

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ protected function fetchAssoc()
*
* @return object|stdClass
*/
protected function fetchObject($className = 'stdClass')
protected function fetchObject($className = 'stdClass', ?DataConverter $converter = null)
{
return new $className();
}
Expand Down
Loading

0 comments on commit 4cad258

Please sign in to comment.