From ff6c5e8da489dacbc0596439910972b8b90bb560 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 20 Jun 2024 08:19:28 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/db/CapacitorStorage.php | 6 +++++- src/db/IDatabase.php | 8 +++++++- src/db/_private/_base.php | 4 ++++ src/db/pdo/Pdo.php | 10 ++++++++-- src/db/pdo/_query_base.php | 4 ---- src/db/sqlite/Sqlite.php | 15 ++++++++++++--- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/db/CapacitorStorage.php b/src/db/CapacitorStorage.php index d808656..862f1e1 100644 --- a/src/db/CapacitorStorage.php +++ b/src/db/CapacitorStorage.php @@ -280,11 +280,15 @@ EOT; # aucune modification return 0; } elseif ($insert) { - $this->db()->exec([ + $id = $this->db()->exec([ "insert", "into" => $tableName, "values" => $row, ]); + if (count($primaryKeys) == 1 && $rowIds === null) { + # mettre à jour avec l'id généré + $values[$primaryKeys[0]] = $id; + } } else { $this->db()->exec([ "update", diff --git a/src/db/IDatabase.php b/src/db/IDatabase.php index fc8ea2c..b5a9047 100644 --- a/src/db/IDatabase.php +++ b/src/db/IDatabase.php @@ -8,7 +8,13 @@ interface IDatabase { function rollback(): void; - /** @return int|bool en fonction des implémentations */ + /** + * - si c'est un insert, retourner l'identifiant autogénéré de la ligne + * - sinon retourner le nombre de lignes modifiées en cas de succès, ou false + * en cas d'échec + * + * @return int|false + */ function exec($query, ?array $params=null); function get($query, ?array $params=null, bool $entireRow=false); diff --git a/src/db/_private/_base.php b/src/db/_private/_base.php index a5671ab..e7e828c 100644 --- a/src/db/_private/_base.php +++ b/src/db/_private/_base.php @@ -255,4 +255,8 @@ abstract class _base { /** @var ?array */ protected $meta; + + function isInsert(): bool { + return ($this->meta["isa"] ?? null) === "insert"; + } } diff --git a/src/db/pdo/Pdo.php b/src/db/pdo/Pdo.php index 837290d..b51d4fc 100644 --- a/src/db/pdo/Pdo.php +++ b/src/db/pdo/Pdo.php @@ -125,7 +125,11 @@ class Pdo implements IDatabase { return $this->db()->exec($query); } - /** @return int|false */ + private static function is_insert(?string $sql): bool { + if ($sql === null) return false; + return preg_match('/^\s*insert\b/i', $sql); + } + function exec($query, ?array $params=null) { $db = $this->db(); $query = new _query_base($query, $params); @@ -134,7 +138,9 @@ class Pdo implements IDatabase { if ($query->isInsert()) return $db->lastInsertId(); else return $stmt->rowCount(); } else { - return $db->exec($sql); + $rowCount = $db->exec($sql); + if (self::is_insert($sql)) return $db->lastInsertId(); + else return $rowCount; } } diff --git a/src/db/pdo/_query_base.php b/src/db/pdo/_query_base.php index 7ea2eb1..f339f11 100644 --- a/src/db/pdo/_query_base.php +++ b/src/db/pdo/_query_base.php @@ -95,8 +95,4 @@ class _query_base extends _base { return false; } } - - function isInsert(): bool { - return ($this->meta["isa"] ?? null) === "insert"; - } } diff --git a/src/db/sqlite/Sqlite.php b/src/db/sqlite/Sqlite.php index b77b630..99577f4 100644 --- a/src/db/sqlite/Sqlite.php +++ b/src/db/sqlite/Sqlite.php @@ -146,7 +146,11 @@ class Sqlite implements IDatabase { return $this->db()->exec($query); } - /** @return bool */ + private static function is_insert(?string $sql): bool { + if ($sql === null) return false; + return preg_match('/^\s*insert\b/i', $sql); + } + function exec($query, ?array $params=null) { $db = $this->db(); $query = new _query_base($query, $params); @@ -154,12 +158,17 @@ class Sqlite implements IDatabase { try { $result = $stmt->execute(); if ($result === false) return false; - return $result->finalize(); + $result->finalize(); + if ($query->isInsert()) return $db->lastInsertRowID(); + else return $db->changes(); } finally { $stmt->close(); } } else { - return $db->exec($sql); + $result = $db->exec($sql); + if ($result === false) return false; + if (self::is_insert($sql)) return $db->lastInsertRowID(); + else return $db->changes(); } }