PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/joomla/database/driver/mysql.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 413 lines | 174 code | 51 blank | 188 comment | 16 complexity | 78b400109ca641f847fdb267359316ee MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Database
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * MySQL database driver
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Database
  15. * @see http://dev.mysql.com/doc/
  16. * @since 12.1
  17. */
  18. class JDatabaseDriverMysql extends JDatabaseDriverMysqli
  19. {
  20. /**
  21. * The name of the database driver.
  22. *
  23. * @var string
  24. * @since 12.1
  25. */
  26. public $name = 'mysql';
  27. /**
  28. * Constructor.
  29. *
  30. * @param array $options Array of database options with keys: host, user, password, database, select.
  31. *
  32. * @since 12.1
  33. */
  34. public function __construct($options)
  35. {
  36. // Get some basic values from the options.
  37. $options['host'] = (isset($options['host'])) ? $options['host'] : 'localhost';
  38. $options['user'] = (isset($options['user'])) ? $options['user'] : 'root';
  39. $options['password'] = (isset($options['password'])) ? $options['password'] : '';
  40. $options['database'] = (isset($options['database'])) ? $options['database'] : '';
  41. $options['select'] = (isset($options['select'])) ? (bool) $options['select'] : true;
  42. // Finalize initialisation.
  43. parent::__construct($options);
  44. }
  45. /**
  46. * Destructor.
  47. *
  48. * @since 12.1
  49. */
  50. public function __destruct()
  51. {
  52. if (is_resource($this->connection))
  53. {
  54. mysql_close($this->connection);
  55. }
  56. }
  57. /**
  58. * Connects to the database if needed.
  59. *
  60. * @return void Returns void if the database connected successfully.
  61. *
  62. * @since 12.1
  63. * @throws RuntimeException
  64. */
  65. public function connect()
  66. {
  67. if ($this->connection)
  68. {
  69. return;
  70. }
  71. // Make sure the MySQL extension for PHP is installed and enabled.
  72. if (!function_exists('mysql_connect'))
  73. {
  74. throw new RuntimeException('Could not connect to MySQL.');
  75. }
  76. // Attempt to connect to the server.
  77. if (!($this->connection = @ mysql_connect($this->options['host'], $this->options['user'], $this->options['password'], true)))
  78. {
  79. throw new RuntimeException('Could not connect to MySQL.');
  80. }
  81. // Set sql_mode to non_strict mode
  82. mysql_query("SET @@SESSION.sql_mode = '';", $this->connection);
  83. // If auto-select is enabled select the given database.
  84. if ($this->options['select'] && !empty($this->options['database']))
  85. {
  86. $this->select($this->options['database']);
  87. }
  88. // Set charactersets (needed for MySQL 4.1.2+).
  89. $this->setUTF();
  90. }
  91. /**
  92. * Disconnects the database.
  93. *
  94. * @return void
  95. *
  96. * @since 12.1
  97. */
  98. public function disconnect()
  99. {
  100. // Close the connection.
  101. mysql_close($this->connection);
  102. $this->connection = null;
  103. }
  104. /**
  105. * Method to escape a string for usage in an SQL statement.
  106. *
  107. * @param string $text The string to be escaped.
  108. * @param boolean $extra Optional parameter to provide extra escaping.
  109. *
  110. * @return string The escaped string.
  111. *
  112. * @since 12.1
  113. */
  114. public function escape($text, $extra = false)
  115. {
  116. $this->connect();
  117. $result = mysql_real_escape_string($text, $this->getConnection());
  118. if ($extra)
  119. {
  120. $result = addcslashes($result, '%_');
  121. }
  122. return $result;
  123. }
  124. /**
  125. * Test to see if the MySQL connector is available.
  126. *
  127. * @return boolean True on success, false otherwise.
  128. *
  129. * @since 12.1
  130. */
  131. public static function isSupported()
  132. {
  133. return (function_exists('mysql_connect'));
  134. }
  135. /**
  136. * Determines if the connection to the server is active.
  137. *
  138. * @return boolean True if connected to the database engine.
  139. *
  140. * @since 12.1
  141. */
  142. public function connected()
  143. {
  144. if (is_resource($this->connection))
  145. {
  146. return @mysql_ping($this->connection);
  147. }
  148. return false;
  149. }
  150. /**
  151. * Get the number of affected rows for the previous executed SQL statement.
  152. *
  153. * @return integer The number of affected rows.
  154. *
  155. * @since 12.1
  156. */
  157. public function getAffectedRows()
  158. {
  159. $this->connect();
  160. return mysql_affected_rows($this->connection);
  161. }
  162. /**
  163. * Get the number of returned rows for the previous executed SQL statement.
  164. *
  165. * @param resource $cursor An optional database cursor resource to extract the row count from.
  166. *
  167. * @return integer The number of returned rows.
  168. *
  169. * @since 12.1
  170. */
  171. public function getNumRows($cursor = null)
  172. {
  173. $this->connect();
  174. return mysql_num_rows($cursor ? $cursor : $this->cursor);
  175. }
  176. /**
  177. * Get the version of the database connector.
  178. *
  179. * @return string The database connector version.
  180. *
  181. * @since 12.1
  182. */
  183. public function getVersion()
  184. {
  185. $this->connect();
  186. return mysql_get_server_info($this->connection);
  187. }
  188. /**
  189. * Method to get the auto-incremented value from the last INSERT statement.
  190. *
  191. * @return integer The value of the auto-increment field from the last inserted row.
  192. *
  193. * @since 12.1
  194. */
  195. public function insertid()
  196. {
  197. $this->connect();
  198. return mysql_insert_id($this->connection);
  199. }
  200. /**
  201. * Execute the SQL statement.
  202. *
  203. * @return mixed A database cursor resource on success, boolean false on failure.
  204. *
  205. * @since 12.1
  206. * @throws RuntimeException
  207. */
  208. public function execute()
  209. {
  210. $this->connect();
  211. if (!is_resource($this->connection))
  212. {
  213. JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'database');
  214. throw new RuntimeException($this->errorMsg, $this->errorNum);
  215. }
  216. // Take a local copy so that we don't modify the original query and cause issues later
  217. $query = $this->replacePrefix((string) $this->sql);
  218. if ($this->limit > 0 || $this->offset > 0)
  219. {
  220. $query .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
  221. }
  222. // Increment the query counter.
  223. $this->count++;
  224. // If debugging is enabled then let's log the query.
  225. if ($this->debug)
  226. {
  227. // Add the query to the object queue.
  228. $this->log[] = $query;
  229. JLog::add($query, JLog::DEBUG, 'databasequery');
  230. }
  231. // Reset the error values.
  232. $this->errorNum = 0;
  233. $this->errorMsg = '';
  234. // Execute the query. Error suppression is used here to prevent warnings/notices that the connection has been lost.
  235. $this->cursor = @mysql_query($query, $this->connection);
  236. // If an error occurred handle it.
  237. if (!$this->cursor)
  238. {
  239. // Check if the server was disconnected.
  240. if (!$this->connected())
  241. {
  242. try
  243. {
  244. // Attempt to reconnect.
  245. $this->connection = null;
  246. $this->connect();
  247. }
  248. // If connect fails, ignore that exception and throw the normal exception.
  249. catch (RuntimeException $e)
  250. {
  251. // Get the error number and message.
  252. $this->errorNum = (int) mysql_errno($this->connection);
  253. $this->errorMsg = (string) mysql_error($this->connection) . ' SQL=' . $query;
  254. // Throw the normal query exception.
  255. JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'databasequery');
  256. throw new RuntimeException($this->errorMsg, $this->errorNum);
  257. }
  258. // Since we were able to reconnect, run the query again.
  259. return $this->execute();
  260. }
  261. // The server was not disconnected.
  262. else
  263. {
  264. // Get the error number and message.
  265. $this->errorNum = (int) mysql_errno($this->connection);
  266. $this->errorMsg = (string) mysql_error($this->connection) . ' SQL=' . $query;
  267. // Throw the normal query exception.
  268. JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'databasequery');
  269. throw new RuntimeException($this->errorMsg, $this->errorNum);
  270. }
  271. }
  272. return $this->cursor;
  273. }
  274. /**
  275. * Select a database for use.
  276. *
  277. * @param string $database The name of the database to select for use.
  278. *
  279. * @return boolean True if the database was successfully selected.
  280. *
  281. * @since 12.1
  282. * @throws RuntimeException
  283. */
  284. public function select($database)
  285. {
  286. $this->connect();
  287. if (!$database)
  288. {
  289. return false;
  290. }
  291. if (!mysql_select_db($database, $this->connection))
  292. {
  293. throw new RuntimeException('Could not connect to database');
  294. }
  295. return true;
  296. }
  297. /**
  298. * Set the connection to use UTF-8 character encoding.
  299. *
  300. * @return boolean True on success.
  301. *
  302. * @since 12.1
  303. */
  304. public function setUTF()
  305. {
  306. $this->connect();
  307. return mysql_set_charset('utf8', $this->connection);
  308. }
  309. /**
  310. * Method to fetch a row from the result set cursor as an array.
  311. *
  312. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  313. *
  314. * @return mixed Either the next row from the result set or false if there are no more rows.
  315. *
  316. * @since 12.1
  317. */
  318. protected function fetchArray($cursor = null)
  319. {
  320. return mysql_fetch_row($cursor ? $cursor : $this->cursor);
  321. }
  322. /**
  323. * Method to fetch a row from the result set cursor as an associative array.
  324. *
  325. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  326. *
  327. * @return mixed Either the next row from the result set or false if there are no more rows.
  328. *
  329. * @since 12.1
  330. */
  331. protected function fetchAssoc($cursor = null)
  332. {
  333. return mysql_fetch_assoc($cursor ? $cursor : $this->cursor);
  334. }
  335. /**
  336. * Method to fetch a row from the result set cursor as an object.
  337. *
  338. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  339. * @param string $class The class name to use for the returned row object.
  340. *
  341. * @return mixed Either the next row from the result set or false if there are no more rows.
  342. *
  343. * @since 12.1
  344. */
  345. protected function fetchObject($cursor = null, $class = 'stdClass')
  346. {
  347. return mysql_fetch_object($cursor ? $cursor : $this->cursor, $class);
  348. }
  349. /**
  350. * Method to free up the memory used for the result set.
  351. *
  352. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  353. *
  354. * @return void
  355. *
  356. * @since 12.1
  357. */
  358. protected function freeResult($cursor = null)
  359. {
  360. mysql_free_result($cursor ? $cursor : $this->cursor);
  361. }
  362. }