From 1c57fcf041c66a588cf67c5e73dadcacac9d9340 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 5 Jun 2024 11:44:13 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/db/CapacitorChannel.php | 2 +- src/db/mysql/Mysql.php | 7 +++++++ src/db/mysql/MysqlStorage.php | 5 +++-- src/db/pdo/_query_base.php | 6 ++++-- src/file/Stream.php | 17 ++++++++++++++--- src/php/iter/AbstractIterator.php | 5 +++++ src/php/time/DateTime.php | 8 ++++---- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/db/CapacitorChannel.php b/src/db/CapacitorChannel.php index a61fa78..0090700 100644 --- a/src/db/CapacitorChannel.php +++ b/src/db/CapacitorChannel.php @@ -32,7 +32,7 @@ class CapacitorChannel { if ($primaryKeys === null && $columnDefinitions !== null) { $index = 0; foreach ($columnDefinitions as $col => $def) { - if ($col == $index) { + if ($col === $index) { $index++; if (preg_match('/\bprimary\s+key\s+\((.+)\)/i', $def, $ms)) { $primaryKeys = preg_split('/\s*,\s*/', trim($ms[1])); diff --git a/src/db/mysql/Mysql.php b/src/db/mysql/Mysql.php index 90a1239..c4b8162 100644 --- a/src/db/mysql/Mysql.php +++ b/src/db/mysql/Mysql.php @@ -4,4 +4,11 @@ namespace nur\sery\db\mysql; use nur\sery\db\pdo\Pdo; class Mysql extends Pdo { + function getDbname(): ?string { + $url = $this->dbconn["name"] ?? null; + if ($url !== null && preg_match('/^mysql(?::|.*;)dbname=([^;]+)/i', $url, $ms)) { + return $ms[1]; + } + return null; + } } diff --git a/src/db/mysql/MysqlStorage.php b/src/db/mysql/MysqlStorage.php index 7b0e2c1..aa0a941 100644 --- a/src/db/mysql/MysqlStorage.php +++ b/src/db/mysql/MysqlStorage.php @@ -36,10 +36,11 @@ class MysqlStorage extends CapacitorStorage { } function _exists(CapacitorChannel $channel): bool { - $tableName = $this->mysql->get([ + $mysql = $this->mysql; + $tableName = $mysql->get([ "select table_name from information_schema.tables", "where" => [ - #"table_schema" => database_name #XXX + "table_schema" => $mysql->getDbname(), "table_name" => $channel->getTableName(), ], ]); diff --git a/src/db/pdo/_query_base.php b/src/db/pdo/_query_base.php index ffc3bca..a33800d 100644 --- a/src/db/pdo/_query_base.php +++ b/src/db/pdo/_query_base.php @@ -71,13 +71,15 @@ class _query_base extends _base { } elseif (is_string($value)) { if (self::is_sqldate($value)) { # déjà dans le bon format - } elseif (Date::isa_date($value)) { + } elseif (Date::isa_date($value, true)) { $value = new Date($value); $value = $value->format('Y-m-d'); - } elseif (DateTime::isa_datetime($value)) { + } elseif (DateTime::isa_datetime($value, true)) { $value = new DateTime($value); $value = $value->format('Y-m-d H:i:s'); } + } elseif (is_bool($value)) { + $value = $value? 1: 0; } } diff --git a/src/file/Stream.php b/src/file/Stream.php index c8a4c16..ce02919 100644 --- a/src/file/Stream.php +++ b/src/file/Stream.php @@ -159,8 +159,8 @@ class Stream extends AbstractIterator implements IReader, IWriter { } else { # il faut déterminer le type de fichier CSV en lisant la première ligne $pos = IOException::ensure_valid(ftell($fd)); - $line = IOException::ensure_valid(fgets($fd)); - $line = strpbrk($line, ",;\t"); + $line = fgets($fd); + if ($line !== false) $line = strpbrk($line, ",;\t"); if ($line === false) { # aucun séparateur trouvé, prender la valeur par défaut $flavour = static::DEFAULT_CSV_FLAVOUR; @@ -308,11 +308,22 @@ class Stream extends AbstractIterator implements IReader, IWriter { } } - protected function _teardown(): void { + private function _rewindFd(): void { $md = stream_get_meta_data($this->fd); if ($md["seekable"]) $this->fseek(0); } + protected function _teardown(): void { + $this->_rewindFd(); + } + + function rewind(): void { + # il faut toujours faire un rewind sur la resource, que l'itérateur aie été + # initialisé ou non + if ($this->_hasIteratorBeenSetup()) parent::rewind(); + else $this->_rewindFd(); + } + ############################################################################# # Writer diff --git a/src/php/iter/AbstractIterator.php b/src/php/iter/AbstractIterator.php index b74e24e..c3392e6 100644 --- a/src/php/iter/AbstractIterator.php +++ b/src/php/iter/AbstractIterator.php @@ -75,6 +75,11 @@ abstract class AbstractIterator implements Iterator, ICloseable { # Implémentation par défaut private $setup = false; + + protected function _hasIteratorBeenSetup(): bool { + return $this->setup; + } + private $valid = false; private $toredown = true; diff --git a/src/php/time/DateTime.php b/src/php/time/DateTime.php index e8f27ba..9a1b2a5 100644 --- a/src/php/time/DateTime.php +++ b/src/php/time/DateTime.php @@ -43,18 +43,18 @@ class DateTime extends \DateTime { preg_match(self::YMDHISZ_PATTERN, $datetime); } - static function isa_datetime($datetime): bool { + static function isa_datetime($datetime, bool $frOnly=false): bool { if ($datetime instanceof DateTimeInterface) return true; if (!is_int($datetime) && !is_string($datetime)) return false; return preg_match(self::DMYHIS_PATTERN, $datetime) || - preg_match(self::YMDHISZ_PATTERN, $datetime); + (!$frOnly && preg_match(self::YMDHISZ_PATTERN, $datetime)); } - static function isa_date($datetime): bool { + static function isa_date($datetime, bool $frOnly=false): bool { if ($datetime instanceof DateTimeInterface) return true; if (!is_int($datetime) && !is_string($datetime)) return false; return preg_match(self::DMY_PATTERN, $datetime) || - preg_match(self::YMD_PATTERN, $datetime); + (!$frOnly && preg_match(self::YMD_PATTERN, $datetime)); } static function _YmdHMSZ_format(\DateTime $datetime): string {