summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2008-09-28 00:03:33 +0000
committerDries Buytaert <dries@buytaert.net>2008-09-28 00:03:33 +0000
commit5b86f43a7588daa7e3c37911020ce3ac85d8ddb1 (patch)
treebc565f3b597c8226cb08cfb23431fb3b684b2cd6
parentbe5e42221e3d027940e704f1e97bf0aa0b37e06f (diff)
downloadbrdo-5b86f43a7588daa7e3c37911020ce3ac85d8ddb1.tar.gz
brdo-5b86f43a7588daa7e3c37911020ce3ac85d8ddb1.tar.bz2
- Patch #298669 by Crell, moshe weitzman: added query logging per connection.
-rw-r--r--includes/database/database.inc150
-rw-r--r--modules/simpletest/tests/database_test.test68
2 files changed, 217 insertions, 1 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc
index f237de62b..afa772d58 100644
--- a/includes/database/database.inc
+++ b/includes/database/database.inc
@@ -146,6 +146,22 @@ abstract class DatabaseConnection extends PDO {
*/
public $lastStatement;
+ /**
+ * The database target this connection is for.
+ *
+ * We need this information for later auditing and logging.
+ *
+ * @var string
+ */
+ protected $target = NULL;
+
+ /**
+ * The current database logging object for this connection.
+ *
+ * @var DatabaseLog
+ */
+ protected $logger = NULL;
+
function __construct($dsn, $username, $password, $driver_options = array()) {
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; // Because the other methods don't seem to work right.
parent::__construct($dsn, $username, $password, $driver_options);
@@ -270,6 +286,55 @@ abstract class DatabaseConnection extends PDO {
}
/**
+ * Tell this connection object what its target value is.
+ *
+ * This is needed for logging and auditing. It's sloppy to do in the
+ * constructor because the constructor for child classes has a different
+ * signature. We therefore also ensure that this function is only ever
+ * called once.
+ *
+ * @param $target
+ * The target this connection is for. Set to NULL (default) to disable
+ * logging entirely.
+ */
+ public function setTarget($target = NULL) {
+ if (!isset($this->target)) {
+ $this->target = $target;
+ }
+ }
+
+ /**
+ * Returns the target this connection is associated with.
+ *
+ * @return
+ * The target string of this connection.
+ */
+ public function getTarget() {
+ return $this->target;
+ }
+
+ /**
+ * Associate a logging object with this connection.
+ *
+ * @param $logger
+ * The logging object we want to use.
+ */
+ public function setLogger(DatabaseLog $logger) {
+ $this->logger = $logger;
+ }
+
+ /**
+ * Get the current logging object for this connection.
+ *
+ * @return
+ * The current logging object for this connection. If there isn't one,
+ * NULL is returned.
+ */
+ public function getLogger() {
+ return $this->logger;
+ }
+
+ /**
* Create the appropriate sequence name for a given table and serial field.
*
* This information is exposed to all database drivers, although it is only
@@ -680,6 +745,67 @@ abstract class Database {
static protected $activeKey = 'default';
/**
+ * An array of active query log objects.
+ *
+ * @var array
+ */
+ static protected $logs = array();
+
+ /**
+ * Start logging a given logging key on the specified connection.
+ *
+ * @see DatabaseLog
+ * @param $logging_key
+ * The logging key to log.
+ * @param $key
+ * The database connection key for which we want to log.
+ * @return
+ * The query log object. Note that the log object does support richer
+ * methods than the few exposed through the Database class, so in some
+ * cases it may be desirable to access it directly.
+ */
+ final public static function startLog($logging_key, $key = 'default') {
+ if (empty(self::$logs[$key])) {
+ self::$logs[$key] = new DatabaseLog($key);
+
+ // Every target already active for this connection key needs to have
+ // the logging object associated with it.
+ foreach (self::$connections[$key] as $connection) {
+ $connection->setLogger(self::$logs[$key]);
+ }
+ }
+ self::$logs[$key]->start($logging_key);
+
+ return self::$logs[$key];
+ }
+
+ /**
+ * Retrieve the queries logged on for given logging key.
+ *
+ * This method also ends logging for the specified key. To get the query log
+ * to date without ending the logger request the logging object by starting
+ * it again (which does nothing to an open log key) and call methods on it as
+ * desired.
+ *
+ * @see DatabaseLog
+ * @param $logging_key
+ * The logging key to log.
+ * @param $key
+ * The database connection key for which we want to log.
+ * @return
+ * The query log for the specified logging key and connection.
+ */
+ final public static function getLog($logging_key, $key = 'default') {
+ if (empty(self::$logs[$key])) {
+ return NULL;
+ }
+
+ $queries = self::$logs[$key]->get($logging_key);
+ self::$logs[$key]->end($logging_key);
+ return $queries;
+ }
+
+ /**
* Gets the active connection object for the specified target.
*
* @return
@@ -864,6 +990,15 @@ abstract class Database {
$driver_class = 'DatabaseConnection_' . $driver;
require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
self::$connections[$key][$target] = new $driver_class(self::$databaseInfo[$key][$target]);
+ self::$connections[$key][$target]->setTarget($target);
+
+ // If we have any active logging objects for this connection key, we need
+ // to associate them with the connection we just opened.
+ if (!empty(self::$logs[$key])) {
+ foreach (self::$logs[$key] as $log) {
+ self::$connections[$key][$target]->setLogger($log);
+ }
+ }
// We need to pass around the simpletest database prefix in the request
// and we put that in the user_agent header.
@@ -1069,7 +1204,20 @@ class DatabaseStatement extends PDOStatement {
}
}
$this->dbh->lastStatement = $this;
- return parent::execute($args);
+
+ $logger = $this->dbh->getLogger();
+ if (!empty($logger)) {
+ $query_start = microtime(TRUE);
+ }
+
+ $return = parent::execute($args);
+
+ if (!empty($logger)) {
+ $query_end = microtime(TRUE);
+ $logger->log($this, $args, $query_end - $query_start);
+ }
+
+ return $return;
}
/**
diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test
index ec6e6a9e3..80c8ee301 100644
--- a/modules/simpletest/tests/database_test.test
+++ b/modules/simpletest/tests/database_test.test
@@ -1655,3 +1655,71 @@ class DatabaseRegressionTestCase extends DatabaseTestCase {
$this->assertIdentical($name, $from_database, t("The database handles UTF-8 characters cleanly."));
}
}
+
+/**
+ * Query logging tests.
+ */
+class DatabaseLoggingTestCase extends DatabaseTestCase {
+
+ function getInfo() {
+ return array(
+ 'name' => t('Query logging'),
+ 'description' => t('Test the query logging facility.'),
+ 'group' => t('Database'),
+ );
+ }
+
+ /**
+ * Test that we can log the existence of a query.
+ */
+ function testEnableLogging() {
+ try {
+ Database::startLog('testing');
+
+ db_query("SELECT name FROM {test} WHERE age > :age", array(':age' => 25))->fetchCol();
+ db_query("SELECT age FROM {test} WHERE name > :name", array(':name' => 'Ringo'))->fetchCol();
+
+ $queries = Database::getLog('testing', 'default');
+
+ // For debugging, since SimpleTest offers no good debugging mechanism (irony!).
+ // $this->pass(print_r($queries, 1));
+
+ $this->assertEqual(count($queries), 2, 'Correct number of queries recorded.');
+
+ foreach ($queries as $query) {
+ $this->assertEqual($query['caller']['function'], __FUNCTION__, "Correct function in query log.");
+ }
+
+ // For debugging, since SimpleTest offers no good debugging meachnism (irony!)
+ $this->pass(print_r($_SESSION['lg_debug'], 1));
+ $_SESSION['lg_debug'] = array();
+ }
+ catch(Exception $e) {
+ $this->assertTrue(FALSE, $e->getMessage());
+ }
+ }
+
+ /**
+ * Test that we can run two logs in parallel.
+ */
+ function testEnableMultiLogging() {
+ try {
+ Database::startLog('testing1');
+
+ db_query("SELECT name FROM {test} WHERE age > :age", array(':age' => 25))->fetchCol();
+
+ Database::startLog('testing2');
+
+ db_query("SELECT age FROM {test} WHERE name > :name", array(':name' => 'Ringo'))->fetchCol();
+
+ $queries1 = Database::getLog('testing1');
+ $queries2 = Database::getLog('testing2');
+
+ $this->assertEqual(count($queries1), 2, 'Correct number of queries recorded for log 1.');
+ $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for log 2.');
+ }
+ catch(Exception $e) {
+ $this->assertTrue(FALSE, $e->getMessage());
+ }
+ }
+}