Created
March 28, 2012 05:56
-
-
Save jameswyse/2224100 to your computer and use it in GitHub Desktop.
Perch ADODB MySQL Driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define("PERCH_DB_CACHE_TIME", 3600); | |
define("PERCH_DB_CACHE_DIR", "/tmp/cache/adodb/"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once('adodb5/adodb.inc.php'); | |
class PerchDB_MySQL | |
{ | |
private $link = false; | |
private $errored = false; | |
static public $queries = 0; | |
private $enableCache = true; | |
function __destruct() | |
{ | |
$this->close_link(); | |
} | |
private function open_link() | |
{ | |
$GLOBALS['$ADODB_COUNTRECS'] = false; | |
$GLOBALS['ADODB_CACHE_DIR'] = PERCH_DB_CACHE_DIR; | |
$dsn = 'mysql://' . PERCH_DB_USERNAME . ':' . PERCH_DB_PASSWORD . '@' . PERCH_DB_SERVER .'/' . PERCH_DB_DATABASE . '?persist&cachesecs='.PERCH_DB_CACHE_TIME; | |
$this->link = ADONewConnection($dsn); | |
//$this->link->debug=true; | |
if (!$this->link) { | |
switch(PERCH_ERROR_MODE) | |
{ | |
case 'SILENT': | |
break; | |
case 'ECHO': | |
if (!$this->errored) { | |
echo 'Could not connect to the database. Please check that the username and password are correct.'; | |
$this->errored = true; | |
} | |
break; | |
default: | |
PerchUtil::redirect(PERCH_LOGINPATH.'/error/db.php'); | |
break; | |
} | |
PerchUtil::debug("Could not create DB link!", 'error'); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
private function close_link() | |
{ | |
if ($this->link) { | |
$this->link->Close(); | |
unset($this->link); | |
$this->link = false; | |
} | |
} | |
private function get_link() | |
{ | |
if (!$this->link) { | |
$this->open_link(); | |
} | |
return $this->link; | |
} | |
public function execute($sql,$cache=true) | |
{ | |
$link = $this->get_link(); | |
if (!$link) return false; | |
if($cache && $this->enableCache) { | |
$result = $link->CacheExecute($sql); | |
PerchUtil::debug($sql, 'db-cache'); | |
} | |
else { | |
$result = $link->Execute($sql); | |
PerchUtil::debug($sql, 'db'); | |
} | |
self::$queries++; | |
if ($link->ErrorMsg()) { | |
PerchUtil::debug("Invalid query: " . $link->ErrorMsg(), 'error'); | |
return false; | |
} | |
$newid = $link->Insert_ID(); | |
if (!$newid) { | |
self::$queries++; | |
return $link->Affected_Rows(); | |
} | |
return $newid; | |
} | |
public function get_rows($sql, $cache=true) | |
{ | |
$link = $this->get_link(); | |
if (!$link) return false; | |
if ($this->enableCache && $cache) { | |
$r = $link->CacheGetAll($sql); | |
PerchUtil::debug($sql, 'db-cache'); | |
} | |
else { | |
$r = $link->GetAll($sql); | |
PerchUtil::debug($sql, 'db'); | |
} | |
self::$queries++; | |
if ($r) return $r; | |
else { | |
PerchUtil::debug("Invalid query: " . $link->ErrorMsg(), 'error'); | |
return false; | |
} | |
} | |
public function get_row($sql,$cache=true) | |
{ | |
$link = $this->get_link(); | |
if (!$link) return false; | |
if($this->enableCache && $cache) { | |
$r = $link->CacheGetRow($sql); | |
PerchUtil::debug($sql, 'db-cache'); | |
} | |
else { | |
$r = $link->GetRow($sql); | |
PerchUtil::debug($sql, 'db'); | |
} | |
self::$queries++; | |
if ($r) return $r; | |
else { | |
PerchUtil::debug("Invalid query: " . $link->ErrorMsg(), 'error'); | |
return false; | |
} | |
} | |
public function get_value($sql,$cache=true) | |
{ | |
$result = $this->get_row($sql,$cache); | |
if (is_array($result)) { | |
foreach($result as $val) { | |
return $val; | |
} | |
} | |
return false; | |
} | |
public function get_count($sql) | |
{ | |
$result = $this->get_value($sql); | |
return intval($result); | |
} | |
public function insert($table, $data) | |
{ | |
$cols = array(); | |
$vals = array(); | |
foreach($data as $key => $value) { | |
$cols[] = $key; | |
$vals[] = $this->pdb($value); | |
} | |
$sql = 'INSERT INTO ' . $table . '(' . implode(',', $cols) . ') VALUES(' . implode(',', $vals) . ')'; | |
$link = $this->get_link(); | |
if (!$link) return false; | |
$execute = $this->execute($sql,false); | |
$link->CacheFlush(); | |
return $execute; | |
} | |
public function update($table, $data, $id_column, $id) | |
{ | |
$sql = 'UPDATE ' . $table . ' SET '; | |
$items = array(); | |
foreach($data as $key => $value) { | |
$items[] = $key . ' = ' . $this->pdb($value); | |
} | |
$sql .= implode(', ', $items); | |
$sql .= ' WHERE ' . $id_column . ' = ' . $this->pdb($id); | |
$link = $this->get_link(); | |
if (!$link) return false; | |
$execute = $this->execute($sql,false); | |
$link->CacheFlush(); | |
return $execute; | |
} | |
public function delete($table, $id_column, $id, $limit=false) | |
{ | |
$sql = 'DELETE FROM ' . $table . ' WHERE ' . $id_column . ' = ' . $this->pdb($id); | |
if ($limit) { | |
$sql .= ' LIMIT ' . $limit; | |
} | |
$link = $this->get_link(); | |
if (!$link) return false; | |
$execute = $this->execute($sql,false); | |
$link->CacheFlush(); | |
return $execute; | |
} | |
public function pdb($value) | |
{ | |
// Stripslashes | |
if (get_magic_quotes_runtime()) { | |
$value = stripslashes($value); | |
} | |
$link = $this->get_link(); | |
if (!$link) return false; | |
// Quote | |
switch(gettype($value)) { | |
case 'integer': | |
case 'double': | |
$escape = $value; | |
break; | |
case 'string': | |
$escape = "'" . mysql_real_escape_string($value) . "'"; | |
break; | |
case 'NULL': | |
$escape = 'NULL'; | |
break; | |
default: | |
$escape = "'" . mysql_real_escape_string($value) . "'"; | |
} | |
return $escape; | |
} | |
public function get_table_meta($table) | |
{ | |
$sql = 'SELECT * FROM ' . $table . ' LIMIT 1'; | |
if($this->enableCache) $r = $link->CacheGetRow($sql); | |
else $r = $link->GetRow($sql); | |
self::$queries++; | |
if ($r) { | |
return $r; | |
}else{ | |
PerchUtil::debug("Invalid query: " . $link->ErrorMsg(), 'error'); | |
return false; | |
} | |
} | |
} | |
?> |
Warning: this breaks pagination within perch blog. I've solved this by rewriting the perch_blog_custom function, I can provide details if anyone is interested.
I'd be interested. Are you still maintaining this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be sure to install the
adodb5
directory in to/admin/lib