Skip to content

Commit

Permalink
Use explicit cast to make use of index
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Apr 12, 2022
1 parent 541eeb0 commit c04e5c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/Sharing/DeckShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OCA\Deck\Sharing;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use OC\Files\Cache\Cache;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board;
Expand Down Expand Up @@ -710,6 +711,13 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset): arra
}

$qb = $this->dbConnection->getQueryBuilder();
// Avoid using implicit cast in order to make use of the index in the join on MySQL/MariaDB
// FIXME: Once >= Nextcloud 24 this can be dropped due to https://github.com/nextcloud/server/pull/30471
if ($this->dbConnection->getDatabasePlatform() instanceof MySQLPlatform) {
$cardIdExpression = $qb->createFunction('CAST(dc.id as CHAR)');
} else {
$cardIdExpression = $qb->expr()->castColumn('dc.id', IQueryBuilder::PARAM_STR);
}
$qb->select('s.*',
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
Expand All @@ -720,7 +728,7 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset): arra
->orderBy('s.id')
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
->leftJoin('s', 'deck_cards', 'dc', $qb->expr()->eq($qb->expr()->castColumn('dc.id', IQueryBuilder::PARAM_STR), 's.share_with'))
->leftJoin('s', 'deck_cards', 'dc', $qb->expr()->eq($cardIdExpression, 's.share_with'))
->leftJoin('dc', 'deck_stacks', 'ds', $qb->expr()->eq('dc.stack_id', 'ds.id'))
->leftJoin('ds', 'deck_boards', 'db', $qb->expr()->eq('ds.board_id', 'db.id'));

Expand Down Expand Up @@ -781,6 +789,13 @@ public function getSharedWithByType(int $cardId, int $shareType, $limit, $offset
$shares = [];

$qb = $this->dbConnection->getQueryBuilder();
// Avoid using implicit cast in order to make use of the index in the join on MySQL/MariaDB
// FIXME: Once >= Nextcloud 24 this can be dropped due to https://github.com/nextcloud/server/pull/30471
if ($this->dbConnection->getDatabasePlatform() instanceof MySQLPlatform) {
$cardIdExpression = $qb->createFunction('CAST(dc.id as CHAR)');
} else {
$cardIdExpression = $qb->expr()->castColumn('dc.id', IQueryBuilder::PARAM_STR);
}
$qb->select('s.*',
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
Expand All @@ -791,7 +806,7 @@ public function getSharedWithByType(int $cardId, int $shareType, $limit, $offset
->orderBy('s.id')
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
->leftJoin('s', 'deck_cards', 'dc', $qb->expr()->eq($qb->expr()->castColumn('dc.id', IQueryBuilder::PARAM_STR), 's.share_with'));
->leftJoin('s', 'deck_cards', 'dc', $qb->expr()->eq($cardIdExpression, 's.share_with'));

if ($limit !== -1) {
$qb->setMaxResults($limit);
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<referencedClass name="OC\*" />
<referencedClass name="OC" />
<referencedClass name="OC\Security\CSP\ContentSecurityPolicyNonceManager" />
<referencedClass name="Doctrine\DBAL\Platforms\MySQLPlatform" />
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass>
Expand All @@ -48,6 +49,8 @@
<referencedClass name="Doctrine\DBAL\Schema\SchemaException" />
<referencedClass name="Doctrine\DBAL\Driver\Statement" />
<referencedClass name="Doctrine\DBAL\Schema\Table" />
<referencedClass name="Doctrine\DBAL\Platforms\AbstractPlatform" />
<referencedClass name="Doctrine\DBAL\Platforms\MySQLPlatform" />
<referencedClass name="OC\Security\CSP\ContentSecurityPolicyNonceManager" />
</errorLevel>
</UndefinedDocblockClass>
Expand Down

0 comments on commit c04e5c1

Please sign in to comment.