ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
sql.php
Go to the documentation of this file.
1 <?php
16 if (!class_exists('DB')) {
17  require_once 'DB.php';
18 }
19 
43 class Log_sql extends Log
44 {
50  var $_dsn = '';
51 
58  var $_sql = '';
59 
65  var $_options = array('persistent' => true);
66 
72  var $_db = null;
73 
79  var $_statement = null;
80 
86  var $_existingConnection = false;
87 
93  var $_table = 'log_table';
94 
100  var $_sequence = 'log_id';
101 
108  var $_identLimit = 16;
109 
110 
120  function Log_sql($name, $ident = '', $conf = array(),
121  $level = PEAR_LOG_DEBUG)
122  {
123  $this->_id = md5(microtime());
124  $this->_table = $name;
125  $this->_mask = Log::UPTO($level);
126 
127  /* Now that we have a table name, assign our SQL statement. */
128  if (!empty($conf['sql'])) {
129  $this->_sql = $conf['sql'];
130  } else {
131  $this->_sql = 'INSERT INTO ' . $this->_table .
132  ' (id, logtime, ident, priority, message)' .
133  ' VALUES(?, CURRENT_TIMESTAMP, ?, ?, ?)';
134  }
135 
136  /* If an options array was provided, use it. */
137  if (isset($conf['options']) && is_array($conf['options'])) {
138  $this->_options = $conf['options'];
139  }
140 
141  /* If a specific sequence name was provided, use it. */
142  if (!empty($conf['sequence'])) {
143  $this->_sequence = $conf['sequence'];
144  }
145 
146  /* If a specific sequence name was provided, use it. */
147  if (isset($conf['identLimit'])) {
148  $this->_identLimit = $conf['identLimit'];
149  }
150 
151  /* Now that the ident limit is confirmed, set the ident string. */
152  $this->setIdent($ident);
153 
154  /* If an existing database connection was provided, use it. */
155  if (isset($conf['db'])) {
156  $this->_db = &$conf['db'];
157  $this->_existingConnection = true;
158  $this->_opened = true;
159  } else {
160  $this->_dsn = $conf['dsn'];
161  }
162  }
163 
171  function open()
172  {
173  if (!$this->_opened) {
174  /* Use the DSN and options to create a database connection. */
175  $this->_db = &DB::connect($this->_dsn, $this->_options);
176  if (DB::isError($this->_db)) {
177  return false;
178  }
179 
180  /* Create a prepared statement for repeated use in log(). */
181  if (!$this->_prepareStatement()) {
182  return false;
183  }
184 
185  /* We now consider out connection open. */
186  $this->_opened = true;
187  }
188 
189  return $this->_opened;
190  }
191 
200  function close()
201  {
202  if ($this->_opened && !$this->_existingConnection) {
203  $this->_opened = false;
204  $this->_db->freePrepared($this->_statement);
205  return $this->_db->disconnect();
206  }
207 
208  return ($this->_opened === false);
209  }
210 
221  function setIdent($ident)
222  {
223  $this->_ident = substr($ident, 0, $this->_identLimit);
224  }
225 
239  function log($message, $priority = null)
240  {
241  /* If a priority hasn't been specified, use the default value. */
242  if ($priority === null) {
243  $priority = $this->_priority;
244  }
245 
246  /* Abort early if the priority is above the maximum logging level. */
247  if (!$this->_isMasked($priority)) {
248  return false;
249  }
250 
251  /* If the connection isn't open and can't be opened, return failure. */
252  if (!$this->_opened && !$this->open()) {
253  return false;
254  }
255 
256  /* If we don't already have our statement object yet, create it. */
257  if (!is_object($this->_statement) && !$this->_prepareStatement()) {
258  return false;
259  }
260 
261  /* Extract the string representation of the message. */
262  $message = $this->_extractMessage($message);
263 
264  /* Build our set of values for this log entry. */
265  $id = $this->_db->nextId($this->_sequence);
266  $values = array($id, $this->_ident, $priority, $message);
267 
268  /* Execute the SQL query for this log entry insertion. */
269  $result =& $this->_db->execute($this->_statement, $values);
270  if (DB::isError($result)) {
271  return false;
272  }
273 
274  $this->_announce(array('priority' => $priority, 'message' => $message));
275 
276  return true;
277  }
278 
287  function _prepareStatement()
288  {
289  $this->_statement = $this->_db->prepare($this->_sql);
290 
291  /* Return success if we didn't generate an error. */
292  return (DB::isError($this->_statement) === false);
293  }
294 }