bcast-bridge/web/include/Database.class.php

98 lines
1.8 KiB
PHP
Raw Normal View History

2006-12-11 21:36:44 +01:00
<?php
class Database {
static private $instance;
private $dbh;
private $stats;
private $result;
static function getInstance() {
if (!isset(self::$instance))
self::$instance = new Database();
return self::$instance;
}
/*
* TODO: errorhandling?
*/
private function __construct() {
$this->dbh = mysql_pconnect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME, $this->dbh);
$stats = array();
}
/*
* TODO: errorhandling?
*/
function query($sql) {
$start = microtime(true);
$this->result = mysql_query($sql, $this->dbh);
$error = mysql_error($this->dbh);
$end = microtime(true);
$this->stats[] = array('query' => $sql, 'error' => $error, 'time' => $end - $start);
return $this->result;
}
function fetch_assoc() {
return mysql_fetch_assoc($this->result);
}
function fetch_array() {
return mysql_fetch_array($this->result);
}
function fetch_row() {
return mysql_fetch_row($this->result);
}
function num_rows() {
return mysql_num_rows($this->result);
}
function insert_id() {
return mysql_insert_id();
}
function affected_rows() {
return mysql_affected_rows($this->result);
}
static function getQueryStats() {
$sum = 0;
$errors = 0;
$stats = self::$instance->stats;
unset($stats['last']);
if (count($stats) > 0) {
foreach ($stats as $key => $val) {
$sum += $val['time'];
if (!empty($val['error']))
$errors++;
}
foreach ($stats as $key => $val)
$stats[$key]['percent'] = round($val['time'] / $sum, 2);
}
$stats['last'] = array('count' => count($stats), 'errors' => $errors, 'sum' => round($sum, 4));
return $stats;
}
static function resetQueryStats() {
unset($instance->stats);
}
function close($force = false) {
if ($force) {
mysql_close($instance->dbh);
unset($instance);
}
}
}
?>