Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlite.php 6.9 KiB

17 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * $Header: /home/ppcvs/paypal_php_sdk/Log/sqlite.php,v 1.1 2006/02/19 08:22:29 dennis Exp $
  4. *
  5. * @version $Revision: 1.1 $
  6. * @package Log
  7. */
  8. /**
  9. * The Log_sqlite class is a concrete implementation of the Log::
  10. * abstract class which sends messages to an Sqlite database.
  11. * Each entry occupies a separate row in the database.
  12. *
  13. * This implementation uses PHP native Sqlite functions.
  14. *
  15. * CREATE TABLE log_table (
  16. * id INTEGER PRIMARY KEY NOT NULL,
  17. * logtime NOT NULL,
  18. * ident CHAR(16) NOT NULL,
  19. * priority INT NOT NULL,
  20. * message
  21. * );
  22. *
  23. * @author Bertrand Mansion <bmansion@mamasam.com>
  24. * @author Jon Parise <jon@php.net>
  25. * @since Log 1.8.3
  26. * @package Log
  27. *
  28. * @example sqlite.php Using the Sqlite handler.
  29. */
  30. class Log_sqlite extends Log
  31. {
  32. /**
  33. * Array containing the connection defaults
  34. * @var array
  35. * @access private
  36. */
  37. var $_options = array('mode' => 0666,
  38. 'persistent' => false);
  39. /**
  40. * Object holding the database handle.
  41. * @var object
  42. * @access private
  43. */
  44. var $_db = null;
  45. /**
  46. * Flag indicating that we're using an existing database connection.
  47. * @var boolean
  48. * @access private
  49. */
  50. var $_existingConnection = false;
  51. /**
  52. * String holding the database table to use.
  53. * @var string
  54. * @access private
  55. */
  56. var $_table = 'log_table';
  57. /**
  58. * Constructs a new sql logging object.
  59. *
  60. * @param string $name The target SQL table.
  61. * @param string $ident The identification field.
  62. * @param mixed $conf Can be an array of configuration options used
  63. * to open a new database connection
  64. * or an already opened sqlite connection.
  65. * @param int $level Log messages up to and including this level.
  66. * @access public
  67. */
  68. function Log_sqlite($name, $ident = '', &$conf, $level = PEAR_LOG_DEBUG)
  69. {
  70. $this->_id = md5(microtime());
  71. $this->_table = $name;
  72. $this->_ident = $ident;
  73. $this->_mask = Log::UPTO($level);
  74. if (is_array($conf)) {
  75. foreach ($conf as $k => $opt) {
  76. $this->_options[$k] = $opt;
  77. }
  78. } else {
  79. // If an existing database connection was provided, use it.
  80. $this->_db =& $conf;
  81. $this->_existingConnection = true;
  82. }
  83. }
  84. /**
  85. * Opens a connection to the database, if it has not already
  86. * been opened. This is implicitly called by log(), if necessary.
  87. *
  88. * @return boolean True on success, false on failure.
  89. * @access public
  90. */
  91. function open()
  92. {
  93. if (is_resource($this->_db)) {
  94. $this->_opened = true;
  95. return $this->_createTable();
  96. } else {
  97. /* Set the connection function based on the 'persistent' option. */
  98. if (empty($this->_options['persistent'])) {
  99. $connectFunction = 'sqlite_open';
  100. } else {
  101. $connectFunction = 'sqlite_popen';
  102. }
  103. /* Attempt to connect to the database. */
  104. if ($this->_db = $connectFunction($this->_options['filename'],
  105. (int)$this->_options['mode'],
  106. $error)) {
  107. $this->_opened = true;
  108. return $this->_createTable();
  109. }
  110. }
  111. return $this->_opened;
  112. }
  113. /**
  114. * Closes the connection to the database if it is still open and we were
  115. * the ones that opened it. It is the caller's responsible to close an
  116. * existing connection that was passed to us via $conf['db'].
  117. *
  118. * @return boolean True on success, false on failure.
  119. * @access public
  120. */
  121. function close()
  122. {
  123. /* We never close existing connections. */
  124. if ($this->_existingConnection) {
  125. return false;
  126. }
  127. if ($this->_opened) {
  128. $this->_opened = false;
  129. sqlite_close($this->_db);
  130. }
  131. return ($this->_opened === false);
  132. }
  133. /**
  134. * Inserts $message to the currently open database. Calls open(),
  135. * if necessary. Also passes the message along to any Log_observer
  136. * instances that are observing this Log.
  137. *
  138. * @param mixed $message String or object containing the message to log.
  139. * @param string $priority The priority of the message. Valid
  140. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  141. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  142. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  143. * @return boolean True on success or false on failure.
  144. * @access public
  145. */
  146. function log($message, $priority = null)
  147. {
  148. /* If a priority hasn't been specified, use the default value. */
  149. if ($priority === null) {
  150. $priority = $this->_priority;
  151. }
  152. /* Abort early if the priority is above the maximum logging level. */
  153. if (!$this->_isMasked($priority)) {
  154. return false;
  155. }
  156. /* If the connection isn't open and can't be opened, return failure. */
  157. if (!$this->_opened && !$this->open()) {
  158. return false;
  159. }
  160. // Extract the string representation of the message.
  161. $message = $this->_extractMessage($message);
  162. // Build the SQL query for this log entry insertion.
  163. $q = sprintf('INSERT INTO [%s] (logtime, ident, priority, message) ' .
  164. "VALUES ('%s', '%s', %d, '%s')",
  165. $this->_table,
  166. strftime('%Y-%m-%d %H:%M:%S', time()),
  167. sqlite_escape_string($this->_ident),
  168. $priority,
  169. sqlite_escape_string($message));
  170. if (!($res = @sqlite_unbuffered_query($this->_db, $q))) {
  171. return false;
  172. }
  173. $this->_announce(array('priority' => $priority, 'message' => $message));
  174. return true;
  175. }
  176. /**
  177. * Checks whether the log table exists and creates it if necessary.
  178. *
  179. * @return boolean True on success or false on failure.
  180. * @access private
  181. */
  182. function _createTable()
  183. {
  184. $q = "SELECT name FROM sqlite_master WHERE name='" . $this->_table .
  185. "' AND type='table'";
  186. $res = sqlite_query($this->_db, $q);
  187. if (sqlite_num_rows($res) == 0) {
  188. $q = 'CREATE TABLE [' . $this->_table . '] (' .
  189. 'id INTEGER PRIMARY KEY NOT NULL, ' .
  190. 'logtime NOT NULL, ' .
  191. 'ident CHAR(16) NOT NULL, ' .
  192. 'priority INT NOT NULL, ' .
  193. 'message)';
  194. if (!($res = sqlite_unbuffered_query($this->_db, $q))) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. }