Logging: Print enum before cast in SqlQuer::bindValue
authorHannah von Reth <hannah.vonreth@owncloud.com>
Fri, 17 Jul 2020 12:01:48 +0000 (14:01 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:59:15 +0000 (10:59 +0100)
src/common/ownsql.cpp
src/common/ownsql.h
src/common/syncjournaldb.cpp

index 3411f38b083891064d4c622b1ac6188267247319..4c0165eee3d0836d168954c8cf52fbe743c82c18 100644 (file)
@@ -361,10 +361,8 @@ auto SqlQuery::next() -> NextResult
     return result;
 }
 
-void SqlQuery::bindValue(int pos, const QVariant &value)
+void SqlQuery::bindValueInternal(int pos, const QVariant &value)
 {
-    qCDebug(lcSql) << "SQL bind" << pos << value;
-
     int res = -1;
     if (!_stmt) {
         ASSERT(false);
index 7e0b5f058ee2f43cd5128291eeadc58beb594b95..3eb530ce4d4867a6e56ecac979041f267563bfec 100644 (file)
@@ -19,6 +19,7 @@
 #ifndef OWNSQL_H
 #define OWNSQL_H
 
+#include <QLoggingCategory>
 #include <QObject>
 #include <QVariant>
 
@@ -28,6 +29,7 @@ struct sqlite3;
 struct sqlite3_stmt;
 
 namespace OCC {
+OCSYNC_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcSql)
 
 class SqlQuery;
 
@@ -136,13 +138,28 @@ public:
     };
     NextResult next();
 
-    void bindValue(int pos, const QVariant &value);
+    template<class T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
+    void bindValue(int pos, const T &value)
+    {
+        qCDebug(lcSql) << "SQL bind" << pos << value;
+        bindValueInternal(pos, static_cast<int>(value));
+    }
+
+    template<class T, typename std::enable_if<!std::is_enum<T>::value, int>::type = 0>
+    void bindValue(int pos, const T &value)
+    {
+        qCDebug(lcSql) << "SQL bind" << pos << value;
+        bindValueInternal(pos, value);
+    }
+
     QString lastQuery() const;
     int numRowsAffected();
     void reset_and_clear_bindings();
     void finish();
 
 private:
+    void bindValueInternal(int pos, const QVariant &value);
+
     SqlDatabase *_sqldb = nullptr;
     sqlite3 *_db = nullptr;
     sqlite3_stmt *_stmt = nullptr;
index c301ddccfed71a6adbec5c09f2ced8a6df36a00d..4ed9732dc3e13125cb5966e9c0f2fd16597fdaa2 100644 (file)
@@ -2272,7 +2272,7 @@ void SyncJournalDb::PinStateInterface::setForPath(const QByteArray &path, PinSta
             "INSERT OR REPLACE INTO flags(path, pinState) VALUES(?1, ?2);"),
         _db->_db));
     query.bindValue(1, path);
-    query.bindValue(2, static_cast<int>(state));
+    query.bindValue(2, state);
     query.exec();
 }