renommer SqliteCapacitor en SqliteStorage

This commit is contained in:
Jephté Clain 2024-05-25 09:39:43 +04:00
parent 0515de9642
commit 58a037bd98
4 changed files with 84 additions and 68 deletions

View File

@ -34,16 +34,20 @@ class Capacitor {
return $this->storage->_charge($this->channel, $item, $func, $args);
}
function discharge($filter=null, ?bool $reset=null): iterable {
return $this->storage->_discharge($this->channel, $reset);
}
function count($filter=null): int {
return $this->storage->_count($this->channel, $filter);
}
function discharge($filter=null, ?bool $reset=null): iterable {
return $this->storage->_discharge($this->channel, $filter, $reset);
function one($filter): ?array {
return $this->storage->_one($this->channel, $filter);
}
function get($filter) {
return $this->storage->_get($this->channel, $filter);
function all($filter): iterable {
return $this->storage->_all($this->channel, $filter);
}
function each($filter, ?callable $func=null, ?array $args=null): int {

View File

@ -50,6 +50,13 @@ abstract class CapacitorStorage {
return $this->_charge($this->getChannel($channel), $item, $func, $args);
}
abstract function _discharge(CapacitorChannel $channel, bool $reset=true): iterable;
/** décharger les données du canal spécifié */
function discharge(?string $channel, bool $reset=true): iterable {
return $this->_discharge($this->getChannel($channel), $reset);
}
abstract function _count(CapacitorChannel $channel, $filter): int;
/** indiquer le nombre d'éléments du canal spécifié */
@ -57,22 +64,26 @@ abstract class CapacitorStorage {
return $this->_count($this->getChannel($channel), $filter);
}
abstract function _discharge(CapacitorChannel $channel, $filter, ?bool $reset): iterable;
/** décharger les données du canal spécifié */
function discharge(?string $channel, $filter=null, ?bool $reset=null): iterable {
return $this->_discharge($this->getChannel($channel), $filter, $reset);
}
abstract function _get(CapacitorChannel $channel, $filter);
abstract function _one(CapacitorChannel $channel, $filter): ?array;
/**
* obtenir l'élément identifié par les clés spécifiées sur le canal spécifié
* obtenir la ligne correspondant au filtre sur le canal spécifié
*
* si $filter n'est pas un tableau, il est transformé en ["_id" => $filter]
* si $filter n'est pas un tableau, il est transformé en ["id_" => $filter]
*/
function get(?string $channel, $filter) {
return $this->_get($this->getChannel($channel), $filter);
function one(?string $channel, $filter): ?array {
return $this->_one($this->getChannel($channel), $filter);
}
abstract function _all(CapacitorChannel $channel, $filter): iterable;
/**
* obtenir les lignes correspondant au filtre sur le canal spécifié
*
* si $filter n'est pas un tableau, il est transformé en ["id_" => $filter]
*/
function all(?string $channel, $filter): iterable {
return $this->_one($this->getChannel($channel), $filter);
}
abstract function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): int;

View File

@ -9,9 +9,9 @@ use nur\sery\str;
use nur\sery\ValueException;
/**
* Class SqliteCapacitor
* Class SqliteStorage
*/
class SqliteCapacitor extends CapacitorStorage {
class SqliteStorage extends CapacitorStorage {
function __construct($sqlite) {
$this->sqlite = Sqlite::with($sqlite);
}
@ -204,6 +204,17 @@ class SqliteCapacitor extends CapacitorStorage {
return 1;
}
function _discharge(CapacitorChannel $channel, bool $reset=true): iterable {
$rows = $this->sqlite->all([
"select item__",
"from" => $channel->getTableName(),
]);
foreach ($rows as $row) {
yield unserialize($row['item__']);
}
if ($reset) $this->_reset($channel);
}
protected function verifixFilter(CapacitorChannel $channel, &$filter): void {
if ($filter !== null && !is_array($filter)) {
$id = $filter;
@ -222,34 +233,30 @@ class SqliteCapacitor extends CapacitorStorage {
]);
}
function _discharge(CapacitorChannel $channel, $filter, ?bool $reset): iterable {
function _one(CapacitorChannel $channel, $filter): ?array {
if ($filter === null) throw ValueException::null("filter");
$this->verifixFilter($channel, $filter);
$row = $this->sqlite->one([
"select",
"from" => $channel->getTableName(),
"where" => $filter,
]);
return $this->unserialize($channel, $row);
}
function _all(CapacitorChannel $channel, $filter): iterable {
$this->verifixFilter($channel, $filter);
if ($reset === null) $reset = $filter === null;
$rows = $this->sqlite->all([
"select item__",
"select",
"from" => $channel->getTableName(),
"where" => $filter,
]);
foreach ($rows as $row) {
yield unserialize($row['item__']);
yield $this->unserialize($channel, $row);
}
if ($reset) $this->_reset($channel);
}
function _get(CapacitorChannel $channel, $filter) {
if ($filter === null) throw ValueException::null("filter");
$this->verifixFilter($channel, $filter);
$row = $this->sqlite->one([
"select item__",
"from" => $channel->getTableName(),
"where" => $filter,
]);
if ($row === null) return null;
else return unserialize($row["item__"]);
}
function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): int {
$this->verifixFilter($channel, $filter);
if ($func === null) $func = [$channel, "onEach"];
$onEach = func::_prepare($func);
$sqlite = $this->sqlite;
@ -259,15 +266,9 @@ class SqliteCapacitor extends CapacitorStorage {
$sqlite->beginTransaction();
$commitThreshold = $channel->getEachCommitThreshold();
try {
$rows = $sqlite->all([
"select",
"from" => $tableName,
"where" => $filter,
]);
$args ??= [];
foreach ($rows as $row) {
$values = $this->unserialize($channel, $row);
$updates = func::_call($onEach, [$values["item"], $values, ...$args]);
foreach ($this->_all($channel, $filter) as $row) {
$updates = func::_call($onEach, [$row["item"], $row, ...$args]);
if (is_array($updates)) {
$updates = $this->serialize($channel, $updates);
if (array_key_exists("item__", $updates)) {

View File

@ -5,32 +5,32 @@ use nulib\tests\TestCase;
use nur\sery\db\Capacitor;
use nur\sery\db\CapacitorChannel;
class SqliteCapacitorTest extends TestCase {
function _testChargeStrings(SqliteCapacitor $capacitor, ?string $channel) {
$capacitor->reset($channel);
$capacitor->charge($channel, "first");
$capacitor->charge($channel, "second");
$capacitor->charge($channel, "third");
$items = iterator_to_array($capacitor->discharge($channel, null, false));
class SqliteStorageTest extends TestCase {
function _testChargeStrings(SqliteStorage $storage, ?string $channel) {
$storage->reset($channel);
$storage->charge($channel, "first");
$storage->charge($channel, "second");
$storage->charge($channel, "third");
$items = iterator_to_array($storage->discharge($channel, false));
self::assertSame(["first", "second", "third"], $items);
}
function _testChargeArrays(SqliteCapacitor $capacitor, ?string $channel) {
$capacitor->reset($channel);
$capacitor->charge($channel, ["id" => 10, "name" => "first"]);
$capacitor->charge($channel, ["name" => "second", "id" => 20]);
$capacitor->charge($channel, ["name" => "third", "id" => "30"]);
function _testChargeArrays(SqliteStorage $storage, ?string $channel) {
$storage->reset($channel);
$storage->charge($channel, ["id" => 10, "name" => "first"]);
$storage->charge($channel, ["name" => "second", "id" => 20]);
$storage->charge($channel, ["name" => "third", "id" => "30"]);
}
function testChargeStrings() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$this->_testChargeStrings($capacitor, null);
$capacitor->close();
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$this->_testChargeStrings($storage, null);
$storage->close();
}
function testChargeArrays() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$capacitor->addChannel(new class extends CapacitorChannel {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$storage->addChannel(new class extends CapacitorChannel {
const NAME = "arrays";
function getKeyDefinitions(): ?array {
return ["id" => "integer"];
@ -40,14 +40,14 @@ class SqliteCapacitorTest extends TestCase {
}
});
$this->_testChargeStrings($capacitor, "strings");
$this->_testChargeArrays($capacitor, "arrays");
$capacitor->close();
$this->_testChargeStrings($storage, "strings");
$this->_testChargeArrays($storage, "arrays");
$storage->close();
}
function testEach() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($capacitor, new class extends CapacitorChannel {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($storage, new class extends CapacitorChannel {
const NAME = "each";
function getKeyDefinitions(): ?array {
@ -86,8 +86,8 @@ class SqliteCapacitorTest extends TestCase {
}
function testPrimayKey() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($capacitor, new class extends CapacitorChannel {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($storage, new class extends CapacitorChannel {
const NAME = "pk";
function getKeyDefinitions(): ?array {