Replace some usages of OC_DB in OC\Share\* with query builder

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-11-06 13:02:14 +01:00 committed by backportbot[bot]
parent f8b550dab7
commit 73a09a4f54
2 changed files with 39 additions and 29 deletions

View File

@ -31,6 +31,7 @@
namespace OC\Share; namespace OC\Share;
use OC\HintException; use OC\HintException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Share\IShare; use OCP\Share\IShare;
class Helper extends \OC\Share\Constants { class Helper extends \OC\Share\Constants {
@ -89,31 +90,30 @@ class Helper extends \OC\Share\Constants {
$changeParent = []; $changeParent = [];
$parents = [$parent]; $parents = [$parent];
while (!empty($parents)) { while (!empty($parents)) {
$parents = "'".implode("','", $parents)."'"; $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
// Check the owner on the first search of reshares, useful for $query->select(
// finding and deleting the reshares by a single user of a group share 'id', 'share_with', 'item_type', 'share_type',
$params = []; 'item_target', 'file_target', 'parent'
if (count($ids) == 1 && isset($uidOwner)) { )
// FIXME: don't concat $parents, use Docrine's PARAM_INT_ARRAY approach ->from('share')
$queryString = 'SELECT `id`, `share_with`, `item_type`, `share_type`, ' . ->where($query->expr()->in('parent', $query->createNamedParameter(
'`item_target`, `file_target`, `parent` ' . $parents, IQueryBuilder::PARAM_INT_ARRAY
'FROM `*PREFIX*share` ' . )));
'WHERE `parent` IN ('.$parents.') AND `uid_owner` = ? ';
$params[] = $uidOwner; if (count($ids) === 1 && isset($uidOwner)) {
} else { // Check the owner on the first search of reshares, useful for
$queryString = 'SELECT `id`, `share_with`, `item_type`, `share_type`, ' . // finding and deleting the reshares by a single user of a group share
'`item_target`, `file_target`, `parent`, `uid_owner` ' . $query->andWhere($query->expr()->eq('uid_owner', $uidOwner));
'FROM `*PREFIX*share` WHERE `parent` IN ('.$parents.') ';
} }
if ($excludeGroupChildren) { if ($excludeGroupChildren) {
$queryString .= ' AND `share_type` != ?'; $query->andWhere($query->expr()->eq('share_type', self::$shareTypeGroupUserUnique));
$params[] = self::$shareTypeGroupUserUnique;
} }
$query = \OC_DB::prepare($queryString);
$result = $query->execute($params); $result = $query->execute();
// Reset parents array, only go through loop again if items are found // Reset parents array, only go through loop again if items are found
$parents = []; $parents = [];
while ($item = $result->fetchRow()) { while ($item = $result->fetch()) {
$tmpItem = [ $tmpItem = [
'id' => $item['id'], 'id' => $item['id'],
'shareWith' => $item['share_with'], 'shareWith' => $item['share_with'],
@ -135,20 +135,24 @@ class Helper extends \OC\Share\Constants {
$parents[] = $item['id']; $parents[] = $item['id'];
} }
} }
$result->closeCursor();
} }
if ($excludeParent) { if ($excludeParent) {
unset($ids[0]); unset($ids[0]);
} }
if (!empty($changeParent)) { if (!empty($changeParent)) {
$idList = "'".implode("','", $changeParent)."'"; $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `parent` = ? WHERE `id` IN ('.$idList.')'); $query->update('share')
$query->execute([$newParent]); ->set('parent', $query->createNamedParameter($newParent, IQueryBuilder::PARAM_INT))
->where($query->expr()->in('id', $query->createNamedParameter($changeParent, IQueryBuilder::PARAM_INT_ARRAY)));
$query->execute();
} }
if (!empty($ids)) { if (!empty($ids)) {
$idList = "'".implode("','", $ids)."'"; $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `id` IN ('.$idList.')'); $query->delete('share')
->where($query->expr()->in('id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
$query->execute(); $query->execute();
} }

View File

@ -687,14 +687,20 @@ class Share extends Constants {
// Remove root from file source paths if retrieving own shared items // Remove root from file source paths if retrieving own shared items
if (isset($uidOwner) && isset($row['path'])) { if (isset($uidOwner) && isset($row['path'])) {
if (isset($row['parent'])) { if (isset($row['parent'])) {
$query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$parentResult = $query->execute([$row['parent']]); $query->select('file_target')
if ($result === false) { ->from('share')
->where($query->expr()->eq('id', $query->createNamedParameter($row['parent'])));
$result = $query->execute();
$parentRow = $result->fetch();
$result->closeCursor();
if ($parentRow === false) {
\OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' .
\OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where,
ILogger::ERROR); ILogger::ERROR);
} else { } else {
$parentRow = $parentResult->fetchRow();
$tmpPath = $parentRow['file_target']; $tmpPath = $parentRow['file_target'];
// find the right position where the row path continues from the target path // find the right position where the row path continues from the target path
$pos = strrpos($row['path'], $parentRow['file_target']); $pos = strrpos($row['path'], $parentRow['file_target']);