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); 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 { function count($filter=null): int {
return $this->storage->_count($this->channel, $filter); return $this->storage->_count($this->channel, $filter);
} }
function discharge($filter=null, ?bool $reset=null): iterable { function one($filter): ?array {
return $this->storage->_discharge($this->channel, $filter, $reset); return $this->storage->_one($this->channel, $filter);
} }
function get($filter) { function all($filter): iterable {
return $this->storage->_get($this->channel, $filter); return $this->storage->_all($this->channel, $filter);
} }
function each($filter, ?callable $func=null, ?array $args=null): int { 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); 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; abstract function _count(CapacitorChannel $channel, $filter): int;
/** indiquer le nombre d'éléments du canal spécifié */ /** indiquer le nombre d'éléments du canal spécifié */
@ -57,22 +64,26 @@ abstract class CapacitorStorage {
return $this->_count($this->getChannel($channel), $filter); return $this->_count($this->getChannel($channel), $filter);
} }
abstract function _discharge(CapacitorChannel $channel, $filter, ?bool $reset): iterable; abstract function _one(CapacitorChannel $channel, $filter): ?array;
/** 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);
/** /**
* 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) { function one(?string $channel, $filter): ?array {
return $this->_get($this->getChannel($channel), $filter); 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; 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; use nur\sery\ValueException;
/** /**
* Class SqliteCapacitor * Class SqliteStorage
*/ */
class SqliteCapacitor extends CapacitorStorage { class SqliteStorage extends CapacitorStorage {
function __construct($sqlite) { function __construct($sqlite) {
$this->sqlite = Sqlite::with($sqlite); $this->sqlite = Sqlite::with($sqlite);
} }
@ -204,6 +204,17 @@ class SqliteCapacitor extends CapacitorStorage {
return 1; 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 { protected function verifixFilter(CapacitorChannel $channel, &$filter): void {
if ($filter !== null && !is_array($filter)) { if ($filter !== null && !is_array($filter)) {
$id = $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); $this->verifixFilter($channel, $filter);
if ($reset === null) $reset = $filter === null;
$rows = $this->sqlite->all([ $rows = $this->sqlite->all([
"select item__", "select",
"from" => $channel->getTableName(), "from" => $channel->getTableName(),
"where" => $filter, "where" => $filter,
]); ]);
foreach ($rows as $row) { 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 { function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): int {
$this->verifixFilter($channel, $filter);
if ($func === null) $func = [$channel, "onEach"]; if ($func === null) $func = [$channel, "onEach"];
$onEach = func::_prepare($func); $onEach = func::_prepare($func);
$sqlite = $this->sqlite; $sqlite = $this->sqlite;
@ -259,15 +266,9 @@ class SqliteCapacitor extends CapacitorStorage {
$sqlite->beginTransaction(); $sqlite->beginTransaction();
$commitThreshold = $channel->getEachCommitThreshold(); $commitThreshold = $channel->getEachCommitThreshold();
try { try {
$rows = $sqlite->all([
"select",
"from" => $tableName,
"where" => $filter,
]);
$args ??= []; $args ??= [];
foreach ($rows as $row) { foreach ($this->_all($channel, $filter) as $row) {
$values = $this->unserialize($channel, $row); $updates = func::_call($onEach, [$row["item"], $row, ...$args]);
$updates = func::_call($onEach, [$values["item"], $values, ...$args]);
if (is_array($updates)) { if (is_array($updates)) {
$updates = $this->serialize($channel, $updates); $updates = $this->serialize($channel, $updates);
if (array_key_exists("item__", $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\Capacitor;
use nur\sery\db\CapacitorChannel; use nur\sery\db\CapacitorChannel;
class SqliteCapacitorTest extends TestCase { class SqliteStorageTest extends TestCase {
function _testChargeStrings(SqliteCapacitor $capacitor, ?string $channel) { function _testChargeStrings(SqliteStorage $storage, ?string $channel) {
$capacitor->reset($channel); $storage->reset($channel);
$capacitor->charge($channel, "first"); $storage->charge($channel, "first");
$capacitor->charge($channel, "second"); $storage->charge($channel, "second");
$capacitor->charge($channel, "third"); $storage->charge($channel, "third");
$items = iterator_to_array($capacitor->discharge($channel, null, false)); $items = iterator_to_array($storage->discharge($channel, false));
self::assertSame(["first", "second", "third"], $items); self::assertSame(["first", "second", "third"], $items);
} }
function _testChargeArrays(SqliteCapacitor $capacitor, ?string $channel) { function _testChargeArrays(SqliteStorage $storage, ?string $channel) {
$capacitor->reset($channel); $storage->reset($channel);
$capacitor->charge($channel, ["id" => 10, "name" => "first"]); $storage->charge($channel, ["id" => 10, "name" => "first"]);
$capacitor->charge($channel, ["name" => "second", "id" => 20]); $storage->charge($channel, ["name" => "second", "id" => 20]);
$capacitor->charge($channel, ["name" => "third", "id" => "30"]); $storage->charge($channel, ["name" => "third", "id" => "30"]);
} }
function testChargeStrings() { function testChargeStrings() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db'); $storage = new SqliteStorage(__DIR__.'/capacitor.db');
$this->_testChargeStrings($capacitor, null); $this->_testChargeStrings($storage, null);
$capacitor->close(); $storage->close();
} }
function testChargeArrays() { function testChargeArrays() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db'); $storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor->addChannel(new class extends CapacitorChannel { $storage->addChannel(new class extends CapacitorChannel {
const NAME = "arrays"; const NAME = "arrays";
function getKeyDefinitions(): ?array { function getKeyDefinitions(): ?array {
return ["id" => "integer"]; return ["id" => "integer"];
@ -40,14 +40,14 @@ class SqliteCapacitorTest extends TestCase {
} }
}); });
$this->_testChargeStrings($capacitor, "strings"); $this->_testChargeStrings($storage, "strings");
$this->_testChargeArrays($capacitor, "arrays"); $this->_testChargeArrays($storage, "arrays");
$capacitor->close(); $storage->close();
} }
function testEach() { function testEach() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db'); $storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($capacitor, new class extends CapacitorChannel { $capacitor = new Capacitor($storage, new class extends CapacitorChannel {
const NAME = "each"; const NAME = "each";
function getKeyDefinitions(): ?array { function getKeyDefinitions(): ?array {
@ -86,8 +86,8 @@ class SqliteCapacitorTest extends TestCase {
} }
function testPrimayKey() { function testPrimayKey() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db'); $storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($capacitor, new class extends CapacitorChannel { $capacitor = new Capacitor($storage, new class extends CapacitorChannel {
const NAME = "pk"; const NAME = "pk";
function getKeyDefinitions(): ?array { function getKeyDefinitions(): ?array {