aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2021-03-04 15:46:17 +0100
committerGitHub <noreply@github.com>2021-03-04 15:46:17 +0100
commit7b7358868d722b9e337cbcd236fa7a20de2133a0 (patch)
tree57dabedcf7d1579fb3956958278dc6a418ee4fcc
parent3bbacb2f541a513f47e0744ab8a629b936a091d8 (diff)
parent76a6328d10ceaf6774d205a04338782e577512ee (diff)
downloadnextcloud-server-7b7358868d722b9e337cbcd236fa7a20de2133a0.tar.gz
nextcloud-server-7b7358868d722b9e337cbcd236fa7a20de2133a0.zip
Merge pull request #23665 from nextcloud/enh/querybuilder/execute_query_update
Add executeQuery and executeUpdate
-rw-r--r--lib/private/DB/QueryBuilder/QueryBuilder.php37
-rw-r--r--lib/public/DB/QueryBuilder/IQueryBuilder.php23
2 files changed, 60 insertions, 0 deletions
diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php
index aa0327e89d9..83b233ac1b5 100644
--- a/lib/private/DB/QueryBuilder/QueryBuilder.php
+++ b/lib/private/DB/QueryBuilder/QueryBuilder.php
@@ -291,6 +291,43 @@ class QueryBuilder implements IQueryBuilder {
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.
*
diff --git a/lib/public/DB/QueryBuilder/IQueryBuilder.php b/lib/public/DB/QueryBuilder/IQueryBuilder.php
index 0a5d42e48c5..91ffab02f48 100644
--- a/lib/public/DB/QueryBuilder/IQueryBuilder.php
+++ b/lib/public/DB/QueryBuilder/IQueryBuilder.php
@@ -159,10 +159,33 @@ interface IQueryBuilder {
* @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.
*
* <code>