]> git.wh0rd.org - tt-rss.git/blobdiff - classes/db/pdo.php
Fixing bugs found by static analysis
[tt-rss.git] / classes / db / pdo.php
index aaac892c61cc3662b96b5274492e70fe5eac85a6..126f5150a75365ff64b2ed82a0cac202114832a8 100644 (file)
@@ -3,10 +3,14 @@ class Db_PDO implements IDb {
        private $pdo;
 
        function connect($host, $user, $pass, $db, $port) {
-               $connstr = DB_TYPE . ":host=$host;dbname=$db;charset=utf8";
+               $connstr = DB_TYPE . ":host=$host;dbname=$db";
+
+               if (DB_TYPE == "mysql") $connstr .= ";charset=utf8";
 
                try {
                        $this->pdo = new PDO($connstr, $user, $pass);
+                       $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+                       $this->init();
                } catch (PDOException $e) {
                        die($e->getMessage());
                }
@@ -24,7 +28,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 +59,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() {
@@ -81,7 +79,22 @@ class Db_PDO implements IDb {
        }
 
        function last_error() {
-               return join(" ", $pdo->errorInfo());
+               return join(" ", $this->pdo->errorInfo());
+       }
+
+       function init() {
+               switch (DB_TYPE) {
+               case "pgsql":
+                       $this->query("set client_encoding = 'UTF-8'");
+                       $this->query("set datestyle = 'ISO, european'");
+                       $this->query("set TIME ZONE 0");
+               case "mysql":
+                       $this->query("SET time_zone = '+0:0'");
+                       return;
+               }
+
+               return true;
        }
+
 }
 ?>