modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-07-12 12:05:15 +04:00
parent abada2401d
commit dbaf9af194
2 changed files with 39 additions and 12 deletions

View File

@ -27,20 +27,28 @@ class Pdo implements IDatabase {
} }
} }
static function config_errmodeException_lowerCase(self $pdo) { static function config_errmodeException_lowerCase(self $pdo): void {
$pdo->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $pdo->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$pdo->db->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER); $pdo->db->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER);
} }
const CONFIG_errmodeException_lowerCase = [self::class, "config_errmodeException_lowerCase"];
const OPTIONS = [ static function config_unbufferedQueries(self $pdo): void {
$pdo->db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
}
const CONFIG_unbufferedQueries = [self::class, "config_unbufferedQueries"];
protected const OPTIONS = [
\PDO::ATTR_PERSISTENT => true, \PDO::ATTR_PERSISTENT => true,
]; ];
const CONFIG = [ protected const DEFAULT_CONFIG = [
[self::class, "config_errmodeException_lowerCase"], self::CONFIG_errmodeException_lowerCase,
]; ];
const MIGRATE = null; protected const CONFIG = null;
protected const MIGRATE = null;
const dbconn_SCHEMA = [ const dbconn_SCHEMA = [
"name" => "string", "name" => "string",
@ -51,6 +59,7 @@ class Pdo implements IDatabase {
const params_SCHEMA = [ const params_SCHEMA = [
"dbconn" => ["array"], "dbconn" => ["array"],
"options" => ["?array|callable"], "options" => ["?array|callable"],
"replace_config" => ["?array|callable"],
"config" => ["?array|callable"], "config" => ["?array|callable"],
"migrate" => ["?array|string|callable"], "migrate" => ["?array|string|callable"],
"auto_open" => ["bool", true], "auto_open" => ["bool", true],
@ -75,7 +84,13 @@ class Pdo implements IDatabase {
# options # options
$this->options = $params["options"] ?? static::OPTIONS; $this->options = $params["options"] ?? static::OPTIONS;
# configuration # configuration
$this->config = $params["config"] ?? static::CONFIG; $config = $params["replace_config"];
if ($config === null) {
$config = $params["config"] ?? static::CONFIG;
if (is_callable($config)) $config = [$config];
$config = cl::merge(static::DEFAULT_CONFIG, $config);
}
$this->config = $config;
# migrations # migrations
$this->migration = $params["migrate"] ?? static::MIGRATE; $this->migration = $params["migrate"] ?? static::MIGRATE;
# #

View File

@ -38,31 +38,36 @@ class Sqlite implements IDatabase {
static function config_enableExceptions(self $sqlite): void { static function config_enableExceptions(self $sqlite): void {
$sqlite->db->enableExceptions(true); $sqlite->db->enableExceptions(true);
} }
const CONFIG_enableExceptions = [self::class, "config_enableExceptions"];
/** /**
* @var int temps maximum à attendre que la base soit accessible si elle est * @var int temps maximum à attendre que la base soit accessible si elle est
* verrouillée * verrouillée
*/ */
const BUSY_TIMEOUT = 30 * 1000; protected const BUSY_TIMEOUT = 30 * 1000;
static function config_busyTimeout(self $sqlite): void { static function config_busyTimeout(self $sqlite): void {
$sqlite->db->busyTimeout(static::BUSY_TIMEOUT); $sqlite->db->busyTimeout(static::BUSY_TIMEOUT);
} }
const CONFIG_busyTimeout = [self::class, "config_busyTimeout"];
static function config_enableWalIfAllowed(self $sqlite): void { static function config_enableWalIfAllowed(self $sqlite): void {
if ($sqlite->isWalAllowed()) { if ($sqlite->isWalAllowed()) {
$sqlite->db->exec("PRAGMA journal_mode=WAL"); $sqlite->db->exec("PRAGMA journal_mode=WAL");
} }
} }
const CONFIG_enableWalIfAllowed = [self::class, "config_enableWalIfAllowed"];
const ALLOW_WAL = null; const ALLOW_WAL = null;
const CONFIG = [ const DEFAULT_CONFIG = [
[self::class, "config_enableExceptions"], self::CONFIG_enableExceptions,
[self::class, "config_busyTimeout"], self::CONFIG_busyTimeout,
[self::class, "config_enableWalIfAllowed"], self::CONFIG_enableWalIfAllowed,
]; ];
const CONFIG = null;
const MIGRATE = null; const MIGRATE = null;
const params_SCHEMA = [ const params_SCHEMA = [
@ -70,6 +75,7 @@ class Sqlite implements IDatabase {
"flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE], "flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE],
"encryption_key" => ["string", ""], "encryption_key" => ["string", ""],
"allow_wal" => ["?bool"], "allow_wal" => ["?bool"],
"replace_config" => ["?array|callable"],
"config" => ["?array|callable"], "config" => ["?array|callable"],
"migrate" => ["?array|string|callable"], "migrate" => ["?array|string|callable"],
"auto_open" => ["bool", true], "auto_open" => ["bool", true],
@ -91,7 +97,13 @@ class Sqlite implements IDatabase {
$defaultAllowWal = static::ALLOW_WAL ?? !$inMemory; $defaultAllowWal = static::ALLOW_WAL ?? !$inMemory;
$this->allowWal = $params["allow_wal"] ?? $defaultAllowWal; $this->allowWal = $params["allow_wal"] ?? $defaultAllowWal;
# configuration # configuration
$this->config = $params["config"] ?? static::CONFIG; $config = $params["replace_config"];
if ($config === null) {
$config = $params["config"] ?? static::CONFIG;
if (is_callable($config)) $config = [$config];
$config = cl::merge(static::DEFAULT_CONFIG, $config);
}
$this->config = $config;
# migrations # migrations
$this->migration = $params["migrate"] ?? static::MIGRATE; $this->migration = $params["migrate"] ?? static::MIGRATE;
# #