modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-07 15:39:19 +04:00
parent 2fbe3ec09c
commit 83b041e970
2 changed files with 27 additions and 21 deletions

View File

@ -143,46 +143,47 @@ class CapacitorChannel {
return array_key_exists("modified_", $rowValues); return array_key_exists("modified_", $rowValues);
} }
final function serialize($item): string { final function serialize($item): ?string {
return serialize($item); return $item !== null? serialize($item): null;
} }
final function unserialize(string $string) { final function unserialize(?string $serial) {
return unserialize($string); return $serial !== null? unserialize($serial): null;
} }
const SUM_DEFINITION = "varchar(40)"; const SUM_DEFINITION = "varchar(40)";
final function sum(?string $string): string { final function sum(?string $serial, $value=null): ?string {
return sha1($string); if ($serial === null) $serial = $this->serialize($value);
return $serial !== null? sha1($serial): null;
} }
final function isSerialKey(string &$key): bool { final function isSerialCol(string &$key): bool {
return str::del_suffix($key, "__"); return str::del_suffix($key, "__");
} }
final function getSumKeys(string $key): array { final function getSumCols(string $key): array {
return ["${key}__", "${key}__sum_"]; return ["${key}__", "${key}__sum_"];
} }
function getSum(string $key, $value): array { function getSum(string $key, $value): array {
$keys = $this->getSumKeys($key); $sumCols = $this->getSumCols($key);
if ($value !== null) $value = $this->serialize($value); $serial = $this->serialize($value);
$sum = $this->sum($value); $sum = $this->sum($serial, $value);
return array_combine($keys, [$value, $sum]); return array_combine($sumCols, [$serial, $sum]);
} }
function wasSumModified(string $key, $value, array $prowValues): bool { function wasSumModified(string $key, $value, array $prowValues): bool {
$sumKey = $this->getSumKeys($key)[1]; $sumCol = $this->getSumCols($key)[1];
$sum = $this->getSum($key, $value)[$sumKey]; $sum = $this->sum(null, $value);
$psum = $prowValues[$sumKey] ?? sha1(serialize($prowValues[$key] ?? null)); $psum = $prowValues[$sumCol] ?? $this->sum(null, $prowValues[$key] ?? null);
return $sum !== $psum; return $sum !== $psum;
} }
function _wasSumModified(string $key, array $row, array $prow): bool { function _wasSumModified(string $key, array $row, array $prow): bool {
$sumKey = $this->getSumKeys($key)[1]; $sumCol = $this->getSumCols($key)[1];
$sum = $row[$sumKey] ?? null; $sum = $row[$sumCol] ?? null;
$psum = $prow[$sumKey] ?? null; $psum = $prow[$sumCol] ?? null;
return $sum !== $psum; return $sum !== $psum;
} }

View File

@ -58,9 +58,14 @@ abstract class CapacitorStorage {
$key = $col; $key = $col;
if ($key === $index) { if ($key === $index) {
$index++; $index++;
} elseif ($channel->isSerialKey($key)) { } elseif ($channel->isSerialCol($key)) {
[$serialCol, $sumCol] = $channel->getSumCols($key);
if (array_key_exists($key, $values)) { if (array_key_exists($key, $values)) {
A::merge($row, $channel->getSum($key, $values[$key])); $sum = $channel->getSum($key, $values[$key]);
$row[$serialCol] = $sum[$serialCol];
if (array_key_exists($sumCol, $cols)) {
$row[$sumCol] = $sum[$sumCol];
}
} }
} else { } else {
if (array_key_exists($key, $values)) { if (array_key_exists($key, $values)) {
@ -82,7 +87,7 @@ abstract class CapacitorStorage {
if ($key === $index) { if ($key === $index) {
$index++; $index++;
} elseif (!array_key_exists($col, $row)) { } elseif (!array_key_exists($col, $row)) {
} elseif ($channel->isSerialKey($key)) { } elseif ($channel->isSerialCol($key)) {
$value = $row[$col]; $value = $row[$col];
if ($value !== null) $value = $channel->unserialize($value); if ($value !== null) $value = $channel->unserialize($value);
$values[$key] = $value; $values[$key] = $value;