summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-09-15 09:00:34 -0100
committerMaxence Lange <maxence@artificial-owl.com>2019-09-25 14:09:01 +0200
commit445b52b049e00c201f20656586d159082fd6c99c (patch)
tree91540ca8b86744409274f8cec10d3824c6965678 /lib
parent9920fb13fe7aa9d439c8656c1b909845bd906be7 (diff)
first migration to ExtendedQueryBuilder
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Db/CoreRequestBuilder.php57
-rw-r--r--lib/Db/StreamRequestBuilder.php62
-rw-r--r--lib/Exceptions/RowNotFoundException.php39
3 files changed, 41 insertions, 117 deletions
diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php
index 461b3931..8d75f7fe 100644
--- a/lib/Db/CoreRequestBuilder.php
+++ b/lib/Db/CoreRequestBuilder.php
@@ -31,7 +31,6 @@ declare(strict_types=1);
namespace OCA\Social\Db;
-use daita\MySmallPhpTools\IQueryRow;
use DateInterval;
use DateTime;
use Doctrine\DBAL\Query\QueryBuilder;
@@ -41,7 +40,6 @@ use OC\DB\SchemaWrapper;
use OCA\Social\AP;
use OCA\Social\Exceptions\DateTimeException;
use OCA\Social\Exceptions\InvalidResourceException;
-use OCA\Social\Exceptions\RowNotFoundException;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\ActivityPub\Object\Document;
use OCA\Social\Model\ActivityPub\Object\Follow;
@@ -104,7 +102,6 @@ class CoreRequestBuilder {
protected $miscService;
-
/** @var Person */
protected $viewer = null;
@@ -145,6 +142,14 @@ class CoreRequestBuilder {
/**
+ * @return IDBConnection
+ */
+ public function getConnection(): IDBConnection {
+ return $this->dbConnection;
+ }
+
+
+ /**
* @param Person $viewer
*/
public function setViewer(Person $viewer) {
@@ -291,7 +296,7 @@ class CoreRequestBuilder {
* @param string $username
*/
protected function searchInPreferredUsername(IQueryBuilder &$qb, string $username) {
- $dbConn = $this->dbConnection;
+ $dbConn = $this->getConnection();
$this->searchInDBField(
$qb, 'preferred_username', $dbConn->escapeLikeParameter($username) . '%'
);
@@ -348,7 +353,7 @@ class CoreRequestBuilder {
* @param bool $all
*/
protected function searchInHashtag(IQueryBuilder &$qb, string $hashtag, bool $all = false) {
- $dbConn = $this->dbConnection;
+ $dbConn = $this->getConnection();
$this->searchInDBField(
$qb, 'hashtag', (($all) ? '%' : '') . $dbConn->escapeLikeParameter($hashtag) . '%'
);
@@ -419,7 +424,7 @@ class CoreRequestBuilder {
* @param string $account
*/
protected function searchInAccount(IQueryBuilder &$qb, string $account) {
- $dbConn = $this->dbConnection;
+ $dbConn = $this->getConnection();
$this->searchInDBField($qb, 'account', $dbConn->escapeLikeParameter($account) . '%');
}
@@ -1207,46 +1212,6 @@ class CoreRequestBuilder {
/**
- * @param IQueryBuilder $qb
- * @param callable $method
- *
- * @return IQueryRow
- * @throws RowNotFoundException
- */
- public function getRow(IQueryBuilder $qb, callable $method): IQueryRow {
- $cursor = $qb->execute();
- $data = $cursor->fetch();
- $cursor->closeCursor();
-
- if ($data === false) {
- throw new RowNotFoundException();
- }
-
- return $method($data);
- }
-
- /**
- * @param IQueryBuilder $qb
- * @param callable $method
- *
- * @return IQueryRow[]
- */
- public function getRows(IQueryBuilder $qb, callable $method): array {
- $rows = [];
- $cursor = $qb->execute();
- while ($data = $cursor->fetch()) {
- try {
- $rows[] = $method($data);
- } catch (Exception $e) {
- }
- }
- $cursor->closeCursor();
-
- return $rows;
- }
-
-
- /**
* @param Person $actor
* @param array $data
*/
diff --git a/lib/Db/StreamRequestBuilder.php b/lib/Db/StreamRequestBuilder.php
index 7e5d6803..e64b7205 100644
--- a/lib/Db/StreamRequestBuilder.php
+++ b/lib/Db/StreamRequestBuilder.php
@@ -31,12 +31,12 @@ namespace OCA\Social\Db;
use daita\MySmallPhpTools\Exceptions\CacheItemNotFoundException;
+use daita\MySmallPhpTools\Exceptions\RowNotFoundException;
use daita\MySmallPhpTools\Traits\TArrayTools;
use Doctrine\DBAL\Query\QueryBuilder;
use OCA\Social\AP;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\ItemUnknownException;
-use OCA\Social\Exceptions\RowNotFoundException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\StreamNotFoundException;
use OCA\Social\Model\ActivityPub\ACore;
@@ -62,11 +62,11 @@ class StreamRequestBuilder extends CoreRequestBuilder {
/**
* Base of the Sql Insert request
*
- * @return IQueryBuilder
+ * @return SocialQueryBuilder
*/
- protected function getStreamInsertSql(): IQueryBuilder {
- $qb = $this->dbConnection->getQueryBuilder();
- $qb->insert(self::TABLE_STREAM);
+ protected function getStreamInsertSql(): SocialQueryBuilder {
+ $qb = $this->getQueryBuilder();
+ $qb->insert(self::TABLE_STREAMS);
return $qb;
}
@@ -75,11 +75,11 @@ class StreamRequestBuilder extends CoreRequestBuilder {
/**
* Base of the Sql Update request
*
- * @return IQueryBuilder
+ * @return SocialQueryBuilder
*/
- protected function getStreamUpdateSql(): IQueryBuilder {
- $qb = $this->dbConnection->getQueryBuilder();
- $qb->update(self::TABLE_STREAM);
+ protected function getStreamUpdateSql(): SocialQueryBuilder {
+ $qb = $this->getQueryBuilder();
+ $qb->update(self::TABLE_STREAMS);
return $qb;
}
@@ -88,10 +88,10 @@ class StreamRequestBuilder extends CoreRequestBuilder {
/**
* Base of the Sql Select request for Shares
*
- * @return IQueryBuilder
+ * @return SocialQueryBuilder
*/
- protected function getStreamSelectSql(): IQueryBuilder {
- $qb = $this->dbConnection->getQueryBuilder();
+ protected function getStreamSelectSql(): SocialQueryBuilder {
+ $qb = $this->getQueryBuilder();
/** @noinspection PhpMethodParametersCountMismatchInspection */
$qb->selectDistinct('s.id')
@@ -112,10 +112,10 @@ class StreamRequestBuilder extends CoreRequestBuilder {
/**
* Base of the Sql Select request for Shares
*
- * @return IQueryBuilder
+ * @return SocialQueryBuilder
*/
- protected function countNotesSelectSql(): IQueryBuilder {
- $qb = $this->dbConnection->getQueryBuilder();
+ protected function countNotesSelectSql(): SocialQueryBuilder {
+ $qb = $this->getQueryBuilder();
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
->from(self::TABLE_STREAM, 's');
@@ -128,11 +128,11 @@ class StreamRequestBuilder extends CoreRequestBuilder {
/**
* Base of the Sql Delete request
*
- * @return IQueryBuilder
+ * @return SocialQueryBuilder
*/
- protected function getStreamDeleteSql(): IQueryBuilder {
- $qb = $this->dbConnection->getQueryBuilder();
- $qb->delete(self::TABLE_STREAM);
+ protected function getStreamDeleteSql(): SocialQueryBuilder {
+ $qb = $this->getQueryBuilder();
+ $qb->delete(self::TABLE_STREAMS);
return $qb;
}
@@ -323,9 +323,8 @@ class StreamRequestBuilder extends CoreRequestBuilder {
*
* @return string
*/
- protected function exprValueWithinJsonFormat(IQueryBuilder $qb, string $field, string $value
- ): string {
- $dbConn = $this->dbConnection;
+ protected function exprValueWithinJsonFormat(IQueryBuilder $qb, string $field, string $value): string {
+ $dbConn = $this->getConnection();
$expr = $qb->expr();
return $expr->iLike(
@@ -342,9 +341,8 @@ class StreamRequestBuilder extends CoreRequestBuilder {
*
* @return string
*/
- protected function exprValueNotWithinJsonFormat(IQueryBuilder $qb, string $field, string $value
- ): string {
- $dbConn = $this->dbConnection;
+ protected function exprValueNotWithinJsonFormat(IQueryBuilder $qb, string $field, string $value): string {
+ $dbConn = $this->getConnection();
$expr = $qb->expr();
$func = $qb->func();
@@ -450,15 +448,15 @@ class StreamRequestBuilder extends CoreRequestBuilder {
/**
- * @param IQueryBuilder $qb
+ * @param SocialQueryBuilder $qb
*
* @return Stream
* @throws StreamNotFoundException
*/
- protected function getStreamFromRequest(IQueryBuilder $qb): Stream {
+ protected function getStreamFromRequest(SocialQueryBuilder $qb): Stream {
/** @var Stream $result */
try {
- $result = $this->getRow($qb, [$this, 'parseStreamSelectSql']);
+ $result = $qb->getRow([$this, 'parseStreamSelectSql']);
} catch (RowNotFoundException $e) {
throw new StreamNotFoundException($e->getMessage());
}
@@ -468,13 +466,13 @@ class StreamRequestBuilder extends CoreRequestBuilder {
/**
- * @param IQueryBuilder $qb
+ * @param SocialQueryBuilder $qb
*
* @return Stream[]
*/
- public function getStreamsFromRequest(IQueryBuilder $qb): array {
+ public function getStreamsFromRequest(SocialQueryBuilder $qb): array {
/** @var Stream[] $result */
- $result = $this->getRows($qb, [$this, 'parseStreamSelectSql']);
+ $result = $qb->getRows([$this, 'parseStreamSelectSql']);
return $result;
}
@@ -488,7 +486,7 @@ class StreamRequestBuilder extends CoreRequestBuilder {
* @throws ItemUnknownException
* @throws SocialAppConfigException
*/
- protected function parseStreamSelectSql(array $data, string $as = Stream::TYPE): Stream {
+ public function parseStreamSelectSql(array $data, string $as = Stream::TYPE): Stream {
if ($as === Stream::TYPE) {
$as = $this->get('type', $data, Stream::TYPE);
}
diff --git a/lib/Exceptions/RowNotFoundException.php b/lib/Exceptions/RowNotFoundException.php
deleted file mode 100644
index 90195abd..00000000
--- a/lib/Exceptions/RowNotFoundException.php
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-
-/**
- * Nextcloud - Social Support
- *
- * This file is licensed under the Affero General Public License version 3 or
- * later. See the COPYING file.
- *
- * @author Maxence Lange <maxence@artificial-owl.com>
- * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
- * @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 OCA\Social\Exceptions;
-
-
-use Exception;
-
-
-class RowNotFoundException extends Exception {
-
-}
-