1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-15 03:17:38 +01:00
vichan/inc/database.php

106 lines
2.6 KiB
PHP
Raw Normal View History

2010-12-17 15:18:03 +01:00
<?php
2011-04-13 14:21:07 +02:00
if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) {
// You cannot request this file directly.
header('Location: ../', true, 302);
exit;
}
2010-12-17 15:18:03 +01:00
class PreparedQueryDebug {
protected $query;
public function __construct($query) {
global $pdo;
$this->query = $pdo->prepare($query);
}
public function __call($function, $args) {
global $config, $debug;
if($config['debug'] && $function == 'execute') {
$start = microtime(true);
}
$return = call_user_func_array(Array($this->query, $function), $args);
if($config['debug'] && $function == 'execute') {
$time = round((microtime(true) - $start) * 1000, 2) . 'ms';
$debug['sql'][] = Array(
'query' => $this->query->queryString,
'rows' => $this->query->rowCount(),
'time' => '~' . $time
);
}
return $return;
}
}
2010-12-17 15:18:03 +01:00
function sql_open() {
2011-02-12 07:25:15 +01:00
global $pdo, $config;
2010-12-17 15:18:03 +01:00
if($pdo) return true;
2011-02-12 07:25:15 +01:00
$dsn = $config['db']['type'] . ':host=' . $config['db']['server'] . ';dbname=' . $config['db']['database'];
if(!empty($config['db']['dsn']))
$dsn .= ';' . $config['db']['dsn'];
2010-12-17 15:18:03 +01:00
try {
$options = Array(PDO::ATTR_TIMEOUT => $config['db']['timeout']);
2011-11-05 07:35:07 +01:00
$options = Array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
if($config['db']['persistent'])
$options[PDO::ATTR_PERSISTENT] = true;
2011-11-05 07:35:07 +01:00
return $pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options);
2010-12-17 15:18:03 +01:00
} catch(PDOException $e) {
$message = $e->getMessage();
// Remove any sensitive information
2011-02-12 07:25:15 +01:00
$message = str_replace($config['db']['user'], '<em>hidden</em>', $message);
$message = str_replace($config['db']['password'], '<em>hidden</em>', $message);
// Print error
error('Database error: ' . $message);
2010-12-17 15:18:03 +01:00
}
}
function prepare($query) {
2011-05-21 07:22:10 +02:00
global $pdo, $debug, $config;
sql_open();
if($config['debug'])
return new PreparedQueryDebug($query);
2010-12-17 15:18:03 +01:00
return $pdo->prepare($query);
}
function query($query) {
2011-05-21 07:22:10 +02:00
global $pdo, $debug, $config;
sql_open();
if($config['debug']) {
$start = microtime(true);
$query = $pdo->query($query);
2011-10-10 12:49:45 +02:00
if(!$query)
return false;
$time = round((microtime(true) - $start) * 1000, 2) . 'ms';
$debug['sql'][] = Array(
'query' => $query->queryString,
'rows' => $query->rowCount(),
'time' => '~' . $time
);
return $query;
} else {
return $pdo->query($query);
}
2010-12-17 15:18:03 +01:00
}
function db_error($PDOStatement=null) {
global $pdo;
if(isset($PDOStatement)) {
$err = $PDOStatement->errorInfo();
return $err[2];
} else {
$err = $pdo->errorInfo();
return $err[2];
}
}
2011-08-14 16:49:52 +02:00
?>