modifs.mineures sans commentaires
This commit is contained in:
parent
a38fc17607
commit
7a3b0e456d
|
@ -0,0 +1,64 @@
|
||||||
|
<?php
|
||||||
|
namespace nur\sery\db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AbstractCapacitor: implémentation de base d'un {@link ICapacitor}
|
||||||
|
*/
|
||||||
|
abstract class AbstractCapacitor implements ICapacitor {
|
||||||
|
abstract protected function getChannel(?string $name=null): CapacitorChannel;
|
||||||
|
|
||||||
|
abstract function _exists(CapacitorChannel $channel): bool;
|
||||||
|
|
||||||
|
/** tester si le canal spécifié existe */
|
||||||
|
function exists(?string $channel=null): bool {
|
||||||
|
return $this->_exists($this->getChannel($channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract function _reset(CapacitorChannel $channel): void;
|
||||||
|
|
||||||
|
/** supprimer le canal spécifié */
|
||||||
|
function reset(?string $channel=null): void {
|
||||||
|
$this->_reset($this->getChannel($channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract function _charge($item, CapacitorChannel $channel): void;
|
||||||
|
|
||||||
|
/** charger une valeur dans le canal */
|
||||||
|
function charge($item, ?string $channel=null): void {
|
||||||
|
$this->_charge($item, $this->getChannel($channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract function _discharge($keys=null, CapacitorChannel $channel=null, ?bool $reset=null): iterable;
|
||||||
|
|
||||||
|
/** décharger les données du canal spécifié */
|
||||||
|
function discharge($keys=null, ?string $channel=null, ?bool $reset=null): iterable {
|
||||||
|
return $this->_discharge($keys, $this->getChannel($channel), $reset);
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract function _get($keys, CapacitorChannel $channel=null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* obtenir l'élément identifié par les clés spécifiées sur le canal spécifié
|
||||||
|
*
|
||||||
|
* si $keys n'est pas un tableau, il est transformé en ["_id" => $keys]
|
||||||
|
*/
|
||||||
|
function get($keys, ?string $channel=null) {
|
||||||
|
return $this->_get($keys, $this->getChannel($channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract function _each($keys, callable $func, ?array $args=null, CapacitorChannel $channel=null): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* appeler une fonction pour chaque élément du canal spécifié.
|
||||||
|
*
|
||||||
|
* $keys permet de filtrer parmi les élements chargés
|
||||||
|
*
|
||||||
|
* si $func retourne un tableau, il est utilisé pour mettre à jour
|
||||||
|
* l'enregistrement.
|
||||||
|
*/
|
||||||
|
function each($keys, callable $func, ?array $args=null, ?string $channel=null): void {
|
||||||
|
$this->_each($keys, $func, $args, $this->getChannel($channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract function close(): void;
|
||||||
|
}
|
|
@ -3,16 +3,18 @@ namespace nur\sery\db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Capacitor: un objet permettant d'attaquer un canal spécique d'une
|
* Class Capacitor: un objet permettant d'attaquer un canal spécique d'une
|
||||||
* instance de {@link ICapacitor}
|
* instance de {@link AbstractCapacitor}
|
||||||
*/
|
*/
|
||||||
class Capacitor {
|
class Capacitor {
|
||||||
function __construct(ICapacitor $capacitor, CapacitorChannel $channel) {
|
function __construct(AbstractCapacitor $capacitor, CapacitorChannel $channel) {
|
||||||
$this->capacitor = $capacitor;
|
$this->capacitor = $capacitor;
|
||||||
$this->channel = $channel;
|
$this->channel = $channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var AbstractCapacitor */
|
||||||
protected $capacitor;
|
protected $capacitor;
|
||||||
|
|
||||||
|
/** @var CapacitorChannel */
|
||||||
protected $channel;
|
protected $channel;
|
||||||
|
|
||||||
function exists(): bool {
|
function exists(): bool {
|
||||||
|
|
|
@ -2,15 +2,15 @@
|
||||||
namespace nur\sery\db\sqlite;
|
namespace nur\sery\db\sqlite;
|
||||||
|
|
||||||
use nur\sery\cl;
|
use nur\sery\cl;
|
||||||
|
use nur\sery\db\AbstractCapacitor;
|
||||||
use nur\sery\db\CapacitorChannel;
|
use nur\sery\db\CapacitorChannel;
|
||||||
use nur\sery\db\ICapacitor;
|
|
||||||
use nur\sery\php\func;
|
use nur\sery\php\func;
|
||||||
use nur\sery\ValueException;
|
use nur\sery\ValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SqliteCapacitor
|
* Class SqliteCapacitor
|
||||||
*/
|
*/
|
||||||
class SqliteCapacitor implements ICapacitor {
|
class SqliteCapacitor extends AbstractCapacitor{
|
||||||
function __construct($sqlite) {
|
function __construct($sqlite) {
|
||||||
$this->sqlite = Sqlite::with($sqlite);
|
$this->sqlite = Sqlite::with($sqlite);
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ class SqliteCapacitor implements ICapacitor {
|
||||||
return $channel;
|
return $channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function channel(?string $name=null): CapacitorChannel {
|
protected function getChannel(?string $name=null): CapacitorChannel {
|
||||||
$name = CapacitorChannel::verifix_name($name);
|
$name = CapacitorChannel::verifix_name($name);
|
||||||
$channel = $this->channels[$name] ?? null;
|
$channel = $this->channels[$name] ?? null;
|
||||||
if ($channel === null) {
|
if ($channel === null) {
|
||||||
|
@ -65,11 +65,6 @@ class SqliteCapacitor implements ICapacitor {
|
||||||
return $tableName !== null;
|
return $tableName !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** tester si le canal spécifié existe */
|
|
||||||
function exists(?string $channel=null): bool {
|
|
||||||
return $this->_exists($this->channel($channel));
|
|
||||||
}
|
|
||||||
|
|
||||||
function _reset(CapacitorChannel $channel): void {
|
function _reset(CapacitorChannel $channel): void {
|
||||||
$this->sqlite->exec([
|
$this->sqlite->exec([
|
||||||
"drop table if exists",
|
"drop table if exists",
|
||||||
|
@ -78,11 +73,6 @@ class SqliteCapacitor implements ICapacitor {
|
||||||
$channel->setCreated(false);
|
$channel->setCreated(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** supprimer le canal spécifié */
|
|
||||||
function reset(?string $channel=null): void {
|
|
||||||
$this->_reset($this->channel($channel));
|
|
||||||
}
|
|
||||||
|
|
||||||
function _charge($item, CapacitorChannel $channel): void {
|
function _charge($item, CapacitorChannel $channel): void {
|
||||||
$this->_create($channel);
|
$this->_create($channel);
|
||||||
$values = cl::merge($channel->getKeyValues($item), [
|
$values = cl::merge($channel->getKeyValues($item), [
|
||||||
|
@ -95,11 +85,6 @@ class SqliteCapacitor implements ICapacitor {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** charger une valeur dans le canal */
|
|
||||||
function charge($item, ?string $channel=null): void {
|
|
||||||
$this->_charge($item, $this->channel($channel));
|
|
||||||
}
|
|
||||||
|
|
||||||
function _discharge($keys=null, CapacitorChannel $channel=null, ?bool $reset=null): iterable {
|
function _discharge($keys=null, CapacitorChannel $channel=null, ?bool $reset=null): iterable {
|
||||||
if ($keys !== null && !is_array($keys)) $keys = ["_id" => $keys];
|
if ($keys !== null && !is_array($keys)) $keys = ["_id" => $keys];
|
||||||
if ($reset === null) $reset = $keys === null;
|
if ($reset === null) $reset = $keys === null;
|
||||||
|
@ -115,11 +100,6 @@ class SqliteCapacitor implements ICapacitor {
|
||||||
if ($reset) $this->_reset($channel);
|
if ($reset) $this->_reset($channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** décharger les données du canal spécifié */
|
|
||||||
function discharge($keys=null, ?string $channel=null, ?bool $reset=null): iterable {
|
|
||||||
return $this->_discharge($keys, $this->channel($channel), $reset);
|
|
||||||
}
|
|
||||||
|
|
||||||
function _get($keys, CapacitorChannel $channel=null) {
|
function _get($keys, CapacitorChannel $channel=null) {
|
||||||
if ($keys === null) throw ValueException::null("keys");
|
if ($keys === null) throw ValueException::null("keys");
|
||||||
if (!is_array($keys)) $keys = ["_id" => $keys];
|
if (!is_array($keys)) $keys = ["_id" => $keys];
|
||||||
|
@ -132,15 +112,6 @@ class SqliteCapacitor implements ICapacitor {
|
||||||
else return unserialize($row["_item"]);
|
else return unserialize($row["_item"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* obtenir l'élément identifié par les clés spécifiées sur le canal spécifié
|
|
||||||
*
|
|
||||||
* si $keys n'est pas un tableau, il est transformé en ["_id" => $keys]
|
|
||||||
*/
|
|
||||||
function get($keys, ?string $channel=null) {
|
|
||||||
return $this->_get($keys, $this->channel($channel));
|
|
||||||
}
|
|
||||||
|
|
||||||
function _each($keys, callable $func, ?array $args=null, CapacitorChannel $channel=null): void {
|
function _each($keys, callable $func, ?array $args=null, CapacitorChannel $channel=null): void {
|
||||||
if ($keys !== null && !is_array($keys)) $keys = ["_id" => $keys];
|
if ($keys !== null && !is_array($keys)) $keys = ["_id" => $keys];
|
||||||
$context = func::_prepare($func);
|
$context = func::_prepare($func);
|
||||||
|
@ -169,18 +140,6 @@ class SqliteCapacitor implements ICapacitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* appeler une fonction pour chaque élément du canal spécifié.
|
|
||||||
*
|
|
||||||
* $keys permet de filtrer parmi les élements chargés
|
|
||||||
*
|
|
||||||
* si $func retourne un tableau, il est utilisé pour mettre à jour
|
|
||||||
* l'enregistrement.
|
|
||||||
*/
|
|
||||||
function each($keys, callable $func, ?array $args=null, ?string $channel=null): void {
|
|
||||||
$this->_each($keys, $func, $args, $this->channel($channel));
|
|
||||||
}
|
|
||||||
|
|
||||||
function close(): void {
|
function close(): void {
|
||||||
$this->sqlite->close();
|
$this->sqlite->close();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue