Skip to content

Instantly share code, notes, and snippets.

@themasch
Created September 9, 2010 11:16
Show Gist options
  • Save themasch/571736 to your computer and use it in GitHub Desktop.
Save themasch/571736 to your computer and use it in GitHub Desktop.
<?php
/**
* Sound-Generation.FM v3.0
*
* @copyright Copyright (c) 2010 by sound-generation.fm
* @author Rico "Mahribarius" Schäfer <drognanexcata@gmx.de>
* @author Mark "MaSch" Schmale <ma.schmale@googlemail.com>
* @package SG.FMv3
* @version $Id: DB.php 119 2010-06-03 00:40:56Z masch $
*/
/**
* Datenbank
*
* Abstraktion für PDO inklusive singleton.
* @author Rico "Mahribarius" Schäfer <drognanexcata@gmx.de>
* @author Mark "MaSch" Schmale <ma.schmale@googlemail.com>
* @package SG.FMv3
*/
class DB extends PDO {
/**
* einzige Instanz dieser Klasse
* @var DB
*/
protected static $_instance = null;
protected $_schema = null;
protected $_host = null;
/**
* erzeugt die Datenbankverbindung mit der angegeben Konfiguration
* @param stdClass $cfg
*/
public static function createConnection($cfg) {
self::$_instance = new self($cfg);
}
/**
* Constructor
*
* @param stdClass $cfg
*/
public function __construct($cfg) {
$this->_schema = $cfg->name;
$this->_host = $cfg->host;
$dsn = 'mysql:dbname='.$cfg->name.';host='.$cfg->host;
$usr = $cfg->user;
$pas = $cfg->pass;
$options = array( DB::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
DB::MYSQL_ATTR_USE_BUFFERED_QUERY => true);
try {
parent::__construct($dsn, $usr, $pas, $options);
}
catch(PDOException $ex) {
Log::error('kann Datenbankverbindung nicht herstellen', array('msg' => $ex->getMessage(), 'code' => $ex->getCode()), 'DB');
throw new Exception_Fatal('Kann keine Datenbankverbindung herstellen');
}
}
/**
* gibt die einzige Instanz dieser Klasse zurück wenn eine existiert.
* @return DB
*/
public static function getInstance() {
if(self::$_instance === null || !(self::$_instance instanceOf DB)) {
Log::error('Datenbankverbindung noch nicht hergestellt!');
throw new Exception_Fatal('Datenbankverbindung noch nicht hergestellt!');
}
return self::$_instance;
}
public function getSchema() {
return $this->_schema;
}
public function getHost() {
return $this->_host;
}
/**
* Prüft ob eine Tabelle existiert
*
* @param string $tbl
* @return boolean
*/
public function tabelExists($tbl) {
$check_qry = 'SHOW TABLE STATUS LIKE '.$this->quote($tbl);
$stmt = $this->prepare($check_qry);
if($stmt === false || !($stmt instanceOf PDOStatement)) {
Log::error('Abfrage gescheitert', array('errinfo' => $this->errorInfo(),
'query' => $check_qry));
throw new Exception_Fatal('Fehlerhafte SQL Abfrage!');
}
$res = $stmt->execute();
if(!$res) {
Log::error('Abfrage gescheitert', array('errinfo' => $this->errorInfo(),
'query' => $check_qry));
throw new Exception_Fatal('Fehlerhafte SQL Abfrage!');
} else {
$data = $stmt->fetchAll();
if(count($data) >= 1) { // normal sollte === 1 auch passen.. aber sicher ist sicher
return true;
} else {
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment