PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Logging » Log » Bug #5703

sql: allow arbitrary sql usage

Details

Request #5703sql: allow arbitrary sql usage
Submitted2005-10-17 01:15 UTC
Fromnorbert_m at php dot net
Assignedjon
StatusClosed
PackageLog
PHP VersionIrrelevant
OSIrrelevant
Roadmaps(Not assigned)

Comments

[2005-10-17 01:15 UTC] norbert_m at php dot net

Description:
------------
Would be nice to allow arbitrary SQL usage without modifying the code of Log_sql and/or subclassing it.

Here is my patch which allows SQL to be passed in as an option in the configuration array (note that it is from SVN thus probably can't be applied directly to CVS--the good news is though that it is based on the latest revision (sql.php Rev 1.36)):

It keeps BC perfectly of course.

Please note that the patch contains a patch to Bug #5702 as well.

Index: sql.php
===================================================================
--- sql.php (revision 94)
+++ sql.php (working copy)
@@ -57,6 +57,13 @@
var $_db = null;

/**
+ * Raw SQL string to initiate the statement.
+ * Will be initialized in the constructor.
+ * @var string
+ */
+ var $_sql = null;
+
+ /**
* Resource holding the prepared statement handle.
* @var resource
* @access private
@@ -107,6 +114,9 @@
{
$this->_id = md5(microtime());
$this->_table = $name;
+ $this->_sql = 'INSERT INTO ' . $this->_table .
+ ' (id, logtime, ident, priority, message)' .
+ ' VALUES(?, CURRENT_TIMESTAMP, ?, ?, ?)';
$this->_mask = Log::UPTO($level);

/* If an options array was provided, use it. */
@@ -115,6 +125,12 @@
$this->_options = $conf['options'];
}

+ /* If an SQL query was provided, use it. */
+ if (!empty($conf['sql']))
+ {
+ $this->_sql = $conf['sql'];
+ }
+
/* If a specific sequence name was provided, use it. */
if (!empty($conf['sequence'])) {
$this->_sequence = $conf['sequence'];
@@ -132,6 +148,10 @@
if (isset($conf['db'])) {
$this->_db = &$conf['db'];
$this->_existingConnection = true;
+ $this->_statement = $this->_db->prepare($this->_sql);
+ if (DB::isError($this->_statement)) {
+ return false;
+ }
$this->_opened = true;
} else {
$this->_dsn = $conf['dsn'];
@@ -155,10 +175,7 @@
}

/* Create a prepared statement for repeated use in log(). */
- $this->_statement =
- $this->_db->prepare('INSERT INTO ' . $this->_table .
- ' (id, logtime, ident, priority, message)' .
- ' VALUES(?, CURRENT_TIMESTAMP, ?, ?, ?)');
+ $this->_statement = $this->_db->prepare($this->_sql);
if (DB::isError($this->_statement)) {
return false;
}