Quellcode durchsuchen

Db: Add wiping of pin state for subtrees

Christian Kamm vor 7 Jahren
Ursprung
Commit
aa382eda29
3 geänderte Dateien mit 38 neuen und 0 gelöschten Zeilen
  1. 14 0
      src/common/syncjournaldb.cpp
  2. 8 0
      src/common/syncjournaldb.h
  3. 16 0
      test/testsyncjournaldb.cpp

+ 14 - 0
src/common/syncjournaldb.cpp

@@ -2136,6 +2136,20 @@ void SyncJournalDb::setPinStateForPath(const QByteArray &path, PinState state)
     query.exec();
 }
 
+void SyncJournalDb::wipePinStateForPathAndBelow(const QByteArray &path)
+{
+    QMutexLocker lock(&_mutex);
+    if (!checkConnect())
+        return;
+
+    auto &query = _wipePinStateQuery;
+    ASSERT(query.initOrReset(QByteArrayLiteral(
+            "DELETE FROM flags WHERE " IS_PREFIX_PATH_OR_EQUAL("?1", "path") ";"),
+        _db));
+    query.bindValue(1, path);
+    query.exec();
+}
+
 void SyncJournalDb::commit(const QString &context, bool startTrans)
 {
     QMutexLocker lock(&_mutex);

+ 8 - 0
src/common/syncjournaldb.h

@@ -294,6 +294,13 @@ public:
      */
     void setPinStateForPath(const QByteArray &path, PinState state);
 
+    /**
+     * Wipes pin states for a path and below.
+     *
+     * Used when the user asks a subtree to have a particular pin state.
+     */
+    void wipePinStateForPathAndBelow(const QByteArray &path);
+
     /**
      * Only used for auto-test:
      * when positive, will decrease the counter for every database operation.
@@ -361,6 +368,7 @@ private:
     SqlQuery _getRawPinStateQuery;
     SqlQuery _getEffectivePinStateQuery;
     SqlQuery _setPinStateQuery;
+    SqlQuery _wipePinStateQuery;
 
     /* Storing etags to these folders, or their parent folders, is filtered out.
      *

+ 16 - 0
test/testsyncjournaldb.cpp

@@ -336,6 +336,14 @@ private slots:
             }
             return *state;
         };
+        auto getRaw = [&](const QByteArray &path) -> PinState {
+            auto state = _db.rawPinStateForPath(path);
+            if (!state) {
+                QTest::qFail("couldn't read pin state", __FILE__, __LINE__);
+                return PinState::Inherited;
+            }
+            return *state;
+        };
 
         // Make a thrice-nested setup
         make("local", PinState::AlwaysLocal);
@@ -394,6 +402,14 @@ private slots:
         QCOMPARE(get("online"), PinState::OnlineOnly);
         QCOMPARE(get("inherit"), PinState::AlwaysLocal);
         QCOMPARE(get("nonexistant"), PinState::AlwaysLocal);
+
+        // Wiping
+        QCOMPARE(getRaw("local/local"), PinState::AlwaysLocal);
+        _db.wipePinStateForPathAndBelow("local/local");
+        QCOMPARE(getRaw("local"), PinState::AlwaysLocal);
+        QCOMPARE(getRaw("local/local"), PinState::Inherited);
+        QCOMPARE(getRaw("local/local/local"), PinState::Inherited);
+        QCOMPARE(getRaw("local/local/online"), PinState::Inherited);
     }
 
 private: