diff --git a/src/db/pdo/Pdo.php b/src/db/pdo/Pdo.php index 393a310..3ce131c 100644 --- a/src/db/pdo/Pdo.php +++ b/src/db/pdo/Pdo.php @@ -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_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, ]; - const CONFIG = [ - [self::class, "config_errmodeException_lowerCase"], + protected const DEFAULT_CONFIG = [ + self::CONFIG_errmodeException_lowerCase, ]; - const MIGRATE = null; + protected const CONFIG = null; + + protected const MIGRATE = null; const dbconn_SCHEMA = [ "name" => "string", @@ -51,6 +59,7 @@ class Pdo implements IDatabase { const params_SCHEMA = [ "dbconn" => ["array"], "options" => ["?array|callable"], + "replace_config" => ["?array|callable"], "config" => ["?array|callable"], "migrate" => ["?array|string|callable"], "auto_open" => ["bool", true], @@ -75,7 +84,13 @@ class Pdo implements IDatabase { # options $this->options = $params["options"] ?? static::OPTIONS; # 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 $this->migration = $params["migrate"] ?? static::MIGRATE; # diff --git a/src/db/sqlite/Sqlite.php b/src/db/sqlite/Sqlite.php index 63bd943..0a24859 100644 --- a/src/db/sqlite/Sqlite.php +++ b/src/db/sqlite/Sqlite.php @@ -38,31 +38,36 @@ class Sqlite implements IDatabase { static function config_enableExceptions(self $sqlite): void { $sqlite->db->enableExceptions(true); } + const CONFIG_enableExceptions = [self::class, "config_enableExceptions"]; /** * @var int temps maximum à attendre que la base soit accessible si elle est * verrouillée */ - const BUSY_TIMEOUT = 30 * 1000; + protected const BUSY_TIMEOUT = 30 * 1000; static function config_busyTimeout(self $sqlite): void { $sqlite->db->busyTimeout(static::BUSY_TIMEOUT); } + const CONFIG_busyTimeout = [self::class, "config_busyTimeout"]; static function config_enableWalIfAllowed(self $sqlite): void { if ($sqlite->isWalAllowed()) { $sqlite->db->exec("PRAGMA journal_mode=WAL"); } } + const CONFIG_enableWalIfAllowed = [self::class, "config_enableWalIfAllowed"]; const ALLOW_WAL = null; - const CONFIG = [ - [self::class, "config_enableExceptions"], - [self::class, "config_busyTimeout"], - [self::class, "config_enableWalIfAllowed"], + const DEFAULT_CONFIG = [ + self::CONFIG_enableExceptions, + self::CONFIG_busyTimeout, + self::CONFIG_enableWalIfAllowed, ]; + const CONFIG = null; + const MIGRATE = null; const params_SCHEMA = [ @@ -70,6 +75,7 @@ class Sqlite implements IDatabase { "flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE], "encryption_key" => ["string", ""], "allow_wal" => ["?bool"], + "replace_config" => ["?array|callable"], "config" => ["?array|callable"], "migrate" => ["?array|string|callable"], "auto_open" => ["bool", true], @@ -91,7 +97,13 @@ class Sqlite implements IDatabase { $defaultAllowWal = static::ALLOW_WAL ?? !$inMemory; $this->allowWal = $params["allow_wal"] ?? $defaultAllowWal; # 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 $this->migration = $params["migrate"] ?? static::MIGRATE; #