Automatically exported from code.google.com/p/planningalerts
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

18 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * $Header: /home/ppcvs/paypal_php_sdk/Log/sql.php,v 1.1 2006/02/19 08:22:29 dennis Exp $
  4. * $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $
  5. *
  6. * @version $Revision: 1.1 $
  7. * @package Log
  8. */
  9. /** PEAR's DB package */
  10. require_once 'DB.php';
  11. /**
  12. * The Log_sql class is a concrete implementation of the Log::
  13. * abstract class which sends messages to an SQL server. Each entry
  14. * occupies a separate row in the database.
  15. *
  16. * This implementation uses PHP's PEAR database abstraction layer.
  17. *
  18. * CREATE TABLE log_table (
  19. * id INT NOT NULL,
  20. * logtime TIMESTAMP NOT NULL,
  21. * ident CHAR(16) NOT NULL,
  22. * priority INT NOT NULL,
  23. * message VARCHAR(200),
  24. * PRIMARY KEY (id)
  25. * );
  26. *
  27. * @author Jon Parise <jon@php.net>
  28. * @since Horde 1.3
  29. * @since Log 1.0
  30. * @package Log
  31. *
  32. * @example sql.php Using the SQL handler.
  33. */
  34. class Log_sql extends Log
  35. {
  36. /**
  37. * Variable containing the DSN information.
  38. * @var mixed
  39. * @access private
  40. */
  41. var $_dsn = '';
  42. /**
  43. * String containing the SQL insertion statement.
  44. *
  45. * @var string
  46. * @access private
  47. */
  48. var $_sql = '';
  49. /**
  50. * Array containing our set of DB configuration options.
  51. * @var array
  52. * @access private
  53. */
  54. var $_options = array('persistent' => true);
  55. /**
  56. * Object holding the database handle.
  57. * @var object
  58. * @access private
  59. */
  60. var $_db = null;
  61. /**
  62. * Resource holding the prepared statement handle.
  63. * @var resource
  64. * @access private
  65. */
  66. var $_statement = null;
  67. /**
  68. * Flag indicating that we're using an existing database connection.
  69. * @var boolean
  70. * @access private
  71. */
  72. var $_existingConnection = false;
  73. /**
  74. * String holding the database table to use.
  75. * @var string
  76. * @access private
  77. */
  78. var $_table = 'log_table';
  79. /**
  80. * String holding the name of the ID sequence.
  81. * @var string
  82. * @access private
  83. */
  84. var $_sequence = 'log_id';
  85. /**
  86. * Maximum length of the $ident string. This corresponds to the size of
  87. * the 'ident' column in the SQL table.
  88. * @var integer
  89. * @access private
  90. */
  91. var $_identLimit = 16;
  92. /**
  93. * Constructs a new sql logging object.
  94. *
  95. * @param string $name The target SQL table.
  96. * @param string $ident The identification field.
  97. * @param array $conf The connection configuration array.
  98. * @param int $level Log messages up to and including this level.
  99. * @access public
  100. */
  101. function Log_sql($name, $ident = '', $conf = array(),
  102. $level = PEAR_LOG_DEBUG)
  103. {
  104. $this->_id = md5(microtime());
  105. $this->_table = $name;
  106. $this->_mask = Log::UPTO($level);
  107. /* Now that we have a table name, assign our SQL statement. */
  108. if (!empty($this->_sql)) {
  109. $this->_sql = $conf['sql'];
  110. } else {
  111. $this->_sql = 'INSERT INTO ' . $this->_table .
  112. ' (id, logtime, ident, priority, message)' .
  113. ' VALUES(?, CURRENT_TIMESTAMP, ?, ?, ?)';
  114. }
  115. /* If an options array was provided, use it. */
  116. if (isset($conf['options']) && is_array($conf['options'])) {
  117. $this->_options = $conf['options'];
  118. }
  119. /* If a specific sequence name was provided, use it. */
  120. if (!empty($conf['sequence'])) {
  121. $this->_sequence = $conf['sequence'];
  122. }
  123. /* If a specific sequence name was provided, use it. */
  124. if (isset($conf['identLimit'])) {
  125. $this->_identLimit = $conf['identLimit'];
  126. }
  127. /* Now that the ident limit is confirmed, set the ident string. */
  128. $this->setIdent($ident);
  129. /* If an existing database connection was provided, use it. */
  130. if (isset($conf['db'])) {
  131. $this->_db = &$conf['db'];
  132. $this->_existingConnection = true;
  133. $this->_opened = true;
  134. } else {
  135. $this->_dsn = $conf['dsn'];
  136. }
  137. }
  138. /**
  139. * Opens a connection to the database, if it has not already
  140. * been opened. This is implicitly called by log(), if necessary.
  141. *
  142. * @return boolean True on success, false on failure.
  143. * @access public
  144. */
  145. function open()
  146. {
  147. if (!$this->_opened) {
  148. /* Use the DSN and options to create a database connection. */
  149. $this->_db = &DB::connect($this->_dsn, $this->_options);
  150. if (DB::isError($this->_db)) {
  151. return false;
  152. }
  153. /* Create a prepared statement for repeated use in log(). */
  154. if (!$this->_prepareStatement()) {
  155. return false;
  156. }
  157. /* We now consider out connection open. */
  158. $this->_opened = true;
  159. }
  160. return $this->_opened;
  161. }
  162. /**
  163. * Closes the connection to the database if it is still open and we were
  164. * the ones that opened it. It is the caller's responsible to close an
  165. * existing connection that was passed to us via $conf['db'].
  166. *
  167. * @return boolean True on success, false on failure.
  168. * @access public
  169. */
  170. function close()
  171. {
  172. if ($this->_opened && !$this->_existingConnection) {
  173. $this->_opened = false;
  174. $this->_db->freePrepared($this->_statement);
  175. return $this->_db->disconnect();
  176. }
  177. return ($this->_opened === false);
  178. }
  179. /**
  180. * Sets this Log instance's identification string. Note that this
  181. * SQL-specific implementation will limit the length of the $ident string
  182. * to sixteen (16) characters.
  183. *
  184. * @param string $ident The new identification string.
  185. *
  186. * @access public
  187. * @since Log 1.8.5
  188. */
  189. function setIdent($ident)
  190. {
  191. $this->_ident = substr($ident, 0, $this->_identLimit);
  192. }
  193. /**
  194. * Inserts $message to the currently open database. Calls open(),
  195. * if necessary. Also passes the message along to any Log_observer
  196. * instances that are observing this Log.
  197. *
  198. * @param mixed $message String or object containing the message to log.
  199. * @param string $priority The priority of the message. Valid
  200. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  201. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  202. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  203. * @return boolean True on success or false on failure.
  204. * @access public
  205. */
  206. function log($message, $priority = null)
  207. {
  208. /* If a priority hasn't been specified, use the default value. */
  209. if ($priority === null) {
  210. $priority = $this->_priority;
  211. }
  212. /* Abort early if the priority is above the maximum logging level. */
  213. if (!$this->_isMasked($priority)) {
  214. return false;
  215. }
  216. /* If the connection isn't open and can't be opened, return failure. */
  217. if (!$this->_opened && !$this->open()) {
  218. return false;
  219. }
  220. /* If we don't already have our statement object yet, create it. */
  221. if (!is_object($this->_statement) && !$this->_prepareStatement()) {
  222. return false;
  223. }
  224. /* Extract the string representation of the message. */
  225. $message = $this->_extractMessage($message);
  226. /* Build our set of values for this log entry. */
  227. $id = $this->_db->nextId($this->_sequence);
  228. $values = array($id, $this->_ident, $priority, $message);
  229. /* Execute the SQL query for this log entry insertion. */
  230. $result =& $this->_db->execute($this->_statement, $values);
  231. if (DB::isError($result)) {
  232. return false;
  233. }
  234. $this->_announce(array('priority' => $priority, 'message' => $message));
  235. return true;
  236. }
  237. /**
  238. * Prepare the SQL insertion statement.
  239. *
  240. * @return boolean True if the statement was successfully created.
  241. *
  242. * @access private
  243. * @since Log 1.9.1
  244. */
  245. function _prepareStatement()
  246. {
  247. $this->_statement = $this->_db->prepare($this->_sql);
  248. /* Return success if we didn't generate an error. */
  249. return (DB::isError($this->_statement) === false);
  250. }
  251. }