1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-03 13:17:24 +02:00

Mysql class optimization and minor fixes.

This commit is contained in:
Cameron
2019-02-11 15:40:10 -08:00
parent dfea1cbce2
commit c34f8b41f3

View File

@@ -104,8 +104,9 @@ class e_db_mysql
private $pdoBind= false; private $pdoBind= false;
/** @var e107_db_debug */ /** @var e107_db_debug */
protected $dbg = null; private $dbg;
private $debugMode = false;
/** /**
* Constructor - gets language options from the cookie or session * Constructor - gets language options from the cookie or session
@@ -147,9 +148,11 @@ class e_db_mysql
if (E107_DEBUG_LEVEL > 0) if (E107_DEBUG_LEVEL > 0)
{ {
$this->dbg = e107::getDebug(); $this->debugMode = true;
} }
$this->dbg = e107::getDebug();
} }
function getPDO() function getPDO()
@@ -157,6 +160,10 @@ class e_db_mysql
return $this->pdo; return $this->pdo;
} }
function debugMode($bool)
{
$this->debugMode = (bool) $bool;
}
function getMode() function getMode()
{ {
@@ -312,7 +319,7 @@ class e_db_mysql
{ {
$this->mySQLlastErrText = $ex->getMessage(); $this->mySQLlastErrText = $ex->getMessage();
$this->mySQLLastErrNum = $ex->getCode(); $this->mySQLLastErrNum = $ex->getCode();
e107::getDebug()->log($ex); // Useful for Debug. $this->dbg->log($this->mySQLlastErrText); // Useful for Debug.
return false; return false;
} }
@@ -349,6 +356,7 @@ class e_db_mysql
*/ */
public function getServerInfo() public function getServerInfo()
{ {
$this->provide_mySQLaccess();
return $this->mySqlServerInfo; return $this->mySqlServerInfo;
} }
@@ -417,7 +425,7 @@ class e_db_mysql
*/ */
function db_Mark_Time($sMarker) function db_Mark_Time($sMarker)
{ {
if($this->dbg === null) if($this->debugMode !== true)
{ {
return null; return null;
} }
@@ -465,7 +473,7 @@ class e_db_mysql
* @param string|array $query * @param string|array $query
* @param string $query['PREPARE'] PDO Format query. * @param string $query['PREPARE'] PDO Format query.
*@param array $query['BIND'] eg. array['my_field'] = array('value'=>'whatever', 'type'=>'str'); *@param array $query['BIND'] eg. array['my_field'] = array('value'=>'whatever', 'type'=>'str');
* @param unknown $rli * @param object $rli
* @return boolean|PDOStatement | resource - as mysql_query() function. * @return boolean|PDOStatement | resource - as mysql_query() function.
* FALSE indicates an error * FALSE indicates an error
* For SELECT, SHOW, DESCRIBE, EXPLAIN and others returning a result set, returns a resource * For SELECT, SHOW, DESCRIBE, EXPLAIN and others returning a result set, returns a resource
@@ -594,7 +602,7 @@ class e_db_mysql
} }
if (E107_DEBUG_LEVEL) if ($this->debugMode === true)
{ {
/** @var $db_debug e107_db_debug */ /** @var $db_debug e107_db_debug */
global $db_debug; global $db_debug;
@@ -1991,7 +1999,7 @@ class e_db_mysql
* @param string fields to retrieve * @param string fields to retrieve
* @desc returns fields as structured array * @desc returns fields as structured array
* @access public * @access public
* @return rows of the database as an array. * @return array rows of the database as an array.
*/ */
function rows($fields = 'ALL', $amount = FALSE, $maximum = FALSE, $ordermode=FALSE) function rows($fields = 'ALL', $amount = FALSE, $maximum = FALSE, $ordermode=FALSE)
{ {
@@ -2516,7 +2524,7 @@ class e_db_mysql
* @param $table * @param $table
* @return bool * @return bool
*/ */
function isEmpty($table) function isEmpty($table=null)
{ {
if(empty($table)) if(empty($table))
{ {
@@ -2543,16 +2551,26 @@ class e_db_mysql
*/ */
private function db_mySQLtableList($language='') private function db_mySQLtableList($language='')
{ {
$database = !empty($this->mySQLdefaultdb) ? "FROM ".$this->mySQLdefaultdb : "";
$prefix = $this->mySQLPrefix;
if(strpos($prefix, ".") !== false) // eg. `my_database`.$prefix
{
$tmp = explode(".",$prefix);
$prefix = $tmp[1];
}
if($language) if($language)
{ {
if(!isset($this->mySQLtableListLanguage[$language])) if(!isset($this->mySQLtableListLanguage[$language]))
{ {
$table = array(); $table = array();
if($res = $this->db_Query("SHOW TABLES LIKE '".$this->mySQLPrefix."lan_".strtolower($language)."%' ")) if($res = $this->db_Query("SHOW TABLES ".$database." LIKE '".$prefix."lan_".strtolower($language)."%' "))
{ {
while($rows = $this->fetch('num')) while($rows = $this->fetch('num'))
{ {
$table[] = str_replace($this->mySQLPrefix,"",$rows[0]); $table[] = str_replace($prefix,"",$rows[0]);
} }
} }
$ret = array($language=>$table); $ret = array($language=>$table);
@@ -2568,9 +2586,9 @@ class e_db_mysql
{ {
$table = array(); $table = array();
if($res = $this->db_Query("SHOW TABLES LIKE '".$this->mySQLPrefix."%' ")) if($res = $this->db_Query("SHOW TABLES ".$database." LIKE '".$prefix."%' "))
{ {
$length = strlen($this->mySQLPrefix); $length = strlen($prefix);
while($rows = $this->fetch('num')) while($rows = $this->fetch('num'))
{ {
$table[] = substr($rows[0],$length); $table[] = substr($rows[0],$length);
@@ -2669,7 +2687,7 @@ class e_db_mysql
return false; return false;
} }
if($fields == '*') if($fields === '*')
{ {
$fields = $this->db_FieldList($table); $fields = $this->db_FieldList($table);
unset($fields[0]); // Remove primary_id. unset($fields[0]); // Remove primary_id.
@@ -2680,6 +2698,12 @@ class e_db_mysql
$fieldList = $fields; $fieldList = $fields;
} }
if(empty($fields))
{
$this->mysqlLastErrText = "copyRow \$fields list was empty";
return false;
}
$id = $this->gen("INSERT INTO ".$this->mySQLPrefix.$table."(".$fieldList.") SELECT ".$fieldList." FROM ".$this->mySQLPrefix.$table." WHERE ".$args); $id = $this->gen("INSERT INTO ".$this->mySQLPrefix.$table."(".$fieldList.") SELECT ".$fieldList." FROM ".$this->mySQLPrefix.$table." WHERE ".$args);
$lastInsertId = $this->lastInsertId(); $lastInsertId = $this->lastInsertId();
return ($id && $lastInsertId) ? $lastInsertId : false; return ($id && $lastInsertId) ? $lastInsertId : false;
@@ -2707,7 +2731,7 @@ class e_db_mysql
$row = $this->fetch('num'); $row = $this->fetch('num');
$qry = $row[1]; $qry = $row[1];
// $qry = str_replace($old, $new, $qry); // $qry = str_replace($old, $new, $qry);
$qry = preg_replace("#CREATE\sTABLE\s`{0,1}".$old."`{0,1}\s#", "CREATE TABLE `{$new}` ", $qry, 1); // More selective search $qry = preg_replace("#CREATE\sTABLE\s`{0,1}".$old."`{0,1}\s#", "CREATE TABLE {$new} ", $qry, 1); // More selective search
} }
else else
{ {
@@ -2875,7 +2899,7 @@ class e_db_mysql
/** /**
* @return text string relating to error (empty string if no error) * @return string relating to error (empty string if no error)
* @param string $from * @param string $from
* @desc Calling method from within this class * @desc Calling method from within this class
* @access private * @access private
@@ -2889,12 +2913,12 @@ class e_db_mysql
{ {
$this->mySQLerror = true; $this->mySQLerror = true;
if($this->mySQLlastErrNum == 0) if($this->mySQLlastErrNum === 0)
{ {
return null; return null;
} }
return $this->mySQLlastErrText; return $from." :: ".$this->mySQLlastErrText;
} }