wibble database / cache connection functions, make them repeatable, and set persistant DB connections as an option

This commit is contained in:
Shish 2012-06-23 23:57:34 +01:00
parent 63437d1e09
commit 008cc4253b
2 changed files with 26 additions and 14 deletions

View File

@ -276,6 +276,30 @@ class Database {
* stored in the config file * stored in the config file
*/ */
public function Database() { public function Database() {
$this->connect_cache();
$this->connect_db();
}
private function connect_cache() {
if(!is_null($this->cache)) return;
$matches = array();
if(defined("CACHE_DSN") && CACHE_DSN && preg_match("#(memcache|apc)://(.*)#", CACHE_DSN, $matches)) {
if($matches[1] == "memcache") {
$this->cache = new MemcacheCache($matches[2]);
}
else if($matches[1] == "apc") {
$this->cache = new APCCache($matches[2]);
}
}
else {
$this->cache = new NoCache();
}
}
private function connect_db() {
if(!is_null($this->db)) return;
# FIXME: detect ADODB URI, automatically translate PDO DSN # FIXME: detect ADODB URI, automatically translate PDO DSN
/* /*
@ -289,7 +313,7 @@ class Database {
if(preg_match("/password=([^;]*)/", DATABASE_DSN, $matches)) $db_pass=$matches[1]; if(preg_match("/password=([^;]*)/", DATABASE_DSN, $matches)) $db_pass=$matches[1];
$db_params = array( $db_params = array(
PDO::ATTR_PERSISTENT => true, PDO::ATTR_PERSISTENT => DATABASE_KA,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
); );
if(defined("HIPHOP")) $this->db = new PDO(DATABASE_DSN, $db_user, $db_pass); if(defined("HIPHOP")) $this->db = new PDO(DATABASE_DSN, $db_user, $db_pass);
@ -309,19 +333,6 @@ class Database {
die('Unknown PDO driver: '.$db_proto); die('Unknown PDO driver: '.$db_proto);
} }
$matches = array();
if( defined("CACHE_DSN") && CACHE_DSN && preg_match("#(memcache|apc)://(.*)#", CACHE_DSN, $matches)) {
if($matches[1] == "memcache") {
$this->cache = new MemcacheCache($matches[2]);
}
else if($matches[1] == "apc") {
$this->cache = new APCCache($matches[2]);
}
}
else {
$this->cache = new NoCache();
}
$this->engine->init($this->db); $this->engine->init($this->db);
} }

View File

@ -20,6 +20,7 @@
*/ */
function _d($name, $value) {if(!defined($name)) define($name, $value);} function _d($name, $value) {if(!defined($name)) define($name, $value);}
_d("DATABASE_DSN", null); // string PDO database connection details _d("DATABASE_DSN", null); // string PDO database connection details
_d("DATABASE_KA", true); // string Keep database connection alive
_d("CACHE_DSN", null); // string cache connection details _d("CACHE_DSN", null); // string cache connection details
_d("DEBUG", false); // boolean print various debugging details _d("DEBUG", false); // boolean print various debugging details
_d("DEBUG_SQL", false); // boolean dump SQL queries to data/sql.log _d("DEBUG_SQL", false); // boolean dump SQL queries to data/sql.log