5 private $error_log = false;
6 private $error_mail = false;
7 private $lastInsertTable = null;
9 public function __construct($driver, $host, $dbname, $user, $passwd)
11 $this->db = new PDO(sprintf('%s:host=%s;dbname=%s', $driver, $host, $dbname), $user, $passwd);
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'");
19 public function enableErrorLog()
21 $this->error_log = true;
24 public function disableErrorLog()
26 $this->error_log = false;
29 public function setErrorMail($address)
31 $this->error_mail = $address;
34 private function logQuery($sql)
36 if ($this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
39 private function handleError($sth, $sql)
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)
46 if ($this->error_mail && defined('MAIL_FROM') && defined('MAIL_FROM_NAME')) {
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']);
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'],
64 debug_print_backtrace();
65 $body .= ob_get_contents()."\n";
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";
76 public function errorInfo()
78 return $this->db->errorInfo();
81 public function quote($string)
83 if (is_null($string)) return 'NULL';
84 if (strtolower($string) == 'now()' || strtolower($string) == 'current_timestamp') return 'now()';
85 return $this->db->quote($string);
88 public function lastInsertId()
90 if (DBDRIVER == 'pgsql')
91 return $this->db->lastInsertId($this->lastInsertTable.'_id_seq');
93 return $this->db->lastInsertId();
96 public function query($sql)
98 $this->logQuery($sql);
99 $sth = $this->db->query($sql);
101 if ($sth === false) $this->handleError($sth,$sql);
103 if (preg_match('/INSERT\s+INTO\s+(\S+)\s+/i', $sql, $matches))
104 $this->lastInsertTable = $matches[1];
109 public function execute($sql)
111 return $this->query($sql) !== false;
114 public function fetchValue($sql)
116 $sth = $this->query($sql);
117 if ($sth === false) return false;
118 $ret = $sth->fetch(PDO::FETCH_BOTH);
119 if ($ret === false) return false;
123 public function fetchAssoc($sql)
125 $sth = $this->query($sql);
126 if ($sth === false) return false;
127 return $sth->fetch(PDO::FETCH_ASSOC);
130 public function fetchObject($sql)
132 $sth = $this->query($sql);
133 if ($sth === false) return false;
134 return $sth->fetchObject();
137 public function fetchAssocList($sql)
140 $sth = $this->query($sql);
141 if ($sth === false) return $result;
142 while ($row = $sth->fetch(PDO::FETCH_ASSOC))
147 public function fetchObjectList($sql)
150 $sth = $this->query($sql);
151 if ($sth === false) return $result;
152 while ($row = $sth->fetchObject())