From 55ea8cdc81e450166db99098d5eefaa70c4b1858 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 28 May 2024 09:36:53 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/db/sqlite/Sqlite.php | 30 ++++++++---- src/db/sqlite/SqliteStorage.php | 6 +-- src/wip/db/pdo/Pdo.php | 81 +++++++++++++++++++++++++++++++++ src/wip/db/pdo/_config.php | 36 +++++++++++++++ 4 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 src/wip/db/pdo/Pdo.php create mode 100644 src/wip/db/pdo/_config.php diff --git a/src/db/sqlite/Sqlite.php b/src/db/sqlite/Sqlite.php index 4c3cbc5..20ab230 100644 --- a/src/db/sqlite/Sqlite.php +++ b/src/db/sqlite/Sqlite.php @@ -50,7 +50,7 @@ class Sqlite { const MIGRATE = null; - const SCHEMA = [ + const params_SCHEMA = [ "file" => ["string", ""], "flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE], "encryption_key" => ["string", ""], @@ -63,14 +63,14 @@ class Sqlite { function __construct(?string $file=null, ?array $params=null) { if ($file !== null) $params["file"] = $file; ##schéma - $defaultFile = self::SCHEMA["file"][1]; + $defaultFile = self::params_SCHEMA["file"][1]; $this->file = $file = strval($params["file"] ?? $defaultFile); $inMemory = $file === ":memory:"; # - $defaultFlags = self::SCHEMA["flags"][1]; + $defaultFlags = self::params_SCHEMA["flags"][1]; $this->flags = intval($params["flags"] ?? $defaultFlags); # - $defaultEncryptionKey = self::SCHEMA["encryption_key"][1]; + $defaultEncryptionKey = self::params_SCHEMA["encryption_key"][1]; $this->encryptionKey = strval($params["encryption_key"] ?? $defaultEncryptionKey); # $defaultAllowWal = static::ALLOW_WAL ?? !$inMemory; @@ -80,7 +80,7 @@ class Sqlite { # migrations $this->migration = $params["migrate"] ?? static::MIGRATE; # - $defaultAutoOpen = self::SCHEMA["auto_open"][1]; + $defaultAutoOpen = self::params_SCHEMA["auto_open"][1]; if ($params["auto_open"] ?? $defaultAutoOpen) { $this->open(); } @@ -201,10 +201,16 @@ class Sqlite { return $this->get($query, $params, true); } - protected function _fetchResult(SQLite3Result $result, ?SQLite3Stmt $stmt=null): Generator { + protected function _fetchResult(SQLite3Result $result, ?SQLite3Stmt $stmt=null, $primaryKeys=null): Generator { + if ($primaryKeys !== null) $primaryKeys = cl::with($primaryKeys); try { while (($row = $result->fetchArray(SQLITE3_ASSOC)) !== false) { - yield $row; + if ($primaryKeys !== null) { + $key = implode("-", cl::select($row, $primaryKeys)); + yield $key => $row; + } else { + yield $row; + } } } finally { $result->finalize(); @@ -212,15 +218,19 @@ class Sqlite { } } - function all($query, ?array $params=null): iterable { + /** + * si $primaryKeys est fourni, le résultat est indexé sur la(es) colonne(s) + * spécifiée(s) + */ + function all($query, ?array $params=null, $primaryKeys=null): iterable { $db = $this->db(); $query = new _query($query, $params); if ($query->useStmt($db, $stmt, $sql)) { $result = $this->checkResult($stmt->execute()); - return $this->_fetchResult($result, $stmt); + return $this->_fetchResult($result, $stmt, $primaryKeys); } else { $result = $this->checkResult($db->query($sql)); - return $this->_fetchResult($result); + return $this->_fetchResult($result, null, $primaryKeys); } } } diff --git a/src/db/sqlite/SqliteStorage.php b/src/db/sqlite/SqliteStorage.php index 648d699..1049001 100644 --- a/src/db/sqlite/SqliteStorage.php +++ b/src/db/sqlite/SqliteStorage.php @@ -250,9 +250,9 @@ class SqliteStorage extends CapacitorStorage { "select", "from" => $channel->getTableName(), "where" => $filter, - ]); - foreach ($rows as $row) { - yield $this->unserialize($channel, $row); + ], null, "id_"); + foreach ($rows as $key => $row) { + yield $key => $this->unserialize($channel, $row); } } diff --git a/src/wip/db/pdo/Pdo.php b/src/wip/db/pdo/Pdo.php new file mode 100644 index 0000000..d122841 --- /dev/null +++ b/src/wip/db/pdo/Pdo.php @@ -0,0 +1,81 @@ +db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $pdo->db->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER); + } + + const OPTIONS = [ + \PDO::ATTR_PERSISTENT => true, + ]; + + const CONFIG = [ + [self::class, "config_errmodeException_lowerCase"], + ]; + + const MIGRATE = null; + + const dbconn_SCHEMA = [ + "name" => "string", + "user" => "?string", + "pass" => "?string", + ]; + + const params_SCHEMA = [ + "options" => ["?array"], + "config" => ["?array|callable"], + "migrate" => ["?array|string|callable"], + "auto_open" => ["bool", true], + ]; + + function __construct($dbconn, ?array $params=null) { + if (!is_array($dbconn)) $dbconn = ["name" => $dbconn]; + $this->dbname = $dbconn["name"]; + $this->dbuser = $dbconn["user"] ?? null; + $this->dbpass = $dbconn["pass"] ?? null; + # options + $this->options = cl::with($params["options"] ?? static::OPTIONS); + # configuration + $this->config = $params["config"] ?? static::CONFIG; + # migrations + $this->migration = $params["migrate"] ?? static::MIGRATE; + # + $defaultAutoOpen = self::params_SCHEMA["auto_open"][1]; + if ($params["auto_open"] ?? $defaultAutoOpen) { + $this->open(); + } + } + + protected ?string $dbconn; + + protected ?string $dbuser; + + protected ?string $dbpass; + + protected array $options; + + /** @var array|string|callable */ + protected $config; + + /** @var array|string|callable */ + protected $migration; + + protected ?\PDO $db; + + function open(): self { + if ($this->db === null) { + $this->db = new \PDO($this->dbname, $this->dbuser, $this->dbpass, $this->options); + _config::with($this->config)->configure($this); + _migration::with($this->migration)->migrate($this); + } + return $this; + } + + function close(): void { + $this->db = null; + } +} diff --git a/src/wip/db/pdo/_config.php b/src/wip/db/pdo/_config.php new file mode 100644 index 0000000..3a92fb4 --- /dev/null +++ b/src/wip/db/pdo/_config.php @@ -0,0 +1,36 @@ +configs = $configs; + } + + /** @var array */ + protected $configs; + + function configure(Pdo $pdo): void { + foreach ($this->configs as $key => $config) { + if (is_string($config) && !func::is_method($config)) { + $pdo->exec($config); + } else { + func::ensure_func($config, $this, $args); + func::call($config, $pdo, $key, ...$args); + } + } + } +}