Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobocoder <anthon.pang@gmail.com>2009-09-07 22:48:16 +0400
committerrobocoder <anthon.pang@gmail.com>2009-09-07 22:48:16 +0400
commita1ae0bb25af94ccbcafbb3e61964ccdf66db143f (patch)
treeab6a61b7999236c9b881a20a224d45806a9c7e99 /core/Tracker/Db.php
parentd78a62bceb1605df0930db26c9a3b119a41b3ea6 (diff)
fixes #425 - provide MySQLi support; added a factory method and refactored Tracker/Db.php into separate adapters
git-svn-id: http://dev.piwik.org/svn/trunk@1456 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'core/Tracker/Db.php')
-rw-r--r--core/Tracker/Db.php233
1 files changed, 70 insertions, 163 deletions
diff --git a/core/Tracker/Db.php b/core/Tracker/Db.php
index 29bed3a420..6616e74c74 100644
--- a/core/Tracker/Db.php
+++ b/core/Tracker/Db.php
@@ -1,57 +1,47 @@
<?php
/**
* Piwik - Open source web analytics
- *
+ *
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
* @version $Id$
- *
+ *
* @category Piwik
* @package Piwik
*/
/**
- * Simple database PDO wrapper.
+ * Simple database wrapper.
* We can't afford to have a dependency with the Zend_Db module in Tracker.
* We wrote this simple class
*
* @package Piwik
* @subpackage Piwik_Tracker
*/
-class Piwik_Tracker_Db
+abstract class Piwik_Tracker_Db
{
- private $connection = null;
- private $username;
- private $password;
-
- static private $profiling = false;
+ protected static $profiling = false;
protected $queriesProfiling = array();
/**
- * Builds the DB object
+ * Enables the SQL profiling.
+ * For each query, saves in the DB the time spent on this query.
+ * Very useful to see the slow query under heavy load.
+ * You can then use Piwik::printSqlProfilingReportTracker();
+ * to display the SQLProfiling report and see which queries take time, etc.
*/
- public function __construct( $dbInfo, $driverName = 'mysql')
+ public function enableProfiling()
{
- if(isset($dbInfo['unix_socket']) && $dbInfo['unix_socket'][0] == '/')
- {
- $this->dsn = $driverName.":dbname=${dbInfo['dbname']};unix_socket=${dbInfo['unix_socket']}";
- }
- else if ($dbInfo['port'][0] == '/')
- {
- $this->dsn = $driverName.":dbname=${dbInfo['dbname']};unix_socket=${dbInfo['port']}";
- }
- else
- {
- $this->dsn = $driverName.":dbname=${dbInfo['dbname']};host=${dbInfo['host']};port=${dbInfo['port']}";
- }
- $this->username = $dbInfo['username'];
- $this->password = $dbInfo['password'];
+ self::$profiling = true;
}
- public function __destruct()
+ /**
+ * Disables the SQL profiling logging.
+ */
+ public function disableProfiling()
{
- $this->connection = null;
+ self::$profiling = false;
}
/**
@@ -60,58 +50,74 @@ class Piwik_Tracker_Db
*
* @return bool
*/
- static public function isProfilingEnabled()
+ public function isProfilingEnabled()
{
return self::$profiling;
}
-
+
/**
- * Enables the SQL profiling.
- * For each query, saves in the DB the time spent on this query.
- * Very useful to see the slow query under heavy load.
- * You can then use Piwik::printSqlProfilingReportTracker();
- * to display the SQLProfiling report and see which queries take time, etc.
+ * Initialize profiler
+ *
+ * @return Piwik_Timer
*/
- static public function enableProfiling()
+ protected function initProfiler()
{
- self::$profiling = true;
+ return new Piwik_Timer;
}
-
- /**
- * Disables the SQL profiling logging.
- */
- static public function disableProfiling()
+
+ /**
+ * Record query profile
+ *
+ * @param string $query
+ * @param Piwik_Timer $timer
+ */
+ protected function recordQueryProfile( $query, $timer )
{
- self::$profiling = false;
+ if(!isset($this->queriesProfiling[$query])) $this->queriesProfiling[$query] = array('sum_time_ms' => 0, 'count' => 0);
+ $time = $timer->getTimeMs(2);
+ $time += $this->queriesProfiling[$query]['sum_time_ms'];
+ $count = $this->queriesProfiling[$query]['count'] + 1;
+ $this->queriesProfiling[$query] = array('sum_time_ms' => $time, 'count' => $count);
}
/**
- * Connects to the DB
- *
- * @throws Exception if there was an error connecting the DB
+ * When destroyed, if SQL profiled enabled, logs the SQL profiling information
*/
- public function connect()
+ public function recordProfiling()
{
- if(self::$profiling)
+ if(is_null($this->connection))
{
- $timer = $this->initProfiler();
+ return;
}
+
+ // turn off the profiler so we don't profile the following queries
+ self::$profiling = false;
- $this->connection = new PDO($this->dsn, $this->username, $this->password);
- $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- // we may want to setAttribute(PDO::ATTR_TIMEOUT ) to a few seconds (default is 60) in case the DB is locked
- // the piwik.php would stay waiting for the database... bad!
- // we delete the password from this object "just in case" it could be printed
- $this->password = '';
-
- if(self::$profiling)
+ foreach($this->queriesProfiling as $query => $info)
{
- $this->recordQueryProfile('connect', $timer);
+ $time = $info['sum_time_ms'];
+ $count = $info['count'];
+
+ $queryProfiling = "INSERT INTO ".Piwik_Common::prefixTable('log_profiling')."
+ (query,count,sum_time_ms) VALUES (?,$count,$time)
+ ON DUPLICATE KEY
+ UPDATE count=count+$count,sum_time_ms=sum_time_ms+$time";
+ $this->query($queryProfiling,array($query));
}
+
+ // turn back on profiling
+ self::$profiling = true;
}
+
+ /**
+ * Connects to the DB
+ *
+ * @throws Exception if there was an error connecting the DB
+ */
+ abstract public function connect();
/**
- * Disconnects from the Mysql server
+ * Disconnects from the server
*/
public function disconnect()
{
@@ -130,19 +136,7 @@ class Piwik_Tracker_Db
* @see also query()
* @throws Exception if an exception occured
*/
- public function fetchAll( $query, $parameters = array() )
- {
- try {
- $sth = $this->query( $query, $parameters );
- if($sth === false)
- {
- return false;
- }
- return $sth->fetchAll(PDO::FETCH_ASSOC);
- } catch (PDOException $e) {
- throw new Exception("Error query: ".$e->getMessage());
- }
- }
+ abstract public function fetchAll( $query, $parameters = array() );
/**
* Returns the first row of a query result, using optional bound parameters.
@@ -153,19 +147,7 @@ class Piwik_Tracker_Db
*
* @throws Exception if an exception occured
*/
- public function fetch( $query, $parameters = array() )
- {
- try {
- $sth = $this->query( $query, $parameters );
- if($sth === false)
- {
- return false;
- }
- return $sth->fetch(PDO::FETCH_ASSOC);
- } catch (PDOException $e) {
- throw new Exception("Error query: ".$e->getMessage());
- }
- }
+ abstract public function fetch( $query, $parameters = array() );
/**
* This function is a proxy to fetch(), used to maintain compatibility with Zend_Db interface
@@ -175,7 +157,7 @@ class Piwik_Tracker_Db
{
return $this->fetch($query, $parameters);
}
-
+
/**
* Executes a query, using optional bound parameters.
*
@@ -185,88 +167,13 @@ class Piwik_Tracker_Db
* @return PDOStatement or false if failed
* @throws Exception if an exception occured
*/
- public function query($query, $parameters = array())
- {
- if(is_null($this->connection))
- {
- return false;
- }
- try {
- if(self::$profiling)
- {
- $timer = $this->initProfiler();
- }
-
- if(!is_array($parameters))
- {
- $parameters = array( $parameters );
- }
- $sth = $this->connection->prepare($query);
- $sth->execute( $parameters );
-
- if(self::$profiling)
- {
- $this->recordQueryProfile($query, $timer);
- }
- return $sth;
- } catch (PDOException $e) {
- throw new Exception("Error query: ".$e->getMessage() . "
- In query: $query
- Parameters: ".var_export($parameters, true));
- }
- }
-
- protected function initProfiler()
- {
- return new Piwik_Timer;
- }
-
- protected function recordQueryProfile( $query, $timer )
- {
- if(!isset($this->queriesProfiling[$query])) $this->queriesProfiling[$query] = array('sum_time_ms' => 0, 'count' => 0);
- $time = $timer->getTimeMs(2);
- $time += $this->queriesProfiling[$query]['sum_time_ms'];
- $count = $this->queriesProfiling[$query]['count'] + 1;
- $this->queriesProfiling[$query] = array('sum_time_ms' => $time, 'count' => $count);
- }
-
+ abstract public function query($query, $parameters = array());
+
/**
* Returns the last inserted ID in the DB
* Wrapper of PDO::lastInsertId()
*
* @return int
*/
- public function lastInsertId()
- {
- return $this->connection->lastInsertId();
- }
-
- /**
- * When destroyed, if SQL profiled enabled, logs the SQL profiling information
- */
- public function recordProfiling()
- {
- if(is_null($this->connection))
- {
- return;
- }
-
- // turn off the profiler so we don't profile the following queries
- self::$profiling = false;
-
- foreach($this->queriesProfiling as $query => $info)
- {
- $time = $info['sum_time_ms'];
- $count = $info['count'];
-
- $queryProfiling = "INSERT INTO ".Piwik_Common::prefixTable('log_profiling')."
- (query,count,sum_time_ms) VALUES (?,$count,$time)
- ON DUPLICATE KEY
- UPDATE count=count+$count,sum_time_ms=sum_time_ms+$time";
- $this->query($queryProfiling,array($query));
- }
-
- // turn back on profiling
- self::$profiling = true;
+ abstract public function lastInsertId();
}
-}