From 368de874b425911eb36ac17c3265f36cfe87e34e Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 20 Aug 2025 10:37:32 +0400 Subject: [PATCH] corriger ensure pour utiliser query --- php/src/db/pdo/Pdo.php | 35 ++++++++++++++++++++++++----------- php/src/db/pgsql/Pgsql.php | 36 +++++++++++++++++++++++++----------- php/src/db/sqlite/Sqlite.php | 36 +++++++++++++++++++++++++----------- 3 files changed, 74 insertions(+), 33 deletions(-) diff --git a/php/src/db/pdo/Pdo.php b/php/src/db/pdo/Pdo.php index fc324dc..10da1c6 100644 --- a/php/src/db/pdo/Pdo.php +++ b/php/src/db/pdo/Pdo.php @@ -134,17 +134,6 @@ class Pdo implements IDatabase { return $this; } - const SQL_CHECK_LIVE = "select 1"; - - function ensure(): self { - try { - $this->_exec(static::SQL_CHECK_LIVE); - } catch (\PDOException $e) { - $this->open(true); - } - return $this; - } - function close(): void { $this->db = null; } @@ -159,6 +148,30 @@ class Pdo implements IDatabase { return $this->db()->exec($query); } + /** @return array|null */ + function _query(string $query) { + $db = $this->db(); + /** @var \PDOStatement $stmt */ + $stmt = $db->query($query); + if ($stmt === false) return null; + try { + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + } finally { + $stmt->closeCursor(); + } + } + + const SQL_CHECK_LIVE = "select 1"; + + function ensure(): self { + try { + $this->_query(static::SQL_CHECK_LIVE); + } catch (\PDOException $e) { + $this->open(true); + } + return $this; + } + function exec($query, ?array $params=null) { $db = $this->db(); $query = new _pdoQuery($query, $params); diff --git a/php/src/db/pgsql/Pgsql.php b/php/src/db/pgsql/Pgsql.php index deeebac..ca9b7e9 100644 --- a/php/src/db/pgsql/Pgsql.php +++ b/php/src/db/pgsql/Pgsql.php @@ -173,17 +173,6 @@ class Pgsql implements IDatabase { return $this; } - const SQL_CHECK_LIVE = "select 1"; - - function ensure(): self { - try { - $this->_exec(static::SQL_CHECK_LIVE); - } catch (\PDOException $e) { - $this->open(true); - } - return $this; - } - function close(): self { if ($this->db !== null) { pg_close($this->db); @@ -204,6 +193,31 @@ class Pgsql implements IDatabase { return true; } + function _query(string $query): ?array { + $result = pg_query($this->db(), $query); + if ($result === false) return null; + try { + $rows = []; + while (($row = pg_fetch_assoc($result)) !== false) { + $rows[] = $row; + } + return $rows; + } finally { + pg_free_result($result); + } + } + + const SQL_CHECK_LIVE = "select 1"; + + function ensure(): self { + try { + $this->_query(static::SQL_CHECK_LIVE); + } catch (\PDOException $e) { + $this->open(true); + } + return $this; + } + function getLastSerial() { $db = $this->db(); $result = @pg_query($db, "select lastval()"); diff --git a/php/src/db/sqlite/Sqlite.php b/php/src/db/sqlite/Sqlite.php index ece4379..1d52f2c 100644 --- a/php/src/db/sqlite/Sqlite.php +++ b/php/src/db/sqlite/Sqlite.php @@ -167,17 +167,6 @@ class Sqlite implements IDatabase { return $this; } - const SQL_CHECK_LIVE = "select 1"; - - function ensure(): self { - try { - $this->_exec(static::SQL_CHECK_LIVE); - } catch (\PDOException $e) { - $this->open(true); - } - return $this; - } - function close(): void { if ($this->db !== null) { $this->db->close(); @@ -203,6 +192,31 @@ class Sqlite implements IDatabase { return $this->db()->exec($query); } + function _query(string $query): ?array { + $result = $this->db()->query($query); + if ($result === false) return null; + try { + $rows = []; + while (($row = $result->fetchArray(SQLITE3_ASSOC)) !== false) { + $rows[] = $row; + } + return $rows; + } finally { + $result->finalize(); + } + } + + const SQL_CHECK_LIVE = "select 1"; + + function ensure(): self { + try { + $this->_query(static::SQL_CHECK_LIVE); + } catch (\PDOException $e) { + $this->open(true); + } + return $this; + } + function exec($query, ?array $params=null) { $db = $this->db(); $query = new _sqliteQuery($query, $params);