Commit 853ce248 authored by webchick's avatar webchick
Browse files

Temporary roll-back of #298669: This wasn't quite ready yet and broke SimpleTest in a big way.

parent 5b86f43a
......@@ -146,22 +146,6 @@ 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);
......@@ -285,55 +269,6 @@ protected function prepareQuery($query) {
return $statements[$query];
}
/**
* 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.
*
......@@ -744,67 +679,6 @@ 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.
*
......@@ -990,15 +864,6 @@ final protected static function openConnection($key, $target) {
$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.
......@@ -1204,20 +1069,7 @@ public function execute($args, $options) {
}
}
$this->dbh->lastStatement = $this;
$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;
return parent::execute($args);
}
/**
......
......@@ -1655,71 +1655,3 @@ 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());
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment