par défaut, vérifier la connexion avant chaque transaction

This commit is contained in:
Jephté Clain 2025-10-05 20:52:32 +04:00
parent f52da16f44
commit ff4ef34037
5 changed files with 45 additions and 15 deletions

View File

@ -17,7 +17,7 @@ abstract class CapacitorStorage {
abstract function db(): IDatabase; abstract function db(): IDatabase;
function ensureLive(): self { function ensureLive(): self {
$this->db()->ensure(); $this->db()->ensureLive();
return $this; return $this;
} }

View File

@ -17,7 +17,7 @@ interface IDatabase extends ITransactor {
* transactions en cours sont perdues. cette méthode est donc prévue pour * transactions en cours sont perdues. cette méthode est donc prévue pour
* vérifier la validité de la connexion avant de lancer une transaction * vérifier la validité de la connexion avant de lancer une transaction
*/ */
function ensure(): self; function ensureLive(): self;
/** /**
* - si c'est un insert, retourner l'identifiant autogénéré de la ligne * - si c'est un insert, retourner l'identifiant autogénéré de la ligne

View File

@ -28,6 +28,7 @@ class Pdo implements IDatabase {
"options" => $pdo->options, "options" => $pdo->options,
"config" => $pdo->config, "config" => $pdo->config,
"migration" => $pdo->migration, "migration" => $pdo->migration,
"autocheck" => $pdo->autocheck,
], $params)); ], $params));
} else { } else {
return new static($pdo, $params); return new static($pdo, $params);
@ -52,6 +53,10 @@ class Pdo implements IDatabase {
protected const MIGRATION = null; protected const MIGRATION = null;
protected const AUTOCHECK = true;
protected const AUTOOPEN = true;
const dbconn_SCHEMA = [ const dbconn_SCHEMA = [
"name" => "string", "name" => "string",
"user" => "?string", "user" => "?string",
@ -64,7 +69,8 @@ class Pdo implements IDatabase {
"replace_config" => ["?array|callable"], "replace_config" => ["?array|callable"],
"config" => ["?array|callable"], "config" => ["?array|callable"],
"migration" => ["?array|string|callable"], "migration" => ["?array|string|callable"],
"auto_open" => ["bool", true], "autocheck" => ["bool", self::AUTOCHECK],
"autoopen" => ["bool", self::AUTOOPEN],
]; ];
function __construct($dbconn=null, ?array $params=null) { function __construct($dbconn=null, ?array $params=null) {
@ -96,8 +102,8 @@ class Pdo implements IDatabase {
# migrations # migrations
$this->migration = $params["migration"] ?? static::MIGRATION; $this->migration = $params["migration"] ?? static::MIGRATION;
# #
$defaultAutoOpen = self::params_SCHEMA["auto_open"][1]; $this->autocheck = $params["autocheck"] ?? static::AUTOCHECK;
if ($params["auto_open"] ?? $defaultAutoOpen) { if ($params["autoopen"] ?? static::AUTOOPEN) {
$this->open(); $this->open();
} }
} }
@ -113,6 +119,8 @@ class Pdo implements IDatabase {
/** @var array|string|callable */ /** @var array|string|callable */
protected $migration; protected $migration;
protected bool $autocheck;
protected ?\PDO $db = null; protected ?\PDO $db = null;
function getSql($query, ?array $params=null): string { function getSql($query, ?array $params=null): string {
@ -163,7 +171,7 @@ class Pdo implements IDatabase {
const SQL_CHECK_LIVE = "select 1"; const SQL_CHECK_LIVE = "select 1";
function ensure(): self { function ensureLive(): self {
try { try {
$this->_query(static::SQL_CHECK_LIVE); $this->_query(static::SQL_CHECK_LIVE);
} catch (\PDOException $e) { } catch (\PDOException $e) {
@ -206,6 +214,9 @@ class Pdo implements IDatabase {
} }
function beginTransaction(?callable $func=null, bool $commit=true): void { function beginTransaction(?callable $func=null, bool $commit=true): void {
# s'assurer que la connexion à la BDD est active avant de commencer une
# transaction
if ($this->autocheck) $this->ensureLive();
$this->db()->beginTransaction(); $this->db()->beginTransaction();
if ($this->transactors !== null) { if ($this->transactors !== null) {
foreach ($this->transactors as $transactor) { foreach ($this->transactors as $transactor) {

View File

@ -34,7 +34,6 @@ class Pgsql implements IDatabase {
} }
} }
protected const OPTIONS = [ protected const OPTIONS = [
# XXX désactiver les connexions persistantes par défaut # XXX désactiver les connexions persistantes par défaut
# pour réactiver par défaut, il faudrait vérifier la connexion à chaque fois # pour réactiver par défaut, il faudrait vérifier la connexion à chaque fois
@ -49,13 +48,18 @@ class Pgsql implements IDatabase {
const MIGRATION = null; const MIGRATION = null;
protected const AUTOCHECK = true;
protected const AUTOOPEN = true;
const params_SCHEMA = [ const params_SCHEMA = [
"dbconn" => ["array"], "dbconn" => ["array"],
"options" => ["?array|callable"], "options" => ["?array|callable"],
"replace_config" => ["?array|callable"], "replace_config" => ["?array|callable"],
"config" => ["?array|callable"], "config" => ["?array|callable"],
"migration" => ["?array|string|callable"], "migration" => ["?array|string|callable"],
"auto_open" => ["bool", true], "autocheck" => ["bool", self::AUTOCHECK],
"autoopen" => ["bool", self::AUTOOPEN],
]; ];
const dbconn_SCHEMA = [ const dbconn_SCHEMA = [
@ -113,8 +117,8 @@ class Pgsql implements IDatabase {
# migrations # migrations
$this->migration = $params["migration"] ?? static::MIGRATION; $this->migration = $params["migration"] ?? static::MIGRATION;
# #
$defaultAutoOpen = self::params_SCHEMA["auto_open"][1]; $this->autocheck = $params["autocheck"] ?? static::AUTOCHECK;
if ($params["auto_open"] ?? $defaultAutoOpen) { if ($params["autoopen"] ?? static::AUTOOPEN) {
$this->open(); $this->open();
} }
} }
@ -130,6 +134,8 @@ class Pgsql implements IDatabase {
/** @var array|string|callable */ /** @var array|string|callable */
protected $migration; protected $migration;
protected bool $autocheck;
/** @var resource */ /** @var resource */
protected $db = null; protected $db = null;
@ -209,7 +215,7 @@ class Pgsql implements IDatabase {
const SQL_CHECK_LIVE = "select 1"; const SQL_CHECK_LIVE = "select 1";
function ensure(): self { function ensureLive(): self {
try { try {
$this->_query(static::SQL_CHECK_LIVE); $this->_query(static::SQL_CHECK_LIVE);
} catch (\PDOException $e) { } catch (\PDOException $e) {
@ -267,6 +273,9 @@ class Pgsql implements IDatabase {
} }
function beginTransaction(?callable $func=null, bool $commit=true): void { function beginTransaction(?callable $func=null, bool $commit=true): void {
# s'assurer que la connexion à la BDD est active avant de commencer une
# transaction
if ($this->autocheck) $this->ensureLive();
$this->_exec("begin"); $this->_exec("begin");
if ($this->transactors !== null) { if ($this->transactors !== null) {
foreach ($this->transactors as $transactor) { foreach ($this->transactors as $transactor) {

View File

@ -80,6 +80,10 @@ class Sqlite implements IDatabase {
const MIGRATION = null; const MIGRATION = null;
protected const AUTOCHECK = true;
protected const AUTOOPEN = true;
const params_SCHEMA = [ const params_SCHEMA = [
"file" => ["string", ""], "file" => ["string", ""],
"flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE], "flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE],
@ -88,7 +92,8 @@ class Sqlite implements IDatabase {
"replace_config" => ["?array|callable"], "replace_config" => ["?array|callable"],
"config" => ["?array|callable"], "config" => ["?array|callable"],
"migration" => ["?array|string|callable"], "migration" => ["?array|string|callable"],
"auto_open" => ["bool", true], "autocheck" => ["bool", self::AUTOCHECK],
"autoopen" => ["bool", self::AUTOOPEN],
]; ];
function __construct(?string $file=null, ?array $params=null) { function __construct(?string $file=null, ?array $params=null) {
@ -117,9 +122,9 @@ class Sqlite implements IDatabase {
# migrations # migrations
$this->migration = $params["migration"] ?? static::MIGRATION; $this->migration = $params["migration"] ?? static::MIGRATION;
# #
$defaultAutoOpen = self::params_SCHEMA["auto_open"][1];
$this->inTransaction = false; $this->inTransaction = false;
if ($params["auto_open"] ?? $defaultAutoOpen) { $this->autocheck = $params["autocheck"] ?? static::AUTOCHECK;
if ($params["autoopen"] ?? static::AUTOOPEN) {
$this->open(); $this->open();
} }
} }
@ -147,6 +152,8 @@ class Sqlite implements IDatabase {
/** @var array|string|callable */ /** @var array|string|callable */
protected $migration; protected $migration;
protected bool $autocheck;
/** @var SQLite3 */ /** @var SQLite3 */
protected $db; protected $db;
@ -208,7 +215,7 @@ class Sqlite implements IDatabase {
const SQL_CHECK_LIVE = "select 1"; const SQL_CHECK_LIVE = "select 1";
function ensure(): self { function ensureLive(): self {
try { try {
$this->_query(static::SQL_CHECK_LIVE); $this->_query(static::SQL_CHECK_LIVE);
} catch (\PDOException $e) { } catch (\PDOException $e) {
@ -259,6 +266,9 @@ class Sqlite implements IDatabase {
} }
function beginTransaction(?callable $func=null, bool $commit=true): void { function beginTransaction(?callable $func=null, bool $commit=true): void {
# s'assurer que la connexion à la BDD est active avant de commencer une
# transaction
if ($this->autocheck) $this->ensureLive();
$this->db()->exec("begin"); $this->db()->exec("begin");
$this->inTransaction = true; $this->inTransaction = true;
if ($this->transactors !== null) { if ($this->transactors !== null) {