move from OC_DB to IDBConnection
This commit is contained in:
parent
8bffc84766
commit
236c3c62cc
|
@ -35,6 +35,7 @@
|
|||
namespace OC\Files\Cache;
|
||||
|
||||
use \OCP\Files\IMimeTypeLoader;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
/**
|
||||
* Metadata cache for a storage
|
||||
|
@ -70,6 +71,11 @@ class Cache {
|
|||
/** @var IMimeTypeLoader */
|
||||
protected $mimetypeLoader;
|
||||
|
||||
/**
|
||||
* @var IDBConnection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @param \OC\Files\Storage\Storage|string $storage
|
||||
*/
|
||||
|
@ -85,6 +91,7 @@ class Cache {
|
|||
|
||||
$this->storageCache = new Storage($storage);
|
||||
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
|
||||
$this->connection = \OC::$server->getDatabaseConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,8 +143,8 @@ class Cache {
|
|||
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
|
||||
`storage_mtime`, `encrypted`, `etag`, `permissions`
|
||||
FROM `*PREFIX*filecache` ' . $where;
|
||||
$result = \OC_DB::executeAudited($sql, $params);
|
||||
$data = $result->fetchRow();
|
||||
$result = $this->connection->executeQuery($sql, $params);
|
||||
$data = $result->fetch();
|
||||
|
||||
//FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
|
||||
//PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false
|
||||
|
@ -191,7 +198,7 @@ class Cache {
|
|||
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
|
||||
`storage_mtime`, `encrypted`, `etag`, `permissions`
|
||||
FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC';
|
||||
$result = \OC_DB::executeAudited($sql, array($fileId));
|
||||
$result = $this->connection->executeQuery($sql, [$fileId]);
|
||||
$files = $result->fetchAll();
|
||||
foreach ($files as &$file) {
|
||||
$file['mimetype'] = $this->mimetypeLoader->getMimetypeById($file['mimetype']);
|
||||
|
@ -255,8 +262,9 @@ class Cache {
|
|||
if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
|
||||
'storage',
|
||||
'path_hash',
|
||||
])) {
|
||||
return (int)\OC_DB::insertid('*PREFIX*filecache');
|
||||
])
|
||||
) {
|
||||
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
|
||||
}
|
||||
|
||||
// The file was created in the mean time
|
||||
|
@ -297,7 +305,7 @@ class Cache {
|
|||
// some databases (Postgres) don't like superfluous updates
|
||||
$sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' .
|
||||
'WHERE (' . implode(' <> ? OR ', $queryParts) . ' <> ? ) AND `fileid` = ? ';
|
||||
\OC_DB::executeAudited($sql, $params);
|
||||
$this->connection->executeQuery($sql, $params);
|
||||
|
||||
}
|
||||
|
||||
|
@ -365,8 +373,8 @@ class Cache {
|
|||
$pathHash = md5($file);
|
||||
|
||||
$sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
|
||||
$result = \OC_DB::executeAudited($sql, array($this->getNumericStorageId(), $pathHash));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
|
||||
if ($row = $result->fetch()) {
|
||||
return $row['fileid'];
|
||||
} else {
|
||||
return -1;
|
||||
|
@ -411,7 +419,7 @@ class Cache {
|
|||
public function remove($file) {
|
||||
$entry = $this->get($file);
|
||||
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
|
||||
\OC_DB::executeAudited($sql, array($entry['fileid']));
|
||||
$this->connection->executeQuery($sql, array($entry['fileid']));
|
||||
if ($entry['mimetype'] === 'httpd/unix-directory') {
|
||||
$this->removeChildren($entry);
|
||||
}
|
||||
|
@ -442,7 +450,7 @@ class Cache {
|
|||
$this->removeChildren($folder);
|
||||
}
|
||||
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?';
|
||||
\OC_DB::executeAudited($sql, array($entry['fileid']));
|
||||
$this->connection->executeQuery($sql, array($entry['fileid']));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -491,20 +499,20 @@ class Cache {
|
|||
if ($sourceData['mimetype'] === 'httpd/unix-directory') {
|
||||
//find all child entries
|
||||
$sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?';
|
||||
$result = \OC_DB::executeAudited($sql, [$sourceStorageId, $sourcePath . '/%']);
|
||||
$result = $this->connection->executeQuery($sql, [$sourceStorageId, $sourcePath . '/%']);
|
||||
$childEntries = $result->fetchAll();
|
||||
$sourceLength = strlen($sourcePath);
|
||||
\OC_DB::beginTransaction();
|
||||
$query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
|
||||
$this->connection->beginTransaction();
|
||||
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
|
||||
|
||||
foreach ($childEntries as $child) {
|
||||
$newTargetPath = $targetPath . substr($child['path'], $sourceLength);
|
||||
\OC_DB::executeAudited($query, [$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]);
|
||||
$query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]);
|
||||
}
|
||||
\OC_DB::executeAudited($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
|
||||
\OC_DB::commit();
|
||||
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
|
||||
$this->connection->commit();
|
||||
} else {
|
||||
\OC_DB::executeAudited($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
|
||||
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -514,10 +522,10 @@ class Cache {
|
|||
*/
|
||||
public function clear() {
|
||||
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
|
||||
\OC_DB::executeAudited($sql, array($this->getNumericStorageId()));
|
||||
$this->connection->executeQuery($sql, array($this->getNumericStorageId()));
|
||||
|
||||
$sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
|
||||
\OC_DB::executeAudited($sql, array($this->storageId));
|
||||
$this->connection->executeQuery($sql, array($this->storageId));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -538,8 +546,8 @@ class Cache {
|
|||
|
||||
$pathHash = md5($file);
|
||||
$sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
|
||||
$result = \OC_DB::executeAudited($sql, array($this->getNumericStorageId(), $pathHash));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
|
||||
if ($row = $result->fetch()) {
|
||||
if ((int)$row['size'] === -1) {
|
||||
return self::SHALLOW;
|
||||
} else {
|
||||
|
@ -571,12 +579,12 @@ class Cache {
|
|||
`etag`, `permissions`
|
||||
FROM `*PREFIX*filecache`
|
||||
WHERE `storage` = ? AND `name` ILIKE ?';
|
||||
$result = \OC_DB::executeAudited($sql,
|
||||
array($this->getNumericStorageId(), $pattern)
|
||||
$result = $this->connection->executeQuery($sql,
|
||||
[$this->getNumericStorageId(), $pattern]
|
||||
);
|
||||
|
||||
$files = array();
|
||||
while ($row = $result->fetchRow()) {
|
||||
$files = [];
|
||||
while ($row = $result->fetch()) {
|
||||
$row['mimetype'] = $this->mimetypeLoader->getMimetypeById($row['mimetype']);
|
||||
$row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
|
||||
$files[] = $row;
|
||||
|
@ -600,9 +608,9 @@ class Cache {
|
|||
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag`, `permissions`
|
||||
FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?';
|
||||
$mimetype = $this->mimetypeLoader->getId($mimetype);
|
||||
$result = \OC_DB::executeAudited($sql, array($mimetype, $this->getNumericStorageId()));
|
||||
$result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId()));
|
||||
$files = array();
|
||||
while ($row = $result->fetchRow()) {
|
||||
while ($row = $result->fetch()) {
|
||||
$row['mimetype'] = $this->mimetypeLoader->getMimetypeById($row['mimetype']);
|
||||
$row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
|
||||
$files[] = $row;
|
||||
|
@ -640,16 +648,16 @@ class Cache {
|
|||
} else {
|
||||
$sql .= 'AND `tag`.`category` = ? ';
|
||||
}
|
||||
$result = \OC_DB::executeAudited(
|
||||
$result = $this->connection->executeQuery(
|
||||
$sql,
|
||||
array(
|
||||
[
|
||||
$this->getNumericStorageId(),
|
||||
$userId,
|
||||
$tag
|
||||
)
|
||||
]
|
||||
);
|
||||
$files = array();
|
||||
while ($row = $result->fetchRow()) {
|
||||
while ($row = $result->fetch()) {
|
||||
$files[] = $row;
|
||||
}
|
||||
return $files;
|
||||
|
@ -689,8 +697,8 @@ class Cache {
|
|||
$sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' .
|
||||
'FROM `*PREFIX*filecache` ' .
|
||||
'WHERE `parent` = ? AND `storage` = ?';
|
||||
$result = \OC_DB::executeAudited($sql, array($id, $this->getNumericStorageId()));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
|
||||
if ($row = $result->fetch()) {
|
||||
$result->closeCursor();
|
||||
list($sum, $min) = array_values($row);
|
||||
$sum = 0 + $sum;
|
||||
|
@ -721,9 +729,9 @@ class Cache {
|
|||
*/
|
||||
public function getAll() {
|
||||
$sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?';
|
||||
$result = \OC_DB::executeAudited($sql, array($this->getNumericStorageId()));
|
||||
$result = $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
|
||||
$ids = array();
|
||||
while ($row = $result->fetchRow()) {
|
||||
while ($row = $result->fetch()) {
|
||||
$ids[] = $row['fileid'];
|
||||
}
|
||||
return $ids;
|
||||
|
@ -739,10 +747,10 @@ class Cache {
|
|||
* @return string|bool the path of the folder or false when no folder matched
|
||||
*/
|
||||
public function getIncomplete() {
|
||||
$query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache`'
|
||||
$query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`'
|
||||
. ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1);
|
||||
$result = \OC_DB::executeAudited($query, array($this->getNumericStorageId()));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$query->execute([$this->getNumericStorageId()]);
|
||||
if ($row = $query->fetch()) {
|
||||
return $row['path'];
|
||||
} else {
|
||||
return false;
|
||||
|
@ -757,8 +765,8 @@ class Cache {
|
|||
*/
|
||||
public function getPathById($id) {
|
||||
$sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?';
|
||||
$result = \OC_DB::executeAudited($sql, array($id, $this->getNumericStorageId()));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
|
||||
if ($row = $result->fetch()) {
|
||||
// Oracle stores empty strings as null...
|
||||
if ($row['path'] === null) {
|
||||
return '';
|
||||
|
@ -779,9 +787,10 @@ class Cache {
|
|||
* @return array first element holding the storage id, second the path
|
||||
*/
|
||||
static public function getById($id) {
|
||||
$connection = \OC::$server->getDatabaseConnection();
|
||||
$sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
|
||||
$result = \OC_DB::executeAudited($sql, array($id));
|
||||
if ($row = $result->fetchRow()) {
|
||||
$result = $connection->executeQuery($sql, array($id));
|
||||
if ($row = $result->fetch()) {
|
||||
$numericId = $row['storage'];
|
||||
$path = $row['path'];
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue