Browse Source

Merge pull request #12351 from cakephp/3next-log-queries

Deprecate logQueries()
Mark Story 7 years ago
parent
commit
12588b9029

+ 30 - 1
src/Database/Connection.php

@@ -120,7 +120,7 @@ class Connection implements ConnectionInterface
         $this->setDriver($driver, $config);
         $this->setDriver($driver, $config);
 
 
         if (!empty($config['log'])) {
         if (!empty($config['log'])) {
-            $this->logQueries($config['log']);
+            $this->enableQueryLogging($config['log']);
         }
         }
     }
     }
 
 
@@ -847,9 +847,15 @@ class Connection implements ConnectionInterface
 
 
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
+     *
+     * @deprecated 3.7.0 Use enableQueryLogging() and isQueryLoggingEnabled() instead.
      */
      */
     public function logQueries($enable = null)
     public function logQueries($enable = null)
     {
     {
+        deprecationWarning(
+            'Connection::logQueries() is deprecated. ' .
+            'Use enableQueryLogging() and isQueryLoggingEnabled() instead.'
+        );
         if ($enable === null) {
         if ($enable === null) {
             return $this->_logQueries;
             return $this->_logQueries;
         }
         }
@@ -857,6 +863,29 @@ class Connection implements ConnectionInterface
     }
     }
 
 
     /**
     /**
+     * Enable/disable query logging
+     *
+     * @param bool $value Enable/disable query logging
+     * @return $this
+     */
+    public function enableQueryLogging($value)
+    {
+        $this->_logQueries = (bool)$value;
+
+        return $this;
+    }
+
+    /**
+     * Check if query logging is enabled.
+     *
+     * @return bool
+     */
+    public function isQueryLoggingEnabled()
+    {
+        return $this->_logQueries;
+    }
+
+    /**
      * {@inheritDoc}
      * {@inheritDoc}
      *
      *
      * @deprecated 3.5.0 Use getLogger() and setLogger() instead.
      * @deprecated 3.5.0 Use getLogger() and setLogger() instead.

+ 2 - 0
src/Datasource/ConnectionInterface.php

@@ -25,6 +25,8 @@ namespace Cake\Datasource;
  * @method \Cake\Database\Query newQuery()
  * @method \Cake\Database\Query newQuery()
  * @method \Cake\Database\StatementInterface prepare($sql)
  * @method \Cake\Database\StatementInterface prepare($sql)
  * @method \Cake\Database\StatementInterface execute($query, $params = [], array $types = [])
  * @method \Cake\Database\StatementInterface execute($query, $params = [], array $types = [])
+ * @method $this enableQueryLogging($value)
+ * @method bool isQueryLoggingEnabled()
  * @method string quote($value, $type = null)
  * @method string quote($value, $type = null)
  */
  */
 interface ConnectionInterface
 interface ConnectionInterface

+ 17 - 3
src/TestSuite/Fixture/FixtureManager.php

@@ -369,9 +369,19 @@ class FixtureManager
         $dbs = $this->_fixtureConnections($fixtures);
         $dbs = $this->_fixtureConnections($fixtures);
         foreach ($dbs as $connection => $fixtures) {
         foreach ($dbs as $connection => $fixtures) {
             $db = ConnectionManager::get($connection);
             $db = ConnectionManager::get($connection);
-            $logQueries = $db->logQueries();
+            $newMethods = method_exists($db, 'isQueryLoggingEnabled');
+            if ($newMethods) {
+                $logQueries = $db->isQueryLoggingEnabled();
+            } else {
+                $logQueries = $db->logQueries();
+            }
+
             if ($logQueries && !$this->_debug) {
             if ($logQueries && !$this->_debug) {
-                $db->logQueries(false);
+                if ($newMethods) {
+                    $db->enableQueryLogging(false);
+                } else {
+                    $db->logQueries(false);
+                }
             }
             }
             $db->transactional(function ($db) use ($fixtures, $operation) {
             $db->transactional(function ($db) use ($fixtures, $operation) {
                 $db->disableConstraints(function ($db) use ($fixtures, $operation) {
                 $db->disableConstraints(function ($db) use ($fixtures, $operation) {
@@ -379,7 +389,11 @@ class FixtureManager
                 });
                 });
             });
             });
             if ($logQueries) {
             if ($logQueries) {
-                $db->logQueries(true);
+                if ($newMethods) {
+                    $db->enableQueryLogging(true);
+                } else {
+                    $db->logQueries(true);
+                }
             }
             }
         }
         }
     }
     }

+ 25 - 8
tests/TestCase/Database/ConnectionTest.php

@@ -906,13 +906,13 @@ class ConnectionTest extends TestCase
     public function testLoggerDecorator()
     public function testLoggerDecorator()
     {
     {
         $logger = new QueryLogger;
         $logger = new QueryLogger;
-        $this->connection->logQueries(true);
+        $this->connection->enableQueryLogging(true);
         $this->connection->setLogger($logger);
         $this->connection->setLogger($logger);
         $st = $this->connection->prepare('SELECT 1');
         $st = $this->connection->prepare('SELECT 1');
         $this->assertInstanceOf(LoggingStatement::class, $st);
         $this->assertInstanceOf(LoggingStatement::class, $st);
         $this->assertSame($logger, $st->getLogger());
         $this->assertSame($logger, $st->getLogger());
 
 
-        $this->connection->logQueries(false);
+        $this->connection->enableQueryLogging(false);
         $st = $this->connection->prepare('SELECT 1');
         $st = $this->connection->prepare('SELECT 1');
         $this->assertNotInstanceOf('\Cake\Database\Log\LoggingStatement', $st);
         $this->assertNotInstanceOf('\Cake\Database\Log\LoggingStatement', $st);
     }
     }
@@ -920,15 +920,32 @@ class ConnectionTest extends TestCase
     /**
     /**
      * test logQueries method
      * test logQueries method
      *
      *
+     * @deprecated
      * @return void
      * @return void
      */
      */
     public function testLogQueries()
     public function testLogQueries()
     {
     {
-        $this->connection->logQueries(true);
-        $this->assertTrue($this->connection->logQueries());
+        $this->deprecated(function () {
+            $this->connection->logQueries(true);
+            $this->assertTrue($this->connection->logQueries());
+
+            $this->connection->logQueries(false);
+            $this->assertFalse($this->connection->logQueries());
+        });
+    }
+
+    /**
+     * test enableQueryLogging method
+     *
+     * @return void
+     */
+    public function testEnableQueryLogging()
+    {
+        $this->connection->enableQueryLogging(true);
+        $this->assertTrue($this->connection->isQueryLoggingEnabled());
 
 
-        $this->connection->logQueries(false);
-        $this->assertFalse($this->connection->logQueries());
+        $this->connection->enableQueryLogging(false);
+        $this->assertFalse($this->connection->isQueryLoggingEnabled());
     }
     }
 
 
     /**
     /**
@@ -960,7 +977,7 @@ class ConnectionTest extends TestCase
             ->setMethods(['connect'])
             ->setMethods(['connect'])
             ->disableOriginalConstructor()
             ->disableOriginalConstructor()
             ->getMock();
             ->getMock();
-        $connection->logQueries(true);
+        $connection->enableQueryLogging(true);
 
 
         $driver = $this->getMockFormDriver();
         $driver = $this->getMockFormDriver();
         $connection->setDriver($driver);
         $connection->setDriver($driver);
@@ -1004,7 +1021,7 @@ class ConnectionTest extends TestCase
                 $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
                 $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
                 $this->attributeEqualTo('query', 'COMMIT')
                 $this->attributeEqualTo('query', 'COMMIT')
             ));
             ));
-        $connection->logQueries(true);
+        $connection->enableQueryLogging(true);
         $connection->begin();
         $connection->begin();
         $connection->commit();
         $connection->commit();
     }
     }

+ 6 - 6
tests/TestCase/TestSuite/FixtureManagerTest.php

@@ -72,8 +72,8 @@ class FixtureManagerTest extends TestCase
     public function testLogSchemaWithDebug()
     public function testLogSchemaWithDebug()
     {
     {
         $db = ConnectionManager::get('test');
         $db = ConnectionManager::get('test');
-        $restore = $db->logQueries();
-        $db->logQueries(true);
+        $restore = $db->isQueryLoggingEnabled();
+        $db->enableQueryLogging(true);
 
 
         $this->manager->setDebug(true);
         $this->manager->setDebug(true);
         $buffer = new ConsoleOutput();
         $buffer = new ConsoleOutput();
@@ -91,7 +91,7 @@ class FixtureManagerTest extends TestCase
         $this->manager->load($test);
         $this->manager->load($test);
         $this->manager->shutdown();
         $this->manager->shutdown();
 
 
-        $db->logQueries($restore);
+        $db->enableQueryLogging($restore);
         $this->assertContains('CREATE TABLE', implode('', $buffer->messages()));
         $this->assertContains('CREATE TABLE', implode('', $buffer->messages()));
     }
     }
 
 
@@ -104,8 +104,8 @@ class FixtureManagerTest extends TestCase
     public function testResetDbIfTableExists()
     public function testResetDbIfTableExists()
     {
     {
         $db = ConnectionManager::get('test');
         $db = ConnectionManager::get('test');
-        $restore = $db->logQueries();
-        $db->logQueries(true);
+        $restore = $db->isQueryLoggingEnabled();
+        $db->enableQueryLogging(true);
 
 
         $this->manager->setDebug(true);
         $this->manager->setDebug(true);
         $buffer = new ConsoleOutput();
         $buffer = new ConsoleOutput();
@@ -129,7 +129,7 @@ class FixtureManagerTest extends TestCase
         $this->manager->fixturize($test);
         $this->manager->fixturize($test);
         $this->manager->load($test);
         $this->manager->load($test);
 
 
-        $db->logQueries($restore);
+        $db->enableQueryLogging($restore);
         $this->assertContains('DROP TABLE', implode('', $buffer->messages()));
         $this->assertContains('DROP TABLE', implode('', $buffer->messages()));
     }
     }