modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-20 08:19:28 +04:00
parent b6a3011fef
commit ff6c5e8da4
6 changed files with 36 additions and 11 deletions

View File

@ -280,11 +280,15 @@ EOT;
# aucune modification # aucune modification
return 0; return 0;
} elseif ($insert) { } elseif ($insert) {
$this->db()->exec([ $id = $this->db()->exec([
"insert", "insert",
"into" => $tableName, "into" => $tableName,
"values" => $row, "values" => $row,
]); ]);
if (count($primaryKeys) == 1 && $rowIds === null) {
# mettre à jour avec l'id généré
$values[$primaryKeys[0]] = $id;
}
} else { } else {
$this->db()->exec([ $this->db()->exec([
"update", "update",

View File

@ -8,7 +8,13 @@ interface IDatabase {
function rollback(): void; 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 exec($query, ?array $params=null);
function get($query, ?array $params=null, bool $entireRow=false); function get($query, ?array $params=null, bool $entireRow=false);

View File

@ -255,4 +255,8 @@ abstract class _base {
/** @var ?array */ /** @var ?array */
protected $meta; protected $meta;
function isInsert(): bool {
return ($this->meta["isa"] ?? null) === "insert";
}
} }

View File

@ -125,7 +125,11 @@ class Pdo implements IDatabase {
return $this->db()->exec($query); 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) { function exec($query, ?array $params=null) {
$db = $this->db(); $db = $this->db();
$query = new _query_base($query, $params); $query = new _query_base($query, $params);
@ -134,7 +138,9 @@ class Pdo implements IDatabase {
if ($query->isInsert()) return $db->lastInsertId(); if ($query->isInsert()) return $db->lastInsertId();
else return $stmt->rowCount(); else return $stmt->rowCount();
} else { } else {
return $db->exec($sql); $rowCount = $db->exec($sql);
if (self::is_insert($sql)) return $db->lastInsertId();
else return $rowCount;
} }
} }

View File

@ -95,8 +95,4 @@ class _query_base extends _base {
return false; return false;
} }
} }
function isInsert(): bool {
return ($this->meta["isa"] ?? null) === "insert";
}
} }

View File

@ -146,7 +146,11 @@ class Sqlite implements IDatabase {
return $this->db()->exec($query); 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) { function exec($query, ?array $params=null) {
$db = $this->db(); $db = $this->db();
$query = new _query_base($query, $params); $query = new _query_base($query, $params);
@ -154,12 +158,17 @@ class Sqlite implements IDatabase {
try { try {
$result = $stmt->execute(); $result = $stmt->execute();
if ($result === false) return false; if ($result === false) return false;
return $result->finalize(); $result->finalize();
if ($query->isInsert()) return $db->lastInsertRowID();
else return $db->changes();
} finally { } finally {
$stmt->close(); $stmt->close();
} }
} else { } 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();
} }
} }