Source for file MySQL.class.php

Documentation is available at MySQL.class.php

  1. <?php
  2. /**
  3. * XOAD Events MySQL Provider file.
  4. *
  5. * <p>This file defines the {@link XOAD_Events_Storage_MySQL} Class.</p>
  6. * <p>Example:</p>
  7. * <code>
  8. * <?php
  9. *
  10. * require_once('xoad.php');
  11. *
  12. * require_once(XOAD_BASE . '/classes/events/storage/MySQL.class.php');
  13. *
  14. * $storage = new XOAD_Events_Storage_MySQL('server=?;user=?;password=?;database=?;[port=?]');
  15. *
  16. * $storage->postEvent('event', 'class');
  17. *
  18. * ?>
  19. * </code>
  20. *
  21. * @author Stanimir Angeloff
  22. *
  23. * @package XOAD
  24. *
  25. * @subpackage XOAD_Events
  26. *
  27. * @version 0.6.0.0
  28. *
  29. */
  30.  
  31. /**
  32. * Defines the table name in the MySQL database that is used to save the
  33. * events information (default value: xoad_events).
  34. *
  35. * @ignore
  36. *
  37. */
  38. define('XOAD_EVENTS_TABLE_NAME', 'xoad_events');
  39.  
  40. /**
  41. * XOAD Events Storage MySQL Class.
  42. *
  43. * <p>This class is a {@link XOAD_Events_Storage} successor.</p>
  44. * <p>The class allows you to save events information in
  45. * MySQL database.</p>
  46. * <p>Example:</p>
  47. * <code>
  48. * <?php
  49. *
  50. * require_once('xoad.php');
  51. *
  52. * require_once(XOAD_BASE . '/classes/events/storage/MySQL.class.php');
  53. *
  54. * $storage = new XOAD_Events_Storage_MySQL('server=?;user=?;password=?;database=?;[port=?]');
  55. *
  56. * $storage->postEvent('event', 'class');
  57. *
  58. * ?>
  59. * </code>
  60. *
  61. * @author Stanimir Angeloff
  62. *
  63. * @package XOAD
  64. *
  65. * @subpackage XOAD_Events
  66. *
  67. * @version 0.6.0.0
  68. *
  69. */
  70. class XOAD_Events_Storage_MySQL extends XOAD_Events_Storage
  71. {
  72. /**
  73. * Holds the MySQL server used in the connection string.
  74. *
  75. * @access protected
  76. *
  77. * @var string
  78. *
  79. */
  80. var $server;
  81.  
  82. /**
  83. * Holds the MySQL user used in the connection string.
  84. *
  85. * @access protected
  86. *
  87. * @var string
  88. *
  89. */
  90. var $user;
  91.  
  92. /**
  93. * Holds the MySQL password used in the connection string.
  94. *
  95. * @access protected
  96. *
  97. * @var string
  98. *
  99. */
  100. var $password;
  101.  
  102. /**
  103. * Holds the MySQL database used in the connection string.
  104. *
  105. * @access protected
  106. *
  107. * @var string
  108. *
  109. */
  110. var $database;
  111.  
  112. /**
  113. * Holds the MySQL port used in the connection string.
  114. *
  115. * @access protected
  116. *
  117. * @var string
  118. *
  119. */
  120. var $port = 3306;
  121.  
  122. /**
  123. * Indicates whether to open a new connection to the MySQL server
  124. * if an old one already exists.
  125. *
  126. * @access protected
  127. *
  128. * @var bool
  129. *
  130. */
  131. var $openNew;
  132.  
  133. /**
  134. * Creates a new instance of the {@link XOAD_Events_Storage_MySQL} class.
  135. *
  136. * @access public
  137. *
  138. * @param string $dsn The data source name and parameters to use
  139. * when connecting to MySQL.
  140. *
  141. */
  142. function XOAD_Events_Storage_MySQL($dsn)
  143. {
  144. parent::XOAD_Events_Storage($dsn);
  145. }
  146.  
  147. /**
  148. * Retrieves a static instance of the {@link XOAD_Events_Storage_MySQL} class.
  149. *
  150. * <p>This method overrides {@link XOAD_Events_Storage::getStorage}.</p>
  151. *
  152. * @access public
  153. *
  154. * @param string $dsn The data source name and parameters to use
  155. * when connecting to MySQL.
  156. *
  157. * @return object A static instance to the
  158. * {@link XOAD_Events_Storage_MySQL} class.
  159. *
  160. * @static
  161. *
  162. */
  163. function &getStorage($dsn)
  164. {
  165. static $instance;
  166.  
  167. if ( ! isset($instance)) {
  168.  
  169. $instance = new XOAD_Events_Storage_MySQL($dsn);
  170. }
  171.  
  172. return $instance;
  173. }
  174.  
  175. /**
  176. * Creates a MySQL connection link.
  177. *
  178. * @access private
  179. *
  180. * @return resource
  181. *
  182. */
  183. function &getConnection()
  184. {
  185. $server = $this->server;
  186.  
  187. if ($this->port != 3306) {
  188.  
  189. $server .= ':' . $this->port;
  190. }
  191.  
  192. $connection = mysql_connect($server, $this->user, $this->password, $this->openNew);
  193.  
  194. mysql_select_db($this->database, $connection);
  195.  
  196. return $connection;
  197. }
  198.  
  199. /**
  200. * Closes a MySQL connection link.
  201. *
  202. * @access private
  203. *
  204. * @return void
  205. *
  206. */
  207. function closeConnection(&$connection)
  208. {
  209. mysql_close($connection);
  210.  
  211. $connection = null;
  212. }
  213.  
  214. /**
  215. * Escapes special characters in the {@link $unescapedString},
  216. * taking into account the current charset of the connection.
  217. *
  218. * @access private
  219. *
  220. * @return string
  221. *
  222. */
  223. function escapeString($unescapedString, $connection)
  224. {
  225. if (function_exists('mysql_real_escape_string')) {
  226.  
  227. return mysql_real_escape_string($unescapedString, $connection);
  228. }
  229.  
  230. return mysql_escape_string($unescapedString);
  231. }
  232.  
  233. /**
  234. * Posts multiple events to the database.
  235. *
  236. * <p>Valid keys for each event are:</p>
  237. * - event - the event name (case-sensitive);
  238. * - className - the class that is the source of the event;
  239. * - sender - the sender object of the event;
  240. * - data - the data associated with the event;
  241. * - filter - the event filter data (case-insensitive);
  242. * using this key you can post events with
  243. * the same name but with different filter data;
  244. * the client will respond to them individually;
  245. * - time - the event start time (seconds since the Unix
  246. * Epoch (January 1 1970 00:00:00 GMT);
  247. * - lifetime - the event lifetime (in seconds);
  248. *
  249. * @access public
  250. *
  251. * @param array $eventsData Array containing associative arrays
  252. * with information for each event.
  253. *
  254. * @return bool true on success, false otherwise.
  255. *
  256. */
  257. function postMultipleEvents($eventsData)
  258. {
  259. $connection =& $this->getConnection();
  260.  
  261. foreach ($eventsData as $event) {
  262.  
  263. if ( ! parent::postMultipleEvents($event)) {
  264.  
  265. continue;
  266. }
  267.  
  268. $sqlQuery = '
  269. INSERT INTO
  270. `' . XOAD_EVENTS_TABLE_NAME . '`
  271. (
  272. `event`,
  273. `className`,
  274. `filter`,
  275. `sender`,
  276. `data`,
  277. `time`,
  278. `endTime`
  279. )
  280. VALUES
  281. (
  282. \'' . $this->escapeString($event['event'], $connection) . '\',
  283. \'' . $this->escapeString($event['className'], $connection) . '\',
  284. ';
  285.  
  286. if (isset($event['filter'])) {
  287.  
  288. $sqlQuery .= '\'' . $this->escapeString($event['filter'], $connection) . '\',';
  289.  
  290. } else {
  291.  
  292. $sqlQuery .= 'NULL,';
  293. }
  294.  
  295. if (isset($event['sender'])) {
  296.  
  297. $sqlQuery .= '\'' . $this->escapeString(serialize($event['sender']), $connection) . '\',';
  298.  
  299. } else {
  300.  
  301. $sqlQuery .= 'NULL,';
  302. }
  303.  
  304. if (isset($event['data'])) {
  305.  
  306. $sqlQuery .= '\'' . $this->escapeString(serialize($event['data']), $connection) . '\',';
  307.  
  308. } else {
  309.  
  310. $sqlQuery .= 'NULL,';
  311. }
  312.  
  313. $sqlQuery .= '
  314. ' . $this->escapeString($event['time'], $connection) . ',
  315. ' . $this->escapeString($event['time'] + $event['lifetime'], $connection) . '
  316. )';
  317.  
  318. mysql_query($sqlQuery, $connection);
  319. }
  320.  
  321. $this->closeConnection($connection);
  322.  
  323. return true;
  324. }
  325.  
  326. /**
  327. * Deletes old events from the database.
  328. *
  329. * <p>This method is called before calling {@link filterEvents}
  330. * or {@link filterMultipleEvents} to delete all expired events
  331. * from the database.</p>
  332. *
  333. * @access public
  334. *
  335. * @return bool true on success, false otherwise.
  336. *
  337. */
  338. function cleanEvents()
  339. {
  340. $connection =& $this->getConnection();
  341.  
  342. $time = parent::cleanEvents();
  343.  
  344. $sqlQuery = '
  345. DELETE FROM
  346. `' . XOAD_EVENTS_TABLE_NAME . '`
  347. WHERE
  348. `endTime` < ' . $this->escapeString($time, $connection) . '
  349. ';
  350.  
  351. mysql_query($sqlQuery, $connection);
  352.  
  353. $this->closeConnection($connection);
  354.  
  355. return true;
  356. }
  357.  
  358. /**
  359. * Filters the events in the database using multiple criterias.
  360. *
  361. * <p>Valid keys for each event are:</p>
  362. * - event - the event name (case-sensitive);
  363. * - className - the class that is the source of the event;
  364. * - filter - the event filter data (case-insensitive);
  365. * using this argument you can filter events with
  366. * the same name but with different filter data;
  367. * - time - the event start time (seconds since the Unix
  368. * Epoch (January 1 1970 00:00:00 GMT).
  369. *
  370. * @access public
  371. *
  372. * @param array $eventsData Array containing associative arrays
  373. * with information for each event.
  374. *
  375. * @return array Sequental array that contains all events that match the
  376. * supplied criterias, ordered by time (ascending).
  377. *
  378. */
  379. function filterMultipleEvents($eventsData)
  380. {
  381. $connection =& $this->getConnection();
  382.  
  383. $sqlQuery = '
  384. SELECT
  385. `event`,
  386. `className`,
  387. `filter`,
  388. `sender`,
  389. `data`,
  390. `time`,
  391. `endTime`
  392. FROM
  393. `' . XOAD_EVENTS_TABLE_NAME . '`
  394. WHERE
  395. ';
  396.  
  397. $index = 0;
  398.  
  399. $length = sizeof($eventsData);
  400.  
  401. foreach ($eventsData as $event) {
  402.  
  403. if ( ! parent::filterMultipleEvents($event)) {
  404.  
  405. continue;
  406. }
  407.  
  408. $sqlQuery .= '(
  409. `time` > ' . $this->escapeString($event['time'], $connection) . '
  410. AND
  411. `event` = \'' . $this->escapeString($event['event'], $connection) . '\'
  412. AND
  413. `className` = \'' . $this->escapeString($event['className'], $connection) . '\'
  414. ';
  415.  
  416. if (isset($event['filter'])) {
  417.  
  418. $sqlQuery .= 'AND `filter` = \'' . $this->escapeString($event['filter'], $connection) . '\'';
  419. }
  420.  
  421. if ($index < $length - 1) {
  422.  
  423. $sqlQuery .= ') OR ';
  424.  
  425. } else {
  426.  
  427. $sqlQuery .= ')';
  428. }
  429.  
  430. $index ++;
  431. }
  432.  
  433. $sqlQuery .= '
  434. ORDER BY
  435. `time` ASC
  436. ';
  437.  
  438. $events = array();
  439.  
  440. $sqlResult = mysql_query($sqlQuery, $connection);
  441.  
  442. while (($row = mysql_fetch_assoc($sqlResult)) !== false) {
  443.  
  444. $events[] = array(
  445. 'event' => $row['event'],
  446. 'className' => $row['className'],
  447. 'filter' => $row['filter'],
  448. 'time' => (float) $row['time'],
  449. 'endTime' => (float) $row['endTime'],
  450. 'eventData' => array(
  451. 'sender' => ($row['sender'] === null ? null : unserialize($row['sender'])),
  452. 'data' => ($row['data'] === null ? null : unserialize($row['data']))
  453. ));
  454. }
  455.  
  456. mysql_free_result($sqlResult);
  457.  
  458. $this->closeConnection($connection);
  459.  
  460. return $events;
  461. }
  462. }
  463. ?>

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