par défaut, vérifier la connexion avant chaque transaction
This commit is contained in:
		
							parent
							
								
									f52da16f44
								
							
						
					
					
						commit
						ff4ef34037
					
				| @ -17,7 +17,7 @@ abstract class CapacitorStorage { | ||||
|   abstract function db(): IDatabase; | ||||
| 
 | ||||
|   function ensureLive(): self { | ||||
|     $this->db()->ensure(); | ||||
|     $this->db()->ensureLive(); | ||||
|     return $this; | ||||
|   } | ||||
| 
 | ||||
|  | ||||
| @ -17,7 +17,7 @@ interface IDatabase extends ITransactor { | ||||
|    * 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 | ||||
|    */ | ||||
|   function ensure(): self; | ||||
|   function ensureLive(): self; | ||||
| 
 | ||||
|   /** | ||||
|    * - si c'est un insert, retourner l'identifiant autogénéré de la ligne | ||||
|  | ||||
| @ -28,6 +28,7 @@ class Pdo implements IDatabase { | ||||
|         "options" => $pdo->options, | ||||
|         "config" => $pdo->config, | ||||
|         "migration" => $pdo->migration, | ||||
|         "autocheck" => $pdo->autocheck, | ||||
|       ], $params)); | ||||
|     } else { | ||||
|       return new static($pdo, $params); | ||||
| @ -52,6 +53,10 @@ class Pdo implements IDatabase { | ||||
| 
 | ||||
|   protected const MIGRATION = null; | ||||
| 
 | ||||
|   protected const AUTOCHECK = true; | ||||
| 
 | ||||
|   protected const AUTOOPEN = true; | ||||
| 
 | ||||
|   const dbconn_SCHEMA = [ | ||||
|     "name" => "string", | ||||
|     "user" => "?string", | ||||
| @ -64,7 +69,8 @@ class Pdo implements IDatabase { | ||||
|     "replace_config" => ["?array|callable"], | ||||
|     "config" => ["?array|callable"], | ||||
|     "migration" => ["?array|string|callable"], | ||||
|     "auto_open" => ["bool", true], | ||||
|     "autocheck" => ["bool", self::AUTOCHECK], | ||||
|     "autoopen" => ["bool", self::AUTOOPEN], | ||||
|   ]; | ||||
| 
 | ||||
|   function __construct($dbconn=null, ?array $params=null) { | ||||
| @ -96,8 +102,8 @@ class Pdo implements IDatabase { | ||||
|     # migrations
 | ||||
|     $this->migration = $params["migration"] ?? static::MIGRATION; | ||||
|     #
 | ||||
|     $defaultAutoOpen = self::params_SCHEMA["auto_open"][1]; | ||||
|     if ($params["auto_open"] ?? $defaultAutoOpen) { | ||||
|     $this->autocheck = $params["autocheck"] ?? static::AUTOCHECK; | ||||
|     if ($params["autoopen"] ?? static::AUTOOPEN) { | ||||
|       $this->open(); | ||||
|     } | ||||
|   } | ||||
| @ -113,6 +119,8 @@ class Pdo implements IDatabase { | ||||
|   /** @var array|string|callable */ | ||||
|   protected $migration; | ||||
| 
 | ||||
|   protected bool $autocheck; | ||||
| 
 | ||||
|   protected ?\PDO $db = null; | ||||
| 
 | ||||
|   function getSql($query, ?array $params=null): string { | ||||
| @ -163,7 +171,7 @@ class Pdo implements IDatabase { | ||||
| 
 | ||||
|   const SQL_CHECK_LIVE = "select 1"; | ||||
| 
 | ||||
|   function ensure(): self { | ||||
|   function ensureLive(): self { | ||||
|     try { | ||||
|       $this->_query(static::SQL_CHECK_LIVE); | ||||
|     } catch (\PDOException $e) { | ||||
| @ -206,6 +214,9 @@ class Pdo implements IDatabase { | ||||
|   } | ||||
| 
 | ||||
|   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(); | ||||
|     if ($this->transactors !== null) { | ||||
|       foreach ($this->transactors as $transactor) { | ||||
|  | ||||
| @ -34,7 +34,6 @@ class Pgsql implements IDatabase { | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
| 
 | ||||
|   protected const OPTIONS = [ | ||||
|     # XXX désactiver les connexions persistantes par défaut
 | ||||
|     # 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; | ||||
| 
 | ||||
|   protected const AUTOCHECK = true; | ||||
| 
 | ||||
|   protected const AUTOOPEN = true; | ||||
| 
 | ||||
|   const params_SCHEMA = [ | ||||
|     "dbconn" => ["array"], | ||||
|     "options" => ["?array|callable"], | ||||
|     "replace_config" => ["?array|callable"], | ||||
|     "config" => ["?array|callable"], | ||||
|     "migration" => ["?array|string|callable"], | ||||
|     "auto_open" => ["bool", true], | ||||
|     "autocheck" => ["bool", self::AUTOCHECK], | ||||
|     "autoopen" => ["bool", self::AUTOOPEN], | ||||
|   ]; | ||||
| 
 | ||||
|   const dbconn_SCHEMA = [ | ||||
| @ -113,8 +117,8 @@ class Pgsql implements IDatabase { | ||||
|     # migrations
 | ||||
|     $this->migration = $params["migration"] ?? static::MIGRATION; | ||||
|     #
 | ||||
|     $defaultAutoOpen = self::params_SCHEMA["auto_open"][1]; | ||||
|     if ($params["auto_open"] ?? $defaultAutoOpen) { | ||||
|     $this->autocheck = $params["autocheck"] ?? static::AUTOCHECK; | ||||
|     if ($params["autoopen"] ?? static::AUTOOPEN) { | ||||
|       $this->open(); | ||||
|     } | ||||
|   } | ||||
| @ -130,6 +134,8 @@ class Pgsql implements IDatabase { | ||||
|   /** @var array|string|callable */ | ||||
|   protected $migration; | ||||
| 
 | ||||
|   protected bool $autocheck; | ||||
| 
 | ||||
|   /** @var resource */ | ||||
|   protected $db = null; | ||||
| 
 | ||||
| @ -209,7 +215,7 @@ class Pgsql implements IDatabase { | ||||
| 
 | ||||
|   const SQL_CHECK_LIVE = "select 1"; | ||||
| 
 | ||||
|   function ensure(): self { | ||||
|   function ensureLive(): self { | ||||
|     try { | ||||
|       $this->_query(static::SQL_CHECK_LIVE); | ||||
|     } catch (\PDOException $e) { | ||||
| @ -267,6 +273,9 @@ class Pgsql implements IDatabase { | ||||
|   } | ||||
| 
 | ||||
|   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"); | ||||
|     if ($this->transactors !== null) { | ||||
|       foreach ($this->transactors as $transactor) { | ||||
|  | ||||
| @ -80,6 +80,10 @@ class Sqlite implements IDatabase { | ||||
| 
 | ||||
|   const MIGRATION = null; | ||||
| 
 | ||||
|   protected const AUTOCHECK = true; | ||||
| 
 | ||||
|   protected const AUTOOPEN = true; | ||||
| 
 | ||||
|   const params_SCHEMA = [ | ||||
|     "file" => ["string", ""], | ||||
|     "flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE], | ||||
| @ -88,7 +92,8 @@ class Sqlite implements IDatabase { | ||||
|     "replace_config" => ["?array|callable"], | ||||
|     "config" => ["?array|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) { | ||||
| @ -117,9 +122,9 @@ class Sqlite implements IDatabase { | ||||
|     # migrations
 | ||||
|     $this->migration = $params["migration"] ?? static::MIGRATION; | ||||
|     #
 | ||||
|     $defaultAutoOpen = self::params_SCHEMA["auto_open"][1]; | ||||
|     $this->inTransaction = false; | ||||
|     if ($params["auto_open"] ?? $defaultAutoOpen) { | ||||
|     $this->autocheck = $params["autocheck"] ?? static::AUTOCHECK; | ||||
|     if ($params["autoopen"] ?? static::AUTOOPEN) { | ||||
|       $this->open(); | ||||
|     } | ||||
|   } | ||||
| @ -147,6 +152,8 @@ class Sqlite implements IDatabase { | ||||
|   /** @var array|string|callable */ | ||||
|   protected $migration; | ||||
| 
 | ||||
|   protected bool $autocheck; | ||||
| 
 | ||||
|   /** @var SQLite3 */ | ||||
|   protected $db; | ||||
| 
 | ||||
| @ -208,7 +215,7 @@ class Sqlite implements IDatabase { | ||||
| 
 | ||||
|   const SQL_CHECK_LIVE = "select 1"; | ||||
| 
 | ||||
|   function ensure(): self { | ||||
|   function ensureLive(): self { | ||||
|     try { | ||||
|       $this->_query(static::SQL_CHECK_LIVE); | ||||
|     } catch (\PDOException $e) { | ||||
| @ -259,6 +266,9 @@ class Sqlite implements IDatabase { | ||||
|   } | ||||
| 
 | ||||
|   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->inTransaction = true; | ||||
|     if ($this->transactors !== null) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user