1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 13:47:31 +02:00

Support SQL_CALC_FOUND_ROWS, alternative DB prefix, new DB link

This commit is contained in:
e107steved
2008-05-14 20:20:32 +00:00
parent 28c89a3271
commit 91c3e4eab2

View File

@@ -12,8 +12,8 @@
| GNU General Public License (http://gnu.org). | GNU General Public License (http://gnu.org).
| |
| $Source: /cvs_backup/e107_0.8/e107_handlers/mysql_class.php,v $ | $Source: /cvs_backup/e107_0.8/e107_handlers/mysql_class.php,v $
| $Revision: 1.22 $ | $Revision: 1.23 $
| $Date: 2007-12-30 11:03:57 $ | $Date: 2008-05-14 20:20:32 $
| $Author: e107steved $ | $Author: e107steved $
| |
+----------------------------------------------------------------------------+ +----------------------------------------------------------------------------+
@@ -24,13 +24,13 @@ if (!defined('e107_INIT')) { exit; }
$db_time = 0.0; // Global total time spent in all db object queries $db_time = 0.0; // Global total time spent in all db object queries
$db_mySQLQueryCount = 0; // Global total number of db object queries (all db's) $db_mySQLQueryCount = 0; // Global total number of db object queries (all db's)
$db_ConnectionID = NULL; $db_ConnectionID = NULL; // Stores ID for the first DB connection used - which should be the main E107 DB - then used as default
/** /**
* MySQL Abstraction class * MySQL Abstraction class
* *
* @package e107 * @package e107
* @version $Revision: 1.22 $ * @version $Revision: 1.23 $
* @author $Author: e107steved $ * @author $Author: e107steved $
*/ */
class db { class db {
@@ -39,6 +39,7 @@ class db {
var $mySQLuser; var $mySQLuser;
var $mySQLpassword; var $mySQLpassword;
var $mySQLdefaultdb; var $mySQLdefaultdb;
var $mySQLPrefix;
var $mySQLaccess; var $mySQLaccess;
var $mySQLresult; var $mySQLresult;
var $mySQLrows; var $mySQLrows;
@@ -48,22 +49,29 @@ class db {
var $mySQLinfo; var $mySQLinfo;
var $tabset; var $tabset;
var $total_results; // Total number of results
/** /**
* @return db * @return db
* @desc db constructor gets language options from the cookie or session * @desc db constructor gets language options from the cookie or session
* @access public * @access public
*/ */
function db() { function db()
global $pref, $eTraffic; {
$eTraffic->BumpWho('Create db object', 1); global $pref, $eTraffic, $db_defaultPrefix;
$langid = 'e107language_'.$pref['cookie_name']; $eTraffic->BumpWho('Create db object', 1);
if ($pref['user_tracking'] == 'session') { $this->mySQLPrefix = MPREFIX; // Set the default prefix - may be overridden
if (!isset($_SESSION[$langid])) { return; } $langid = 'e107language_'.$pref['cookie_name'];
$this->mySQLlanguage = $_SESSION[$langid]; if ($pref['user_tracking'] == 'session')
} else { {
if (!isset($_COOKIE[$langid])) { return; } if (!isset($_SESSION[$langid])) { return; }
$this->mySQLlanguage = $_COOKIE[$langid]; $this->mySQLlanguage = $_SESSION[$langid];
} }
else
{
if (!isset($_COOKIE[$langid])) { return; }
$this->mySQLlanguage = $_COOKIE[$langid];
}
} }
/** /**
@@ -84,41 +92,43 @@ class db {
* *
* @access public * @access public
*/ */
function db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb) function db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb, $newLink = FALSE, $mySQLPrefix = MPREFIX)
{ {
global $eTraffic, $db_ConnectionID; global $eTraffic, $db_ConnectionID, $db_defaultPrefix;
$eTraffic->BumpWho('db Connect', 1); $eTraffic->BumpWho('db Connect', 1);
$this->mySQLserver = $mySQLserver; $this->mySQLserver = $mySQLserver;
$this->mySQLuser = $mySQLuser; $this->mySQLuser = $mySQLuser;
$this->mySQLpassword = $mySQLpassword; $this->mySQLpassword = $mySQLpassword;
$this->mySQLdefaultdb = $mySQLdefaultdb; $this->mySQLdefaultdb = $mySQLdefaultdb;
$this->mySQLPrefix = $mySQLPrefix;
$temp = $this->mySQLerror; $temp = $this->mySQLerror;
$this->mySQLerror = FALSE; $this->mySQLerror = FALSE;
if(defined("USE_PERSISTANT_DB") && USE_PERSISTANT_DB == true){ if(defined("USE_PERSISTANT_DB") && USE_PERSISTANT_DB == true)
if (!$this->mySQLaccess = @mysql_pconnect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword)) { {
return 'e1'; if (!$this->mySQLaccess = @mysql_pconnect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword)) // No persistent link parameter permitted
} else { {
if (!@mysql_select_db($this->mySQLdefaultdb,$this->mySQLaccess)) { return 'e1';
return 'e2';
} else {
$this->dbError('dbConnect/SelectDB');
}
}
} else {
if (!$this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword)) {
return 'e1';
} else {
if (!@mysql_select_db($this->mySQLdefaultdb,$this->mySQLaccess)) {
return 'e2';
} else {
$this->dbError('dbConnect/SelectDB');
}
}
} }
}
else
{
if (!$this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink))
{
return 'e1';
}
}
if (!@mysql_select_db($this->mySQLdefaultdb,$this->mySQLaccess))
{
return 'e2';
}
$this->dbError('dbConnect/SelectDB');
if ($db_ConnectionID == NULL) $db_ConnectionID = $this->mySQLaccess; // Save the connection resource if ($db_ConnectionID == NULL) $db_ConnectionID = $this->mySQLaccess; // Save the connection resource
return TRUE;
} }
@@ -128,11 +138,13 @@ class db {
* @desc Enter description here... * @desc Enter description here...
* @access private * @access private
*/ */
function db_Mark_Time($sMarker) { function db_Mark_Time($sMarker)
if (E107_DEBUG_LEVEL > 0) { {
global $db_debug; if (E107_DEBUG_LEVEL > 0)
$db_debug->Mark_Time($sMarker); {
} global $db_debug;
$db_debug->Mark_Time($sMarker);
}
} }
/** /**
@@ -238,33 +250,35 @@ class db {
* *
* @access public * @access public
*/ */
function db_Select($table, $fields = '*', $arg = '', $mode = 'default', $debug = FALSE, $log_type = '', $log_remark = '') { function db_Select($table, $fields = '*', $arg = '', $mode = 'default', $debug = FALSE, $log_type = '', $log_remark = '')
global $db_mySQLQueryCount; {
global $db_mySQLQueryCount;
$table = $this->db_IsLang($table); $table = $this->db_IsLang($table);
$this->mySQLcurTable = $table; $this->mySQLcurTable = $table;
if ($arg != '' && $mode == 'default') if ($arg != '' && $mode == 'default')
{ {
if ($this->mySQLresult = $this->db_Query('SELECT '.$fields.' FROM '.MPREFIX.$table.' WHERE '.$arg, NULL, 'db_Select', $debug, $log_type, $log_remark)) { if ($this->mySQLresult = $this->db_Query('SELECT '.$fields.' FROM '.$this->mySQLPrefix.$table.' WHERE '.$arg, NULL, 'db_Select', $debug, $log_type, $log_remark)) {
$this->dbError('dbQuery'); $this->dbError('dbQuery');
return $this->db_Rows(); return $this->db_Rows();
} else { } else {
$this->dbError("db_Select (SELECT $fields FROM ".MPREFIX."{$table} WHERE {$arg})"); $this->dbError("db_Select (SELECT $fields FROM ".$this->mySQLPrefix."{$table} WHERE {$arg})");
return FALSE; return FALSE;
} }
} elseif ($arg != '' && $mode != 'default') { } elseif ($arg != '' && $mode != 'default') {
if ($this->mySQLresult = $this->db_Query('SELECT '.$fields.' FROM '.MPREFIX.$table.' '.$arg, NULL, 'db_Select', $debug, $log_type, $log_remark)) { if ($this->mySQLresult = $this->db_Query('SELECT '.$fields.' FROM '.$this->mySQLPrefix.$table.' '.$arg, NULL, 'db_Select', $debug, $log_type, $log_remark)) {
$this->dbError('dbQuery'); $this->dbError('dbQuery');
return $this->db_Rows(); return $this->db_Rows();
} else { } else {
$this->dbError("db_Select (SELECT {$fields} FROM ".MPREFIX."{$table} {$arg})"); $this->dbError("db_Select (SELECT {$fields} FROM ".$this->mySQLPrefix."{$table} {$arg})");
return FALSE; return FALSE;
} }
} else { } else {
if ($this->mySQLresult = $this->db_Query('SELECT '.$fields.' FROM '.MPREFIX.$table, NULL, 'db_Select', $debug, $log_type, $log_remark)) { if ($this->mySQLresult = $this->db_Query('SELECT '.$fields.' FROM '.$this->mySQLPrefix.$table, NULL, 'db_Select', $debug, $log_type, $log_remark)) {
$this->dbError('dbQuery'); $this->dbError('dbQuery');
return $this->db_Rows(); return $this->db_Rows();
} else { } else {
$this->dbError("db_Select (SELECT {$fields} FROM ".MPREFIX."{$table})"); $this->dbError("db_Select (SELECT {$fields} FROM ".$this->mySQLPrefix."{$table})");
return FALSE; return FALSE;
} }
} }
@@ -289,11 +303,11 @@ class db {
{ {
$keyList= "`".implode("`,`", array_keys($arg))."`"; $keyList= "`".implode("`,`", array_keys($arg))."`";
$valList= "'".implode("','", $arg)."'"; $valList= "'".implode("','", $arg)."'";
$query = "INSERT INTO `".MPREFIX."{$table}` ({$keyList}) VALUES ({$valList})"; $query = "INSERT INTO `".$this->mySQLPrefix."{$table}` ({$keyList}) VALUES ({$valList})";
} }
else else
{ {
$query = 'INSERT INTO '.MPREFIX."{$table} VALUES ({$arg})"; $query = 'INSERT INTO '.$this->mySQLPrefix."{$table} VALUES ({$arg})";
} }
if(!$this->mySQLaccess) if(!$this->mySQLaccess)
@@ -340,7 +354,7 @@ class db {
$this->mySQLaccess = $db_ConnectionID; $this->mySQLaccess = $db_ConnectionID;
} }
if ($result = $this->mySQLresult = $this->db_Query('UPDATE '.MPREFIX.$table.' SET '.$arg, NULL, 'db_Update', $debug, $log_type, $log_remark)) { if ($result = $this->mySQLresult = $this->db_Query('UPDATE '.$this->mySQLPrefix.$table.' SET '.$arg, NULL, 'db_Update', $debug, $log_type, $log_remark)) {
$result = mysql_affected_rows($this->mySQLaccess); $result = mysql_affected_rows($this->mySQLaccess);
if ($result == -1) return FALSE; // Error return from mysql_affected_rows if ($result == -1) return FALSE; // Error return from mysql_affected_rows
return $result; return $result;
@@ -378,7 +392,7 @@ class db {
} }
$vars = ''; $vars = '';
} }
if ($result = $this->mySQLresult = $this->db_Query('UPDATE '.MPREFIX.$table.' SET '.$new_data.$vars.' '.$arg, NULL, 'db_UpdateArray', $debug, $log_type, $log_remark)) if ($result = $this->mySQLresult = $this->db_Query('UPDATE '.$this->mySQLPrefix.$table.' SET '.$new_data.$vars.' '.$arg, NULL, 'db_UpdateArray', $debug, $log_type, $log_remark))
{ {
$result = mysql_affected_rows($this->mySQLaccess); $result = mysql_affected_rows($this->mySQLaccess);
if ($result == -1) return FALSE; // Error return from mysql_affected_rows if ($result == -1) return FALSE; // Error return from mysql_affected_rows
@@ -447,7 +461,7 @@ class db {
} }
$this->mySQLcurTable = $table; $this->mySQLcurTable = $table;
$query='SELECT COUNT'.$fields.' FROM '.MPREFIX.$table.' '.$arg; $query='SELECT COUNT'.$fields.' FROM '.$this->mySQLPrefix.$table.' '.$arg;
if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark)) { if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark)) {
$rows = $this->mySQLrows = @mysql_fetch_array($this->mySQLresult); $rows = $this->mySQLrows = @mysql_fetch_array($this->mySQLresult);
return $rows[0]; return $rows[0];
@@ -504,14 +518,14 @@ class db {
if (!$arg) { if (!$arg) {
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.MPREFIX.$table, NULL, 'db_Delete', $debug, $log_type, $log_remark)) { if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table, NULL, 'db_Delete', $debug, $log_type, $log_remark)) {
return $result; return $result;
} else { } else {
$this->dbError("db_Delete ($arg)"); $this->dbError("db_Delete ($arg)");
return FALSE; return FALSE;
} }
} else { } else {
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.MPREFIX.$table.' WHERE '.$arg, NULL, 'db_Delete', $debug, $log_type, $log_remark)) { if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table.' WHERE '.$arg, NULL, 'db_Delete', $debug, $log_type, $log_remark)) {
$tmp = mysql_affected_rows($this->mySQLaccess); $tmp = mysql_affected_rows($this->mySQLaccess);
return $tmp; return $tmp;
} else { } else {
@@ -569,7 +583,7 @@ class db {
/* /*
changes by jalist 19/01/05: changes by jalist 19/01/05:
added string replace on table prefix to tidy up long database queries added string replace on table prefix to tidy up long database queries
usage: instead of sending "SELECT * FROM ".MPREFIX."table", do "SELECT * FROM #table" usage: instead of sending "SELECT * FROM ".$this->mySQLPrefix."table", do "SELECT * FROM #table"
Returns result compatible with mysql_query - may be TRUE for some results, resource ID for others Returns result compatible with mysql_query - may be TRUE for some results, resource ID for others
*/ */
@@ -596,6 +610,12 @@ class db {
else else
{ // Successful query which does return a row count - get the count and return it { // Successful query which does return a row count - get the count and return it
$this->dbError('db_Select_gen'); $this->dbError('db_Select_gen');
if (strpos($query,'SQL_CALC_FOUND_ROWS') !== FALSE)
{ // Need to get the total record count as well. Return code is a resource identifier
$fr = mysql_query("SELECT FOUND_ROWS()", $this->mySQLaccess);
$rc = mysql_fetch_array($fr);
$this->total_results = $rc['FOUND_ROWS()'];
}
return $this->db_Rows(); return $this->db_Rows();
} }
} }
@@ -608,7 +628,7 @@ class db {
$this->mySQLcurTable = $table; $this->mySQLcurTable = $table;
$this->tabset = true; $this->tabset = true;
} }
return ' `'.MPREFIX.$table.'`'.substr($matches[0],-1); return ' `'.$this->mySQLPrefix.$table.'`'.substr($matches[0],-1);
} }
@@ -678,12 +698,12 @@ class db {
} }
foreach($mySQLtablelist as $tab){ foreach($mySQLtablelist as $tab){
if(stristr($tab, MPREFIX."lan_") !== FALSE){ if(stristr($tab, $this->mySQLPrefix."lan_") !== FALSE){
$tmp = explode("_",str_replace(MPREFIX."lan_","",$tab)); $tmp = explode("_",str_replace($this->mySQLPrefix."lan_","",$tab));
$lng = $tmp[0]; $lng = $tmp[0];
foreach($table as $t){ foreach($table as $t){
if(eregi($t."$",$tab)){ if(eregi($t."$",$tab)){
$lanlist[$lng][MPREFIX.$t] = $tab; $lanlist[$lng][$this->mySQLPrefix.$t] = $tab;
} }
} }
} }
@@ -692,7 +712,7 @@ class db {
} }
// ------------------------- // -------------------------
if (in_array(MPREFIX.$mltable, $mySQLtablelist)) { if (in_array($this->mySQLPrefix.$mltable, $mySQLtablelist)) {
return $mltable; return $mltable;
} }
return $table; return $table;
@@ -750,7 +770,7 @@ class db {
function db_Query_all($query,$debug=""){ function db_Query_all($query,$debug=""){
$error = ""; $error = "";
$query = str_replace("#",MPREFIX,$query); $query = str_replace("#",$this->mySQLPrefix,$query);
if(!$this->db_Query($query)){ // run query on the default language first. if(!$this->db_Query($query)){ // run query on the default language first.
$error .= $query. " failed"; $error .= $query. " failed";
@@ -758,8 +778,8 @@ class db {
$tmp = explode(" ",$query); $tmp = explode(" ",$query);
foreach($tmp as $val){ foreach($tmp as $val){
if(strpos($val,MPREFIX) !== FALSE){ if(strpos($val,$this->mySQLPrefix) !== FALSE){
$table[] = str_replace(MPREFIX,"",$val); $table[] = str_replace($this->mySQLPrefix,"",$val);
$search[] = $val; $search[] = $val;
} }
} }
@@ -803,7 +823,7 @@ class db {
$this->mySQLaccess = $db_ConnectionID; $this->mySQLaccess = $db_ConnectionID;
} }
$result = mysql_query("SHOW COLUMNS FROM ".MPREFIX.$table,$this->mySQLaccess); $result = mysql_query("SHOW COLUMNS FROM ".$this->mySQLPrefix.$table,$this->mySQLaccess);
if (mysql_num_rows($result) > 0) if (mysql_num_rows($result) > 0)
{ {
$c=0; $c=0;
@@ -863,7 +883,7 @@ class db {
* LIMIT 0 is 3x slower than LIMIT 1 * LIMIT 0 is 3x slower than LIMIT 1
*/ */
function db_Table_exists($table){ function db_Table_exists($table){
$res = $this->db_Query("SELECT 1 FROM ".MPREFIX.$table." LIMIT 1"); // error if not there $res = $this->db_Query("SELECT 1 FROM ".$this->mySQLPrefix.$table." LIMIT 1"); // error if not there
if ($res) return TRUE; if ($res) return TRUE;
return FALSE; return FALSE;
} }