ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
mdb2.php
Go to the documentation of this file.
1<?php
10require_once 'MDB2.php';
11MDB2::loadFile('Date');
12
34class Log_mdb2 extends Log
35{
41 var $_dsn = '';
42
48 var $_options = array('persistent' => true);
49
55 var $_db = null;
56
62 var $_statement = null;
63
70
76 var $_table = 'log_table';
77
83 var $_sequence = 'log_id';
84
91 var $_identLimit = 16;
92
98 var $_types = array(
99 'id' => 'integer',
100 'logtime' => 'timestamp',
101 'ident' => 'text',
102 'priority' => 'text',
103 'message' => 'clob'
104 );
105
115 function Log_mdb2($name, $ident = '', $conf = array(),
116 $level = PEAR_LOG_DEBUG)
117 {
118 $this->_id = md5(microtime());
119 $this->_table = $name;
120 $this->_mask = Log::UPTO($level);
121
122 /* If an options array was provided, use it. */
123 if (isset($conf['options']) && is_array($conf['options'])) {
124 $this->_options = $conf['options'];
125 }
126
127 /* If a specific sequence name was provided, use it. */
128 if (!empty($conf['sequence'])) {
129 $this->_sequence = $conf['sequence'];
130 }
131
132 /* If a specific sequence name was provided, use it. */
133 if (isset($conf['identLimit'])) {
134 $this->_identLimit = $conf['identLimit'];
135 }
136
137 /* Now that the ident limit is confirmed, set the ident string. */
138 $this->setIdent($ident);
139
140 /* If an existing database connection was provided, use it. */
141 if (isset($conf['db'])) {
142 $this->_db = &$conf['db'];
143 $this->_existingConnection = true;
144 $this->_opened = true;
145 } elseif (isset($conf['singleton'])) {
146 $this->_db = &MDB2::singleton($conf['singleton'], $this->_options);
147 $this->_existingConnection = true;
148 $this->_opened = true;
149 } else {
150 $this->_dsn = $conf['dsn'];
151 }
152 }
153
161 function open()
162 {
163 if (!$this->_opened) {
164 /* Use the DSN and options to create a database connection. */
165 $this->_db = &MDB2::connect($this->_dsn, $this->_options);
166 if (PEAR::isError($this->_db)) {
167 return false;
168 }
169
170 /* Create a prepared statement for repeated use in log(). */
171 if (!$this->_prepareStatement()) {
172 return false;
173 }
174
175 /* We now consider out connection open. */
176 $this->_opened = true;
177 }
178
179 return $this->_opened;
180 }
181
190 function close()
191 {
192 /* If we have a statement object, free it. */
193 if (is_object($this->_statement)) {
194 $this->_statement->free();
195 $this->_statement = null;
196 }
197
198 /* If we opened the database connection, disconnect it. */
199 if ($this->_opened && !$this->_existingConnection) {
200 $this->_opened = false;
201 return $this->_db->disconnect();
202 }
203
204 return ($this->_opened === false);
205 }
206
217 function setIdent($ident)
218 {
219 $this->_ident = substr($ident, 0, $this->_identLimit);
220 }
221
235 function log($message, $priority = null)
236 {
237 /* If a priority hasn't been specified, use the default value. */
238 if ($priority === null) {
239 $priority = $this->_priority;
240 }
241
242 /* Abort early if the priority is above the maximum logging level. */
243 if (!$this->_isMasked($priority)) {
244 return false;
245 }
246
247 /* If the connection isn't open and can't be opened, return failure. */
248 if (!$this->_opened && !$this->open()) {
249 return false;
250 }
251
252 /* If we don't already have a statement object, create one. */
253 if (!is_object($this->_statement) && !$this->_prepareStatement()) {
254 return false;
255 }
256
257 /* Extract the string representation of the message. */
258 $message = $this->_extractMessage($message);
259
260 /* Build our set of values for this log entry. */
261 $values = array(
262 'id' => $this->_db->nextId($this->_sequence),
263 'logtime' => MDB2_Date::mdbNow(),
264 'ident' => $this->_ident,
265 'priority' => $priority,
266 'message' => $message
267 );
268
269 /* Execute the SQL query for this log entry insertion. */
270 $this->_db->expectError(MDB2_ERROR_NOSUCHTABLE);
271 $result = &$this->_statement->execute($values);
272 $this->_db->popExpect();
273
274 /* Attempt to handle any errors. */
275 if (PEAR::isError($result)) {
276 /* We can only handle MDB2_ERROR_NOSUCHTABLE errors. */
277 if ($result->getCode() != MDB2_ERROR_NOSUCHTABLE) {
278 return false;
279 }
280
281 /* Attempt to create the target table. */
282 if (!$this->_createTable()) {
283 return false;
284 }
285
286 /* Recreate our prepared statement resource. */
287 $this->_statement->free();
288 if (!$this->_prepareStatement()) {
289 return false;
290 }
291
292 /* Attempt to re-execute the insertion query. */
293 $result = $this->_statement->execute($values);
294 if (PEAR::isError($result)) {
295 return false;
296 }
297 }
298
299 $this->_announce(array('priority' => $priority, 'message' => $message));
300
301 return true;
302 }
303
310 function _createTable()
311 {
312 $this->_db->loadModule('Manager', null, true);
313 $result = $this->_db->manager->createTable(
314 $this->_table,
315 array(
316 'id' => array('type' => $this->_types['id']),
317 'logtime' => array('type' => $this->_types['logtime']),
318 'ident' => array('type' => $this->_types['ident']),
319 'priority' => array('type' => $this->_types['priority']),
320 'message' => array('type' => $this->_types['message'])
321 )
322 );
323 if (PEAR::isError($result)) {
324 return false;
325 }
326
327 $result = $this->_db->manager->createIndex(
328 $this->_table,
329 'unique_id',
330 array('fields' => array('id' => true), 'unique' => true)
331 );
332 if (PEAR::isError($result)) {
333 return false;
334 }
335
336 return true;
337 }
338
348 {
349 $this->_statement = &$this->_db->prepare(
350 'INSERT INTO ' . $this->_table .
351 ' (id, logtime, ident, priority, message)' .
352 ' VALUES(:id, :logtime, :ident, :priority, :message)',
353 $this->_types, MDB2_PREPARE_MANIP);
354
355 /* Return success if we didn't generate an error. */
356 return (PEAR::isError($this->_statement) === false);
357 }
358}
$result
const MDB2_PREPARE_MANIP
These are just helper constants to more verbosely express parameters to prepare()
Definition: MDB2.php:109
const MDB2_ERROR_NOSUCHTABLE
Definition: MDB2.php:85
const PEAR_LOG_DEBUG
Definition: Log.php:17
_prepareStatement()
Prepare the SQL insertion statement.
Definition: mdb2.php:347
$_sequence
Definition: mdb2.php:83
Log_mdb2($name, $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
Constructs a new sql logging object.
Definition: mdb2.php:115
$_options
Definition: mdb2.php:48
_createTable()
Create the log table in the database.
Definition: mdb2.php:310
$_table
Definition: mdb2.php:76
open()
Opens a connection to the database, if it has not already been opened.
Definition: mdb2.php:161
$_existingConnection
Definition: mdb2.php:69
$_statement
Definition: mdb2.php:62
$_db
Definition: mdb2.php:55
$_dsn
Definition: mdb2.php:41
close()
Closes the connection to the database if it is still open and we were the ones that opened it.
Definition: mdb2.php:190
$_identLimit
Definition: mdb2.php:91
setIdent($ident)
Sets this Log instance's identification string.
Definition: mdb2.php:217
$_types
Definition: mdb2.php:98
log($message, $priority=null)
Inserts $message to the currently open database.
Definition: mdb2.php:235
UPTO($priority)
Calculate the log mask for all priorities up to the given priority.
Definition: Log.php:642
$_priority
Definition: Log.php:69
_isMasked($priority)
Check if the given priority is included in the current level mask.
Definition: Log.php:726
$_opened
Definition: Log.php:45
_announce($event)
Informs each registered observer instance that a new message has been logged.
Definition: Log.php:811
_extractMessage($message)
Returns the string representation of the message data.
Definition: Log.php:417
mdbNow()
return the current datetime
Definition: Date.php:73
loadFile($file)
load a file (like 'Date')
Definition: MDB2.php:516
& connect($dsn, $options=false)
Create a new MDB2 connection object and connect to the specified database.
Definition: MDB2.php:431
& singleton($dsn=null, $options=false)
Returns a MDB2 connection with the requested DSN.
Definition: MDB2.php:481
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
The Log:: class implements both an abstraction for various logging mechanisms and the Subject end of ...