Source for file MySQL.class.php

Documentation is available at MySQL.class.php

  1. <?php
  2. /**
  3. * XOAD_Cache Storage MySQL Provider File.
  4. *
  5. * <p>This file defines the {@link XOAD_Cache_Storage_MySQL} Class.</p>
  6. * <p>You should not include this file directly. It is used
  7. * by {@link XOAD_Cache} extension.</p>
  8. *
  9. * @author Stanimir Angeloff
  10. *
  11. * @package XOAD
  12. *
  13. * @subpackage XOAD_Cache
  14. *
  15. * @version 0.6.0.0
  16. *
  17. */
  18.  
  19. /**
  20. * Defines the table name in the MySQL database that is used to save
  21. * the cached data (default value: xoad_cache).
  22. *
  23. * @ignore
  24. *
  25. */
  26. define('XOAD_CACHE_TABLE_NAME', 'xoad_cache');
  27.  
  28. /**
  29. * XOAD_Cache Storage MySQL Class.
  30. *
  31. * <p>This class is a {@link XOAD_Cache_Storage} successor.</p>
  32. *
  33. * @author Stanimir Angeloff
  34. *
  35. * @package XOAD
  36. *
  37. * @subpackage XOAD_Cache
  38. *
  39. * @version 0.6.0.0
  40. *
  41. */
  42. class XOAD_Cache_Storage_MySQL extends XOAD_Cache_Storage
  43. {
  44. /**
  45. * Holds the MySQL server used in the connection string.
  46. *
  47. * @access protected
  48. *
  49. * @var string
  50. *
  51. */
  52. var $server;
  53.  
  54. /**
  55. * Holds the MySQL user used in the connection string.
  56. *
  57. * @access protected
  58. *
  59. * @var string
  60. *
  61. */
  62. var $user;
  63.  
  64. /**
  65. * Holds the MySQL password used in the connection string.
  66. *
  67. * @access protected
  68. *
  69. * @var string
  70. *
  71. */
  72. var $password;
  73.  
  74. /**
  75. * Holds the MySQL database used in the connection string.
  76. *
  77. * @access protected
  78. *
  79. * @var string
  80. *
  81. */
  82. var $database;
  83.  
  84. /**
  85. * Holds the MySQL port used in the connection string.
  86. *
  87. * @access protected
  88. *
  89. * @var string
  90. *
  91. */
  92. var $port = 3306;
  93.  
  94. /**
  95. * Indicates whether to open a new connection to the MySQL server
  96. * if an old one already exists.
  97. *
  98. * @access protected
  99. *
  100. * @var bool
  101. *
  102. */
  103. var $openNew;
  104.  
  105. /**
  106. * Creates a new instance of the {@link XOAD_Cache_Storage_MySQL} class.
  107. *
  108. * @access public
  109. *
  110. * @param string $dsn The data source name and parameters to use
  111. * when creating the instance.
  112. *
  113. */
  114. function XOAD_Cache_Storage_MySQL($dsn)
  115. {
  116. parent::XOAD_Cache_Storage($dsn);
  117. }
  118.  
  119. /**
  120. * Creates a MySQL connection link.
  121. *
  122. * @access private
  123. *
  124. * @return resource
  125. *
  126. */
  127. function &getConnection()
  128. {
  129. $server = $this->server;
  130.  
  131. if ($this->port != 3306) {
  132.  
  133. $server .= ':' . $this->port;
  134. }
  135.  
  136. $connection = mysql_connect($server, $this->user, $this->password, $this->openNew);
  137.  
  138. mysql_select_db($this->database, $connection);
  139.  
  140. return $connection;
  141. }
  142.  
  143. /**
  144. * Closes a MySQL connection link.
  145. *
  146. * @access private
  147. *
  148. * @return void
  149. *
  150. */
  151. function closeConnection(&$connection)
  152. {
  153. mysql_close($connection);
  154.  
  155. $connection = null;
  156. }
  157.  
  158. /**
  159. * Escapes special characters in the {@link $unescapedString},
  160. * taking into account the current charset of the connection.
  161. *
  162. * @access private
  163. *
  164. * @return string
  165. *
  166. */
  167. function escapeString($unescapedString, $connection)
  168. {
  169. if (function_exists('mysql_real_escape_string')) {
  170.  
  171. return mysql_real_escape_string($unescapedString, $connection);
  172. }
  173.  
  174. return mysql_escape_string($unescapedString);
  175. }
  176.  
  177. /**
  178. * Deletes old data from the cache.
  179. *
  180. * <p>This method is called before calling {@link load} to
  181. * delete all expired data from the cache.</p>
  182. *
  183. * @access public
  184. *
  185. * @return bool true on success, false otherwise.
  186. *
  187. */
  188. function collectGarbage()
  189. {
  190. $connection =& $this->getConnection();
  191.  
  192. $sqlQuery = '
  193. DELETE FROM
  194. `' . XOAD_CACHE_TABLE_NAME . '`
  195. WHERE
  196. `expire` < ' . time();
  197.  
  198. mysql_query($sqlQuery, $connection);
  199.  
  200. $this->closeConnection($connection);
  201.  
  202. return true;
  203. }
  204.  
  205. /**
  206. * Loads data from the cache with a given ID.
  207. *
  208. * @access public
  209. *
  210. * @param string $id The ID of the cached data.
  211. *
  212. * @return mixed The data in the cache with the given ID or null.
  213. *
  214. */
  215. function load($id)
  216. {
  217. $connection =& $this->getConnection();
  218.  
  219. $sqlQuery = '
  220. SELECT
  221. `data`
  222. FROM
  223. `' . XOAD_CACHE_TABLE_NAME . '`
  224. WHERE
  225. `id` = \'' . $this->escapeString($id, $connection) . '\'
  226. AND
  227. `expire` >= ' . time();
  228.  
  229. $returnValue = null;
  230.  
  231. $sqlResult = mysql_query($sqlQuery, $connection);
  232.  
  233. while (($row = mysql_fetch_assoc($sqlResult)) !== false) {
  234.  
  235. $returnValue = $row['data'];
  236. }
  237.  
  238. mysql_free_result($sqlResult);
  239.  
  240. $this->closeConnection($connection);
  241.  
  242. return $returnValue;
  243. }
  244.  
  245. /**
  246. * Saves data in the cache with a given ID and lifetime.
  247. *
  248. * @access public
  249. *
  250. * @param mixed $id The ID to use when saving the data.
  251. *
  252. * @param int $expires The lifetime time in seconds for the
  253. * cached data.
  254. *
  255. * @param mixed $data The data to cache.
  256. *
  257. * @return bool True on success, false otherwise.
  258. *
  259. */
  260. function save($id, $expires, $data)
  261. {
  262. $connection =& $this->getConnection();
  263.  
  264. if (empty($expires)) {
  265.  
  266. $expires = XOAD_CACHE_LIFETIME;
  267. }
  268.  
  269. $sqlQuery = '
  270. INSERT INTO
  271. `' . XOAD_CACHE_TABLE_NAME . '`
  272. (
  273. `id`,
  274. `expire`,
  275. `data`
  276. )
  277. VALUES
  278. (
  279. \'' . $this->escapeString($id, $connection) . '\',
  280. ' . $this->escapeString(time() + $expires, $connection) . ',
  281. \'' . $this->escapeString($data, $connection) . '\'
  282. )';
  283.  
  284. mysql_query($sqlQuery, $connection);
  285.  
  286. $this->closeConnection($connection);
  287.  
  288. return true;
  289. }
  290. }
  291. ?>

Documentation generated on Sat, 12 Nov 2005 20:24:22 +0200 by phpDocumentor 1.3.0RC3