/system/application/stats/core/Tracker/Db/Mysqli.php

https://github.com/isS/Microweber · PHP · 282 lines · 177 code · 26 blank · 79 comment · 26 complexity · 8568b4406cf4602e1b5f9a06ed42a154 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: Mysqli.php 4155 2011-03-20 21:21:51Z vipsoft $
  8. *
  9. * @category Piwik
  10. * @package Piwik
  11. */
  12. /**
  13. * mysqli wrapper
  14. *
  15. * @package Piwik
  16. * @subpackage Piwik_Tracker
  17. */
  18. class Piwik_Tracker_Db_Mysqli extends Piwik_Tracker_Db
  19. {
  20. protected $connection = null;
  21. private $host;
  22. private $port;
  23. private $socket;
  24. private $dbname;
  25. private $username;
  26. private $password;
  27. private $charset;
  28. /**
  29. * Builds the DB object
  30. */
  31. public function __construct( $dbInfo, $driverName = 'mysql')
  32. {
  33. if(isset($dbInfo['unix_socket']) && $dbInfo['unix_socket'][0] == '/')
  34. {
  35. $this->host = null;
  36. $this->port = null;
  37. $this->socket = $dbInfo['unix_socket'];
  38. }
  39. else if ($dbInfo['port'][0] == '/')
  40. {
  41. $this->host = null;
  42. $this->port = null;
  43. $this->socket = $dbInfo['port'];
  44. }
  45. else
  46. {
  47. $this->host = $dbInfo['host'];
  48. $this->port = $dbInfo['port'];
  49. $this->socket = null;
  50. }
  51. $this->dbname = $dbInfo['dbname'];
  52. $this->username = $dbInfo['username'];
  53. $this->password = $dbInfo['password'];
  54. $this->charset = isset($dbInfo['charset']) ? $dbInfo['charset'] : null;
  55. }
  56. public function __destruct()
  57. {
  58. $this->connection = null;
  59. }
  60. /**
  61. * Connects to the DB
  62. *
  63. * @throws Exception if there was an error connecting the DB
  64. */
  65. public function connect()
  66. {
  67. if(self::$profiling)
  68. {
  69. $timer = $this->initProfiler();
  70. }
  71. $this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->dbname, $this->port, $this->socket);
  72. if(!$this->connection || mysqli_connect_errno())
  73. {
  74. throw new Piwik_Tracker_Db_Exception("Connect failed: " . mysqli_connect_error());
  75. }
  76. if($this->charset && !mysqli_set_charset($this->connection, $this->charset))
  77. {
  78. throw new Piwik_Tracker_Db_Exception("Set Charset failed: " . mysqli_error($this->connection));
  79. }
  80. $this->password = '';
  81. if(self::$profiling)
  82. {
  83. $this->recordQueryProfile('connect', $timer);
  84. }
  85. }
  86. /**
  87. * Disconnects from the server
  88. */
  89. public function disconnect()
  90. {
  91. mysqli_close($this->connection);
  92. $this->connection = null;
  93. }
  94. /**
  95. * Returns an array containing all the rows of a query result, using optional bound parameters.
  96. *
  97. * @param string Query
  98. * @param array Parameters to bind
  99. * @see also query()
  100. * @throws Exception if an exception occured
  101. */
  102. public function fetchAll( $query, $parameters = array() )
  103. {
  104. try {
  105. if(self::$profiling)
  106. {
  107. $timer = $this->initProfiler();
  108. }
  109. $rows = array();
  110. $query = $this->prepare( $query, $parameters );
  111. $rs = mysqli_query($this->connection, $query);
  112. if(is_bool($rs))
  113. {
  114. throw new Piwik_Tracker_Db_Exception('fetchAll() failed: ' . mysqli_error($this->connection) . ' : ' . $query);
  115. }
  116. while($row = mysqli_fetch_array($rs, MYSQLI_ASSOC))
  117. {
  118. $rows[] = $row;
  119. }
  120. mysqli_free_result($rs);
  121. if(self::$profiling)
  122. {
  123. $this->recordQueryProfile($query, $timer);
  124. }
  125. return $rows;
  126. } catch (Exception $e) {
  127. throw new Piwik_Tracker_Db_Exception("Error query: ".$e->getMessage());
  128. }
  129. }
  130. /**
  131. * Returns the first row of a query result, using optional bound parameters.
  132. *
  133. * @param string Query
  134. * @param array Parameters to bind
  135. * @see also query()
  136. *
  137. * @throws Exception if an exception occured
  138. */
  139. public function fetch( $query, $parameters = array() )
  140. {
  141. try {
  142. if(self::$profiling)
  143. {
  144. $timer = $this->initProfiler();
  145. }
  146. $query = $this->prepare( $query, $parameters );
  147. $rs = mysqli_query($this->connection, $query);
  148. if(is_bool($rs))
  149. {
  150. throw new Piwik_Tracker_Db_Exception('fetch() failed: ' . mysqli_error($this->connection) . ' : ' . $query);
  151. }
  152. $row = mysqli_fetch_array($rs, MYSQLI_ASSOC);
  153. mysqli_free_result($rs);
  154. if(self::$profiling)
  155. {
  156. $this->recordQueryProfile($query, $timer);
  157. }
  158. return $row;
  159. } catch (Exception $e) {
  160. throw new Piwik_Tracker_Db_Exception("Error query: ".$e->getMessage());
  161. }
  162. }
  163. /**
  164. * Executes a query, using optional bound parameters.
  165. *
  166. * @param string Query
  167. * @param array|string Parameters to bind array('idsite'=> 1)
  168. *
  169. * @return bool|resource false if failed
  170. * @throws Exception if an exception occured
  171. */
  172. public function query($query, $parameters = array())
  173. {
  174. if(is_null($this->connection))
  175. {
  176. return false;
  177. }
  178. try {
  179. if(self::$profiling)
  180. {
  181. $timer = $this->initProfiler();
  182. }
  183. $query = $this->prepare( $query, $parameters );
  184. $result = mysqli_query($this->connection, $query);
  185. if(!is_bool($result))
  186. {
  187. mysqli_free_result($result);
  188. }
  189. if(self::$profiling)
  190. {
  191. $this->recordQueryProfile($query, $timer);
  192. }
  193. return $result;
  194. } catch (Exception $e) {
  195. throw new Piwik_Tracker_Db_Exception("Error query: ".$e->getMessage() . "
  196. In query: $query
  197. Parameters: ".var_export($parameters, true));
  198. }
  199. }
  200. /**
  201. * Returns the last inserted ID in the DB
  202. *
  203. * @return int
  204. */
  205. public function lastInsertId()
  206. {
  207. return mysqli_insert_id($this->connection);
  208. }
  209. /**
  210. * Input is a prepared SQL statement and parameters
  211. * Returns the SQL statement
  212. *
  213. * @param string $query
  214. * @param array $parameters
  215. * @return string
  216. */
  217. private function prepare($query, $parameters) {
  218. if(!$parameters)
  219. {
  220. $parameters = array();
  221. }
  222. else if(!is_array($parameters))
  223. {
  224. $parameters = array( $parameters );
  225. }
  226. foreach($parameters as $i=>$p)
  227. {
  228. $parameters[$i] = addslashes($p);
  229. }
  230. $query = str_replace('?', "'%s'", $query);
  231. array_unshift($parameters, $query);
  232. $query = call_user_func_array('sprintf', $parameters);
  233. return $query;
  234. }
  235. /**
  236. * Test error number
  237. *
  238. * @param Exception $e
  239. * @param string $errno
  240. * @return bool
  241. */
  242. public function isErrNo($e, $errno)
  243. {
  244. return mysqli_errno($this->_connection) == $errno;
  245. }
  246. /**
  247. * Return number of affected rows in last query
  248. *
  249. * @param mixed $queryResult Result from query()
  250. * @return int
  251. */
  252. public function rowCount($queryResult)
  253. {
  254. return mysqli_affected_rows($this->connection);
  255. }
  256. }