Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add events to QueryBuilder class #36572

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@
'OCP\\DB\\IPreparedStatement' => $baseDir . '/lib/public/DB/IPreparedStatement.php',
'OCP\\DB\\IResult' => $baseDir . '/lib/public/DB/IResult.php',
'OCP\\DB\\ISchemaWrapper' => $baseDir . '/lib/public/DB/ISchemaWrapper.php',
'OCP\\DB\\QueryBuilder\\Events\\BeforeQueryExecutedEvent' => $baseDir . '/lib/public/DB/QueryBuilder/Events/BeforeQueryExecutedEvent.php',
'OCP\\DB\\QueryBuilder\\Events\\QueryExecutedEvent' => $baseDir . '/lib/public/DB/QueryBuilder/Events/QueryExecutedEvent.php',
'OCP\\DB\\QueryBuilder\\ICompositeExpression' => $baseDir . '/lib/public/DB/QueryBuilder/ICompositeExpression.php',
'OCP\\DB\\QueryBuilder\\IExpressionBuilder' => $baseDir . '/lib/public/DB/QueryBuilder/IExpressionBuilder.php',
'OCP\\DB\\QueryBuilder\\IFunctionBuilder' => $baseDir . '/lib/public/DB/QueryBuilder/IFunctionBuilder.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\DB\\IPreparedStatement' => __DIR__ . '/../../..' . '/lib/public/DB/IPreparedStatement.php',
'OCP\\DB\\IResult' => __DIR__ . '/../../..' . '/lib/public/DB/IResult.php',
'OCP\\DB\\ISchemaWrapper' => __DIR__ . '/../../..' . '/lib/public/DB/ISchemaWrapper.php',
'OCP\\DB\\QueryBuilder\\Events\\BeforeQueryExecutedEvent' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/Events/BeforeQueryExecutedEvent.php',
'OCP\\DB\\QueryBuilder\\Events\\QueryExecutedEvent' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/Events/QueryExecutedEvent.php',
'OCP\\DB\\QueryBuilder\\ICompositeExpression' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/ICompositeExpression.php',
'OCP\\DB\\QueryBuilder\\IExpressionBuilder' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/IExpressionBuilder.php',
'OCP\\DB\\QueryBuilder\\IFunctionBuilder' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/IFunctionBuilder.php',
Expand Down
20 changes: 19 additions & 1 deletion lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@
use OC\DB\ResultAdapter;
use OC\SystemConfig;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\Events\BeforeQueryExecutedEvent;
use OCP\DB\QueryBuilder\Events\QueryExecutedEvent;
use OCP\DB\QueryBuilder\ICompositeExpression;
use OCP\DB\QueryBuilder\ILiteral;
use OCP\DB\QueryBuilder\IParameter;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;

class QueryBuilder implements IQueryBuilder {
Expand All @@ -76,6 +79,9 @@ class QueryBuilder implements IQueryBuilder {
/** @var string */
protected $lastInsertedTable;

/** @var IEventDispatcher */
private $dispatcher;

/**
* Initializes a new QueryBuilder.
*
Expand All @@ -88,6 +94,7 @@ public function __construct(ConnectionAdapter $connection, SystemConfig $systemC
$this->logger = $logger;
$this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection->getInner());
$this->helper = new QuoteHelper();
$this->dispatcher = \OC::$server->get(IEventDispatcher::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service locators should be avoided. would it be possible to directly inject the dispatcher as constructor argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can make that happen. However, I'm going to push back a little. There are a number of places throughout Nextcloud where this exact method is used (many of which include you as an author, actually):

$eventDispatcher = $server->get(IEventDispatcher::class);

$this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);

$dispatcher = \OC::$server->get(IEventDispatcher::class);

$dispatcher = \OC::$server->get(IEventDispatcher::class);

If I were to inject it as a constructor argument, I would need to edit anywhere the QueryBuilder is instantiated, correct (looks like OC\DB\Connection is the only place)?

}

/**
Expand Down Expand Up @@ -277,7 +284,18 @@ public function execute() {
]);
}

$result = $this->queryBuilder->execute();
$event = new BeforeQueryExecutedEvent($this);
$this->dispatcher->dispatchTyped($event);
$result = $event->getResult();

if ($result === null) {
$result = $this->queryBuilder->execute();
}

$event = new QueryExecutedEvent($this, $result);
$this->dispatcher->dispatchTyped($event);
$result = $event->getResult();

if (is_int($result)) {
return $result;
}
Expand Down
90 changes: 90 additions & 0 deletions lib/public/DB/QueryBuilder/Events/BeforeQueryExecutedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Andrew Summers
*
* @author Andrew Summers
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\DB\QueryBuilder\Events;

use OCP\EventDispatcher\Event;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Result;

/**
* This event is used by apps to intercept, inspect, and potentially modify
* database queries prior to execution. This can be used for deep integrations,
* restricting user access to certain data, redacting information, etc.
*
* The result field can optionally be set by the event listener by executing the
* query in the event handler logic. This allows apps to inspect the results,
* use try/catch blocks around the query execution, and/or modify/re-execute the
* query if necessary. If the result field is not set, the QueryBuilder class
* will execute the query as expected by default.
*
* @see https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/projects.html
* @since 28.0.0
*/
class BeforeQueryExecutedEvent extends Event {
private IQueryBuilder $queryBuilder;
private $result = null;

/**
* @param IQueryBuilder $queryBuilder
* @since 28.0.0
*/
public function __construct(IQueryBuilder $queryBuilder) {
$this->queryBuilder = $queryBuilder;
}

/**
* @return IQueryBuilder
* @since 28.0.0
*/
public function getQueryBuilder(): IQueryBuilder {
return $this->queryBuilder;
}

/**
* @param IQueryBuilder $queryBuilder
* @since 28.0.0
*/
public function setQueryBuilder(IQueryBuilder $queryBuilder) {
$this->queryBuilder = $queryBuilder;
}

/**
* @return Result|int|string
* @since 28.0.0
*/
public function getResult() {
return $this->result;
}

/**
* @param Result|int|string $result
* @since 28.0.0
*/
public function setResult($result) {
$this->result = $result;
}
}
87 changes: 87 additions & 0 deletions lib/public/DB/QueryBuilder/Events/QueryExecutedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Andrew Summers
*
* @author Andrew Summers
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\DB\QueryBuilder\Events;

use OCP\EventDispatcher\Event;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Result;

/**
* This event is used by apps to intercept, inspect, and potentially modify
* the results of database queries after execution. This can be used for deep
* integrations, restricting user access to certain data, redacting information,
* etc.
*
* @see https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/projects.html
* @since 28.0.0
*/
class QueryExecutedEvent extends Event {
private IQueryBuilder $queryBuilder;
private $result;

/**
* @param IQueryBuilder $queryBuilder
* @param $result
* @since 28.0.0
*/
public function __construct(IQueryBuilder $queryBuilder, $result) {
$this->queryBuilder = $queryBuilder;
$this->result = $result;
}

/**
* @return IQueryBuilder
* @since 28.0.0
*/
public function getQueryBuilder(): IQueryBuilder {
return $this->queryBuilder;
}

/**
* @param IQueryBuilder $queryBuilder
* @since 28.0.0
*/
public function setQueryBuilder(IQueryBuilder $queryBuilder) {
$this->queryBuilder = $queryBuilder;
}

/**
* @return Result|int|string
* @since 28.0.0
*/
public function getResult() {
return $this->result;
}

/**
* @param Result|int|string $result
* @since 28.0.0
*/
public function setResult($result) {
$this->result = $result;
}
}