Add errorInfor function to database class
[infodrom/hallinta] / class / database.class.php
1 <?php
2
3 class Database {
4   private $db;
5   private $error_log = false;
6   private $error_mail = false;
7   private $lastInsertTable = null;
8
9   public function __construct($driver, $host, $dbname, $user, $passwd)
10   {
11     $this->db = new PDO(sprintf('%s:host=%s;dbname=%s', $driver, $host, $dbname), $user, $passwd);
12
13     #    $this->db->query("SET NAMES 'utf8'"); 
14     #    $this->db->query("SET CHARACTER SET 'utf8'");
15     #    $this->db->query("SET collation_connection = 'utf8_general_ci'");
16     #    $this->db->query("SET lc_time_names = 'de_DE'");
17   }
18
19   public function enableErrorLog()
20   {
21     $this->error_log = true;
22   }
23
24   public function disableErrorLog()
25   {
26     $this->error_log = false;
27   }
28
29   public function setErrorMail($address)
30   {
31     $this->error_mail = $address;
32   }
33
34   private function logQuery($sql)
35   {
36     if ($this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
37   }
38
39   private function handleError($sth, $sql)
40   {
41     if (!$this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
42     $arr = $this->db->errorInfo();
43     foreach (explode("\n", $arr[2]) as $line)
44       error_log($line);
45
46     if ($this->error_mail && defined('MAIL_FROM') && defined('MAIL_FROM_NAME')) {
47       $mail = new Mail();
48       $mail->env_from(MAIL_FROM);
49       $mail->set('From', mb_encode_mimeheader(utf8_decode(sprintf("%s <%s>", MAIL_FROM_NAME, MAIL_FROM)),'latin1'));
50       $mail->set('To', $this->error_mail);
51       $mail->set('Subject', 'SQL Error in ' . $_SERVER['REQUEST_URI']);
52
53       $body = sprintf("http://%s%s%s\nReferer: %s\nUser: %s\n\n%s\n\nError Number %d\n%s\n\n",
54                       $_SERVER['SERVER_NAME'],
55                       $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '',
56                       $_SERVER['REQUEST_URI'],
57                       $_SERVER['HTTP_REFERER'],
58                       $_SESSION['sys']['login'],
59                       trim($sql),
60                       $arr[1],
61                       $arr[2]);
62
63       ob_start();
64       debug_print_backtrace();
65       $body .= ob_get_contents()."\n";
66       ob_end_clean();
67
68       if (count($_POST)) $body .= 'POST: ' . var_export($_POST,true)."\n";
69       if (count($_GET)) $body .= 'GET: ' . var_export($_GET,true)."\n";
70       if (count($_SESSION)) $body .= 'SESSION: ' . var_export($_SESSION,true)."\n";
71
72       $mail->send($body);
73     }
74   }
75
76   public function errorInfo()
77   {
78     return $this->db->errorInfo();
79   }
80
81   public function quote($string)
82   {
83     if (is_null($string)) return 'NULL';
84     if (strtolower($string) == 'now()' || strtolower($string) == 'current_timestamp') return 'now()';
85     return $this->db->quote($string);
86   }
87
88   public function lastInsertId()
89   {
90     if (DBDRIVER == 'pgsql')
91       return $this->db->lastInsertId($this->lastInsertTable.'_id_seq');
92     else
93       return $this->db->lastInsertId();
94   }
95
96   public function query($sql)
97   {
98     $this->logQuery($sql);
99     $sth = $this->db->query($sql);
100
101     if ($sth === false) $this->handleError($sth,$sql);
102
103     if (preg_match('/INSERT\s+INTO\s+(\S+)\s+/i', $sql, $matches))
104       $this->lastInsertTable = $matches[1];
105
106     return $sth;
107   }
108
109   public function execute($sql)
110   {
111     return $this->query($sql) !== false;
112   }
113
114   public function fetchValue($sql)
115   {
116     $sth = $this->query($sql);
117     if ($sth === false) return false;
118     $ret = $sth->fetch(PDO::FETCH_BOTH);
119     if ($ret === false) return false;
120     return $ret[0];
121   }
122
123   public function fetchAssoc($sql)
124   {
125     $sth = $this->query($sql);
126     if ($sth === false) return false;
127     return $sth->fetch(PDO::FETCH_ASSOC);
128   }
129
130   public function fetchObject($sql)
131   {
132     $sth = $this->query($sql);
133     if ($sth === false) return false;
134     return $sth->fetchObject();
135   }
136
137   public function fetchAssocList($sql)
138   {
139     $result = array();
140     $sth = $this->query($sql);
141     if ($sth === false) return $result;
142     while ($row = $sth->fetch(PDO::FETCH_ASSOC))
143       $result[] = $row;
144     return $result;
145   }
146
147   public function fetchObjectList($sql)
148   {
149     $result = array();
150     $sth = $this->query($sql);
151     if ($sth === false) return $result;
152     while ($row = $sth->fetchObject())
153       $result[] = $row;
154     return $result;
155   }
156
157 }
158
159 ?>