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