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

PDO class optimization

This commit is contained in:
Cameron
2019-02-10 16:19:25 -08:00
parent 42147e2227
commit 4e9f47c8b4
6 changed files with 89 additions and 124 deletions

View File

@@ -47,6 +47,17 @@
}
/**
* Return a list of all registered time markers.
* @return array
*/
public function getTimeMarkers()
{
return $this->aTimeMarks;
}
function e107_db_debug()
{

View File

@@ -1624,7 +1624,7 @@ class e107
*
* @return e107_db_debug
*/
public static function getDebug() //XXX Discuss - possible with current setup?
public static function getDebug()
{
return self::getSingleton('e107_db_debug', true);
}

View File

@@ -162,4 +162,10 @@
$this->setErrorReporting($mode);
}
public function db_Mark_Time($sMarker)
{
$this->markTime($sMarker);
}
}

View File

@@ -33,7 +33,7 @@ class e_db_pdo implements e_db
protected $mySQLlastErrText = ''; // Text of last error - now protected, use getLastErrorText()
protected $mySQLlastQuery = '';
public $mySQLcurTable;
protected $mySQLcurTable;
public $mySQLlanguage;
public $mySQLinfo;
public $tabset;
@@ -51,8 +51,16 @@ class e_db_pdo implements e_db
private $pdo = true; // using PDO or not.
private $pdoBind = false;
/** @var e107_traffic */
private $traffic;
/** @var e107_db_debug */
private $dbg;
private $debugMode = false;
private $queryCount = 0;
/**
* Constructor - gets language options from the cookie or session
@@ -60,17 +68,9 @@ class e_db_pdo implements e_db
*/
public function __construct()
{
/*
if((PHP_MAJOR_VERSION > 6) || !function_exists('mysql_connect') || (defined('e_PDO') && e_PDO === true))
{
$this->pdo = true;
}
*/
$this->traffic = e107::getSingleton('e107_traffic');
$this->traffic->BumpWho('Create db object', 1);
$this->mySQLPrefix = MPREFIX; // Set the default prefix - may be overridden
if($port = e107::getMySQLConfig('port'))
@@ -78,9 +78,19 @@ class e_db_pdo implements e_db
$this->mySQLport = intval($port);
}
// Detect is already done in language handler, use it if not too early
if(defined('e_LANGUAGE')) $this->mySQLlanguage = e107::getLanguage()->e_language;
if(defined('e_LANGUAGE'))
{
$this->mySQLlanguage = e107::getLanguage()->e_language;
}
if (E107_DEBUG_LEVEL > 0)
{
$this->debugMode = true;
}
$this->dbg = e107::getDebug();
}
function getPDO()
@@ -88,6 +98,11 @@ class e_db_pdo implements e_db
return true;
}
function debugMode($bool)
{
$this->debugMode = (bool) $bool;
}
function getMode()
{
@@ -96,83 +111,6 @@ class e_db_pdo implements e_db
return $row['@@sql_mode'];
}
/**
* Connects to mySQL server and selects database - generally not required if your table is in the main DB.<br />
* <br />
* Example using e107 database with variables defined in e107_config.php:<br />
* <code>$sql = new db;
* $sql->db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb);</code>
* <br />
* OR to connect an other database:<br />
* <code>$sql = new db;
* $sql->db_Connect('url_server_database', 'user_database', 'password_database', 'name_of_database');</code>
*
* @param string $mySQLserver IP Or hostname of the MySQL server
* @param string $mySQLuser MySQL username
* @param string $mySQLpassword MySQL Password
* @param string $mySQLdefaultdb The database schema to connect to
* @param string $newLink force a new link connection if TRUE. Default FALSE
* @param string $mySQLPrefix Tables prefix. Default to $mySQLPrefix from e107_config.php
* @return null|string error code
*//*
public function db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb, $newLink = FALSE, $mySQLPrefix = MPREFIX)
{
global $db_ConnectionID, $db_defaultPrefix;
e107::getSingleton('e107_traffic')->BumpWho('db Connect', 1);
$this->mySQLserver = $mySQLserver;
$this->mySQLuser = $mySQLuser;
$this->mySQLpassword = $mySQLpassword;
$this->mySQLdefaultdb = $mySQLdefaultdb;
$this->mySQLPrefix = $mySQLPrefix;
$this->mySQLerror = false;
if(strpos($mySQLserver,':')!==false && substr_count($mySQLserver, ':')===1)
{
list($this->mySQLserver,$this->mySQLport) = explode(':',$mySQLserver,2);
}
try
{
$this->mySQLaccess = new PDO("mysql:host=".$this->mySQLserver."; port=".$this->mySQLport, $this->mySQLuser, $this->mySQLpassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $ex)
{
$this->mySQLlastErrText = $ex->getMessage();
$this->mySQLlastErrNum = $ex->getCode();
return 'e1';
}
$this->mySqlServerInfo = $this->mySQLaccess->query('select version()')->fetchColumn(); // We always need this for db_Set_Charset() - so make generally available
// Set utf8 connection?
//@TODO: simplify when yet undiscovered side-effects will be fixed
$this->setCharset();
$this->setSQLMode();
// if ($this->pdo!== true && !@mysql_select_db($this->mySQLdefaultdb, $this->mySQLaccess))
if (!$this->database($this->mySQLdefaultdb))
{
return 'e2';
}
$this->dbError('dbConnect/SelectDB');
// Save the connection resource
if ($db_ConnectionID == null)
{
$db_ConnectionID = $this->mySQLaccess;
}
return true;
}
*/
/**
* Connect ONLY - used in v2.x
@@ -186,7 +124,7 @@ class e_db_pdo implements e_db
*/
public function connect($mySQLserver, $mySQLuser, $mySQLpassword, $newLink = false)
{
global $db_ConnectionID, $db_defaultPrefix;
global $db_ConnectionID;
$this->traffic->BumpWho('db Connect', 1);
@@ -214,7 +152,7 @@ class e_db_pdo implements e_db
{
$this->mySQLlastErrText = $ex->getMessage();
$this->mySQLLastErrNum = $ex->getCode();
// e107::getDebug()->log($ex); // Useful for Debug. breaks testing.
$this->dbg->log($this->mySQLlastErrText); // Useful for Debug. breaks testing.
return false;
}
@@ -294,14 +232,14 @@ class e_db_pdo implements e_db
* @desc Enter description here...
* @access private
*/
function db_Mark_Time($sMarker)
function markTime($sMarker)
{
if (E107_DEBUG_LEVEL > 0)
if($this->debugMode !== true)
{
/** @var e107_db_debug $db_debug */
global $db_debug;
$db_debug->Mark_Time($sMarker);
return null;
}
$this->dbg->Mark_Time($sMarker);
}
@@ -347,7 +285,7 @@ class e_db_pdo implements e_db
* @param string|array $query
* @param string $query['PREPARE'] PDO Format query.
*@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.
* FALSE indicates an error
* For SELECT, SHOW, DESCRIBE, EXPLAIN and others returning a result set, returns a resource
@@ -355,8 +293,8 @@ class e_db_pdo implements e_db
*/
public function db_Query($query, $rli = NULL, $qry_from = '', $debug = FALSE, $log_type = '', $log_remark = '')
{
global $db_time,$db_mySQLQueryCount,$queryinfo;
$db_mySQLQueryCount++;
global $db_time, $queryinfo;
$this->queryCount++;
$this->mySQLlastQuery = $query;
@@ -439,7 +377,7 @@ class e_db_pdo implements e_db
$this->mySQLresult = $sQryRes;
if (!E107_DEBUG_LEVEL)
if ($this->debugMode !== true)
{
$this->total_results = false;
}
@@ -455,44 +393,45 @@ class e_db_pdo implements e_db
$this->total_results = intval($rc);
}
if (E107_DEBUG_LEVEL)
if ($this->debugMode === true)
{
/** @var $db_debug e107_db_debug */
global $db_debug;
$aTrace = debug_backtrace();
$pTable = $this->mySQLcurTable;
if (!strlen($pTable)) {
if(!strlen($pTable))
{
$pTable = '(complex query)';
} else {
}
else
{
$this->mySQLcurTable = ''; // clear before next query
}
if(is_object($db_debug))
if(is_object($this->dbg))
{
$buglink = is_null($rli) ? $this->mySQLaccess : $rli;
if(is_array($query))
{
$query = "PREPARE: ".$query['PREPARE']."<br />BIND:".print_a($query['BIND'],true); // ,true);
$query = "PREPARE: " . $query['PREPARE'] . "<br />BIND:" . print_a($query['BIND'], true); // ,true);
}
if(isset($ex) && is_object($ex))
{
$query = $ex->getMessage();
$query .= print_a($ex->getTrace(),true);
$query .= print_a($ex->getTrace(), true);
}
if($buglink instanceof PDO)
{
$db_debug->Mark_Query($query, 'PDO', $sQryRes, $aTrace, $mytime, $pTable);
$this->dbg->Mark_Query($query, 'PDO', $sQryRes, $aTrace, $mytime, $pTable);
}
}
else
{
// echo "what happened to db_debug??!!<br />";
}
}
return $sQryRes;
@@ -540,7 +479,7 @@ class e_db_pdo implements e_db
* @param boolean $multi if true, fetch all (multi mode)
* @param string $indexField field name to be used for indexing when in multi mode
* @param boolean $debug
* @return string|array
* @return mixed
*/
public function retrieve($table, $fields = null, $where=null, $multi = false, $indexField = null, $debug = false)
{
@@ -639,6 +578,8 @@ class e_db_pdo implements e_db
break;
}
return null;
}
/**
@@ -657,7 +598,6 @@ class e_db_pdo implements e_db
*/
public function select($table, $fields = '*', $arg = '', $noWhere = false, $debug = FALSE, $log_type = '', $log_remark = '')
{
global $db_mySQLQueryCount;
$table = $this->hasLanguage($table);
@@ -1549,8 +1489,6 @@ class e_db_pdo implements e_db
*/
public function gen($query, $debug = FALSE, $log_type = '', $log_remark = '')
{
global $db_mySQLQueryCount;
$this->tabset = FALSE;
$query .= " "; // temp fix for failing regex below, when there is no space after the table name;
@@ -1852,8 +1790,7 @@ class e_db_pdo implements e_db
*/
public function queryCount()
{
global $db_mySQLQueryCount;
return $db_mySQLQueryCount;
return $this->queryCount;
}
@@ -2307,6 +2244,7 @@ class e_db_pdo implements e_db
return ($mode == 'lan') ? $lan : $nolan;
}
return array();
}

View File

@@ -103,6 +103,9 @@ class e_db_mysql
private $pdo = false; // using PDO or not.
private $pdoBind= false;
/** @var e107_db_debug */
protected $dbg = null;
/**
* Constructor - gets language options from the cookie or session
@@ -141,6 +144,12 @@ class e_db_mysql
}*/
// Detect is already done in language handler, use it if not too early
if(defined('e_LANGUAGE')) $this->mySQLlanguage = e107::getLanguage()->e_language;
if (E107_DEBUG_LEVEL > 0)
{
$this->dbg = e107::getDebug();
}
}
function getPDO()
@@ -408,12 +417,13 @@ class e_db_mysql
*/
function db_Mark_Time($sMarker)
{
if (E107_DEBUG_LEVEL > 0)
if($this->dbg === null)
{
/** @var e107_db_debug $db_debug */
global $db_debug;
$db_debug->Mark_Time($sMarker);
return null;
}
$this->dbg->Mark_Time($sMarker);
}

View File

@@ -170,7 +170,7 @@ class e107_traffic
$this->aTrafficWho[$sWhat][] = "$sFile($sLine)";
}
function Calibrate($tObject, $count = 10)
function Calibrate(e107_traffic $tObject, $count = 10)
{
if (!defined("E107_DBG_TRAFFIC") || !E107_DBG_TRAFFIC)
{