modifs.mineures sans commentaires
This commit is contained in:
parent
d223446e3c
commit
06b1bce249
|
@ -35,4 +35,8 @@ class A {
|
||||||
else $array = [$array];
|
else $array = [$array];
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function merge(?array &$dest, ...$merges): void {
|
||||||
|
$dest = cl::merge($dest, ...$merges);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
namespace nur\sery\db;
|
namespace nur\sery\db;
|
||||||
|
|
||||||
use nur\sery\cl;
|
use nur\sery\cl;
|
||||||
|
use nur\sery\str;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CapacitorChannel: un canal d'une instance de {@link ICapacitor}
|
* Class CapacitorChannel: un canal d'une instance de {@link ICapacitor}
|
||||||
|
@ -132,6 +133,59 @@ class CapacitorChannel {
|
||||||
function verifixId(string &$id): void {
|
function verifixId(string &$id): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retourne true si un nouvel élément ou un élément mis à jour a été chargé.
|
||||||
|
* false si l'élément chargé est identique au précédent.
|
||||||
|
*
|
||||||
|
* cette méthode doit être utilisée dans {@link self::onUpdate()}
|
||||||
|
*/
|
||||||
|
function wasRowModified(array $rowValues): bool {
|
||||||
|
return array_key_exists("modified_", $rowValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
final function serialize($item): string {
|
||||||
|
return serialize($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
final function unserialize(string $string) {
|
||||||
|
return unserialize($string);
|
||||||
|
}
|
||||||
|
|
||||||
|
const SUM_DEFINITION = "varchar(40)";
|
||||||
|
|
||||||
|
final function sum(string $string): string {
|
||||||
|
return sha1($string);
|
||||||
|
}
|
||||||
|
|
||||||
|
final function isSerialKey(string &$key): bool {
|
||||||
|
return str::del_suffix($key, "__");
|
||||||
|
}
|
||||||
|
|
||||||
|
final function getSumKeys(string $key): array {
|
||||||
|
return ["${key}__", "${key}__sum_"];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSum(string $key, $value): array {
|
||||||
|
$keys = $this->getSumKeys($key);
|
||||||
|
if ($value !== null) $value = $this->serialize($value);
|
||||||
|
$sum = $this->sum($value);
|
||||||
|
return array_combine($keys, [$value, $sum]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _wasSumModified(string $key, array $row, array $prow): bool {
|
||||||
|
$sumKey = $this->getSumKeys($key)[1];
|
||||||
|
$sum = $row[$sumKey] ?? null;
|
||||||
|
$psum = $prow[$sumKey] ?? null;
|
||||||
|
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
|
||||||
|
@ -149,16 +203,6 @@ class CapacitorChannel {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* retourne true si un nouvel élément ou un élément mis à jour a été chargé.
|
|
||||||
* false si l'élément chargé est identique au précédent.
|
|
||||||
*
|
|
||||||
* cette méthode doit être utilisée dans {@link self::onUpdate()}
|
|
||||||
*/
|
|
||||||
function wasModified(array $rowValues): bool {
|
|
||||||
return array_key_exists("modified_", $rowValues);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* méthode appelée lors du chargement avec {@link Capacitor::charge()} pour
|
* méthode appelée lors du chargement avec {@link Capacitor::charge()} pour
|
||||||
* mettre à jour un élément existant
|
* mettre à jour un élément existant
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
namespace nur\sery\db;
|
namespace nur\sery\db;
|
||||||
|
|
||||||
|
use nur\sery\A;
|
||||||
use nur\sery\cl;
|
use nur\sery\cl;
|
||||||
use nur\sery\str;
|
use nur\sery\str;
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ abstract class CapacitorStorage {
|
||||||
|
|
||||||
const COLUMN_DEFINITIONS = [
|
const COLUMN_DEFINITIONS = [
|
||||||
"item__" => "text",
|
"item__" => "text",
|
||||||
"sum_" => "varchar(40)",
|
"item__sum_" => CapacitorChannel::SUM_DEFINITION,
|
||||||
"created_" => "datetime",
|
"created_" => "datetime",
|
||||||
"modified_" => "datetime",
|
"modified_" => "datetime",
|
||||||
];
|
];
|
||||||
|
@ -57,16 +58,12 @@ abstract class CapacitorStorage {
|
||||||
$key = $col;
|
$key = $col;
|
||||||
if ($key === $index) {
|
if ($key === $index) {
|
||||||
$index++;
|
$index++;
|
||||||
continue;
|
} elseif (!array_key_exists($key, $values)) {
|
||||||
} elseif (str::del_suffix($key, "__")) {
|
} elseif ($channel->isSerialKey($key)) {
|
||||||
if (!array_key_exists($key, $values)) continue;
|
A::merge($row, $channel->getSum($key, $values[$key]));
|
||||||
$value = $values[$key];
|
|
||||||
if ($value !== null) $value = serialize($value);
|
|
||||||
} else {
|
} else {
|
||||||
if (!array_key_exists($key, $values)) continue;
|
$row[$col] = $values[$key];
|
||||||
$value = $values[$key];
|
|
||||||
}
|
}
|
||||||
$row[$col] = $value;
|
|
||||||
}
|
}
|
||||||
return $row;
|
return $row;
|
||||||
}
|
}
|
||||||
|
@ -81,16 +78,14 @@ abstract class CapacitorStorage {
|
||||||
$key = $col;
|
$key = $col;
|
||||||
if ($key === $index) {
|
if ($key === $index) {
|
||||||
$index++;
|
$index++;
|
||||||
continue;
|
|
||||||
} elseif (!array_key_exists($col, $row)) {
|
} elseif (!array_key_exists($col, $row)) {
|
||||||
continue;
|
} elseif ($channel->isSerialKey($key)) {
|
||||||
} elseif (str::del_suffix($key, "__")) {
|
|
||||||
$value = $row[$col];
|
$value = $row[$col];
|
||||||
if ($value !== null) $value = unserialize($value);
|
if ($value !== null) $value = $channel->unserialize($value);
|
||||||
|
$values[$key] = $value;
|
||||||
} else {
|
} else {
|
||||||
$value = $row[$col];
|
$values[$key] = $row[$col];
|
||||||
}
|
}
|
||||||
$values[$key] = $value;
|
|
||||||
}
|
}
|
||||||
return $values;
|
return $values;
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,12 +66,9 @@ class MysqlStorage extends CapacitorStorage {
|
||||||
$this->_create($channel);
|
$this->_create($channel);
|
||||||
$tableName = $channel->getTableName();
|
$tableName = $channel->getTableName();
|
||||||
$now = date("Y-m-d H:i:s");
|
$now = date("Y-m-d H:i:s");
|
||||||
$item__ = serialize($item);
|
$row = cl::merge(
|
||||||
$sum_ = sha1($item__);
|
$channel->getSum("item", $item),
|
||||||
$row = cl::merge([
|
$this->unserialize($channel, $channel->getItemValues($item)));
|
||||||
"item__" => $item__,
|
|
||||||
"sum_" => $sum_,
|
|
||||||
], $this->unserialize($channel, $channel->getItemValues($item)));
|
|
||||||
$prow = null;
|
$prow = null;
|
||||||
$rowIds = $this->getRowIds($channel, $row, $primaryKeys);
|
$rowIds = $this->getRowIds($channel, $row, $primaryKeys);
|
||||||
if ($rowIds !== null) {
|
if ($rowIds !== null) {
|
||||||
|
@ -97,7 +94,7 @@ class MysqlStorage extends CapacitorStorage {
|
||||||
$args = [$item, $values, ...$args];
|
$args = [$item, $values, ...$args];
|
||||||
} else {
|
} else {
|
||||||
# modification
|
# modification
|
||||||
if ($sum_ !== $prow["sum_"]) {
|
if ($channel->_wasSumModified("item", $row, $prow)) {
|
||||||
$insert = false;
|
$insert = false;
|
||||||
$row = cl::merge($row, [
|
$row = cl::merge($row, [
|
||||||
"modified_" => $now,
|
"modified_" => $now,
|
||||||
|
@ -111,17 +108,12 @@ class MysqlStorage extends CapacitorStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
$updates = func::call($func, ...$args);
|
$updates = func::call($func, ...$args);
|
||||||
if (is_array($updates)) {
|
if (is_array($updates) && $updates) {
|
||||||
if ($insert === null) $insert = false;
|
if ($insert === null) $insert = false;
|
||||||
$updates = $this->serialize($channel, $updates);
|
if (!array_key_exists("modified_", $updates)) {
|
||||||
if (array_key_exists("item__", $updates)) {
|
$updates["modified_"] = $now;
|
||||||
# si item a été mis à jour, il faut mettre à jour sum_
|
|
||||||
$updates["sum_"] = sha1($updates["item__"]);
|
|
||||||
if (!array_key_exists("modified_", $updates)) {
|
|
||||||
$updates["modified_"] = $now;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$row = cl::merge($row, $updates);
|
$row = cl::merge($row, $this->serialize($channel, $updates));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($insert === null) {
|
if ($insert === null) {
|
||||||
|
|
|
@ -64,12 +64,9 @@ class SqliteStorage extends CapacitorStorage {
|
||||||
$this->_create($channel);
|
$this->_create($channel);
|
||||||
$tableName = $channel->getTableName();
|
$tableName = $channel->getTableName();
|
||||||
$now = date("Y-m-d H:i:s");
|
$now = date("Y-m-d H:i:s");
|
||||||
$item__ = serialize($item);
|
$row = cl::merge(
|
||||||
$sum_ = sha1($item__);
|
$channel->getSum("item", $item),
|
||||||
$row = cl::merge([
|
$this->unserialize($channel, $channel->getItemValues($item)));
|
||||||
"item__" => $item__,
|
|
||||||
"sum_" => $sum_,
|
|
||||||
], $this->unserialize($channel, $channel->getItemValues($item)));
|
|
||||||
$prow = null;
|
$prow = null;
|
||||||
$rowIds = $this->getRowIds($channel, $row, $primaryKeys);
|
$rowIds = $this->getRowIds($channel, $row, $primaryKeys);
|
||||||
if ($rowIds !== null) {
|
if ($rowIds !== null) {
|
||||||
|
@ -95,7 +92,7 @@ class SqliteStorage extends CapacitorStorage {
|
||||||
$args = [$item, $values, ...$args];
|
$args = [$item, $values, ...$args];
|
||||||
} else {
|
} else {
|
||||||
# modification
|
# modification
|
||||||
if ($sum_ !== $prow["sum_"]) {
|
if ($channel->_wasSumModified("item", $row, $prow)) {
|
||||||
$insert = false;
|
$insert = false;
|
||||||
$row = cl::merge($row, [
|
$row = cl::merge($row, [
|
||||||
"modified_" => $now,
|
"modified_" => $now,
|
||||||
|
@ -109,17 +106,12 @@ class SqliteStorage extends CapacitorStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
$updates = func::call($func, ...$args);
|
$updates = func::call($func, ...$args);
|
||||||
if (is_array($updates)) {
|
if (is_array($updates) && $updates) {
|
||||||
if ($insert === null) $insert = false;
|
if ($insert === null) $insert = false;
|
||||||
$updates = $this->serialize($channel, $updates);
|
if (!array_key_exists("modified_", $updates)) {
|
||||||
if (array_key_exists("item__", $updates)) {
|
$updates["modified_"] = $now;
|
||||||
# si item a été mis à jour, il faut mettre à jour sum_
|
|
||||||
$updates["sum_"] = sha1($updates["item__"]);
|
|
||||||
if (!array_key_exists("modified_", $updates)) {
|
|
||||||
$updates["modified_"] = $now;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$row = cl::merge($row, $updates);
|
$row = cl::merge($row, $this->serialize($channel, $updates));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($insert === null) {
|
if ($insert === null) {
|
||||||
|
|
|
@ -11,7 +11,7 @@ class Date extends DateTime {
|
||||||
|
|
||||||
function __construct($datetime="now", DateTimeZone $timezone=null) {
|
function __construct($datetime="now", DateTimeZone $timezone=null) {
|
||||||
parent::__construct($datetime, $timezone);
|
parent::__construct($datetime, $timezone);
|
||||||
$this->setTime(0, 0, 0, 0);
|
$this->setTime(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function format($format=self::DEFAULT_FORMAT): string {
|
function format($format=self::DEFAULT_FORMAT): string {
|
||||||
|
|
|
@ -197,6 +197,39 @@ class DateTime extends \DateTime {
|
||||||
return \DateTime::format($format);
|
return \DateTime::format($format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* modifier cet objet pour que l'heure soit à 00:00:00 ce qui le rend propice
|
||||||
|
* à l'utilisation comme borne inférieure d'une période
|
||||||
|
*/
|
||||||
|
function wrapStartOfDay(): self {
|
||||||
|
$this->setTime(0, 0);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* modifier cet objet pour que l'heure soit à 23:59:59.999999 ce qui le rend
|
||||||
|
* propice à l'utilisation comme borne supérieure d'une période
|
||||||
|
*/
|
||||||
|
function wrapEndOfDay(): self {
|
||||||
|
$this->setTime(23, 59, 59, 999999);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPrevDay(int $nbDays=1, bool $skipWeekend=false): self {
|
||||||
|
if ($nbDays == 1 && $skipWeekend && $this->wday == 1) {
|
||||||
|
$nbdays = 3;
|
||||||
|
}
|
||||||
|
return static::with($this->sub(new \DateInterval("P${nbDays}D")));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNextDay(int $nbDays=1, bool $skipWeekend=false): self {
|
||||||
|
if ($nbDays == 1 && $skipWeekend) {
|
||||||
|
$wday = $this->wday;
|
||||||
|
if ($wday > 5) $nbDays = 8 - $this->wday;
|
||||||
|
}
|
||||||
|
return static::with($this->add(new \DateInterval("P${nbDays}D")));
|
||||||
|
}
|
||||||
|
|
||||||
function __toString(): string {
|
function __toString(): string {
|
||||||
return $this->format();
|
return $this->format();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue