Revision 1683
Added by darkviper over 13 years ago
| Database.php | ||
|---|---|---|
| 29 | 29 |
/* -------------------------------------------------------- */ |
| 30 | 30 |
define('DATABASE_CLASS_LOADED', true);
|
| 31 | 31 |
|
| 32 |
class Database {
|
|
| 33 | 32 |
|
| 34 |
// $sdb = 'mysql://user:password@demo.de:3306/datenbank'; |
|
| 33 |
class Database extends DatabaseX {
|
|
| 34 |
|
|
| 35 |
private static $_oInstance = array(); |
|
| 36 |
/* prevent from public instancing */ |
|
| 37 |
protected function __construct() {}
|
|
| 38 |
/* prevent from cloning */ |
|
| 39 |
private function __clone() {}
|
|
| 40 |
/** |
|
| 41 |
* get a valid instance of this class |
|
| 42 |
* @param string $sIdentifier selector for several different instances |
|
| 43 |
* @return object |
|
| 44 |
*/ |
|
| 45 |
public static function getInstance($sIdentifier = 'core') {
|
|
| 46 |
if( !isset(self::$_oInstance[$sIdentifier])) {
|
|
| 47 |
$c = __CLASS__; |
|
| 48 |
self::$_oInstance[$sIdentifier] = new $c; |
|
| 49 |
} |
|
| 50 |
return self::$_oInstance[$sIdentifier]; |
|
| 51 |
} |
|
| 52 |
} |
|
| 35 | 53 |
|
| 54 |
|
|
| 55 |
class DatabaseX {
|
|
| 56 |
|
|
| 36 | 57 |
private $_db_handle = null; // readonly from outside |
| 37 | 58 |
private $_scheme = 'mysql'; |
| 38 | 59 |
private $_hostname = 'localhost'; |
| ... | ... | |
| 48 | 69 |
private $message = array(); |
| 49 | 70 |
private $iQueryCount= 0; |
| 50 | 71 |
|
| 51 |
|
|
| 52 |
// Set DB_URL |
|
| 53 |
function __construct($url = '') {
|
|
| 72 |
/* prevent from public instancing */ |
|
| 73 |
protected function __construct() {}
|
|
| 74 |
/* prevent from cloning */ |
|
| 75 |
private function __clone() {}
|
|
| 76 |
|
|
| 77 |
/** |
|
| 78 |
* Connect to the database |
|
| 79 |
* @param string $url |
|
| 80 |
* @return bool |
|
| 81 |
* |
|
| 82 |
* Example for SQL-Url: 'mysql://user:password@demo.de[:3306]/datenbank' |
|
| 83 |
*/ |
|
| 84 |
public function doConnect($url = '') {
|
|
| 54 | 85 |
if($url != '') {
|
| 55 | 86 |
$aIni = parse_url($url); |
| 56 | 87 |
$this->_scheme = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysql'; |
| ... | ... | |
| 63 | 94 |
}else {
|
| 64 | 95 |
throw new RuntimeException('Missing parameter: unable to connect database');
|
| 65 | 96 |
} |
| 66 |
// Connect to database |
|
| 67 |
$this->connect(); |
|
| 68 |
} |
|
| 69 |
|
|
| 70 |
// Connect to the database |
|
| 71 |
function connect() {
|
|
| 72 | 97 |
$this->_db_handle = mysql_connect($this->_hostname.$this->_hostport, |
| 73 | 98 |
$this->_username, |
| 74 | 99 |
$this->_password); |
Also available in: Unified diff
changed class Database into a Singleton-Class
added forgotten 'static' keyword in ModLanguage
removed version control from sm2 - include.php