From 9ee90455b85350e9f9cd04faf36bad8742141cd9 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 18 Apr 2013 08:20:45 +0400 Subject: [PATCH] add experimental support for PDO (_ENABLE_PDO) --- classes/db.php | 2 +- classes/db/pdo.php | 10 ++-------- classes/db/stmt.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 classes/db/stmt.php diff --git a/classes/db.php b/classes/db.php index 86d2ab897..f31a2a9a3 100644 --- a/classes/db.php +++ b/classes/db.php @@ -8,7 +8,7 @@ class Db implements IDb { $er = error_reporting(E_ALL); - if (class_exists("PDO")) { + if (defined('_ENABLE_PDO') && _ENABLE_PDO && class_exists("PDO")) { $this->adapter = new Db_PDO(); } else { switch (DB_TYPE) { diff --git a/classes/db/pdo.php b/classes/db/pdo.php index aaac892c6..3020dea88 100644 --- a/classes/db/pdo.php +++ b/classes/db/pdo.php @@ -24,7 +24,7 @@ class Db_PDO implements IDb { function query($query, $die_on_error = true) { try { - return $this->pdo->query($query); + return new Db_Stmt($this->pdo->query($query)); } catch (PDOException $e) { user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING); } @@ -55,13 +55,7 @@ class Db_PDO implements IDb { } function fetch_result($result, $row, $param) { - $line = $this->fetch_assoc($result); - - if ($line) - return $line[$param]; - else - return null; - + return $result->fetch_result($row, $param); } function close() { diff --git a/classes/db/stmt.php b/classes/db/stmt.php new file mode 100644 index 000000000..4d3596ef1 --- /dev/null +++ b/classes/db/stmt.php @@ -0,0 +1,32 @@ +stmt = $stmt; + $this->cache = false; + } + + function fetch_result($row, $param) { + if (!$this->cache) { + $this->cache = $this->stmt->fetchAll(); + } + + if (isset($this->cache[$row])) { + return $this->cache[$row][$param]; + } else { + user_error("Unable to jump to row $row", E_USER_WARNING); + return false; + } + } + + function rowCount() { + return $this->stmt->rowCount(); + } + + function fetch() { + return $this->stmt->fetch(); + } +} +?>