diff --git a/classes/Db.php b/classes/Db.php
index 9e4f8bfd3..92e4941dd 100644
--- a/classes/Db.php
+++ b/classes/Db.php
@@ -39,9 +39,6 @@ abstract class DbCore
/** @var string Database password (eg. can be empty !) */
protected $_password;
- /** @var string Database type (MySQL, PgSQL) */
- protected $_type;
-
/** @var string Database name */
protected $_database;
@@ -61,8 +58,19 @@ abstract class DbCore
// array('server' => '192.168.0.15', 'user' => 'rep', 'password' => '123456', 'database' => 'rep'),
// array('server' => '192.168.0.3', 'user' => 'myuser', 'password' => 'mypassword', 'database' => 'mydatabase'),
);
-
+
+ /**
+ * Store last executed query
+ *
+ * @var string
+ */
protected $_lastQuery;
+
+ /**
+ * Last cached query
+ *
+ * @var string
+ */
protected $_lastCached;
/**
@@ -161,8 +169,13 @@ abstract class DbCore
}
if (!isset(self::$_instance[$idServer]))
- self::$_instance[$idServer] = new MySQL(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']);
-
+ {
+ $class = _DB_TYPE_;
+ if (!class_exists($class))
+ $class = 'MySQL';
+ self::$_instance[$idServer] = new $class(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']);
+ }
+
return self::$_instance[$idServer];
}
@@ -419,7 +432,7 @@ abstract class DbCore
$this->_lastQuery = $sql;
if ($use_cache AND _PS_CACHE_ENABLED_)
Cache::getInstance()->deleteQuery($sql);
- return $result;
+ return $result;
}
/**
diff --git a/classes/MySQL.php b/classes/MySQL.php
index 844825ec8..f6fd468c0 100644
--- a/classes/MySQL.php
+++ b/classes/MySQL.php
@@ -53,9 +53,7 @@ class MySQLCore extends Db
*/
public function disconnect()
{
- if ($this->_link)
- @mysql_close($this->_link);
- $this->_link = false;
+ mysql_close($this->_link);
}
/**
diff --git a/override/classes/_Db.php b/override/classes/_Db.php
new file mode 100644
index 000000000..e0c672afa
--- /dev/null
+++ b/override/classes/_Db.php
@@ -0,0 +1,102 @@
+
+* @copyright 2007-2011 PrestaShop SA
+* @version Release: $Revision$
+* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
+* International Registered Trademark & Property of PrestaShop SA
+*/
+
+abstract class Db extends DbCore
+{
+ /**
+ * Add SQL_NO_CACHE in SELECT queries
+ *
+ * @var unknown_type
+ */
+ public $disableCache = true;
+
+ /**
+ * Total of queries
+ *
+ * @var int
+ */
+ public $count = 0;
+
+ /**
+ * List of queries
+ *
+ * @var array
+ */
+ public $queries = array();
+
+ /**
+ * List of uniq queries (replace numbers by XX)
+ *
+ * @var array
+ */
+ public $uniqQueries = array();
+
+ /**
+ * List of tables
+ *
+ * @var array
+ */
+ public $tables = array();
+
+ /**
+ * Execute the query and log some informations
+ *
+ * @see DbCore::query()
+ */
+ public function query($sql)
+ {
+ $uniqSql = preg_replace('/[0-9]+/', 'XX', $sql);
+ if (!isset($this->uniqQueries[$uniqSql]))
+ $this->uniqQueries[$uniqSql] = 0;
+ $this->uniqQueries[$uniqSql]++;
+
+ // No cache for query
+ if ($this->disableCache)
+ $sql = preg_replace('/^select /i', 'SELECT SQL_NO_CACHE ', trim($sql));
+
+ // Get tables in quer
+ preg_match_all('/(from|join)\s+`?'._DB_PREFIX_.'([a-z0-9_-]+)/ui', $sql, $matches);
+ foreach ($matches[2] as $table)
+ {
+ if (!isset($this->tables[$table]))
+ $this->tables[$table] = 0;
+ $this->tables[$table]++;
+ }
+
+ // Execute query
+ $start = microtime(true);
+ $result = parent::query($sql);
+ $end = microtime(true);
+
+ $this->queries[] = array(
+ 'query' => $sql,
+ 'time' => ($end - $start)
+ );
+
+ return $result;
+ }
+}
\ No newline at end of file
diff --git a/override/classes/_FrontController.php b/override/classes/_FrontController.php
index b09efb09f..518b0e199 100755
--- a/override/classes/_FrontController.php
+++ b/override/classes/_FrontController.php
@@ -243,9 +243,9 @@ class FrontController extends FrontControllerCore
$totalSize += filesize($file);
$totalQueryTime = 0;
- foreach (Db::getInstance()->queriesTime as $time)
- $totalQueryTime += $time;
-
+ foreach (Db::getInstance()->queries as $data)
+ $totalQueryTime += $data['time'];
+
$hooktime = Module::getHookTime();
arsort($hooktime);
$totalHookTime = 0;
@@ -302,17 +302,11 @@ class FrontController extends FrontControllerCore
displayContent: '.$this->displayMemoryColor(($this->_memory[5] - $this->_memory[4])).'displayFooter: '.$this->displayMemoryColor(($this->_memory[6] - $this->_memory[5])).'
';
echo '';
-
- $countByTypes = '';
- foreach (Db::getInstance()->countTypes as $type => $count)
- if ($count)
- $countByTypes .= ''.$count.' x '.$type.'';
- $countByTypes = rtrim($countByTypes, ' |');
-
+
echo '
-
SQL Queries: '.$this->displaySQLQueries(Db::getInstance()->count).'
-
+
DB type: '.get_class(Db::getInstance()).'
+
SQL Queries: '.$this->displaySQLQueries(count(Db::getInstance()->queries)).'
Time spent querying: '.$this->displayLoadTimeColor($totalQueryTime).'
@@ -336,15 +330,14 @@ class FrontController extends FrontControllerCore
-
';
- $queries = Db::getInstance()->queriesTime;
- arsort($queries);
- foreach ($queries as $q => $time)
- echo $hr.'
getTimeColor($time * 1000).'>'.round($time * 1000, 3).' ms '.$q;
+
';
+ $queries = Db::getInstance()->queries;
+ foreach ($queries as $data)
+ echo $hr.'
getTimeColor($data['time'] * 1000).'>'.round($data['time'] * 1000, 3).' ms '.$data['query'];
echo '
-
';
- $queries = Db::getInstance()->queries;
+
';
+ $queries = Db::getInstance()->uniqQueries;
arsort($queries);
foreach ($queries as $q => $nb)
echo $hr.'
getQueryColor($nb).'>'.$nb.' '.$q;
diff --git a/override/classes/_MySQL.php b/override/classes/_MySQL.php
deleted file mode 100644
index addb2678f..000000000
--- a/override/classes/_MySQL.php
+++ /dev/null
@@ -1,213 +0,0 @@
-
-* @copyright 2007-2011 PrestaShop SA
-* @version Release: $Revision: 6844 $
-* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
-* International Registered Trademark & Property of PrestaShop SA
-*/
-
-//
-// IMPORTANT : don't forget to delete the underscore _ in the file name if you want to use it !
-//
-
-class MySQL extends MySQLCore
-{
- public $count = 0;
- public $queries = array();
- public $queriesTime = array();
- public $tables = array();
- public $countTypes = array('getRow' => 0, 'getValue' => 0, 'Execute' => 0, 'ExecuteS' => 0, 'delete' => 0, 'q' => 0);
-
- private function disableCache($query)
- {
- return preg_replace('/^select /', 'SELECT SQL_NO_CACHE', trim($query));
- }
-
- public function getRow($query, $use_cache = 1)
- {
- $this->count++;
- $this->countTypes['getRow']++;
- $query2 = preg_replace('/[0-9]+/', '
XX', $query);
- if (!isset($this->queries[$query2]))
- $this->queries[$query2] = 0;
- $this->queries[$query2]++;
- preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
- foreach ($matches[2] as $table)
- {
- if (!isset($this->tables[$table]))
- $this->tables[$table] = 0;
- $this->tables[$table]++;
- }
-
- $query = $this->disableCache($query);
- $t0 = microtime(true);
-
- $return = parent::getRow($query, $use_cache);
-
- if (!isset($this->queriesTime[$query]))
- $this->queriesTime[$query] = microtime(true)-$t0;
-
- return $return;
- }
-
- public function getValue($query, $use_cache = 1)
- {
- $this->count++;
- $this->countTypes['getValue']++;
- $query2 = preg_replace('/[0-9]+/', '
XX', $query);
- if (!isset($this->queries[$query2]))
- $this->queries[$query2] = 0;
- $this->queries[$query2]++;
- preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
- foreach ($matches[2] as $table)
- {
- if (!isset($this->tables[$table]))
- $this->tables[$table] = 0;
- $this->tables[$table]++;
- }
-
- $query = $this->disableCache($query);
- $t0 = microtime(true);
-
- $return = parent::getValue($query, $use_cache);
-
- if (!isset($this->queriesTime[$query]))
- $this->queriesTime[$query] = microtime(true)-$t0;
-
- return $return;
- }
-
- public function Execute($query, $use_cache = 1)
- {
- $this->count++;
- $this->countTypes['Execute']++;
- $query2 = preg_replace('/[0-9]+/', '
XX', $query);
- if (!isset($this->queries[$query2]))
- $this->queries[$query2] = 0;
- $this->queries[$query2]++;
- preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
- foreach ($matches[2] as $table)
- {
- if (!isset($this->tables[$table]))
- $this->tables[$table] = 0;
- $this->tables[$table]++;
- }
-
- $query = $this->disableCache($query);
- $t0 = microtime(true);
-
- $return = parent::Execute($query, $use_cache);
-
- if (!isset($this->queriesTime[$query]))
- $this->queriesTime[$query] = microtime(true)-$t0;
-
- return $return;
- }
-
- public function ExecuteS($query, $array = true, $use_cache = 1)
- {
- $this->count++;
- $this->countTypes['ExecuteS']++;
- $query2 = preg_replace('/[0-9]+/', '
XX', $query);
- if (!isset($this->queries[$query2]))
- $this->queries[$query2] = 0;
- $this->queries[$query2]++;
- preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
- foreach ($matches[2] as $table)
- {
- if (!isset($this->tables[$table]))
- $this->tables[$table] = 0;
- $this->tables[$table]++;
- }
-
- $query = $this->disableCache($query);
- $t0 = microtime(true);
-
- $return = parent::ExecuteS($query, $array, $use_cache);
-
- if (!isset($this->queriesTime[$query]))
- $this->queriesTime[$query] = microtime(true)-$t0;
-
- return $return;
- }
-
- public function delete($table, $where = false, $limit = false, $use_cache = 1)
- {
- $this->_result = false;
- if ($this->_link)
- {
- $query = 'DELETE FROM `'.pSQL($table).'`'.($where ? ' WHERE '.$where : '').($limit ? ' LIMIT '.(int)($limit) : '');
-
- $this->count++;
- $this->countTypes['delete']++;
- $query2 = preg_replace('/[0-9]+/', '
XX', $query);
- if (!isset($this->queries[$query2]))
- $this->queries[$query2] = 0;
- $this->queries[$query2]++;
- preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
- foreach ($matches[2] as $table)
- {
- if (!isset($this->tables[$table]))
- $this->tables[$table] = 0;
- $this->tables[$table]++;
- }
-
- $query = $this->disableCache($query);
- $t0 = microtime(true);
-
- $return = parent::delete($table, $where, $limit, $use_cache);
-
- if (!isset($this->queriesTime[$query]))
- $this->queriesTime[$query] = microtime(true)-$t0;
-
- return $return;
- }
- return false;
- }
-
- protected function q($query, $use_cache = 1)
- {
- $this->count++;
- $this->countTypes['q']++;
- $query2 = preg_replace('/[0-9]+/', '
XX', $query);
- if (!isset($this->queries[$query2]))
- $this->queries[$query2] = 0;
- $this->queries[$query2]++;
- preg_match_all('/(from|join)\s+`?'.preg_replace('/[0-9]+/', 'XX', _DB_PREFIX_).'([a-z0-9_-]+)/ui', $query2, $matches);
- foreach ($matches[2] as $table)
- {
- if (!isset($this->tables[$table]))
- $this->tables[$table] = 0;
- $this->tables[$table]++;
- }
-
- $query = $this->disableCache($query);
- $t0 = microtime(true);
-
- $return = parent::q($query, $use_cache);
-
- if (!isset($this->queriesTime[$query]))
- $this->queriesTime[$query] = microtime(true)-$t0;
-
- return $return;
- }
-}