Skip to content

Commit

Permalink
reafactor: use query builder functions
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <radialapps@gmail.com>
  • Loading branch information
pulsejet committed Apr 3, 2024
1 parent ef75771 commit a6df4fe
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 28 deletions.
11 changes: 6 additions & 5 deletions lib/ClustersBackend/FaceRecognitionBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private function getFaceRecognitionClusters(int $fileid = 0): array
$query = $this->tq->getBuilder();

// SELECT all face clusters
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'));
$count = $query->func()->count(SQL::distinct($query, 'm.fileid'));
$query->select('frp.id')->from('facerecog_persons', 'frp');
$query->selectAlias($count, 'count');
$query->selectAlias('frp.user', 'user_id');
Expand Down Expand Up @@ -314,10 +314,11 @@ private function getFaceRecognitionPersons(int $fileid = 0): array

// SELECT all face clusters
$query->select('frp.name')
->from('facerecog_persons', 'frp');
$query->selectAlias($query->func()->count($query->createFunction('DISTINCT m.fileid')), 'count');
$query->selectAlias($query->func()->min('frp.id'), 'id');
$query->selectAlias('frp.user', 'user_id');
->selectAlias($query->func()->count(SQL::distinct($query, 'm.fileid')), 'count')
->selectAlias($query->func()->min('frp.id'), 'id')
->selectAlias('frp.user', 'user_id')
->from('facerecog_persons', 'frp')
;

// WHERE there are faces with this cluster
$query->innerJoin('frp', 'facerecog_faces', 'frf', $query->expr()->eq('frp.id', 'frf.person'));
Expand Down
2 changes: 1 addition & 1 deletion lib/ClustersBackend/PlacesBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getClustersInternal(int $fileid = 0): array
$query = $this->tq->getBuilder();

// SELECT location name and count of photos
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
$count = $query->func()->count(SQL::distinct($query, 'm.fileid'), 'count');
$query->select('e.osm_id', $count)->from('memories_planet', 'e');

// WHERE these are not special clusters (e.g. timezone)
Expand Down
2 changes: 1 addition & 1 deletion lib/ClustersBackend/RecognizeBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getClustersInternal(int $fileid = 0): array
$query = $this->tq->getBuilder();

// SELECT all face clusters
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
$count = $query->func()->count(SQL::distinct($query, 'm.fileid'), 'count');
$query->select('rfc.id', 'rfc.user_id', 'rfc.title', $count)->from('recognize_face_clusters', 'rfc');

// WHERE there are faces with this cluster
Expand Down
4 changes: 2 additions & 2 deletions lib/ClustersBackend/TagsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getClustersInternal(int $fileid = 0): array
$query = $this->tq->getBuilder();

// SELECT visible tag name and count of photos
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
$count = $query->func()->count(SQL::distinct($query, 'm.fileid'), 'count');
$query->select('st.id', 'st.name', $count)
->from('systemtag', 'st')
->where($query->expr()->eq('st.visibility', $query->expr()->literal(1, \PDO::PARAM_INT)))
Expand All @@ -93,7 +93,7 @@ public function getClustersInternal(int $fileid = 0): array

// GROUP and ORDER by tag name
$query->addGroupBy('st.id');
$query->orderBy($query->createFunction('LOWER(st.name)'), 'ASC');
$query->orderBy($query->func()->lower('st.name'), 'ASC');
$query->addOrderBy('st.id'); // tie-breaker

// SELECT cover photo
Expand Down
8 changes: 4 additions & 4 deletions lib/Db/AlbumsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public function getList(
$query = $this->connection->getQueryBuilder();

// SELECT everything from albums
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
$maxPafId = $query->createFunction('MAX(paf.album_file_id) AS update_id');
$query->select(
'pa.album_id',
'pa.name',
'pa.user',
'pa.created',
'pa.location',
'pa.last_added_photo',
$maxPafId,
$count,
)->from('photos_albums', 'pa');

$query->selectAlias($query->func()->count(SQL::distinct($query, 'm.fileid')), 'count')
->selectAlias($query->func()->max('paf.album_file_id'), 'update_id')
;

if ($shared) {
$ids = $this->getSelfCollaborators($uid);
$query->innerJoin('pa', $this->collaboratorsTable(), 'pc', $query->expr()->andX(
Expand Down
22 changes: 22 additions & 0 deletions lib/Db/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,26 @@ public static function notExists(IQueryBuilder &$query, IQueryBuilder|string &$c

return $query->createFunction("NOT EXISTS ({$clause})");
}

/**
* Create a DISTINCT expression.
*
* @param IQueryBuilder $query The query to create the function on
* @param string $field The field to select distinct values from
*/
public static function distinct(IQueryBuilder &$query, string $field): IQueryFunction
{
return $query->createFunction("DISTINCT {$field}");
}

/**
* Create a AVG expression.
*
* @param IQueryBuilder $query The query to create the function on
* @param string $field The field to average
*/
public static function average(IQueryBuilder &$query, string $field): IQueryFunction
{
return $query->createFunction("AVG({$field})");
}
}
9 changes: 3 additions & 6 deletions lib/Db/TimelineQueryDays.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function getDays(
$query = $this->connection->getQueryBuilder();

// Get all entries also present in filecache
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
$query->select('m.dayid', $count)
$query->select('m.dayid')
->selectAlias($query->func()->count(SQL::distinct($query, 'm.fileid')), 'count')
->from('memories', 'm')
;

Expand Down Expand Up @@ -97,13 +97,10 @@ public function getDay(
// Make new query
$query = $this->connection->getQueryBuilder();

// Get all entries also present in filecache
$fileid = $query->createFunction('DISTINCT m.fileid');

// We don't actually use m.datetaken here, but postgres
// needs that all fields in ORDER BY are also in SELECT
// when using DISTINCT on selected fields
$query->select($fileid, ...TimelineQuery::TIMELINE_SELECT)
$query->select(SQL::distinct($query, 'm.fileid'), ...TimelineQuery::TIMELINE_SELECT)
->from('memories', 'm')
;

Expand Down
10 changes: 5 additions & 5 deletions lib/Db/TimelineQueryMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public function getMapClusters(float $gridLen, string $bounds): array
$query = $this->connection->getQueryBuilder();

// Get the average location of each cluster
$query->selectAlias($query->createFunction('MAX(c.id)'), 'id')
->selectAlias($query->createFunction('COUNT(m.fileid)'), 'count')
->selectAlias($query->createFunction('AVG(c.lat)'), 'lat')
->selectAlias($query->createFunction('AVG(c.lon)'), 'lon')
$query->selectAlias($query->func()->max('c.id'), 'id')
->selectAlias($query->func()->count('m.fileid'), 'count')
->selectAlias(SQL::average($query, 'c.lat'), 'lat')
->selectAlias(SQL::average($query, 'c.lon'), 'lon')
->from('memories_mapclusters', 'c')
;

Expand Down Expand Up @@ -84,7 +84,7 @@ public function getMapClusterPreviews(array $clusterIds): array
$query = $this->connection->getQueryBuilder();

// SELECT all photos with this tag
$query->selectAlias($query->createFunction('MAX(m.fileid)'), 'fileid')
$query->selectAlias($query->func()->max('m.fileid'), 'fileid')
->from('memories', 'm')
->where($query->expr()->in('m.mapcluster', $query->createNamedParameter(
$clusterIds,
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/TimelineWriteFailures.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function clearFailures(File $file): void
public function countFailures(): int
{
$query = $this->connection->getQueryBuilder();
$query->select($query->createFunction('COUNT(fileid)'))
$query->select($query->func()->count('fileid'))
->from('memories_failures')
;

Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/Version505000Date20230821044807.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array
// get count of rows to update
$query = $this->dbc->getQueryBuilder();
$maxCount = $query
->select($query->createFunction('COUNT(m.fileid)'))
->select($query->func()->count('m.fileid'))
->from('memories', 'm')
->executeQuery()
->fetchOne()
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\Memories\Service;

use OCA\Memories\AppInfo\Application;
use OCA\Memories\Db\SQL;
use OCA\Memories\Db\TimelineWrite;
use OCA\Memories\Settings\SystemConfig;
use OCA\Memories\Util;
Expand Down Expand Up @@ -231,7 +232,7 @@ public function cleanupStale(): void
public function getIndexedCount(): int
{
$query = $this->db->getQueryBuilder();
$query->select($query->createFunction('COUNT(DISTINCT fileid)'))
$query->select($query->func()->count(SQL::distinct($query, 'fileid')))
->from('memories')
;

Expand Down
3 changes: 2 additions & 1 deletion lib/Service/Places.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OCA\Memories\Service;

use OCA\Memories\Db\SQL;
use OCA\Memories\Db\TimelineWrite;
use OCA\Memories\Settings\SystemConfig;
use OCP\IConfig;
Expand Down Expand Up @@ -110,7 +111,7 @@ public function queryPoint(float $lat, float $lon): array

// Make query to memories_planet table
$query = $this->connection->getQueryBuilder();
$query->select($query->createFunction('DISTINCT(osm_id)'))
$query->select(SQL::distinct($query, 'osm_id'))
->from('memories_planet_geometry')
->where($query->createFunction($where))
;
Expand Down

0 comments on commit a6df4fe

Please sign in to comment.