* @author Bertrand Mansion * @author Jon Parise * @since Log 1.8.3 * @package Log * * @example sqlite_pdo.php Using the Sqlite PDO handler. */ class Log_sqlite_pdo extends Log { /** * Array containing the connection defaults * @var array * @access private */ var $_options = array('mode' => 0666, 'persistent' => false); /** * Object holding the database handle. * @var object * @access private */ var $_db = null; /** * Flag indicating that we're using an existing database connection. * @var boolean * @access private */ var $_existingConnection = false; /** * String holding the database table to use. * @var string * @access private */ var $_table = 'log_table'; /** * Constructs a new sql logging object. * * @param string $name The target SQL table. * @param string $ident The identification field. * @param mixed $conf Can be an array of configuration options used * to open a new database connection * or an already opened sqlite connection. * @param int $level Log messages up to and including this level. * @access public */ function Log_sqlite_pdo($name, $ident = '', &$conf, $level = PEAR_LOG_DEBUG) { $this->_id = md5(microtime()); $this->_table = $name; $this->_ident = $ident; $this->_mask = Log::UPTO($level); // Set up its own connection // or use an existing database connection if (is_array($conf)) { foreach ($conf as $k => $opt) $this->_options[$k] = $opt; } elseif( $conf instanceof PDO ) { $this->_db =& $conf; $this->_existingConnection = true; } } /** * Opens a connection to the database, if it has not already * been opened. This is implicitly called by log(), if necessary. * * @return boolean True on success, false on failure. * @access public */ function open() { if ( $this->_db instanceof PDO ) { $this->_opened = true; return $this->_createTable(); } else { try{ if( $this->_options['persistent'] === true ) { $this->_db = new PDO( 'sqlite:' . $this->_options['filename'] , NULL, NULL, array( PDO::ATTR_PERSISTENT => true ) ) ; }else{ $this->_db = new PDO( 'sqlite:' . $this->_options['filename'] ); } $this->_opened = true; return $this->_createTable(); }catch( PDOException $exception ){ die($exception->getMessage()); } } return $this->_opened ; } /** * Closes the connection to the database if it is still open and we were * the ones that opened it. It is the caller's responsible to close an * existing connection that was passed to us via $conf['db']. * * @return boolean True on success, false on failure. * @access public */ function close() { /* We never close existing connections. */ if ($this->_existingConnection) { return false; } if ($this->_opened) { $this->_opened = false; $this->_db = NULL ; } return ($this->_opened === false); } /** * Inserts $message to the currently open database. Calls open(), * if necessary. Also passes the message along to any Log_observer * instances that are observing this Log. * * @param mixed $message String or object containing the message to log. * @param string $priority The priority of the message. Valid * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG. * @return boolean True on success or false on failure. * @access public */ function log($message, $priority = null) { /* If a priority hasn't been specified, use the default value. */ if ($priority === null) { $priority = $this->_priority; } /* Abort early if the priority is above the maximum logging level. */ if (!$this->_isMasked($priority)) { return false; } /* If the connection isn't open and can't be opened, return failure. */ if (!$this->_opened && !$this->open()) { return false; } // Extract the string representation of the message. $message = $this->_extractMessage($message); $time = strftime('%Y-%m-%d %H:%M:%S', time()); $sqlInsert = 'INSERT INTO ' . $this->_table . ' (logtime, ident, priority, message )' . ' VALUES (:logtime, :ident, :priority , :message )'; $stmt = $this->_db->prepare( $sqlInsert ); // PDO::PARAM_STR is default $stmt->bindParam(':logtime', $time ); $stmt->bindParam(':ident', $this->_ident ); $stmt->bindParam(':priority', $priority, PDO::PARAM_INT ); $stmt->bindParam(':message', $message ); $stmt->execute(); $this->_announce(array('priority' => $priority, 'message' => $message)); return true ; } /** * Checks whether the log table exists and creates it if necessary. * * @return boolean True on success or false on failure. * @access private */ function _createTable() { // check the table exists $q = 'SELECT name FROM sqlite_master WHERE name="' . $this->_table . '" AND type="table"'; $result = $this->_db->query($q) ; if( $result->fetch() === false ) { $q = 'CREATE TABLE [' . $this->_table . '] (' . 'id INTEGER PRIMARY KEY NOT NULL, ' . 'logtime NOT NULL, ' . 'ident CHAR(16) NOT NULL, ' . 'priority INT NOT NULL, ' . 'message)'; $this->_db->exec( $q ) ; } return true ; } }