Added database result iterator.
This commit is contained in:
parent
18397477d6
commit
c8d16b02e6
6 changed files with 102 additions and 24 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.2401.40206
|
0.2402.62138
|
||||||
|
|
42
composer.lock
generated
42
composer.lock
generated
|
@ -68,25 +68,27 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "nikic/php-parser",
|
"name": "nikic/php-parser",
|
||||||
"version": "v4.18.0",
|
"version": "v5.0.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/nikic/PHP-Parser.git",
|
"url": "https://github.com/nikic/PHP-Parser.git",
|
||||||
"reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999"
|
"reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/1bcbb2179f97633e98bbbc87044ee2611c7d7999",
|
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/4a21235f7e56e713259a6f76bf4b5ea08502b9dc",
|
||||||
"reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999",
|
"reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
"ext-ctype": "*",
|
||||||
|
"ext-json": "*",
|
||||||
"ext-tokenizer": "*",
|
"ext-tokenizer": "*",
|
||||||
"php": ">=7.0"
|
"php": ">=7.4"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"ircmaxell/php-yacc": "^0.0.7",
|
"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": [
|
||||||
"bin/php-parse"
|
"bin/php-parse"
|
||||||
|
@ -94,7 +96,7 @@
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "4.9-dev"
|
"dev-master": "5.0-dev"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
@ -118,9 +120,9 @@
|
||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/nikic/PHP-Parser/issues",
|
"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",
|
"name": "phar-io/manifest",
|
||||||
|
@ -235,16 +237,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpstan/phpstan",
|
"name": "phpstan/phpstan",
|
||||||
"version": "1.10.50",
|
"version": "1.10.57",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/phpstan/phpstan.git",
|
"url": "https://github.com/phpstan/phpstan.git",
|
||||||
"reference": "06a98513ac72c03e8366b5a0cb00750b487032e4"
|
"reference": "1627b1d03446904aaa77593f370c5201d2ecc34e"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/06a98513ac72c03e8366b5a0cb00750b487032e4",
|
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/1627b1d03446904aaa77593f370c5201d2ecc34e",
|
||||||
"reference": "06a98513ac72c03e8366b5a0cb00750b487032e4",
|
"reference": "1627b1d03446904aaa77593f370c5201d2ecc34e",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -293,7 +295,7 @@
|
||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2023-12-13T10:59:42+00:00"
|
"time": "2024-01-24T11:51:34+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/php-code-coverage",
|
"name": "phpunit/php-code-coverage",
|
||||||
|
@ -618,16 +620,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/phpunit",
|
"name": "phpunit/phpunit",
|
||||||
"version": "10.5.5",
|
"version": "10.5.10",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
||||||
"reference": "ed21115d505b4b4f7dc7b5651464e19a2c7f7856"
|
"reference": "50b8e314b6d0dd06521dc31d1abffa73f25f850c"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ed21115d505b4b4f7dc7b5651464e19a2c7f7856",
|
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/50b8e314b6d0dd06521dc31d1abffa73f25f850c",
|
||||||
"reference": "ed21115d505b4b4f7dc7b5651464e19a2c7f7856",
|
"reference": "50b8e314b6d0dd06521dc31d1abffa73f25f850c",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -699,7 +701,7 @@
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
||||||
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
"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": [
|
"funding": [
|
||||||
{
|
{
|
||||||
|
@ -715,7 +717,7 @@
|
||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2023-12-27T15:13:52+00:00"
|
"time": "2024-02-04T09:07:51+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "sebastian/cli-parser",
|
"name": "sebastian/cli-parser",
|
||||||
|
|
56
src/Data/DbResultIterator.php
Normal file
56
src/Data/DbResultIterator.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
// DbResultTrait.php
|
// DbResultTrait.php
|
||||||
// Created: 2023-11-09
|
// Created: 2023-11-09
|
||||||
// Updated: 2023-11-09
|
// Updated: 2024-02-06
|
||||||
|
|
||||||
namespace Index\Data;
|
namespace Index\Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements common IDbResult methods.
|
||||||
|
*/
|
||||||
trait DbResultTrait {
|
trait DbResultTrait {
|
||||||
public function getString(int|string $index): string {
|
public function getString(int|string $index): string {
|
||||||
return (string)$this->getValue($index);
|
return (string)$this->getValue($index);
|
||||||
|
@ -37,4 +40,8 @@ trait DbResultTrait {
|
||||||
public function getBooleanOrNull(int|string $index): ?bool {
|
public function getBooleanOrNull(int|string $index): ?bool {
|
||||||
return $this->isNull($index) ? null : ($this->getInteger($index) !== 0);
|
return $this->isNull($index) ? null : ($this->getInteger($index) !== 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getIterator(callable $construct): DbResultIterator {
|
||||||
|
return new DbResultIterator($this, $construct);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// IDbResult.php
|
// IDbResult.php
|
||||||
// Created: 2021-05-02
|
// Created: 2021-05-02
|
||||||
// Updated: 2023-11-09
|
// Updated: 2024-02-06
|
||||||
|
|
||||||
namespace Index\Data;
|
namespace Index\Data;
|
||||||
|
|
||||||
|
@ -106,4 +106,12 @@ interface IDbResult extends ICloseable {
|
||||||
* @return ?Stream A Stream if data is available, null if not.
|
* @return ?Stream A Stream if data is available, null if not.
|
||||||
*/
|
*/
|
||||||
function getStream(int|string $index): ?Stream;
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
// NullDbResult.php
|
// NullDbResult.php
|
||||||
// Created: 2021-05-02
|
// Created: 2021-05-02
|
||||||
// Updated: 2023-11-09
|
// Updated: 2024-02-06
|
||||||
|
|
||||||
namespace Index\Data\NullDb;
|
namespace Index\Data\NullDb;
|
||||||
|
|
||||||
use Index\Data\IDbResult;
|
use Index\Data\IDbResult;
|
||||||
|
use Index\Data\DbResultIterator;
|
||||||
use Index\IO\Stream;
|
use Index\IO\Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,5 +61,9 @@ class NullDbResult implements IDbResult {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getIterator(callable $construct): DbResultIterator {
|
||||||
|
return new DbResultIterator($this, $construct);
|
||||||
|
}
|
||||||
|
|
||||||
public function close(): void {}
|
public function close(): void {}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue