Skip to content

Commit 0cc6fdb

Browse files
committed
add events to QueryBuilder class
Signed-off-by: summersab <[email protected]> clean up php lint move event classes to `lib/public`
1 parent 24ebc19 commit 0cc6fdb

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed

3rdparty

Submodule 3rdparty updated 382 files

lib/private/DB/QueryBuilder/QueryBuilder.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
use OCP\DB\QueryBuilder\IParameter;
5454
use OCP\DB\QueryBuilder\IQueryBuilder;
5555
use OCP\DB\QueryBuilder\IQueryFunction;
56+
use OCP\DB\QueryBuilder\Events\BeforeQueryExecuted;
57+
use OCP\DB\QueryBuilder\Events\AfterQueryExecuted;
58+
use OCP\EventDispatcher\IEventDispatcher;
5659
use Psr\Log\LoggerInterface;
5760

5861
class QueryBuilder implements IQueryBuilder {
@@ -76,6 +79,9 @@ class QueryBuilder implements IQueryBuilder {
7679
/** @var string */
7780
protected $lastInsertedTable;
7881

82+
/** @var IEventDispatcher */
83+
private $dispatcher;
84+
7985
/**
8086
* Initializes a new QueryBuilder.
8187
*
@@ -88,6 +94,7 @@ public function __construct(ConnectionAdapter $connection, SystemConfig $systemC
8894
$this->logger = $logger;
8995
$this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection->getInner());
9096
$this->helper = new QuoteHelper();
97+
$this->dispatcher = \OC::$server->get(IEventDispatcher::class);
9198
}
9299

93100
/**
@@ -277,7 +284,18 @@ public function execute() {
277284
]);
278285
}
279286

280-
$result = $this->queryBuilder->execute();
287+
$event = new BeforeQueryExecuted($this);
288+
$this->dispatcher->dispatchTyped($event);
289+
$result = $event->getResult();
290+
291+
if ($result === null) {
292+
$result = $this->queryBuilder->execute();
293+
}
294+
295+
$event = new AfterQueryExecuted($this, $result);
296+
$this->dispatcher->dispatchTyped($event);
297+
$result = $event->getResult();
298+
281299
if (is_int($result)) {
282300
return $result;
283301
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Andrew Summers
7+
*
8+
* @author Andrew Summers
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCP\DB\QueryBuilder\Events;
28+
29+
use OCP\EventDispatcher\Event;
30+
use OCP\DB\QueryBuilder\IQueryBuilder;
31+
32+
/**
33+
* This event is used by apps to intercept, inspect, and potentially modify
34+
* the results of database queries after execution. This can be used for deep
35+
* integrations, restricting user access to certain data, redacting information,
36+
* etc.
37+
*
38+
* @see https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/projects.html
39+
* @since 26.0.0
40+
*/
41+
class AfterQueryExecuted extends Event {
42+
private IQueryBuilder $queryBuilder;
43+
private $result;
44+
45+
public function __construct(IQueryBuilder $queryBuilder, $result) {
46+
$this->queryBuilder = $queryBuilder;
47+
$this->result = $result;
48+
}
49+
50+
public function getQueryBuilder() {
51+
return $this->queryBuilder;
52+
}
53+
54+
public function setQueryBuilder() {
55+
return $this->queryBuilder;
56+
}
57+
58+
public function getResult() {
59+
return $this->result;
60+
}
61+
62+
public function setResult() {
63+
return $this->result;
64+
}
65+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Andrew Summers
7+
*
8+
* @author Andrew Summers
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCP\DB\QueryBuilder\Events;
28+
29+
use OCP\EventDispatcher\Event;
30+
use OCP\DB\QueryBuilder\IQueryBuilder;
31+
32+
/**
33+
* This event is used by apps to intercept, inspect, and potentially modify
34+
* database queries prior to execution. This can be used for deep integrations,
35+
* restricting user access to certain data, redacting information, etc.
36+
*
37+
* The result field can optionally be set by the event listener by executing the
38+
* query in the event handler logic. This allows apps to inspect the results,
39+
* use try/catch blocks around the query execution, and/or modify/re-execute the
40+
* query if necessary. If the result field is not set, the QueryBuilder class
41+
* will execute the query as expected by default.
42+
*
43+
* @see https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/projects.html
44+
* @since 26.0.0
45+
*/
46+
class BeforeQueryExecuted extends Event {
47+
private IQueryBuilder $queryBuilder;
48+
private $result = null;
49+
50+
public function __construct(IQueryBuilder $queryBuilder) {
51+
$this->queryBuilder = $queryBuilder;
52+
}
53+
54+
public function getQueryBuilder() {
55+
return $this->queryBuilder;
56+
}
57+
58+
public function setQueryBuilder() {
59+
return $this->queryBuilder;
60+
}
61+
62+
public function getResult() {
63+
return $this->result;
64+
}
65+
66+
public function setResult() {
67+
return $this->result;
68+
}
69+
}

0 commit comments

Comments
 (0)