summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2014-10-03 01:35:07 +0200
committerRobin Appelman <icewind@owncloud.com>2014-10-20 13:38:37 +0200
commitb71d1d3616115653eb928489093fc2581d830cf5 (patch)
treee46ef96d1cf112dff8d9a206381d46ff71c4ad1d
parentd38050cf52a623a0a4d29044bb50d3c247c8245c (diff)
downloadnextcloud-server-b71d1d3616115653eb928489093fc2581d830cf5.tar.gz
nextcloud-server-b71d1d3616115653eb928489093fc2581d830cf5.zip
Add QueryLogger interface to allow apps to get a list of used queries
-rw-r--r--lib/private/debug/dummyquerylogger.php31
-rw-r--r--lib/private/debug/query.php57
-rw-r--r--lib/private/debug/querylogger.php47
-rw-r--r--lib/private/server.php20
-rw-r--r--lib/public/debug/iquery.php26
-rw-r--r--lib/public/debug/iquerylogger.php27
-rw-r--r--lib/public/iservercontainer.php9
7 files changed, 217 insertions, 0 deletions
diff --git a/lib/private/debug/dummyquerylogger.php b/lib/private/debug/dummyquerylogger.php
new file mode 100644
index 00000000000..0c2664e4001
--- /dev/null
+++ b/lib/private/debug/dummyquerylogger.php
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Debug;
+
+use OCP\Debug\IQueryLogger;
+
+class DummyQueryLogger implements IQueryLogger {
+ /**
+ * @param string $sql
+ * @param array $params
+ * @param array $types
+ */
+ public function startQuery($sql, array $params = null, array $types = null) {
+ }
+
+ public function stopQuery() {
+ }
+
+ /**
+ * @return \OCP\Debug\IQuery[]
+ */
+ public function getQueries() {
+ return array();
+ }
+}
diff --git a/lib/private/debug/query.php b/lib/private/debug/query.php
new file mode 100644
index 00000000000..351c7d9daca
--- /dev/null
+++ b/lib/private/debug/query.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Debug;
+
+use OCP\Debug\IQuery;
+
+class Query implements IQuery {
+ private $sql;
+
+ private $params;
+
+ private $start;
+
+ private $end;
+
+ /**
+ * @param string $sql
+ * @param array $params
+ * @param int $start
+ */
+ public function __construct($sql, $params, $start) {
+ $this->sql = $sql;
+ $this->params = $params;
+ $this->start = $start;
+ }
+
+ public function end($time) {
+ $this->end = $time;
+ }
+
+ /**
+ * @return array
+ */
+ public function getParams() {
+ return $this->params;
+ }
+
+ /**
+ * @return string
+ */
+ public function getSql() {
+ return $this->sql;
+ }
+
+ /**
+ * @return int
+ */
+ public function getDuration() {
+ return $this->end - $this->start;
+ }
+}
diff --git a/lib/private/debug/querylogger.php b/lib/private/debug/querylogger.php
new file mode 100644
index 00000000000..990188da975
--- /dev/null
+++ b/lib/private/debug/querylogger.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Debug;
+
+use OCP\Debug\IQueryLogger;
+
+class QueryLogger implements IQueryLogger {
+ /**
+ * @var \OC\Debug\Query
+ */
+ protected $activeQuery;
+
+ /**
+ * @var \OC\Debug\Query[]
+ */
+ protected $queries = array();
+
+ /**
+ * @param string $sql
+ * @param array $params
+ * @param array $types
+ */
+ public function startQuery($sql, array $params = null, array $types = null) {
+ $this->activeQuery = new Query($sql, $params, microtime(true));
+ }
+
+ public function stopQuery() {
+ if ($this->activeQuery) {
+ $this->activeQuery->end(microtime(true));
+ $this->queries[] = $this->activeQuery;
+ $this->activeQuery = null;
+ }
+ }
+
+ /**
+ * @return \OCP\Debug\IQuery[]
+ */
+ public function getQueries() {
+ return $this->queries;
+ }
+}
diff --git a/lib/private/server.php b/lib/private/server.php
index 263f9919023..c0b94a5b4c3 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -6,7 +6,9 @@ use OC\AppFramework\Http\Request;
use OC\AppFramework\Db\Db;
use OC\AppFramework\Utility\SimpleContainer;
use OC\Cache\UserCache;
+use OC\Debug\DummyQueryLogger;
use OC\Debug\EventLogger;
+use OC\Debug\QueryLogger;
use OC\Security\CertificateManager;
use OC\DB\ConnectionWrapper;
use OC\Files\Node\Root;
@@ -229,6 +231,13 @@ class Server extends SimpleContainer implements IServerContainer {
return new DummyEventLogger();
}
});
+ $this->registerService('QueryLogger', function ($c) {
+ if (defined('DEBUG') and DEBUG) {
+ return new QueryLogger();
+ } else {
+ return new DummyQueryLogger();
+ }
+ });
}
/**
@@ -579,4 +588,15 @@ class Server extends SimpleContainer implements IServerContainer {
function getEventLogger() {
return $this->query('EventLogger');
}
+
+ /**
+ * Get the active query logger
+ *
+ * The returned logger only logs data when debug mode is enabled
+ *
+ * @return \OCP\Debug\IQueryLogger
+ */
+ function getQueryLogger() {
+ return $this->query('EventLogger');
+ }
}
diff --git a/lib/public/debug/iquery.php b/lib/public/debug/iquery.php
new file mode 100644
index 00000000000..070c4d61196
--- /dev/null
+++ b/lib/public/debug/iquery.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCP\Debug;
+
+interface IQuery {
+ /**
+ * @return string
+ */
+ public function getSql();
+
+ /**
+ * @return array
+ */
+ public function getParams();
+
+ /**
+ * @return float
+ */
+ public function getDuration();
+}
diff --git a/lib/public/debug/iquerylogger.php b/lib/public/debug/iquerylogger.php
new file mode 100644
index 00000000000..fe8eae089da
--- /dev/null
+++ b/lib/public/debug/iquerylogger.php
@@ -0,0 +1,27 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCP\Debug;
+
+use Doctrine\DBAL\Logging\SQLLogger;
+
+interface IQueryLogger extends SQLLogger {
+ /**
+ * @param string $sql
+ * @param array $params
+ * @param array $types
+ */
+ public function startQuery($sql, array $params = null, array $types = null);
+
+ public function stopQuery();
+
+ /**
+ * @return \OCP\Debug\IQuery[]
+ */
+ public function getQueries();
+}
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php
index 57bbf628993..97ff74385a2 100644
--- a/lib/public/iservercontainer.php
+++ b/lib/public/iservercontainer.php
@@ -255,4 +255,13 @@ interface IServerContainer {
* @return \OCP\Debug\IEventLogger
*/
function getEventLogger();
+
+ /**
+ * Get the active query logger
+ *
+ * The returned logger only logs data when debug mode is enabled
+ *
+ * @return \OCP\Debug\IQueryLogger
+ */
+ function getQueryLogger();
}