Refactored DB from global to singleton

This commit is contained in:
Latif Khalifa
2013-10-12 00:56:27 +02:00
parent e8aca8283b
commit afaf687507
9 changed files with 60 additions and 85 deletions

View File

@@ -63,13 +63,11 @@ class CrashReport
} }
function getTotal($filter) function getTotal($filter)
{ {
global $DB;
$where = $filter->getWhere(); $where = $filter->getWhere();
$q = "select count(id) as total from reports $where"; $q = "select count(id) as total from reports $where";
if (false !== $cached = Memc::getq($q)) return $cached; if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q) OR !$row = $DB->fetchRow($res)) if (!$res = DBH::$db->query($q) OR !$row = DBH::$db->fetchRow($res))
{ {
return 0; return 0;
} }
@@ -82,21 +80,19 @@ class CrashReport
function getReports($filter, $fields = "id, reported, client_version, client_channel, os, gpu, grid, region") function getReports($filter, $fields = "id, reported, client_version, client_channel, os, gpu, grid, region")
{ {
global $DB;
$ret = array(); $ret = array();
$q = "select $fields from reports " . $filter->getWhere() . kl_str_sql(" order by id desc limit !i offset !i", $filter->limit, $filter->offset); $q = "select $fields from reports " . $filter->getWhere() . kl_str_sql(" order by id desc limit !i offset !i", $filter->limit, $filter->offset);
if (false !== $cached = Memc::getq($q)) return $cached; if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q)) if (!$res = DBH::$db->query($q))
{ {
return $ret; return $ret;
} }
while ($row = $DB->fetchRow($res)) while ($row = DBH::$db->fetchRow($res))
{ {
$r = new CrashReport; $r = new CrashReport;
$DB->loadFromDbRow($r, $res, $row); DBH::$db->loadFromDbRow($r, $res, $row);
$ret[] = $r; $ret[] = $r;
} }
@@ -106,18 +102,16 @@ class CrashReport
function getReport($id) function getReport($id)
{ {
global $DB;
$ret = array(); $ret = array();
if (!$res = $DB->query(kl_str_sql("select * from reports where id=!i", $id))) if (!$res = DBH::$db->query(kl_str_sql("select * from reports where id=!i", $id)))
{ {
return null; return null;
} }
if ($row = $DB->fetchRow($res)) if ($row = DBH::$db->fetchRow($res))
{ {
$r = new CrashReport; $r = new CrashReport;
$DB->loadFromDbRow($r, $res, $row); DBH::$db->loadFromDbRow($r, $res, $row);
$r->parseStackTrace($r->raw_stacktrace); $r->parseStackTrace($r->raw_stacktrace);
return $r; return $r;
} }
@@ -127,13 +121,11 @@ class CrashReport
function delete() function delete()
{ {
global $DB; DBH::$db->query(kl_str_sql("delete from reports where id=!i", $this->id));
$DB->query(kl_str_sql("delete from reports where id=!i", $this->id));
} }
function save() function save()
{ {
global $DB;
$this->delete(); $this->delete();
$q = kl_str_sql("insert into reports ( $q = kl_str_sql("insert into reports (
id, id,
@@ -171,7 +163,7 @@ class CrashReport
$this->crash_address, $this->crash_address,
$this->crash_thread, $this->crash_thread,
$this->raw_stacktrace); $this->raw_stacktrace);
if ($res = $DB->query($q)) if ($res = DBH::$db->query($q))
{ {
return true; return true;
} }

View File

@@ -1,7 +1,6 @@
<?php <?php
class DBH class DBH
{ {
public public
$db_name, $db_name,
$db_pass, $db_pass,
@@ -9,6 +8,9 @@ class DBH
$db_host, $db_host,
$dbh, $dbh,
$last_error = ""; $last_error = "";
// instance
static $db = null;
function log($line) function log($line)
{ {
@@ -31,7 +33,16 @@ class DBH
@fwrite($f, "[".date('Y-m-d H:i')."] ".$line."\n"); @fwrite($f, "[".date('Y-m-d H:i')."] ".$line."\n");
} }
function getInstance()
{
if (self::$db === null)
{
self::$db = new DBH;
}
return self::$db;
}
function connect($db_name, $db_host, $db_user, $db_pass) function connect($db_name, $db_host, $db_user, $db_pass)
{ {

View File

@@ -38,12 +38,10 @@ class Option
*/ */
public static function init() public static function init()
{ {
global $DB; if(!$result = DBH::$db->query("SELECT * FROM options")) {
if(!$result = $DB->query("SELECT * FROM options")) {
return false; return false;
} else { } else {
while ($row = $DB->fetchRow($result)) { while ($row = DBH::$db->fetchRow($result)) {
self::$optionArray[$row['name']] = $row['value']; self::$optionArray[$row['name']] = $row['value'];
} }
return true; return true;
@@ -59,9 +57,8 @@ class Option
*/ */
public static function update($name, $value) public static function update($name, $value)
{ {
global $DB; $result = DBH::$db->query(kl_str_sql("DELETE FROM options WHERE name=!s", $name));
$result = $DB->query(kl_str_sql("DELETE FROM options WHERE name=!s", $name)); $result = DBH::$db->query(kl_str_sql("INSERT INTO options (name, value) VALUES(!s,!s)", $name,$value ));
$result = $DB->query(kl_str_sql("INSERT INTO options (name, value) VALUES(!s,!s)", $name,$value ));
self::$optionArray[$name] = $value; self::$optionArray[$name] = $value;
return $result; return $result;
} }

View File

@@ -37,14 +37,13 @@ class ReportParser
function parse($id) function parse($id)
{ {
global $DB;
$q = kl_str_sql("select * from raw_reports where report_id=!i", $id); $q = kl_str_sql("select * from raw_reports where report_id=!i", $id);
if (!$res = $DB->query($q) OR !$row = $DB->fetchRow($res)) if (!$res = DBH::$db->query($q) OR !$row = DBH::$db->fetchRow($res))
{ {
return array(); return array();
} }
$data = new stdClass; $data = new stdClass;
$DB->loadFromDbRow($data, $res, $row); DBH::$db->loadFromDbRow($data, $res, $row);
$data->report = llsd_decode($data->raw_data); $data->report = llsd_decode($data->raw_data);
$data->report["reported"] = $data->reported; $data->report["reported"] = $data->reported;
unset($data->raw_data); unset($data->raw_data);
@@ -62,11 +61,10 @@ class ReportParser
function setProcessed($id, $status) function setProcessed($id, $status)
{ {
global $DB;
$ret = array(); $ret = array();
$q = kl_str_sql("update raw_reports set processed=!i where report_id=!i", $status, $id); $q = kl_str_sql("update raw_reports set processed=!i where report_id=!i", $status, $id);
if ($res = $DB->query($q)) if ($res = DBH::$db->query($q))
{ {
return true; return true;
} }
@@ -78,16 +76,15 @@ class ReportParser
function getUnprocessedIDs() function getUnprocessedIDs()
{ {
global $DB;
$ret = array(); $ret = array();
$q = kl_str_sql("select report_id from raw_reports where processed=!i", 0); $q = kl_str_sql("select report_id from raw_reports where processed=!i", 0);
if (!$res = $DB->query($q)) if (!$res = DBH::$db->query($q))
{ {
return $ret; return $ret;
} }
while ($row = $DB->fetchRow($res)) while ($row = DBH::$db->fetchRow($res))
{ {
$ret[] = (int)$row["report_id"]; $ret[] = (int)$row["report_id"];
} }

View File

@@ -81,19 +81,17 @@ class SearchFilter
function getVersions() function getVersions()
{ {
global $DB;
$ret = array(); $ret = array();
$where = $this->chan ? kl_str_sql("where client_channel=!s", $this->chan) : ''; $where = $this->chan ? kl_str_sql("where client_channel=!s", $this->chan) : '';
$q = "select distinct client_version from reports $where order by client_version desc"; $q = "select distinct client_version from reports $where order by client_version desc";
if (false !== $cached = Memc::getq($q)) return $cached; if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q)) if (!$res = DBH::$db->query($q))
{ {
return $ret; return $ret;
} }
while ($row = $DB->fetchRow($res)) while ($row = DBH::$db->fetchRow($res))
{ {
$ret[] = $row["client_version"]; $ret[] = $row["client_version"];
} }
@@ -104,18 +102,16 @@ class SearchFilter
function getGrids() function getGrids()
{ {
global $DB;
$ret = array(); $ret = array();
$q = "select distinct grid from reports order by grid asc"; $q = "select distinct grid from reports order by grid asc";
if (false !== $cached = Memc::getq($q)) return $cached; if (false !== $cached = Memc::getq($q)) return $cached;
if (!$res = $DB->query($q)) if (!$res = DBH::$db->query($q))
{ {
return $ret; return $ret;
} }
while ($row = $DB->fetchRow($res)) while ($row = DBH::$db->fetchRow($res))
{ {
$ret[] = $row["grid"]; $ret[] = $row["grid"];
} }
@@ -155,7 +151,7 @@ class SearchFilter
<div class="filterelem"> <div class="filterelem">
Version<br/> Version<br/>
<select class="ui-widget-content" name="version" onchange="this.form.submit();" style="width: 100px; margin-top: 3px;"> <select class="ui-widget-content" name="version" onchange="this.form.submit();" style="width: 100px; margin-top: 4px;">
<option value="" <?php echo !$this->version ? 'selected="selected"' : '' ?>>All</option> <option value="" <?php echo !$this->version ? 'selected="selected"' : '' ?>>All</option>
<?php <?php
for($i = 0; $i < count($ver); $i++) for($i = 0; $i < count($ver); $i++)
@@ -179,7 +175,7 @@ for($i = 0; $i < count($ver); $i++)
<div class="filterelem"> <div class="filterelem">
Grid<br/> Grid<br/>
<select class="ui-widget-content" name="grid" onchange="this.form.submit();" style="width: 200px; margin-top: 3px;"> <select class="ui-widget-content" name="grid" onchange="this.form.submit();" style="width: 200px; margin-top: 4px;">
<option value="" <?php echo !$this->grid ? 'selected="selected"' : '' ?>>All</option> <option value="" <?php echo !$this->grid ? 'selected="selected"' : '' ?>>All</option>
<?php <?php
for($i = 0; $i < count($grids); $i++) for($i = 0; $i < count($grids); $i++)

View File

@@ -66,12 +66,10 @@ class Session
function add() function add()
{ {
global $DB;
$this->sid = md5(uniqid(rand())); $this->sid = md5(uniqid(rand()));
$this->expires=time() + $this->timeout; $this->expires=time() + $this->timeout;
$DB->query(kl_str_sql("DELETE from session where sid=!s", $this->sid)); DBH::$db->query(kl_str_sql("DELETE from session where sid=!s", $this->sid));
$q = kl_str_sql("INSERT into session (sid, user_id, authenticated, expires, persist) ". $q = kl_str_sql("INSERT into session (sid, user_id, authenticated, expires, persist) ".
"values (!s, !i, !i, !t, !s)", "values (!s, !i, !i, !t, !s)",
$this->sid, $this->sid,
@@ -81,7 +79,7 @@ class Session
$this->ser_persist $this->ser_persist
); );
if ($DB->query($q)) { if (DBH::$db->query($q)) {
setcookie($this->cookie, $this->sid, NULL, '/' . REL_DIR); setcookie($this->cookie, $this->sid, NULL, '/' . REL_DIR);
return true; return true;
} else { } else {
@@ -91,12 +89,10 @@ class Session
function update() function update()
{ {
global $DB;
$q = kl_str_sql('UPDATE session SET user_id=!i, authenticated=!i, expires=!t, persist=!s WHERE sid=!s', $q = kl_str_sql('UPDATE session SET user_id=!i, authenticated=!i, expires=!t, persist=!s WHERE sid=!s',
$this->user->user_id, $this->authenticated, $this->expires, $this->ser_persist, $this->sid); $this->user->user_id, $this->authenticated, $this->expires, $this->ser_persist, $this->sid);
if (($res = $DB->query($q)) && $DB->affectedRows()) { if (($res = DBH::$db->query($q)) && DBH::$db->affectedRows()) {
return true; return true;
} else { } else {
return $this->add(); return $this->add();
@@ -105,8 +101,6 @@ class Session
function remove($sid = false) function remove($sid = false)
{ {
global $DB;
if (!$sid) { if (!$sid) {
$sid = $this->sid; $sid = $this->sid;
} }
@@ -116,13 +110,12 @@ class Session
$this->user = new User; $this->user = new User;
$this->persist = NULL; $this->persist = NULL;
$this->ser_persist = NULL; $this->ser_persist = NULL;
$DB->query(kl_str_sql("DELETE from session where sid=!s", $sid)); DBH::$db->query(kl_str_sql("DELETE from session where sid=!s", $sid));
setcookie($this->cookie, '', 0, '/' . REL_DIR); setcookie($this->cookie, '', 0, '/' . REL_DIR);
} }
function check() function check()
{ {
global $DB;
if (isset($_GET[$this->cookie])) { if (isset($_GET[$this->cookie])) {
$this->sid = $_GET[$this->cookie]; $this->sid = $_GET[$this->cookie];
} else { } else {
@@ -143,8 +136,8 @@ class Session
} }
$error = false; $error = false;
} else { } else {
$res = $DB->query(kl_str_sql('SELECT * from session where sid=!s', $this->sid)); $res = DBH::$db->query(kl_str_sql('SELECT * from session where sid=!s', $this->sid));
if ($res AND $row = $DB->fetchRow($res)) { if ($res AND $row = DBH::$db->fetchRow($res)) {
$this->authenticated = (int)$row['authenticated']; $this->authenticated = (int)$row['authenticated'];
$this->expires = strtotime($row['expires']); $this->expires = strtotime($row['expires']);
$this->ser_persist = $row['persist']; $this->ser_persist = $row['persist'];

View File

@@ -24,7 +24,6 @@ class User
*/ */
public function save() public function save()
{ {
global $DB;
$query = kl_str_sql('INSERT INTO users( $query = kl_str_sql('INSERT INTO users(
name, name,
email, email,
@@ -41,17 +40,16 @@ class User
$this->is_allowed $this->is_allowed
); );
if (!$res = $DB->query($query)) { if (!$res = DBH::$db->query($query)) {
return false; return false;
} else { } else {
$this->user_id = $DB->insertID(); $this->user_id = DBH::$db->insertID();
return $this->user_id; return $this->user_id;
} }
} }
public function update() public function update()
{ {
global $DB;
$query = kl_str_sql(' $query = kl_str_sql('
UPDATE users SET UPDATE users SET
name=!s, name=!s,
@@ -70,7 +68,7 @@ class User
$this->user_id $this->user_id
); );
//echo $query; //echo $query;
if (!$DB->query($query)) { if (!DBH::$db->query($query)) {
return false; return false;
} else { } else {
return true; return true;
@@ -85,19 +83,17 @@ class User
*/ */
public static function get($id) public static function get($id)
{ {
global $DB;
if (is_null($id)) { if (is_null($id)) {
return new User(); return new User();
} }
$query = kl_str_sql("SELECT * FROM users WHERE user_id=!i",$id); $query = kl_str_sql("SELECT * FROM users WHERE user_id=!i",$id);
if(!$res = $DB->query($query) OR !$row = $DB->fetchRow($res)) { if(!$res = DBH::$db->query($query) OR !$row = DBH::$db->fetchRow($res)) {
return false; return false;
} else { } else {
$user = new User(); $user = new User();
$DB->loadFromDbRow($user, $res, $row); DBH::$db->loadFromDbRow($user, $res, $row);
return $user; return $user;
} }
} }
@@ -124,15 +120,13 @@ class User
*/ */
public static function getByLogin($username) public static function getByLogin($username)
{ {
global $DB;
$query = kl_str_sql('SELECT * FROM users WHERE login=!s', $username); $query = kl_str_sql('SELECT * FROM users WHERE login=!s', $username);
if(!$res = $DB->query($query) OR !$row = $DB->fetchRow($res)) { if(!$res = DBH::$db->query($query) OR !$row = DBH::$db->fetchRow($res)) {
return false; return false;
} else { } else {
$user = new User(); $user = new User();
$DB->loadFromDbRow($user, $res, $row); DBH::$db->loadFromDbRow($user, $res, $row);
return $user; return $user;
} }
} }
@@ -145,14 +139,12 @@ class User
*/ */
public static function getByEmail($email) public static function getByEmail($email)
{ {
global $DB;
$query = kl_str_sql('SELECT * FROM users WHERE cust_id!=1 AND email=!s', $email); $query = kl_str_sql('SELECT * FROM users WHERE cust_id!=1 AND email=!s', $email);
if (!$res = $DB->query($query) OR !$row = $DB->fetchRow($res)) { if (!$res = DBH::$db->query($query) OR !$row = DBH::$db->fetchRow($res)) {
return false; return false;
} else { } else {
$user = new User(); $user = new User();
$DB->loadFromDbRow($user, $res, $row); DBH::$db->loadFromDbRow($user, $res, $row);
return $user; return $user;
} }
} }
@@ -164,16 +156,14 @@ class User
*/ */
public static function getAll() public static function getAll()
{ {
global $DB;
$query = kl_str_sql('SELECT * FROM users ORDER BY is_admin DESC, is_allowed DESC, user_id ASC'); $query = kl_str_sql('SELECT * FROM users ORDER BY is_admin DESC, is_allowed DESC, user_id ASC');
if(!$res = $DB->query($query)) { if(!$res = DBH::$db->query($query)) {
return false; return false;
} else { } else {
$retval=array(); $retval=array();
while($row = $DB->fetchRow($res)) { while($row = DBH::$db->fetchRow($res)) {
$tmp = new User(); $tmp = new User();
$DB->loadFromDbRow($tmp, $res, $row); DBH::$db->loadFromDbRow($tmp, $res, $row);
$retval[] = $tmp; $retval[] = $tmp;
} }
return $retval; return $retval;
@@ -181,10 +171,9 @@ class User
} }
function delete(){ function delete(){
global $DB;
$query=kl_str_sql("DELETE FROM users WHERE user_id=!i",$this->user_id); $query=kl_str_sql("DELETE FROM users WHERE user_id=!i",$this->user_id);
//echo $query; //echo $query;
if(!$res=$DB->query($query)){ if(!$res=DBH::$db->query($query)){
return false; return false;
} }
else{ else{

View File

@@ -57,14 +57,14 @@ if (!defined('IMG_ROOT')) {
} }
$DB = new DBH(); $DB = DBH::getInstance();
$DB_NAME = "singucrash"; $DB_NAME = "singucrash";
$DB_USER = 'singucrash'; $DB_USER = 'singucrash';
$DB_PASS = '-*-secrit-*-'; $DB_PASS = '-*-secrit-*-';
$DB_HOST = 'localhost'; $DB_HOST = 'localhost';
if (!$DB->connect($DB_NAME, $DB_HOST, $DB_USER, $DB_PASS)) { if (!DBH::$db->connect($DB_NAME, $DB_HOST, $DB_USER, $DB_PASS)) {
echo "System is down for mantainence. Please try again later."; echo "System is down for mantainence. Please try again later.";
die(); die();
} }

View File

@@ -13,9 +13,9 @@ $report = file_get_contents("php://input");
if (strlen($report)) if (strlen($report))
{ {
$query = kl_str_sql('insert into raw_reports(raw_data) values (!s)', $report); $query = kl_str_sql('insert into raw_reports(raw_data) values (!s)', $report);
if ($res = $DB->query($query)) if ($res = DBH::$db->query($query))
{ {
$report_id = $DB->insertID(); $report_id = DBH::$db->insertID();
} }
} }