corriger ensure pour utiliser query

This commit is contained in:
Jephté Clain 2025-08-20 10:37:32 +04:00
parent 7587572f2c
commit 368de874b4
3 changed files with 74 additions and 33 deletions

View File

@ -134,17 +134,6 @@ class Pdo implements IDatabase {
return $this; 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 { function close(): void {
$this->db = null; $this->db = null;
} }
@ -159,6 +148,30 @@ class Pdo implements IDatabase {
return $this->db()->exec($query); 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) { function exec($query, ?array $params=null) {
$db = $this->db(); $db = $this->db();
$query = new _pdoQuery($query, $params); $query = new _pdoQuery($query, $params);

View File

@ -173,17 +173,6 @@ class Pgsql implements IDatabase {
return $this; 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 { function close(): self {
if ($this->db !== null) { if ($this->db !== null) {
pg_close($this->db); pg_close($this->db);
@ -204,6 +193,31 @@ class Pgsql implements IDatabase {
return true; 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() { function getLastSerial() {
$db = $this->db(); $db = $this->db();
$result = @pg_query($db, "select lastval()"); $result = @pg_query($db, "select lastval()");

View File

@ -167,17 +167,6 @@ class Sqlite implements IDatabase {
return $this; 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 { function close(): void {
if ($this->db !== null) { if ($this->db !== null) {
$this->db->close(); $this->db->close();
@ -203,6 +192,31 @@ class Sqlite implements IDatabase {
return $this->db()->exec($query); 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) { function exec($query, ?array $params=null) {
$db = $this->db(); $db = $this->db();
$query = new _sqliteQuery($query, $params); $query = new _sqliteQuery($query, $params);