Skip to content

Commit d2d11dd

Browse files
committed
refactor: Prefer using IFunctionBuilder than createFunction
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent dd8b274 commit d2d11dd

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

lib/private/Comments/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ public function getLastCommentDateByActor(
798798

799799
$query = $this->dbConn->getQueryBuilder();
800800
$query->select('actor_id')
801-
->selectAlias($query->createFunction('MAX(' . $query->getColumnName('creation_timestamp') . ')'), 'last_comment')
801+
->selectAlias($query->func()->max('creation_timestamp'), 'last_comment')
802802
->from('comments')
803803
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
804804
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))

lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCP\DB\QueryBuilder\IQueryBuilder;
1414
use OCP\DB\QueryBuilder\IQueryFunction;
1515
use OCP\IDBConnection;
16+
use Override;
1617

1718
class FunctionBuilder implements IFunctionBuilder {
1819
/** @var IDBConnection|Connection */
@@ -105,4 +106,9 @@ public function greatest($x, $y): IQueryFunction {
105106
public function least($x, $y): IQueryFunction {
106107
return new QueryFunction('LEAST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
107108
}
109+
110+
#[Override]
111+
public function now(): IQueryFunction {
112+
return new QueryFunction('NOW()');
113+
}
108114
}

lib/private/Files/Cache/CacheQueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function __construct(
2929
public function selectTagUsage(): self {
3030
$this
3131
->select('systemtag.name', 'systemtag.id', 'systemtag.visibility', 'systemtag.editable', 'systemtag.etag', 'systemtag.color')
32-
->selectAlias($this->createFunction('COUNT(filecache.fileid)'), 'number_files')
33-
->selectAlias($this->createFunction('MAX(filecache.fileid)'), 'ref_file_id')
32+
->selectAlias($this->func()->count('filecache.fileid'), 'number_files')
33+
->selectAlias($this->func()->max('filecache.fileid'), 'ref_file_id')
3434
->from('filecache', 'filecache')
3535
->leftJoin('filecache', 'systemtag_object_mapping', 'systemtagmap', $this->expr()->andX(
3636
$this->expr()->eq('filecache.fileid', $this->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)),

lib/private/FilesMetadata/Service/MetadataRequestService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function store(IFilesMetadata $filesMetadata): void {
6363
->setValue('file_id', $qb->createNamedParameter($filesMetadata->getFileId(), IQueryBuilder::PARAM_INT))
6464
->setValue('json', $qb->createNamedParameter(json_encode($filesMetadata->jsonSerialize())))
6565
->setValue('sync_token', $qb->createNamedParameter($this->generateSyncToken()))
66-
->setValue('last_update', (string)$qb->createFunction('NOW()'));
66+
->setValue('last_update', $qb->func()->now());
6767
$qb->executeStatement();
6868
}
6969

@@ -159,7 +159,7 @@ public function updateMetadata(IFilesMetadata $filesMetadata): int {
159159
->hintShardKey('files_metadata', $this->getStorageId($filesMetadata))
160160
->set('json', $qb->createNamedParameter(json_encode($filesMetadata->jsonSerialize())))
161161
->set('sync_token', $qb->createNamedParameter($this->generateSyncToken()))
162-
->set('last_update', $qb->createFunction('NOW()'))
162+
->set('last_update', $qb->func()->now())
163163
->where(
164164
$expr->andX(
165165
$expr->eq('file_id', $qb->createNamedParameter($filesMetadata->getFileId(), IQueryBuilder::PARAM_INT)),

lib/public/DB/QueryBuilder/IFunctionBuilder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,10 @@ public function greatest($x, $y): IQueryFunction;
170170
* @since 18.0.0
171171
*/
172172
public function least($x, $y): IQueryFunction;
173+
174+
/**
175+
* Get the current date and time as a UNIX timestamp.
176+
* @since 34.0.0
177+
*/
178+
public function now(): IQueryFunction;
173179
}

lib/public/DB/QueryBuilder/IQueryBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,10 @@ public function createPositionalParameter($value, $type = self::PARAM_STR);
10011001
public function createParameter($name);
10021002

10031003
/**
1004-
* Creates a new function
1004+
* Creates a new function.
10051005
*
1006-
* Attention: Column names inside the call have to be quoted before hand
1006+
* @warning Column names inside the call have to be quoted beforehand. In most
1007+
* case you can use the IFunctionBuilder instead.
10071008
*
10081009
* Example:
10091010
* <code>

0 commit comments

Comments
 (0)