]> git.wh0rd.org - tt-rss.git/blame - classes/db/pdo.php
Fixing bugs found by static analysis
[tt-rss.git] / classes / db / pdo.php
CommitLineData
e441b583
AD
1<?php
2class Db_PDO implements IDb {
3 private $pdo;
4
5 function connect($host, $user, $pass, $db, $port) {
73663db3
AD
6 $connstr = DB_TYPE . ":host=$host;dbname=$db";
7
8 if (DB_TYPE == "mysql") $connstr .= ";charset=utf8";
e441b583
AD
9
10 try {
11 $this->pdo = new PDO($connstr, $user, $pass);
73663db3
AD
12 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
13 $this->init();
e441b583
AD
14 } catch (PDOException $e) {
15 die($e->getMessage());
16 }
17
18 return $this->pdo;
19 }
20
21 function escape_string($s, $strip_tags = true) {
22 if ($strip_tags) $s = strip_tags($s);
23
24 $qs = $this->pdo->quote($s);
25
26 return mb_substr($qs, 1, mb_strlen($qs)-2);
27 }
28
29 function query($query, $die_on_error = true) {
30 try {
9ee90455 31 return new Db_Stmt($this->pdo->query($query));
e441b583
AD
32 } catch (PDOException $e) {
33 user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING);
34 }
35 }
36
37 function fetch_assoc($result) {
38 try {
39 if ($result) {
40 return $result->fetch();
41 } else {
42 return null;
43 }
44 } catch (PDOException $e) {
45 user_error($e->getMessage(), E_USER_WARNING);
46 }
47 }
48
49 function num_rows($result) {
50 try {
51 if ($result) {
52 return $result->rowCount();
53 } else {
54 return false;
55 }
56 } catch (PDOException $e) {
57 user_error($e->getMessage(), E_USER_WARNING);
58 }
59 }
60
61 function fetch_result($result, $row, $param) {
9ee90455 62 return $result->fetch_result($row, $param);
e441b583
AD
63 }
64
65 function close() {
66 $this->pdo = null;
67 }
68
69 function affected_rows($result) {
70 try {
71 if ($result) {
72 return $result->rowCount();
73 } else {
74 return null;
75 }
76 } catch (PDOException $e) {
77 user_error($e->getMessage(), E_USER_WARNING);
78 }
79 }
80
81 function last_error() {
6f7798b6 82 return join(" ", $this->pdo->errorInfo());
e441b583 83 }
73663db3
AD
84
85 function init() {
86 switch (DB_TYPE) {
87 case "pgsql":
88 $this->query("set client_encoding = 'UTF-8'");
89 $this->query("set datestyle = 'ISO, european'");
90 $this->query("set TIME ZONE 0");
91 case "mysql":
92 $this->query("SET time_zone = '+0:0'");
93 return;
94 }
95
96 return true;
97 }
98
e441b583
AD
99}
100?>