modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-05 05:21:57 +04:00
parent a7bcfe9ce6
commit 604d9c1fe3
4 changed files with 44 additions and 4 deletions

View File

@ -1,6 +1,9 @@
<?php
namespace nur\sery\db;
use nur\sery\cl;
use nur\sery\str;
/**
* Class CapacitorChannel: un canal d'une instance de {@link ICapacitor}
*/
@ -9,6 +12,10 @@ class CapacitorChannel {
const TABLE_NAME = null;
const KEY_DEFINITIONS = null;
const PRIMARY_KEYS = null;
const EACH_COMMIT_THRESHOLD = 100;
static function verifix_name(?string $name): string {
@ -20,6 +27,25 @@ class CapacitorChannel {
$this->name = self::verifix_name($name ?? static::NAME);
$this->eachCommitThreshold = $eachCommitThreshold ?? static::EACH_COMMIT_THRESHOLD;
$this->created = false;
$keyDefinitions = cl::withn(static::KEY_DEFINITIONS);
$primaryKeys = cl::withn(static::PRIMARY_KEYS);
if ($primaryKeys === null && $keyDefinitions !== null) {
$index = 0;
foreach ($keyDefinitions as $col => $def) {
if ($col == $index) {
$index++;
if (preg_match('/\bprimary\s+key\s+\((.+)\)/i', $def, $ms)) {
$primaryKeys = preg_split('/\s*,\s*/', trim($ms[1]));
}
} else {
if (preg_match('/\bprimary\s+key\b/i', $def)) {
$primaryKeys[] = $col;
}
}
}
}
$this->keyDefinitions = $keyDefinitions;
$this->primaryKeys = $primaryKeys;
}
/** @var string */
@ -54,6 +80,8 @@ class CapacitorChannel {
$this->created = $created;
}
protected ?array $keyDefinitions;
/**
* retourner un ensemble de définitions pour des colonnes supplémentaires à
* insérer lors du chargement d'une valeur
@ -71,7 +99,13 @@ class CapacitorChannel {
* avant d'être retournées à l'utilisateur (sans le suffixe "__")
*/
function getKeyDefinitions(): ?array {
return null;
return $this->keyDefinitions;
}
protected ?array $primaryKeys;
function getPrimaryKeys(): ?array {
return $this->primaryKeys;
}
/**

View File

@ -75,6 +75,12 @@ abstract class CapacitorStorage {
return $this->_one($this->getChannel($channel), $filter);
}
protected function getPrimaryKeys(CapacitorChannel $channel): array {
$primaryKeys = $channel->getPrimaryKeys();
if ($primaryKeys === null) $primaryKeys = ["id_"];
return $primaryKeys;
}
abstract function _all(CapacitorChannel $channel, $filter): iterable;
/**
@ -83,7 +89,7 @@ abstract class CapacitorStorage {
* 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);
return $this->_all($this->getChannel($channel), $filter);
}
abstract function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): int;

View File

@ -251,7 +251,7 @@ class MysqlStorage extends CapacitorStorage {
"select",
"from" => $channel->getTableName(),
"where" => $filter,
], null, "id_");
], null, $this->getPrimaryKeys($channel));
foreach ($rows as $key => $row) {
yield $key => $this->unserialize($channel, $row);
}

View File

@ -250,7 +250,7 @@ class SqliteStorage extends CapacitorStorage {
"select",
"from" => $channel->getTableName(),
"where" => $filter,
], null, "id_");
], null, $this->getPrimaryKeys($channel));
foreach ($rows as $key => $row) {
yield $key => $this->unserialize($channel, $row);
}