/src-all/piwik/core/Tracker/Db/Pdo/Mysql.php

https://github.com/zeon/qpkg-piwik · PHP · 221 lines · 124 code · 17 blank · 80 comment · 17 complexity · 3bc80260d3e9d9fc14577ccdeae86868 MD5 · raw file

  1. <?php
  2. /**
  3. * Piwik - Open source web analytics
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. * @version $Id: Mysql.php 2967 2010-08-20 15:12:43Z vipsoft $
  8. *
  9. * @category Piwik
  10. * @package Piwik
  11. */
  12. /**
  13. * PDO MySQL wrapper
  14. *
  15. * @package Piwik
  16. * @subpackage Piwik_Tracker
  17. */
  18. class Piwik_Tracker_Db_Pdo_Mysql extends Piwik_Tracker_Db
  19. {
  20. protected $connection = null;
  21. private $dsn;
  22. private $username;
  23. private $password;
  24. private $charset;
  25. /**
  26. * Builds the DB object
  27. */
  28. public function __construct( $dbInfo, $driverName = 'mysql')
  29. {
  30. if(isset($dbInfo['unix_socket']) && $dbInfo['unix_socket'][0] == '/')
  31. {
  32. $this->dsn = $driverName.":dbname=${dbInfo['dbname']};unix_socket=${dbInfo['unix_socket']}";
  33. }
  34. else if ($dbInfo['port'][0] == '/')
  35. {
  36. $this->dsn = $driverName.":dbname=${dbInfo['dbname']};unix_socket=${dbInfo['port']}";
  37. }
  38. else
  39. {
  40. $this->dsn = $driverName.":dbname=${dbInfo['dbname']};host=${dbInfo['host']};port=${dbInfo['port']}";
  41. }
  42. $this->username = $dbInfo['username'];
  43. $this->password = $dbInfo['password'];
  44. $this->charset = isset($dbInfo['charset']) ? $dbInfo['charset'] : null;
  45. }
  46. public function __destruct()
  47. {
  48. $this->connection = null;
  49. }
  50. /**
  51. * Connects to the DB
  52. *
  53. * @throws Exception if there was an error connecting the DB
  54. */
  55. public function connect()
  56. {
  57. if(self::$profiling)
  58. {
  59. $timer = $this->initProfiler();
  60. }
  61. $this->connection = new PDO($this->dsn, $this->username, $this->password, $config = array());
  62. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  63. // we may want to setAttribute(PDO::ATTR_TIMEOUT ) to a few seconds (default is 60) in case the DB is locked
  64. // the piwik.php would stay waiting for the database... bad!
  65. // we delete the password from this object "just in case" it could be printed
  66. $this->password = '';
  67. /*
  68. * Lazy initialization via MYSQL_ATTR_INIT_COMMAND depends
  69. * on mysqlnd support, PHP version, and OS.
  70. * see ZF-7428 and http://bugs.php.net/bug.php?id=47224
  71. */
  72. if(!empty($this->charset))
  73. {
  74. $sql = "SET NAMES '" . $this->charset . "'";
  75. $this->connection->exec($sql);
  76. }
  77. if(self::$profiling)
  78. {
  79. $this->recordQueryProfile('connect', $timer);
  80. }
  81. }
  82. /**
  83. * Disconnects from the server
  84. */
  85. public function disconnect()
  86. {
  87. $this->connection = null;
  88. }
  89. /**
  90. * Returns an array containing all the rows of a query result, using optional bound parameters.
  91. *
  92. * @param string Query
  93. * @param array Parameters to bind
  94. * @see also query()
  95. * @throws Exception if an exception occured
  96. */
  97. public function fetchAll( $query, $parameters = array() )
  98. {
  99. try {
  100. $sth = $this->query( $query, $parameters );
  101. if($sth === false)
  102. {
  103. return false;
  104. }
  105. return $sth->fetchAll(PDO::FETCH_ASSOC);
  106. } catch (PDOException $e) {
  107. throw new Exception("Error query: ".$e->getMessage());
  108. }
  109. }
  110. /**
  111. * Returns the first row of a query result, using optional bound parameters.
  112. *
  113. * @param string Query
  114. * @param array Parameters to bind
  115. * @see also query()
  116. *
  117. * @throws Exception if an exception occured
  118. */
  119. public function fetch( $query, $parameters = array() )
  120. {
  121. try {
  122. $sth = $this->query( $query, $parameters );
  123. if($sth === false)
  124. {
  125. return false;
  126. }
  127. return $sth->fetch(PDO::FETCH_ASSOC);
  128. } catch (PDOException $e) {
  129. throw new Exception("Error query: ".$e->getMessage());
  130. }
  131. }
  132. /**
  133. * Executes a query, using optional bound parameters.
  134. *
  135. * @param string Query
  136. * @param array|string Parameters to bind array('idsite'=> 1)
  137. *
  138. * @return PDOStatement or false if failed
  139. * @throws Exception if an exception occured
  140. */
  141. public function query($query, $parameters = array())
  142. {
  143. if(is_null($this->connection))
  144. {
  145. return false;
  146. }
  147. try {
  148. if(self::$profiling)
  149. {
  150. $timer = $this->initProfiler();
  151. }
  152. if(!is_array($parameters))
  153. {
  154. $parameters = array( $parameters );
  155. }
  156. $sth = $this->connection->prepare($query);
  157. $sth->execute( $parameters );
  158. if(self::$profiling)
  159. {
  160. $this->recordQueryProfile($query, $timer);
  161. }
  162. return $sth;
  163. } catch (PDOException $e) {
  164. throw new Exception("Error query: ".$e->getMessage() . "
  165. In query: $query
  166. Parameters: ".var_export($parameters, true));
  167. }
  168. }
  169. /**
  170. * Returns the last inserted ID in the DB
  171. * Wrapper of PDO::lastInsertId()
  172. *
  173. * @return int
  174. */
  175. public function lastInsertId()
  176. {
  177. return $this->connection->lastInsertId();
  178. }
  179. /**
  180. * Test error number
  181. *
  182. * @param Exception $e
  183. * @param string $errno
  184. * @return bool
  185. */
  186. public function isErrNo($e, $errno)
  187. {
  188. if(preg_match('/([0-9]{4})/', $e->getMessage(), $match))
  189. {
  190. return $match[1] == $errno;
  191. }
  192. return false;
  193. }
  194. /**
  195. * Return number of affected rows in last query
  196. *
  197. * @param mixed $queryResult Result from query()
  198. * @return int
  199. */
  200. public function rowCount($queryResult)
  201. {
  202. return $queryResult->rowCount();
  203. }
  204. }