Skip to content
Merged
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
37 changes: 37 additions & 0 deletions lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,43 @@ public function execute() {
return new ResultAdapter($result);
}

public function executeQuery(): IResult {
if ($this->getType() !== \Doctrine\DBAL\Query\QueryBuilder::SELECT) {
throw new \RuntimeException('Invalid query type, expected SELECT query');
}

try {
$result = $this->execute();
} catch (\Doctrine\DBAL\Exception $e) {
throw \OC\DB\Exceptions\DbalException::wrap($e);
}

if ($result instanceof IResult) {
return $result;
}

throw new \RuntimeException('Invalid return type for query');
}

public function executeUpdate(): int {
if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) {
throw new \RuntimeException('Invalid query type, expected INSERT, DELETE or UPDATE query');
}

try {
$result = $this->execute();
} catch (\Doctrine\DBAL\Exception $e) {
throw \OC\DB\Exceptions\DbalException::wrap($e);
}

if (!is_int($result)) {
throw new \RuntimeException('Invalid return type for query');
}

return $result;
}


/**
* Gets the complete SQL string formed by the current specifications of this QueryBuilder.
*
Expand Down
23 changes: 23 additions & 0 deletions lib/public/DB/QueryBuilder/IQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,32 @@ public function getState();
* @return IResult|int
* @throws Exception since 21.0.0
* @since 8.2.0
* @deprecated 22.0.0 Use executeQuery or executeUpdate
*/
public function execute();

/**
* Execute for select statements
*
* @return IResult
* @since 22.0.0
*
* @throws Exception
* @throws \RuntimeException in case of usage with non select query
*/
public function executeQuery(): IResult;

/**
* Execute for insert, update and delete statements
*
* @return int
* @since 22.0.0
*
* @throws Exception
* @throws \RuntimeException in case of usage with select query
*/
public function executeUpdate(): int;

/**
* Gets the complete SQL string formed by the current specifications of this QueryBuilder.
*
Expand Down