From 20cd0ae55b3e56e0fbe34033a023f6a5f1c2b7d6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 21 Jul 2015 14:17:47 +0200 Subject: [PATCH] Add a log message when the Doctrine Query Builder is retrieved --- lib/private/appframework/db/db.php | 9 ---- lib/private/db/connection.php | 44 +++++++++++++++++--- lib/private/db/querybuilder/querybuilder.php | 2 +- lib/public/idbconnection.php | 8 ---- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/lib/private/appframework/db/db.php b/lib/private/appframework/db/db.php index 6134d33b6c98..cde858316871 100644 --- a/lib/private/appframework/db/db.php +++ b/lib/private/appframework/db/db.php @@ -45,15 +45,6 @@ public function __construct(IDBConnection $connection) { $this->connection = $connection; } - /** - * Gets the ExpressionBuilder for the connection. - * - * @return \OCP\DB\QueryBuilder\IExpressionBuilder - */ - public function getExpressionBuilder() { - return $this->connection->getExpressionBuilder(); - } - /** * Gets the ExpressionBuilder for the connection. * diff --git a/lib/private/db/connection.php b/lib/private/db/connection.php index ae5d22bf4d83..def3f2fd1202 100644 --- a/lib/private/db/connection.php +++ b/lib/private/db/connection.php @@ -53,22 +53,54 @@ public function connect() { } } + /** + * Returns a QueryBuilder for the connection. + * + * @return \OCP\DB\QueryBuilder\IQueryBuilder + */ + public function getQueryBuilder() { + return new QueryBuilder($this); + } + + /** + * Gets the QueryBuilder for the connection. + * + * @return \Doctrine\DBAL\Query\QueryBuilder + * @deprecated please use $this->getQueryBuilder() instead + */ + public function createQueryBuilder() { + $backtrace = $this->getCallerBacktrace(); + \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); + return parent::createQueryBuilder(); + } + /** * Gets the ExpressionBuilder for the connection. * - * @return \OCP\DB\QueryBuilder\IExpressionBuilder + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder + * @deprecated please use $this->getQueryBuilder()->expr() instead */ public function getExpressionBuilder() { - return new ExpressionBuilder($this); + $backtrace = $this->getCallerBacktrace(); + \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); + return parent::getExpressionBuilder(); } /** - * Gets the QueryBuilder for the connection. + * Get the file and line that called the method where `getCallerBacktrace()` was used * - * @return \OCP\DB\QueryBuilder\IQueryBuilder + * @return string */ - public function getQueryBuilder() { - return new QueryBuilder($this); + protected function getCallerBacktrace() { + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); + + // 0 is the method where we use `getCallerBacktrace` + // 1 is the target method which uses the method we want to log + if (isset($traces[1])) { + return $traces[1]['file'] . ':' . $traces[1]['line']; + } + + return ''; } /** diff --git a/lib/private/db/querybuilder/querybuilder.php b/lib/private/db/querybuilder/querybuilder.php index 38c3966d9a02..1a1408876f16 100644 --- a/lib/private/db/querybuilder/querybuilder.php +++ b/lib/private/db/querybuilder/querybuilder.php @@ -65,7 +65,7 @@ public function __construct(IDBConnection $connection) { * @return \OCP\DB\QueryBuilder\IExpressionBuilder */ public function expr() { - return $this->connection->getExpressionBuilder(); + return new ExpressionBuilder($this->connection); } /** diff --git a/lib/public/idbconnection.php b/lib/public/idbconnection.php index 7c94611a6847..0d04c43d73e7 100644 --- a/lib/public/idbconnection.php +++ b/lib/public/idbconnection.php @@ -40,14 +40,6 @@ * @since 6.0.0 */ interface IDBConnection { - /** - * Gets the ExpressionBuilder for the connection. - * - * @return \OCP\DB\QueryBuilder\IExpressionBuilder - * @since 8.2.0 - */ - public function getExpressionBuilder(); - /** * Gets the QueryBuilder for the connection. *