From e4f70984bfaf74310c994c64014c42353f2655b1 Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Sat, 26 Oct 2024 07:24:27 +0700 Subject: [PATCH] Handle $stmt->rowCount() for SQLite --- src/Database/PicoDatabase.php | 36 ++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Database/PicoDatabase.php b/src/Database/PicoDatabase.php index 247b340..5a72fb4 100644 --- a/src/Database/PicoDatabase.php +++ b/src/Database/PicoDatabase.php @@ -313,7 +313,18 @@ public function fetch($sql, $tentativeType = PDO::FETCH_ASSOC, $defaultValue = n try { $stmt->execute($params); - $result = $stmt->rowCount() > 0 ? $stmt->fetch($tentativeType) : $defaultValue; + if($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_SQLITE) + { + $result = $stmt->fetch($tentativeType); + if($result === false) + { + $result = $defaultValue; + } + } + else + { + $result = $stmt->rowCount() > 0 ? $stmt->fetch($tentativeType) : $defaultValue; + } } catch (PDOException $e) { $result = $defaultValue; } @@ -340,7 +351,15 @@ public function isRecordExists($sql, $params = null) try { $stmt->execute($params); - return $stmt->rowCount() > 0; + if($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_SQLITE) + { + $result = $stmt->fetch(); + return $result !== false; + } + else + { + return $stmt->rowCount() > 0; + } } catch (PDOException $e) { throw new PDOException($e->getMessage(), intval($e->getCode())); } @@ -367,7 +386,18 @@ public function fetchAll($sql, $tentativeType = PDO::FETCH_ASSOC, $defaultValue try { $stmt->execute($params); - $result = $stmt->rowCount() > 0 ? $stmt->fetchAll($tentativeType) : $defaultValue; + if($this->getDatabaseType() == PicoDatabaseType::DATABASE_TYPE_SQLITE) + { + $result = $stmt->fetch($tentativeType); + if($result === false) + { + $result = $defaultValue; + } + } + else + { + $result = $stmt->rowCount() > 0 ? $stmt->fetchAll($tentativeType) : $defaultValue; + } } catch (PDOException $e) { $result = $defaultValue; }