add experimental support for PDO (_ENABLE_PDO)

This commit is contained in:
Andrew Dolgov 2013-04-18 08:20:45 +04:00
parent 7329ab2dd5
commit 9ee90455b8
3 changed files with 35 additions and 9 deletions

View File

@ -8,7 +8,7 @@ class Db implements IDb {
$er = error_reporting(E_ALL); $er = error_reporting(E_ALL);
if (class_exists("PDO")) { if (defined('_ENABLE_PDO') && _ENABLE_PDO && class_exists("PDO")) {
$this->adapter = new Db_PDO(); $this->adapter = new Db_PDO();
} else { } else {
switch (DB_TYPE) { switch (DB_TYPE) {

View File

@ -24,7 +24,7 @@ class Db_PDO implements IDb {
function query($query, $die_on_error = true) { function query($query, $die_on_error = true) {
try { try {
return $this->pdo->query($query); return new Db_Stmt($this->pdo->query($query));
} catch (PDOException $e) { } catch (PDOException $e) {
user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING); 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) { function fetch_result($result, $row, $param) {
$line = $this->fetch_assoc($result); return $result->fetch_result($row, $param);
if ($line)
return $line[$param];
else
return null;
} }
function close() { function close() {

32
classes/db/stmt.php Normal file
View File

@ -0,0 +1,32 @@
<?php
class Db_Stmt {
private $stmt;
private $cache;
function __construct($stmt) {
$this->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();
}
}
?>