Source for file PearDB.class.php

Documentation is available at PearDB.class.php

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

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