modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-06 17:06:11 +04:00
parent 06b1bce249
commit 5e140610c1
4 changed files with 26 additions and 33 deletions

View File

@ -172,6 +172,13 @@ class CapacitorChannel {
return array_combine($keys, [$value, $sum]); return array_combine($keys, [$value, $sum]);
} }
function wasSumModified(string $key, $value, array $prowValues): bool {
$sumKey = $this->getSumKeys($key)[1];
$sum = $this->getSum($key, $value)[$sumKey];
$psum = $prowValues[$sumKey] ?? sha1(serialize($prowValues[$key] ?? null));
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]; $sumKey = $this->getSumKeys($key)[1];
$sum = $row[$sumKey] ?? null; $sum = $row[$sumKey] ?? null;
@ -179,13 +186,6 @@ class CapacitorChannel {
return $sum !== $psum; return $sum !== $psum;
} }
function wasSumModified(string $key, array $rowValues, array $prowValues): bool {
$sumKey = $this->getSumKeys($key)[1];
$sum = $this->getSum($key, $rowValues[$key] ?? null)[$sumKey];
$psum = $prowValues[$sumKey] ?? sha1(serialize($prowValues[$key] ?? null));
return $sum !== $psum;
}
/** /**
* méthode appelée lors du chargement avec {@link Capacitor::charge()} pour * méthode appelée lors du chargement avec {@link Capacitor::charge()} pour
* créer un nouvel élément * créer un nouvel élément

View File

@ -58,11 +58,14 @@ abstract class CapacitorStorage {
$key = $col; $key = $col;
if ($key === $index) { if ($key === $index) {
$index++; $index++;
} elseif (!array_key_exists($key, $values)) {
} elseif ($channel->isSerialKey($key)) { } elseif ($channel->isSerialKey($key)) {
A::merge($row, $channel->getSum($key, $values[$key])); if (array_key_exists($key, $values)) {
A::merge($row, $channel->getSum($key, $values[$key]));
}
} else { } else {
$row[$col] = $values[$key]; if (array_key_exists($key, $values)) {
$row[$col] = $values[$key];
}
} }
} }
return $row; return $row;

View File

@ -200,22 +200,17 @@ class MysqlStorage extends CapacitorStorage {
$commitThreshold = $channel->getEachCommitThreshold(); $commitThreshold = $channel->getEachCommitThreshold();
try { try {
$args ??= []; $args ??= [];
foreach ($this->_all($channel, $filter) as $row) { foreach ($this->_all($channel, $filter) as $rowValues) {
$rowIds = $this->getRowIds($channel, $row); $rowIds = $this->getRowIds($channel, $rowValues);
$updates = func::_call($onEach, [$row["item"], $row, ...$args]); $updates = func::_call($onEach, [$rowValues["item"], $rowValues, ...$args]);
if (is_array($updates)) { if (is_array($updates)) {
$updates = $this->serialize($channel, $updates); if (!array_key_exists("modified_", $updates)) {
if (array_key_exists("item__", $updates)) { $updates["modified_"] = date("Y-m-d H:i:s");
# si item a été mis à jour, il faut mettre à jour sum_
$updates["sum_"] = sha1($updates["item__"]);
if (!array_key_exists("modified_", $updates)) {
$updates["modified_"] = date("Y-m-d H:i:s");
}
} }
$mysql->exec([ $mysql->exec([
"update", "update",
"table" => $tableName, "table" => $tableName,
"values" => $updates, "values" => $this->serialize($channel, $updates),
"where" => $rowIds, "where" => $rowIds,
]); ]);
if ($commitThreshold !== null) { if ($commitThreshold !== null) {

View File

@ -198,22 +198,17 @@ class SqliteStorage extends CapacitorStorage {
$commitThreshold = $channel->getEachCommitThreshold(); $commitThreshold = $channel->getEachCommitThreshold();
try { try {
$args ??= []; $args ??= [];
foreach ($this->_all($channel, $filter) as $row) { foreach ($this->_all($channel, $filter) as $rowValues) {
$rowIds = $this->getRowIds($channel, $row); $rowIds = $this->getRowIds($channel, $rowValues);
$updates = func::_call($onEach, [$row["item"], $row, ...$args]); $updates = func::_call($onEach, [$rowValues["item"], $rowValues, ...$args]);
if (is_array($updates)) { if (is_array($updates) && $updates) {
$updates = $this->serialize($channel, $updates); if (!array_key_exists("modified_", $updates)) {
if (array_key_exists("item__", $updates)) { $updates["modified_"] = date("Y-m-d H:i:s");
# si item a été mis à jour, il faut mettre à jour sum_
$updates["sum_"] = sha1($updates["item__"]);
if (!array_key_exists("modified_", $updates)) {
$updates["modified_"] = date("Y-m-d H:i:s");
}
} }
$sqlite->exec([ $sqlite->exec([
"update", "update",
"table" => $tableName, "table" => $tableName,
"values" => $updates, "values" => $this->serialize($channel, $updates),
"where" => $rowIds, "where" => $rowIds,
]); ]);
if ($commitThreshold !== null) { if ($commitThreshold !== null) {