db = new PDO(sprintf('%s:host=%s;dbname=%s', $driver, $host, $dbname), $user, $passwd); $this->db->query("SET DateStyle = 'ISO'"); # $this->db->query("SET NAMES 'utf8'"); # $this->db->query("SET CHARACTER SET 'utf8'"); # $this->db->query("SET collation_connection = 'utf8_general_ci'"); # $this->db->query("SET lc_time_names = 'de_DE'"); } public function enableErrorLog() { $this->error_log = true; } public function disableErrorLog() { $this->error_log = false; } public function setErrorMail($address) { $this->error_mail = $address; } private function logQuery($sql) { if ($this->error_log) error_log(trim(str_replace("\n", ' ', $sql))); } private function handleError($sth, $sql, $data=false) { if (!$this->error_log) error_log(trim(str_replace("\n", ' ', $sql))); $arr = $this->db->errorInfo(); foreach (explode("\n", $arr[2]) as $line) error_log($line); if ($this->error_mail && defined('MAIL_FROM') && defined('MAIL_FROM_NAME')) { $mail = new Mail(); $mail->env_from(MAIL_FROM); $mail->set('From', mb_encode_mimeheader(utf8_decode(sprintf("%s <%s>", MAIL_FROM_NAME, MAIL_FROM)),'latin1')); $mail->set('To', $this->error_mail); $mail->set('Subject', 'SQL Error in ' . $_SERVER['REQUEST_URI']); $body = sprintf("http://%s%s%s\nReferer: %s\nUser: %s\n\n%s\n\nError Number %d\n%s\n\n", $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '', $_SERVER['REQUEST_URI'], $_SERVER['HTTP_REFERER'], $_SESSION['sys']['login'], trim($sql), $arr[1], $arr[2]); ob_start(); debug_print_backtrace(); $body .= ob_get_contents()."\n"; ob_end_clean(); if (count($_POST)) $body .= 'POST: ' . var_export($_POST,true)."\n"; if (count($_GET)) $body .= 'GET: ' . var_export($_GET,true)."\n"; if (count($_SESSION)) $body .= 'SESSION: ' . var_export($_SESSION,true)."\n"; if ($data) $body .= 'data: ' . var_export($data,true)."\n"; $mail->send($body); } return $sth; } public function errorInfo() { return $this->db->errorInfo(); } public function quote($string) { if (is_null($string)) return 'NULL'; if (strtolower($string) == 'now()' || strtolower($string) == 'current_timestamp') return 'now()'; return $this->db->quote($string); } public function lastInsertId() { if (DBDRIVER == 'pgsql') return $this->db->lastInsertId($this->lastInsertTable.'_id_seq'); else return $this->db->lastInsertId(); } private function query($sql) { $this->logQuery($sql); $sth = $this->db->query($sql); if ($sth === false) return $this->handleError($sth,$sql); if (preg_match('/INSERT\s+INTO\s+(\S+)\s+/i', $sql, $matches)) $this->lastInsertTable = $matches[1]; return $sth; } public function execute($sql) { return $this->query($sql) !== false; } public function fetchValue($sql) { $sth = $this->query($sql); if ($sth === false) return false; $ret = $sth->fetch(PDO::FETCH_BOTH); if ($ret === false) return false; return $ret[0]; } public function fetchAssoc($sql) { $sth = $this->query($sql); if ($sth === false) return false; return $sth->fetch(PDO::FETCH_ASSOC); } public function fetchObject($sql) { $sth = $this->query($sql); if ($sth === false) return false; return $sth->fetchObject(); } public function fetchAssocList($sql) { $result = array(); $sth = $this->query($sql); if ($sth === false) return $result; while ($row = $sth->fetch(PDO::FETCH_ASSOC)) $result[] = $row; return $result; } public function fetchObjectList($sql) { $result = array(); $sth = $this->query($sql); if ($sth === false) return $result; while ($row = $sth->fetchObject()) $result[] = $row; return $result; } private function executeBind($sql, $data) { $sth = $this->db->prepare($sql); if ($sth === false) return $this->handleError($sth,$sql); foreach ($data as $key => $value) { if(is_int($value)) $param = PDO::PARAM_INT; elseif(is_float($value)) $param = PDO::PARAM_STR; elseif(is_bool($value)) $param = PDO::PARAM_BOOL; elseif(is_null($value)) $param = PDO::PARAM_NULL; elseif(is_string($value)) $param = PDO::PARAM_STR; else $param = FALSE; $sth->bindValue(':'.$key, $value, $param); } $ok = $sth->execute(); if ($ok === false) return $this->handleError($sth,$sql,$data); if (preg_match('/INSERT\s+INTO\s+(\S+)\s+/i', $sql, $matches)) $this->lastInsertTable = $matches[1]; return $ok; } public function insertInto($table, $data) { if (!is_array($data)) throw new Exception('insertInto called without data array'); $columns = array(); foreach ($data as $k => $v) { $columns[] = $k; if (strtolower($v) == 'now()' || strtolower($v) == 'current_timestamp') { $values[] = 'now()'; unset($data[$k]); } else $values[] = ':' . $k; } $sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $table, implode(',', $columns), implode(',', $values)); return $this->executeBind($sql, $data); } public function update($table, $data, $condition) { if (!is_array($data)) throw new Exception('update called without data array'); if (empty($condition)) throw new Exception('update called without condition'); $set = array(); foreach ($data as $k => $v) { if (strtolower($v) == 'now()' || strtolower($v) == 'current_timestamp') { $set[] = $k.'=now()'; unset($data[$k]); } else $set[] = $k.'=:'.$k; } $sql = sprintf("UPDATE %s SET %s WHERE %s", $table, implode(',', $set), $condition); return $this->executeBind($sql, $data); } public function delete($table, $idcolumn, $id) { $sql = sprintf("DELETE FROM %s WHERE %s = %d", $table, $idcolumn, $id); return $this->execute($sql); } } ?>