Added database result iterator.

This commit is contained in:
flash 2024-02-06 21:39:36 +00:00
parent 18397477d6
commit c8d16b02e6
6 changed files with 102 additions and 24 deletions

View File

@ -1 +1 @@
0.2401.40206
0.2402.62138

42
composer.lock generated
View File

@ -68,25 +68,27 @@
},
{
"name": "nikic/php-parser",
"version": "v4.18.0",
"version": "v5.0.0",
"source": {
"type": "git",
"url": "https://github.com/nikic/PHP-Parser.git",
"reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999"
"reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/1bcbb2179f97633e98bbbc87044ee2611c7d7999",
"reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/4a21235f7e56e713259a6f76bf4b5ea08502b9dc",
"reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc",
"shasum": ""
},
"require": {
"ext-ctype": "*",
"ext-json": "*",
"ext-tokenizer": "*",
"php": ">=7.0"
"php": ">=7.4"
},
"require-dev": {
"ircmaxell/php-yacc": "^0.0.7",
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0"
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0"
},
"bin": [
"bin/php-parse"
@ -94,7 +96,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.9-dev"
"dev-master": "5.0-dev"
}
},
"autoload": {
@ -118,9 +120,9 @@
],
"support": {
"issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/v4.18.0"
"source": "https://github.com/nikic/PHP-Parser/tree/v5.0.0"
},
"time": "2023-12-10T21:03:43+00:00"
"time": "2024-01-07T17:17:35+00:00"
},
{
"name": "phar-io/manifest",
@ -235,16 +237,16 @@
},
{
"name": "phpstan/phpstan",
"version": "1.10.50",
"version": "1.10.57",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "06a98513ac72c03e8366b5a0cb00750b487032e4"
"reference": "1627b1d03446904aaa77593f370c5201d2ecc34e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/06a98513ac72c03e8366b5a0cb00750b487032e4",
"reference": "06a98513ac72c03e8366b5a0cb00750b487032e4",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/1627b1d03446904aaa77593f370c5201d2ecc34e",
"reference": "1627b1d03446904aaa77593f370c5201d2ecc34e",
"shasum": ""
},
"require": {
@ -293,7 +295,7 @@
"type": "tidelift"
}
],
"time": "2023-12-13T10:59:42+00:00"
"time": "2024-01-24T11:51:34+00:00"
},
{
"name": "phpunit/php-code-coverage",
@ -618,16 +620,16 @@
},
{
"name": "phpunit/phpunit",
"version": "10.5.5",
"version": "10.5.10",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "ed21115d505b4b4f7dc7b5651464e19a2c7f7856"
"reference": "50b8e314b6d0dd06521dc31d1abffa73f25f850c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ed21115d505b4b4f7dc7b5651464e19a2c7f7856",
"reference": "ed21115d505b4b4f7dc7b5651464e19a2c7f7856",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/50b8e314b6d0dd06521dc31d1abffa73f25f850c",
"reference": "50b8e314b6d0dd06521dc31d1abffa73f25f850c",
"shasum": ""
},
"require": {
@ -699,7 +701,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.5"
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.10"
},
"funding": [
{
@ -715,7 +717,7 @@
"type": "tidelift"
}
],
"time": "2023-12-27T15:13:52+00:00"
"time": "2024-02-04T09:07:51+00:00"
},
{
"name": "sebastian/cli-parser",

View File

@ -0,0 +1,56 @@
<?php
// DbResultIterator.php
// Created: 2024-02-06
// Updated: 2024-02-06
namespace Index\Data;
use InvalidArgumentException;
use Iterator;
/**
* Implements an iterator and constructor wrapper for IDbResult.
*/
class DbResultIterator implements Iterator {
private bool $wasValid;
private object $current;
/**
* Call this through an IDbResult instance instead!
*
* @param IDbResult $result Result to operate on.
* @param callable(IDbResult): object $construct Constructor callback.
*/
public function __construct(
private IDbResult $result,
private $construct
) {
if(!is_callable($construct))
throw new InvalidArgumentException('$construct must be a callable.');
}
public function current(): mixed {
return $this->current;
}
public function key(): mixed {
return spl_object_id($this->current);
}
private function moveNext(): void {
if($this->wasValid = $this->result->next())
$this->current = ($this->construct)($this->result);
}
public function next(): void {
$this->moveNext();
}
public function rewind(): void {
$this->moveNext();
}
public function valid(): bool {
return $this->wasValid;
}
}

View File

@ -1,10 +1,13 @@
<?php
// DbResultTrait.php
// Created: 2023-11-09
// Updated: 2023-11-09
// Updated: 2024-02-06
namespace Index\Data;
/**
* Implements common IDbResult methods.
*/
trait DbResultTrait {
public function getString(int|string $index): string {
return (string)$this->getValue($index);
@ -37,4 +40,8 @@ trait DbResultTrait {
public function getBooleanOrNull(int|string $index): ?bool {
return $this->isNull($index) ? null : ($this->getInteger($index) !== 0);
}
public function getIterator(callable $construct): DbResultIterator {
return new DbResultIterator($this, $construct);
}
}

View File

@ -1,7 +1,7 @@
<?php
// IDbResult.php
// Created: 2021-05-02
// Updated: 2023-11-09
// Updated: 2024-02-06
namespace Index\Data;
@ -106,4 +106,12 @@ interface IDbResult extends ICloseable {
* @return ?Stream A Stream if data is available, null if not.
*/
function getStream(int|string $index): ?Stream;
/**
* Creates an iterator for this result.
*
* @param callable(IDbResult): object $construct Result info constructor.
* @return DbResultIterator Result iterator.
*/
function getIterator(callable $construct): DbResultIterator;
}

View File

@ -1,11 +1,12 @@
<?php
// NullDbResult.php
// Created: 2021-05-02
// Updated: 2023-11-09
// Updated: 2024-02-06
namespace Index\Data\NullDb;
use Index\Data\IDbResult;
use Index\Data\DbResultIterator;
use Index\IO\Stream;
/**
@ -60,5 +61,9 @@ class NullDbResult implements IDbResult {
return null;
}
public function getIterator(callable $construct): DbResultIterator {
return new DbResultIterator($this, $construct);
}
public function close(): void {}
}