30
30
use OCA \Files_External \Lib \InsufficientDataForMeaningfulAnswerException ;
31
31
use OCA \Files_External \Lib \StorageConfig ;
32
32
use OCA \Files_External \Service \GlobalStoragesService ;
33
+ use OCP \DB \QueryBuilder \IQueryBuilder ;
33
34
use OCP \Files \Notify \IChange ;
34
35
use OCP \Files \Notify \INotifyHandler ;
35
36
use OCP \Files \Notify \IRenameChange ;
@@ -48,8 +49,6 @@ class Notify extends Base {
48
49
private $ globalService ;
49
50
/** @var IDBConnection */
50
51
private $ connection ;
51
- /** @var \OCP\DB\QueryBuilder\IQueryBuilder */
52
- private $ updateQuery ;
53
52
/** @var ILogger */
54
53
private $ logger ;
55
54
@@ -58,7 +57,6 @@ function __construct(GlobalStoragesService $globalService, IDBConnection $connec
58
57
$ this ->globalService = $ globalService ;
59
58
$ this ->connection = $ connection ;
60
59
$ this ->logger = $ logger ;
61
- $ this ->updateQuery = $ this ->getUpdateQuery ($ this ->connection );
62
60
}
63
61
64
62
protected function configure () {
@@ -162,13 +160,23 @@ private function markParentAsOutdated($mountId, $path, OutputInterface $output)
162
160
}
163
161
164
162
try {
165
- $ this ->updateQuery -> execute ([ $ parent , $ mountId] );
163
+ $ storageIds = $ this ->getStorageIds ( $ mountId );
166
164
} catch (DriverException $ ex ) {
167
- $ this ->logger ->logException ($ ex , ['app ' => 'files_external ' , 'message ' => 'Error while trying to mark folder as outdated ' , 'level ' => ILogger::WARN ]);
165
+ $ this ->logger ->logException ($ ex , ['app ' => 'files_external ' , 'message ' => 'Error while trying to find correct storage ids. ' , 'level ' => ILogger::WARN ]);
168
166
$ this ->connection = $ this ->reconnectToDatabase ($ this ->connection , $ output );
169
167
$ output ->writeln ('<info>Needed to reconnect to the database</info> ' );
170
- $ this ->updateQuery = $ this ->getUpdateQuery ($ this ->connection );
171
- $ this ->updateQuery ->execute ([$ parent , $ mountId ]);
168
+ $ storageIds = $ this ->getStorageIds ($ mountId );
169
+ }
170
+
171
+ if (!$ storageIds || count ($ storageIds ) === 0 ) {
172
+ throw new StorageNotAvailableException ('No storages found by mount ID ' . $ mountId );
173
+ }
174
+ $ storageIds = array_map ('intval ' , $ storageIds );
175
+
176
+ $ result = $ this ->updateParent ($ storageIds , $ parent );
177
+ if ($ result === 0 ) {
178
+ //TODO: Find existing parent further up the tree in the database and register that folder instead.
179
+ $ this ->logger ->info ('Failed updating parent for " ' . $ path . '" while trying to register change. It may not exist in the filecache. ' , ['app ' => 'files_external ' ]);
172
180
}
173
181
}
174
182
@@ -198,16 +206,24 @@ private function logUpdate(IChange $change, OutputInterface $output) {
198
206
$ output ->writeln ($ text );
199
207
}
200
208
201
- /**
202
- * @return \Doctrine\DBAL\Statement
203
- */
204
- private function getUpdateQuery (IDBConnection $ connection ) {
205
- // the query builder doesn't really like subqueries with parameters
206
- return $ connection ->prepare (
207
- 'UPDATE *PREFIX*filecache SET size = -1
208
- WHERE `path` = ?
209
- AND `storage` IN (SELECT storage_id FROM *PREFIX*mounts WHERE mount_id = ?) '
210
- );
209
+ private function getStorageIds ($ mountId ) {
210
+ $ qb = $ this ->connection ->getQueryBuilder ();
211
+ return $ qb
212
+ ->select ('storage_id ' )
213
+ ->from ('mounts ' )
214
+ ->where ($ qb ->expr ()->eq ('mount_id ' , $ qb ->createNamedParameter ($ mountId , IQueryBuilder::PARAM_INT )))
215
+ ->execute ()->fetchAll (\PDO ::FETCH_COLUMN );
216
+ }
217
+
218
+ private function updateParent ($ storageIds , $ parent ) {
219
+ $ pathHash = md5 (trim (\OC_Util::normalizeUnicode ($ parent ), '/ ' ));
220
+ $ qb = $ this ->connection ->getQueryBuilder ();
221
+ $ qb
222
+ ->update ('filecache ' )
223
+ ->set ('size ' , $ qb ->createNamedParameter (-1 , IQueryBuilder::PARAM_INT ))
224
+ ->where ($ qb ->expr ()->in ('storage ' , $ qb ->createNamedParameter ($ storageIds , IQueryBuilder::PARAM_INT_ARRAY , ':storage_ids ' )))
225
+ ->andWhere ($ qb ->expr ()->eq ('path_hash ' , $ qb ->createNamedParameter ($ pathHash , IQueryBuilder::PARAM_STR )));
226
+ return $ qb ->execute ();
211
227
}
212
228
213
229
/**
0 commit comments