diff --git a/src/libcommonserver/db/db.cpp b/src/libcommonserver/db/db.cpp index ec50f8e0a..7bf5145f3 100644 --- a/src/libcommonserver/db/db.cpp +++ b/src/libcommonserver/db/db.cpp @@ -312,8 +312,8 @@ bool Db::init(const std::string &version) { #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21 << 8)) #endif - if (!prepareQuery(CHECK_TABLE_EXISTENCE_REQUEST_ID, CHECK_TABLE_EXISTENCE_REQUEST)) return false; - if (!prepareQuery(CHECK_COLUMN_EXISTENCE_REQUEST_ID, CHECK_COLUMN_EXISTENCE_REQUEST)) return false; + if (!createAndPrepareRequest(CHECK_TABLE_EXISTENCE_REQUEST_ID, CHECK_TABLE_EXISTENCE_REQUEST)) return false; + if (!createAndPrepareRequest(CHECK_COLUMN_EXISTENCE_REQUEST_ID, CHECK_COLUMN_EXISTENCE_REQUEST)) return false; if (!version.empty()) { // Check if DB is already initialized @@ -325,7 +325,7 @@ bool Db::init(const std::string &version) { if (dbExists) { // Check version LOG_DEBUG(_logger, "Check DB version"); - if (!prepareQuery(SELECT_VERSION_REQUEST_ID, SELECT_VERSION_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_VERSION_REQUEST_ID, SELECT_VERSION_REQUEST)) return false; bool found = false; if (!selectVersion(_fromVersion, found)) { @@ -349,7 +349,7 @@ bool Db::init(const std::string &version) { } // Update version - if (!prepareQuery(UPDATE_VERSION_REQUEST_ID, UPDATE_VERSION_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_VERSION_REQUEST_ID, UPDATE_VERSION_REQUEST)) return false; if (!updateVersion(version, found)) { LOG_WARN(_logger, "Error in Db::updateVersion"); return false; @@ -364,7 +364,7 @@ bool Db::init(const std::string &version) { } else { // Create version table LOG_DEBUG(_logger, "Create version table"); - if (!prepareQuery(CREATE_VERSION_TABLE_ID, CREATE_VERSION_TABLE)) return false; + if (!createAndPrepareRequest(CREATE_VERSION_TABLE_ID, CREATE_VERSION_TABLE)) return false; int errId = -1; std::string error; @@ -376,7 +376,7 @@ bool Db::init(const std::string &version) { // Insert version LOG_DEBUG(_logger, "Insert version " << version); - if (!prepareQuery(INSERT_VERSION_REQUEST_ID, INSERT_VERSION_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_VERSION_REQUEST_ID, INSERT_VERSION_REQUEST)) return false; if (!insertVersion(version)) { LOG_WARN(_logger, "Error in Db::insertVersion"); return false; @@ -501,15 +501,7 @@ bool Db::checkConnect(const std::string &version) { } // SELECT_SQLITE_VERSION - int errId; - std::string error; - - ASSERT(queryCreate(SELECT_SQLITE_VERSION_ID)); - if (!queryPrepare(SELECT_SQLITE_VERSION_ID, SELECT_SQLITE_VERSION, false, errId, error)) { - LOG_WARN(_logger, "Error preparing query:" << SELECT_SQLITE_VERSION_ID); - queryFree(SELECT_SQLITE_VERSION_ID); - return false; - } + if (!createAndPrepareRequest(SELECT_SQLITE_VERSION_ID, SELECT_SQLITE_VERSION)) return false; bool hasData; if (!queryNext(SELECT_SQLITE_VERSION_ID, hasData) || !hasData) { LOG_WARN(_logger, "Error getting query result: " << SELECT_SQLITE_VERSION_ID); @@ -522,12 +514,7 @@ bool Db::checkConnect(const std::string &version) { LOG_DEBUG(_logger, "sqlite3 version=" << result.c_str()); // PRAGMA_LOCKING_MODE - ASSERT(queryCreate(PRAGMA_LOCKING_MODE_ID)); - if (!queryPrepare(PRAGMA_LOCKING_MODE_ID, PRAGMA_LOCKING_MODE, false, errId, error)) { - LOG_WARN(_logger, "Error preparing query:" << PRAGMA_LOCKING_MODE_ID); - queryFree(PRAGMA_LOCKING_MODE_ID); - return false; - } + if (!createAndPrepareRequest(PRAGMA_LOCKING_MODE_ID, PRAGMA_LOCKING_MODE)) return false; if (!queryNext(PRAGMA_LOCKING_MODE_ID, hasData) || !hasData) { LOG_WARN(_logger, "Error getting query result: " << PRAGMA_LOCKING_MODE_ID); queryFree(PRAGMA_LOCKING_MODE_ID); @@ -539,12 +526,7 @@ bool Db::checkConnect(const std::string &version) { // PRAGMA_JOURNAL_MODE std::string sql(PRAGMA_JOURNAL_MODE + _journalMode + ";"); - ASSERT(queryCreate(PRAGMA_JOURNAL_MODE_ID)); - if (!queryPrepare(PRAGMA_JOURNAL_MODE_ID, sql, false, errId, error)) { - LOG_WARN(_logger, "Error preparing query:" << PRAGMA_JOURNAL_MODE_ID); - queryFree(PRAGMA_JOURNAL_MODE_ID); - return false; - } + if (!createAndPrepareRequest(PRAGMA_JOURNAL_MODE_ID, sql.c_str())) return false; if (!queryNext(PRAGMA_JOURNAL_MODE_ID, hasData) || !hasData) { LOG_WARN(_logger, "Error getting query result: " << PRAGMA_JOURNAL_MODE_ID); queryFree(PRAGMA_JOURNAL_MODE_ID); @@ -559,12 +541,7 @@ bool Db::checkConnect(const std::string &version) { std::string synchronousMode = "FULL"; if (_journalMode.compare("WAL") == 0) synchronousMode = "NORMAL"; sql = PRAGMA_SYNCHRONOUS + synchronousMode + ";"; - ASSERT(queryCreate(PRAGMA_SYNCHRONOUS_ID)); - if (!queryPrepare(PRAGMA_SYNCHRONOUS_ID, sql, false, errId, error)) { - LOG_WARN(_logger, "Error preparing query:" << PRAGMA_SYNCHRONOUS_ID); - queryFree(PRAGMA_SYNCHRONOUS_ID); - return false; - } + if (!createAndPrepareRequest(PRAGMA_SYNCHRONOUS_ID, sql.c_str())) return false; if (!queryNext(PRAGMA_SYNCHRONOUS_ID, hasData)) { LOG_WARN(_logger, "Error getting query result: " << PRAGMA_SYNCHRONOUS_ID); queryFree(PRAGMA_SYNCHRONOUS_ID); @@ -574,12 +551,7 @@ bool Db::checkConnect(const std::string &version) { LOG_DEBUG(_logger, "sqlite3 synchronous=" << synchronousMode.c_str()); // PRAGMA_CASE_SENSITIVE_LIKE - ASSERT(queryCreate(PRAGMA_CASE_SENSITIVE_LIKE_ID)); - if (!queryPrepare(PRAGMA_CASE_SENSITIVE_LIKE_ID, PRAGMA_CASE_SENSITIVE_LIKE, false, errId, error)) { - LOG_WARN(_logger, "Error preparing query:" << PRAGMA_CASE_SENSITIVE_LIKE_ID); - queryFree(PRAGMA_CASE_SENSITIVE_LIKE_ID); - return false; - } + if (!createAndPrepareRequest(PRAGMA_CASE_SENSITIVE_LIKE_ID, PRAGMA_CASE_SENSITIVE_LIKE)) return false; if (!queryNext(PRAGMA_CASE_SENSITIVE_LIKE_ID, hasData)) { LOG_WARN(_logger, "Error getting query result: " << PRAGMA_CASE_SENSITIVE_LIKE_ID); queryFree(PRAGMA_CASE_SENSITIVE_LIKE_ID); @@ -589,12 +561,7 @@ bool Db::checkConnect(const std::string &version) { LOG_DEBUG(_logger, "sqlite3 case_sensitivity=ON"); // PRAGMA_FOREIGN_KEYS - ASSERT(queryCreate(PRAGMA_FOREIGN_KEYS_ID)); - if (!queryPrepare(PRAGMA_FOREIGN_KEYS_ID, PRAGMA_FOREIGN_KEYS, false, errId, error)) { - LOG_WARN(_logger, "Error preparing query:" << PRAGMA_FOREIGN_KEYS_ID); - queryFree(PRAGMA_FOREIGN_KEYS_ID); - return false; - } + if (!createAndPrepareRequest(PRAGMA_FOREIGN_KEYS_ID, PRAGMA_FOREIGN_KEYS)) return false; if (!queryNext(PRAGMA_FOREIGN_KEYS_ID, hasData)) { LOG_WARN(_logger, "Error getting query result: " << PRAGMA_FOREIGN_KEYS_ID); queryFree(PRAGMA_FOREIGN_KEYS_ID); @@ -606,21 +573,6 @@ bool Db::checkConnect(const std::string &version) { return true; } -bool Db::prepareQuery(const std::string &queryId, const std::string &query) { - if (!queryCreate(queryId)) { - LOG_WARN(_logger, "SQL Error - Fail to create query " << queryId.c_str()); - return false; - } - - int errId = 0; - std::string error; - if (!queryPrepare(queryId, query, false, errId, error)) { - queryFree(queryId); - return sqlFail(queryId, error); - } - return true; -} - bool Db::addIntegerColumnIfMissing(const std::string &tableName, const std::string &columnName, bool *columnAdded /*= nullptr*/) { const auto requestId = tableName + "add_column_" + columnName; const auto request = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " INTEGER;"; @@ -633,7 +585,7 @@ bool Db::addColumnIfMissing(const std::string &tableName, const std::string &col if (!columnExists(tableName, columnName, exist)) return false; if (!exist) { LOG_INFO(_logger, "Adding column " << columnName.c_str() << " into table " << tableName.c_str()); - if (!prepareQuery(requestId, request)) return false; + if (!createAndPrepareRequest(requestId.c_str(), request.c_str())) return false; int errId = 0; std::string error; if (!queryExec(requestId, errId, error)) { @@ -647,6 +599,21 @@ bool Db::addColumnIfMissing(const std::string &tableName, const std::string &col return true; } +bool Db::createAndPrepareRequest(const char *requestId, const char *query) { + int errId = 0; + std::string error; + + if (!queryCreate(requestId)) { + LOG_FATAL(_logger, "ENFORCE: \"queryCreate(" << requestId << ")\"."); + } + if (!queryPrepare(requestId, query, false, errId, error)) { + queryFree(requestId); + return sqlFail(requestId, error); + } + + return true; +} + bool Db::tableExists(const std::string &tableName, bool &exist) { const std::scoped_lock lock(_mutex); diff --git a/src/libcommonserver/db/db.h b/src/libcommonserver/db/db.h index 7c1025b2c..5043b711f 100644 --- a/src/libcommonserver/db/db.h +++ b/src/libcommonserver/db/db.h @@ -84,11 +84,13 @@ class COMMONSERVER_EXPORT Db { bool sqlFail(const std::string &log, const std::string &error); bool checkConnect(const std::string &version); - bool prepareQuery(const std::string &queryId, const std::string &query); bool addIntegerColumnIfMissing(const std::string &tableName, const std::string &columnName, bool *columnAdded = nullptr); bool addColumnIfMissing(const std::string &tableName, const std::string &columnName, const std::string &requestId, const std::string &request, bool *columnAdded = nullptr); + // Helpers + bool createAndPrepareRequest(const char *requestId, const char *query); + log4cplus::Logger _logger; std::shared_ptr _sqliteDb; std::filesystem::path _dbPath; diff --git a/src/libcommonserver/utility/utility.h b/src/libcommonserver/utility/utility.h index 2d38ed730..d2a7e2e22 100644 --- a/src/libcommonserver/utility/utility.h +++ b/src/libcommonserver/utility/utility.h @@ -188,4 +188,21 @@ struct COMMONSERVER_EXPORT Utility { inline static log4cplus::Logger logger() { return Log::isSet() ? Log::instance()->getLogger() : _logger; } }; + +struct TimeCounter { + explicit TimeCounter(const std::string &name) : _name(name) {} + void start() { _start = clock(); } + void end() { + _end = clock(); + _total += (double) (_end - _start) / CLOCKS_PER_SEC; + } + void trace() { LOG_DEBUG(Log::instance()->getLogger(), "Time counter " << _name.c_str() << " value:" << _total); } + + private: + std::string _name; + clock_t _start = 0; + clock_t _end = 0; + double _total = 0; +}; + } // namespace KDC diff --git a/src/libparms/db/parmsdb.cpp b/src/libparms/db/parmsdb.cpp index 8128b19ae..3fe4360f4 100644 --- a/src/libparms/db/parmsdb.cpp +++ b/src/libparms/db/parmsdb.cpp @@ -788,11 +788,7 @@ bool ParmsDb::create(bool &retry) { std::string error; // Parameters - ASSERT(queryCreate(CREATE_PARAMETERS_TABLE_ID)); - if (!queryPrepare(CREATE_PARAMETERS_TABLE_ID, CREATE_PARAMETERS_TABLE, false, errId, error)) { - queryFree(CREATE_PARAMETERS_TABLE_ID); - return sqlFail(CREATE_PARAMETERS_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_PARAMETERS_TABLE_ID, CREATE_PARAMETERS_TABLE)) return false; if (!queryExec(CREATE_PARAMETERS_TABLE_ID, errId, error)) { // In certain situations the io error can be avoided by switching // to the DELETE journal mode @@ -809,11 +805,7 @@ bool ParmsDb::create(bool &retry) { queryFree(CREATE_PARAMETERS_TABLE_ID); // User - ASSERT(queryCreate(CREATE_USER_TABLE_ID)); - if (!queryPrepare(CREATE_USER_TABLE_ID, CREATE_USER_TABLE, false, errId, error)) { - queryFree(CREATE_USER_TABLE_ID); - return sqlFail(CREATE_USER_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_USER_TABLE_ID, CREATE_USER_TABLE)) return false; if (!queryExec(CREATE_USER_TABLE_ID, errId, error)) { queryFree(CREATE_USER_TABLE_ID); return sqlFail(CREATE_USER_TABLE_ID, error); @@ -821,11 +813,7 @@ bool ParmsDb::create(bool &retry) { queryFree(CREATE_USER_TABLE_ID); // Account - ASSERT(queryCreate(CREATE_ACCOUNT_TABLE_ID)); - if (!queryPrepare(CREATE_ACCOUNT_TABLE_ID, CREATE_ACCOUNT_TABLE, false, errId, error)) { - queryFree(CREATE_ACCOUNT_TABLE_ID); - return sqlFail(CREATE_ACCOUNT_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_ACCOUNT_TABLE_ID, CREATE_ACCOUNT_TABLE)) return false; if (!queryExec(CREATE_ACCOUNT_TABLE_ID, errId, error)) { queryFree(CREATE_ACCOUNT_TABLE_ID); return sqlFail(CREATE_ACCOUNT_TABLE_ID, error); @@ -833,11 +821,7 @@ bool ParmsDb::create(bool &retry) { queryFree(CREATE_ACCOUNT_TABLE_ID); // Drive - ASSERT(queryCreate(CREATE_DRIVE_TABLE_ID)); - if (!queryPrepare(CREATE_DRIVE_TABLE_ID, CREATE_DRIVE_TABLE, false, errId, error)) { - queryFree(CREATE_DRIVE_TABLE_ID); - return sqlFail(CREATE_DRIVE_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_DRIVE_TABLE_ID, CREATE_DRIVE_TABLE)) return false; if (!queryExec(CREATE_DRIVE_TABLE_ID, errId, error)) { queryFree(CREATE_DRIVE_TABLE_ID); return sqlFail(CREATE_DRIVE_TABLE_ID, error); @@ -845,11 +829,7 @@ bool ParmsDb::create(bool &retry) { queryFree(CREATE_DRIVE_TABLE_ID); // Sync - ASSERT(queryCreate(CREATE_SYNC_TABLE_ID)); - if (!queryPrepare(CREATE_SYNC_TABLE_ID, CREATE_SYNC_TABLE, false, errId, error)) { - queryFree(CREATE_SYNC_TABLE_ID); - return sqlFail(CREATE_SYNC_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_SYNC_TABLE_ID, CREATE_SYNC_TABLE)) return false; if (!queryExec(CREATE_SYNC_TABLE_ID, errId, error)) { queryFree(CREATE_SYNC_TABLE_ID); return sqlFail(CREATE_SYNC_TABLE_ID, error); @@ -857,11 +837,7 @@ bool ParmsDb::create(bool &retry) { queryFree(CREATE_SYNC_TABLE_ID); // Exclusion Template - ASSERT(queryCreate(CREATE_EXCLUSION_TEMPLATE_TABLE_ID)); - if (!queryPrepare(CREATE_EXCLUSION_TEMPLATE_TABLE_ID, CREATE_EXCLUSION_TEMPLATE_TABLE, false, errId, error)) { - queryFree(CREATE_EXCLUSION_TEMPLATE_TABLE_ID); - return sqlFail(CREATE_EXCLUSION_TEMPLATE_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_EXCLUSION_TEMPLATE_TABLE_ID, CREATE_EXCLUSION_TEMPLATE_TABLE)) return false; if (!queryExec(CREATE_EXCLUSION_TEMPLATE_TABLE_ID, errId, error)) { queryFree(CREATE_EXCLUSION_TEMPLATE_TABLE_ID); return sqlFail(CREATE_EXCLUSION_TEMPLATE_TABLE_ID, error); @@ -870,11 +846,7 @@ bool ParmsDb::create(bool &retry) { #ifdef __APPLE__ // Exclusion App - ASSERT(queryCreate(CREATE_EXCLUSION_APP_TABLE_ID)); - if (!queryPrepare(CREATE_EXCLUSION_APP_TABLE_ID, CREATE_EXCLUSION_APP_TABLE, false, errId, error)) { - queryFree(CREATE_EXCLUSION_APP_TABLE_ID); - return sqlFail(CREATE_EXCLUSION_APP_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_EXCLUSION_APP_TABLE_ID, CREATE_EXCLUSION_APP_TABLE)) return false; if (!queryExec(CREATE_EXCLUSION_APP_TABLE_ID, errId, error)) { queryFree(CREATE_EXCLUSION_APP_TABLE_ID); return sqlFail(CREATE_EXCLUSION_APP_TABLE_ID, error); @@ -883,11 +855,7 @@ bool ParmsDb::create(bool &retry) { #endif // Error - ASSERT(queryCreate(CREATE_ERROR_TABLE_ID)); - if (!queryPrepare(CREATE_ERROR_TABLE_ID, CREATE_ERROR_TABLE, false, errId, error)) { - queryFree(CREATE_ERROR_TABLE_ID); - return sqlFail(CREATE_ERROR_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_ERROR_TABLE_ID, CREATE_ERROR_TABLE)) return false; if (!queryExec(CREATE_ERROR_TABLE_ID, errId, error)) { queryFree(CREATE_ERROR_TABLE_ID); return sqlFail(CREATE_ERROR_TABLE_ID, error); @@ -901,95 +869,95 @@ bool ParmsDb::create(bool &retry) { } // Migration old selectivesync table - ASSERT(queryCreate(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID)); - if (!queryPrepare(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID, CREATE_MIGRATION_SELECTIVESYNC_TABLE, false, errId, error)) { - queryFree(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID); - return sqlFail(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID, CREATE_MIGRATION_SELECTIVESYNC_TABLE)) return false; if (!queryExec(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID, errId, error)) { queryFree(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID); return sqlFail(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID, error); } queryFree(CREATE_MIGRATION_SELECTIVESYNC_TABLE_ID); - return true; } bool ParmsDb::prepare() { // Parameters - if (!prepareQuery(INSERT_PARAMETERS_REQUEST_ID, INSERT_PARAMETERS_REQUEST)) return false; - if (!prepareQuery(UPDATE_PARAMETERS_REQUEST_ID, UPDATE_PARAMETERS_REQUEST)) return false; - if (!prepareQuery(SELECT_PARAMETERS_REQUEST_ID, SELECT_PARAMETERS_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_PARAMETERS_REQUEST_ID, INSERT_PARAMETERS_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_PARAMETERS_REQUEST_ID, UPDATE_PARAMETERS_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_PARAMETERS_REQUEST_ID, SELECT_PARAMETERS_REQUEST)) return false; // User - if (!prepareQuery(INSERT_USER_REQUEST_ID, INSERT_USER_REQUEST)) return false; - if (!prepareQuery(UPDATE_USER_REQUEST_ID, UPDATE_USER_REQUEST)) return false; - if (!prepareQuery(DELETE_USER_REQUEST_ID, DELETE_USER_REQUEST)) return false; - if (!prepareQuery(SELECT_USER_REQUEST_ID, SELECT_USER_REQUEST)) return false; - if (!prepareQuery(SELECT_USER_BY_USERID_REQUEST_ID, SELECT_USER_BY_USERID_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_USERS_REQUEST_ID, SELECT_ALL_USERS_REQUEST)) return false; - if (!prepareQuery(SELECT_LAST_CONNECTED_USER_REQUEST_ID, SELECT_LAST_CONNECTED_USER_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_USER_REQUEST_ID, INSERT_USER_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_USER_REQUEST_ID, UPDATE_USER_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_USER_REQUEST_ID, DELETE_USER_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_USER_REQUEST_ID, SELECT_USER_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_USER_BY_USERID_REQUEST_ID, SELECT_USER_BY_USERID_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_USERS_REQUEST_ID, SELECT_ALL_USERS_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_LAST_CONNECTED_USER_REQUEST_ID, SELECT_LAST_CONNECTED_USER_REQUEST)) return false; // Account - if (!prepareQuery(INSERT_ACCOUNT_REQUEST_ID, INSERT_ACCOUNT_REQUEST)) return false; - if (!prepareQuery(UPDATE_ACCOUNT_REQUEST_ID, UPDATE_ACCOUNT_REQUEST)) return false; - if (!prepareQuery(DELETE_ACCOUNT_REQUEST_ID, DELETE_ACCOUNT_REQUEST)) return false; - if (!prepareQuery(SELECT_ACCOUNT_REQUEST_ID, SELECT_ACCOUNT_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_ACCOUNTS_REQUEST_ID, SELECT_ALL_ACCOUNTS_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_ACCOUNTS_BY_USER_REQUEST_ID, SELECT_ALL_ACCOUNTS_BY_USER_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_ACCOUNT_REQUEST_ID, INSERT_ACCOUNT_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_ACCOUNT_REQUEST_ID, UPDATE_ACCOUNT_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ACCOUNT_REQUEST_ID, DELETE_ACCOUNT_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ACCOUNT_REQUEST_ID, SELECT_ACCOUNT_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_ACCOUNTS_REQUEST_ID, SELECT_ALL_ACCOUNTS_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_ACCOUNTS_BY_USER_REQUEST_ID, SELECT_ALL_ACCOUNTS_BY_USER_REQUEST)) return false; // Drive - if (!prepareQuery(INSERT_DRIVE_REQUEST_ID, INSERT_DRIVE_REQUEST)) return false; - if (!prepareQuery(UPDATE_DRIVE_REQUEST_ID, UPDATE_DRIVE_REQUEST)) return false; - if (!prepareQuery(DELETE_DRIVE_REQUEST_ID, DELETE_DRIVE_REQUEST)) return false; - if (!prepareQuery(SELECT_DRIVE_REQUEST_ID, SELECT_DRIVE_REQUEST)) return false; - if (!prepareQuery(SELECT_DRIVE_BY_DRIVEID_REQUEST_ID, SELECT_DRIVE_BY_DRIVEID_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_DRIVES_REQUEST_ID, SELECT_ALL_DRIVES_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_DRIVES_BY_ACCOUNT_REQUEST_ID, SELECT_ALL_DRIVES_BY_ACCOUNT_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_DRIVE_REQUEST_ID, INSERT_DRIVE_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_DRIVE_REQUEST_ID, UPDATE_DRIVE_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_DRIVE_REQUEST_ID, DELETE_DRIVE_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_DRIVE_REQUEST_ID, SELECT_DRIVE_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_DRIVE_BY_DRIVEID_REQUEST_ID, SELECT_DRIVE_BY_DRIVEID_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_DRIVES_REQUEST_ID, SELECT_ALL_DRIVES_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_DRIVES_BY_ACCOUNT_REQUEST_ID, SELECT_ALL_DRIVES_BY_ACCOUNT_REQUEST)) return false; // Sync - if (!prepareQuery(INSERT_SYNC_REQUEST_ID, INSERT_SYNC_REQUEST)) return false; - if (!prepareQuery(UPDATE_SYNC_REQUEST_ID, UPDATE_SYNC_REQUEST)) return false; - if (!prepareQuery(UPDATE_SYNC_PAUSED_REQUEST_ID, UPDATE_SYNC_PAUSED_REQUEST)) return false; - if (!prepareQuery(UPDATE_SYNC_HASFULLYCOMPLETED_REQUEST_ID, UPDATE_SYNC_HASFULLYCOMPLETED_REQUEST)) return false; - if (!prepareQuery(DELETE_SYNC_REQUEST_ID, DELETE_SYNC_REQUEST)) return false; - if (!prepareQuery(SELECT_SYNC_REQUEST_ID, SELECT_SYNC_REQUEST)) return false; - if (!prepareQuery(SELECT_SYNC_BY_PATH_REQUEST_ID, SELECT_SYNC_BY_PATH_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_SYNCS_REQUEST_ID, SELECT_ALL_SYNCS_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_SYNCS_BY_DRIVE_REQUEST_ID, SELECT_ALL_SYNCS_BY_DRIVE_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_SYNC_REQUEST_ID, INSERT_SYNC_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_SYNC_REQUEST_ID, UPDATE_SYNC_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_SYNC_PAUSED_REQUEST_ID, UPDATE_SYNC_PAUSED_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_SYNC_HASFULLYCOMPLETED_REQUEST_ID, UPDATE_SYNC_HASFULLYCOMPLETED_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_SYNC_REQUEST_ID, DELETE_SYNC_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_SYNC_REQUEST_ID, SELECT_SYNC_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_SYNC_BY_PATH_REQUEST_ID, SELECT_SYNC_BY_PATH_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_SYNCS_REQUEST_ID, SELECT_ALL_SYNCS_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_SYNCS_BY_DRIVE_REQUEST_ID, SELECT_ALL_SYNCS_BY_DRIVE_REQUEST)) return false; // Exclusion Template - if (!prepareQuery(INSERT_EXCLUSION_TEMPLATE_REQUEST_ID, INSERT_EXCLUSION_TEMPLATE_REQUEST)) return false; - if (!prepareQuery(UPDATE_EXCLUSION_TEMPLATE_REQUEST_ID, UPDATE_EXCLUSION_TEMPLATE_REQUEST)) return false; - if (!prepareQuery(DELETE_EXCLUSION_TEMPLATE_REQUEST_ID, DELETE_EXCLUSION_TEMPLATE_REQUEST)) return false; - if (!prepareQuery(DELETE_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST_ID, DELETE_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST)) + if (!createAndPrepareRequest(INSERT_EXCLUSION_TEMPLATE_REQUEST_ID, INSERT_EXCLUSION_TEMPLATE_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_EXCLUSION_TEMPLATE_REQUEST_ID, UPDATE_EXCLUSION_TEMPLATE_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_EXCLUSION_TEMPLATE_REQUEST_ID, DELETE_EXCLUSION_TEMPLATE_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST_ID, DELETE_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_EXCLUSION_TEMPLATE_REQUEST_ID, SELECT_ALL_EXCLUSION_TEMPLATE_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST_ID, SELECT_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST)) + if (!createAndPrepareRequest(SELECT_ALL_EXCLUSION_TEMPLATE_REQUEST_ID, SELECT_ALL_EXCLUSION_TEMPLATE_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST_ID, SELECT_ALL_EXCLUSION_TEMPLATE_BY_DEF_REQUEST)) return false; #ifdef __APPLE__ // Exclusion App - if (!prepareQuery(INSERT_EXCLUSION_APP_REQUEST_ID, INSERT_EXCLUSION_APP_REQUEST)) return false; - if (!prepareQuery(UPDATE_EXCLUSION_APP_REQUEST_ID, UPDATE_EXCLUSION_APP_REQUEST)) return false; - if (!prepareQuery(DELETE_EXCLUSION_APP_REQUEST_ID, DELETE_EXCLUSION_APP_REQUEST)) return false; - if (!prepareQuery(DELETE_ALL_EXCLUSION_APP_BY_DEF_REQUEST_ID, DELETE_ALL_EXCLUSION_APP_BY_DEF_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_EXCLUSION_APP_REQUEST_ID, SELECT_ALL_EXCLUSION_APP_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_EXCLUSION_APP_BY_DEF_REQUEST_ID, SELECT_ALL_EXCLUSION_APP_BY_DEF_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_EXCLUSION_APP_REQUEST_ID, INSERT_EXCLUSION_APP_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_EXCLUSION_APP_REQUEST_ID, UPDATE_EXCLUSION_APP_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_EXCLUSION_APP_REQUEST_ID, DELETE_EXCLUSION_APP_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ALL_EXCLUSION_APP_BY_DEF_REQUEST_ID, DELETE_ALL_EXCLUSION_APP_BY_DEF_REQUEST)) + return false; + if (!createAndPrepareRequest(SELECT_ALL_EXCLUSION_APP_REQUEST_ID, SELECT_ALL_EXCLUSION_APP_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_EXCLUSION_APP_BY_DEF_REQUEST_ID, SELECT_ALL_EXCLUSION_APP_BY_DEF_REQUEST)) + return false; #endif // Error - if (!prepareQuery(INSERT_ERROR_REQUEST_ID, INSERT_ERROR_REQUEST)) return false; - if (!prepareQuery(UPDATE_ERROR_REQUEST_ID, UPDATE_ERROR_REQUEST)) return false; - if (!prepareQuery(DELETE_ALL_ERROR_BY_EXITCODE_REQUEST_ID, DELETE_ALL_ERROR_BY_EXITCODE_REQUEST)) return false; - if (!prepareQuery(DELETE_ALL_ERROR_BY_EXITCAUSEREQUEST_ID, DELETE_ALL_ERROR_BY_EXITCAUSEREQUEST)) return false; - if (!prepareQuery(DELETE_ALL_ERROR_BY_LEVEL_REQUEST_ID, DELETE_ALL_ERROR_BY_LEVEL_REQUEST)) return false; - if (!prepareQuery(DELETE_ERROR_BY_DBID_REQUEST_ID, DELETE_ERROR_BY_DBID_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_ERROR_BY_LEVEL_AND_SYNCDBID_REQUEST_ID, SELECT_ALL_ERROR_BY_LEVEL_AND_SYNCDBID_REQUEST)) + if (!createAndPrepareRequest(INSERT_ERROR_REQUEST_ID, INSERT_ERROR_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_ERROR_REQUEST_ID, UPDATE_ERROR_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ALL_ERROR_BY_EXITCODE_REQUEST_ID, DELETE_ALL_ERROR_BY_EXITCODE_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ALL_ERROR_BY_EXITCAUSEREQUEST_ID, DELETE_ALL_ERROR_BY_EXITCAUSEREQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ALL_ERROR_BY_LEVEL_REQUEST_ID, DELETE_ALL_ERROR_BY_LEVEL_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ERROR_BY_DBID_REQUEST_ID, DELETE_ERROR_BY_DBID_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_ERROR_BY_LEVEL_AND_SYNCDBID_REQUEST_ID, + SELECT_ALL_ERROR_BY_LEVEL_AND_SYNCDBID_REQUEST)) + return false; + if (!createAndPrepareRequest(SELECT_ALL_CONFLICTS_BY_SYNCDBID_REQUEST_ID, SELECT_ALL_CONFLICTS_BY_SYNCDBID_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_CONFLICTS_BY_SYNCDBID_REQUEST_ID, SELECT_ALL_CONFLICTS_BY_SYNCDBID_REQUEST)) return false; - if (!prepareQuery(SELECT_FILTERED_CONFLICTS_BY_SYNCDBID_REQUEST_ID, SELECT_FILTERED_CONFLICTS_BY_SYNCDBID_REQUEST)) + if (!createAndPrepareRequest(SELECT_FILTERED_CONFLICTS_BY_SYNCDBID_REQUEST_ID, SELECT_FILTERED_CONFLICTS_BY_SYNCDBID_REQUEST)) return false; // Migration old selectivesync table - if (!prepareQuery(INSERT_MIGRATION_SELECTIVESYNC_REQUEST_ID, INSERT_MIGRATION_SELECTIVESYNC_REQUEST)) return false; - if (!prepareQuery(SELECT_ALL_MIGRATION_SELECTIVESYNC_REQUEST_ID, SELECT_ALL_MIGRATION_SELECTIVESYNC_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_MIGRATION_SELECTIVESYNC_REQUEST_ID, INSERT_MIGRATION_SELECTIVESYNC_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_MIGRATION_SELECTIVESYNC_REQUEST_ID, SELECT_ALL_MIGRATION_SELECTIVESYNC_REQUEST)) + return false; // App state if (!prepareAppState()) { - LOG_WARN(_logger, "Error in prepareAppState"); + LOG_WARN(_logger, "Error in createAndPrepareRequestAppState"); return false; } @@ -1023,11 +991,7 @@ bool ParmsDb::upgrade(const std::string & /*fromVersion*/, const std::string & / int errId = 0; std::string error; - ASSERT(queryCreate(UPDATE_PARAMETERS_JOB_REQUEST_ID)); - if (!queryPrepare(UPDATE_PARAMETERS_JOB_REQUEST_ID, UPDATE_PARAMETERS_JOB_REQUEST, false, errId, error)) { - queryFree(UPDATE_PARAMETERS_JOB_REQUEST_ID); - return sqlFail(UPDATE_PARAMETERS_JOB_REQUEST_ID, error); - } + if (!createAndPrepareRequest(UPDATE_PARAMETERS_JOB_REQUEST_ID, UPDATE_PARAMETERS_JOB_REQUEST)) return false; ASSERT(queryResetAndClearBindings(UPDATE_PARAMETERS_JOB_REQUEST_ID)); ASSERT(queryBindValue(UPDATE_PARAMETERS_JOB_REQUEST_ID, 1, Parameters::_uploadSessionParallelJobsDefault)); ASSERT(queryBindValue(UPDATE_PARAMETERS_JOB_REQUEST_ID, 2, Parameters::_jobPoolCapacityFactorDefault)); diff --git a/src/libparms/db/parmsdbappstate.cpp b/src/libparms/db/parmsdbappstate.cpp index 84844a62e..8929529ab 100644 --- a/src/libparms/db/parmsdbappstate.cpp +++ b/src/libparms/db/parmsdbappstate.cpp @@ -49,7 +49,7 @@ namespace KDC { bool ParmsDb::createAppState() { LOG_INFO(_logger, "Creating table app_state"); - if (!prepareQuery(CREATE_APP_STATE_TABLE_ID, CREATE_APP_STATE_TABLE)) return false; + if (!createAndPrepareRequest(CREATE_APP_STATE_TABLE_ID, CREATE_APP_STATE_TABLE)) return false; int errId = 0; std::string error; if (!queryExec(CREATE_APP_STATE_TABLE_ID, errId, error)) { @@ -61,9 +61,9 @@ bool ParmsDb::createAppState() { } bool ParmsDb::prepareAppState() { - if (!prepareQuery(INSERT_APP_STATE_REQUEST_ID, INSERT_APP_STATE_REQUEST)) return false; - if (!prepareQuery(SELECT_APP_STATE_REQUEST_ID, SELECT_APP_STATE_REQUEST)) return false; - if (!prepareQuery(UPDATE_APP_STATE_REQUEST_ID, UPDATE_APP_STATE_REQUEST)) return false; + if (!createAndPrepareRequest(INSERT_APP_STATE_REQUEST_ID, INSERT_APP_STATE_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_APP_STATE_REQUEST_ID, SELECT_APP_STATE_REQUEST)) return false; + if (!createAndPrepareRequest(UPDATE_APP_STATE_REQUEST_ID, UPDATE_APP_STATE_REQUEST)) return false; return true; } diff --git a/src/libsyncengine/db/syncdb.cpp b/src/libsyncengine/db/syncdb.cpp index 8ce941e31..744f8b8c0 100644 --- a/src/libsyncengine/db/syncdb.cpp +++ b/src/libsyncengine/db/syncdb.cpp @@ -211,6 +211,27 @@ "lastModifiedDrive, type, size, checksum, status, syncing FROM node " \ "WHERE nameLocal != nameDrive;" +#define SELECT_ANCESTORS_NODES_REQUEST_ID "select_node13" +#define SELECT_ANCESTORS_NODES_REQUEST \ + "WITH ancestor as (SELECT parentNodeId AS pid, nameLocal AS nl, nameDrive AS nd FROM node WHERE nodeId = ?1 " \ + "UNION ALL " \ + "SELECT parentNodeId, nameLocal, nameDrive FROM ancestor, node WHERE ancestor.pid = node.nodeId) " \ + "SELECT nl, nd FROM ancestor;" + +#define SELECT_ANCESTORS_NODES_LOCAL_REQUEST_ID "select_node14" +#define SELECT_ANCESTORS_NODES_LOCAL_REQUEST \ + "WITH ancestor as (SELECT parentNodeId AS pid, nameLocal AS nl, nameDrive AS nd FROM node WHERE nodeIdLocal = ?1 " \ + "UNION ALL " \ + "SELECT parentNodeId, nameLocal, nameDrive FROM ancestor, node WHERE ancestor.pid = node.nodeId) " \ + "SELECT nl, nd FROM ancestor;" + +#define SELECT_ANCESTORS_NODES_DRIVE_REQUEST_ID "select_node15" +#define SELECT_ANCESTORS_NODES_DRIVE_REQUEST \ + "WITH ancestor as (SELECT parentNodeId AS pid, nameLocal AS nl, nameDrive AS nd FROM node WHERE nodeIdDrive = ?1 " \ + "UNION ALL " \ + "SELECT parentNodeId, nameLocal, nameDrive FROM ancestor, node WHERE ancestor.pid = node.nodeId) " \ + "SELECT nl, nd FROM ancestor;" + // // sync_node // @@ -275,7 +296,7 @@ SyncDb::SyncDb(const std::string &dbPath, const std::string &version, const std: throw std::runtime_error("Cannot open DB!"); } - LOG_INFO(_logger, "SyncDb initialization done: dbPath=" << dbPath.c_str() << " targetNodeId=" << targetNodeId.c_str()); + LOG_INFO(_logger, "SyncDb initialization done: dbPath=" << dbPath << " targetNodeId=" << targetNodeId); } bool SyncDb::create(bool &retry) { @@ -283,11 +304,7 @@ bool SyncDb::create(bool &retry) { std::string error; // Node - ASSERT(queryCreate(CREATE_NODE_TABLE_ID)); - if (!queryPrepare(CREATE_NODE_TABLE_ID, CREATE_NODE_TABLE, false, errId, error)) { - queryFree(CREATE_NODE_TABLE_ID); - return sqlFail(CREATE_NODE_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_NODE_TABLE_ID, CREATE_NODE_TABLE)) return false; if (!queryExec(CREATE_NODE_TABLE_ID, errId, error)) { // In certain situations the io error can be avoided by switching // to the DELETE journal mode @@ -303,55 +320,35 @@ bool SyncDb::create(bool &retry) { } queryFree(CREATE_NODE_TABLE_ID); - ASSERT(queryCreate(CREATE_NODE_TABLE_IDX1_ID)); - if (!queryPrepare(CREATE_NODE_TABLE_IDX1_ID, CREATE_NODE_TABLE_IDX1, false, errId, error)) { - queryFree(CREATE_NODE_TABLE_IDX1_ID); - return sqlFail(CREATE_NODE_TABLE_IDX1_ID, error); - } + if (!createAndPrepareRequest(CREATE_NODE_TABLE_IDX1_ID, CREATE_NODE_TABLE_IDX1)) return false; if (!queryExec(CREATE_NODE_TABLE_IDX1_ID, errId, error)) { queryFree(CREATE_NODE_TABLE_IDX1_ID); return sqlFail(CREATE_NODE_TABLE_IDX1_ID, error); } queryFree(CREATE_NODE_TABLE_IDX1_ID); - ASSERT(queryCreate(CREATE_NODE_TABLE_IDX2_ID)); - if (!queryPrepare(CREATE_NODE_TABLE_IDX2_ID, CREATE_NODE_TABLE_IDX2, false, errId, error)) { - queryFree(CREATE_NODE_TABLE_IDX2_ID); - return sqlFail(CREATE_NODE_TABLE_IDX2_ID, error); - } + if (!createAndPrepareRequest(CREATE_NODE_TABLE_IDX2_ID, CREATE_NODE_TABLE_IDX2)) return false; if (!queryExec(CREATE_NODE_TABLE_IDX2_ID, errId, error)) { queryFree(CREATE_NODE_TABLE_IDX2_ID); return sqlFail(CREATE_NODE_TABLE_IDX2_ID, error); } queryFree(CREATE_NODE_TABLE_IDX2_ID); - ASSERT(queryCreate(CREATE_NODE_TABLE_IDX3_ID)); - if (!queryPrepare(CREATE_NODE_TABLE_IDX3_ID, CREATE_NODE_TABLE_IDX3, false, errId, error)) { - queryFree(CREATE_NODE_TABLE_IDX3_ID); - return sqlFail(CREATE_NODE_TABLE_IDX3_ID, error); - } + if (!createAndPrepareRequest(CREATE_NODE_TABLE_IDX3_ID, CREATE_NODE_TABLE_IDX3)) return false; if (!queryExec(CREATE_NODE_TABLE_IDX3_ID, errId, error)) { queryFree(CREATE_NODE_TABLE_IDX3_ID); return sqlFail(CREATE_NODE_TABLE_IDX3_ID, error); } queryFree(CREATE_NODE_TABLE_IDX3_ID); - ASSERT(queryCreate(CREATE_NODE_TABLE_IDX4_ID)); - if (!queryPrepare(CREATE_NODE_TABLE_IDX4_ID, CREATE_NODE_TABLE_IDX4, false, errId, error)) { - queryFree(CREATE_NODE_TABLE_IDX4_ID); - return sqlFail(CREATE_NODE_TABLE_IDX4_ID, error); - } + if (!createAndPrepareRequest(CREATE_NODE_TABLE_IDX4_ID, CREATE_NODE_TABLE_IDX4)) return false; if (!queryExec(CREATE_NODE_TABLE_IDX4_ID, errId, error)) { queryFree(CREATE_NODE_TABLE_IDX4_ID); return sqlFail(CREATE_NODE_TABLE_IDX4_ID, error); } queryFree(CREATE_NODE_TABLE_IDX4_ID); - ASSERT(queryCreate(CREATE_NODE_TABLE_IDX5_ID)); - if (!queryPrepare(CREATE_NODE_TABLE_IDX5_ID, CREATE_NODE_TABLE_IDX5, false, errId, error)) { - queryFree(CREATE_NODE_TABLE_IDX5_ID); - return sqlFail(CREATE_NODE_TABLE_IDX5_ID, error); - } + if (!createAndPrepareRequest(CREATE_NODE_TABLE_IDX5_ID, CREATE_NODE_TABLE_IDX5)) return false; if (!queryExec(CREATE_NODE_TABLE_IDX5_ID, errId, error)) { queryFree(CREATE_NODE_TABLE_IDX5_ID); return sqlFail(CREATE_NODE_TABLE_IDX5_ID, error); @@ -359,11 +356,7 @@ bool SyncDb::create(bool &retry) { queryFree(CREATE_NODE_TABLE_IDX5_ID); // Sync Node - ASSERT(queryCreate(CREATE_SYNC_NODE_TABLE_ID)); - if (!queryPrepare(CREATE_SYNC_NODE_TABLE_ID, CREATE_SYNC_NODE_TABLE, false, errId, error)) { - queryFree(CREATE_SYNC_NODE_TABLE_ID); - return sqlFail(CREATE_SYNC_NODE_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_SYNC_NODE_TABLE_ID, CREATE_SYNC_NODE_TABLE)) return false; if (!queryExec(CREATE_SYNC_NODE_TABLE_ID, errId, error)) { queryFree(CREATE_SYNC_NODE_TABLE_ID); return sqlFail(CREATE_SYNC_NODE_TABLE_ID, error); @@ -371,11 +364,7 @@ bool SyncDb::create(bool &retry) { queryFree(CREATE_SYNC_NODE_TABLE_ID); // Upload session token table - ASSERT(queryCreate(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID)); - if (!queryPrepare(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, CREATE_UPLOAD_SESSION_TOKEN_TABLE, false, errId, error)) { - queryFree(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID); - return sqlFail(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, CREATE_UPLOAD_SESSION_TOKEN_TABLE)) return false; if (!queryExec(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, errId, error)) { queryFree(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID); return sqlFail(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, error); @@ -386,9 +375,6 @@ bool SyncDb::create(bool &retry) { } bool SyncDb::prepare() { - int errId; - std::string error; - // Node if (!createAndPrepareRequest(INSERT_NODE_REQUEST_ID, INSERT_NODE_REQUEST)) return false; if (!createAndPrepareRequest(UPDATE_NODE_REQUEST_ID, UPDATE_NODE_REQUEST)) return false; @@ -400,126 +386,40 @@ bool SyncDb::prepare() { if (!createAndPrepareRequest(DELETE_NODES_BUT_ROOT_REQUEST_ID, DELETE_NODES_BUT_ROOT_REQUEST)) return false; if (!createAndPrepareRequest(DELETE_NODES_WITH_NULL_PARENTNODEID_REQUEST_ID, DELETE_NODES_WITH_NULL_PARENTNODEID_REQUEST)) return false; - - ASSERT(queryCreate(SELECT_NODE_BY_NODEID_LITE_ID)); - if (!queryPrepare(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_LITE, false, errId, error)) { - queryFree(SELECT_NODE_BY_NODEID_LITE_ID); - return sqlFail(SELECT_NODE_BY_NODEID_LITE_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_BY_NODEID_FULL_ID)); - if (!queryPrepare(SELECT_NODE_BY_NODEID_FULL_ID, SELECT_NODE_BY_NODEID_FULL, false, errId, error)) { - queryFree(SELECT_NODE_BY_NODEID_FULL_ID); - return sqlFail(SELECT_NODE_BY_NODEID_FULL_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_BY_NODEIDLOCAL_ID)); - if (!queryPrepare(SELECT_NODE_BY_NODEIDLOCAL_ID, SELECT_NODE_BY_NODEIDLOCAL, false, errId, error)) { - queryFree(SELECT_NODE_BY_NODEIDLOCAL_ID); - return sqlFail(SELECT_NODE_BY_NODEIDLOCAL_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_BY_NODEIDDRIVE_ID)); - if (!queryPrepare(SELECT_NODE_BY_NODEIDDRIVE_ID, SELECT_NODE_BY_NODEIDDRIVE, false, errId, error)) { - queryFree(SELECT_NODE_BY_NODEIDDRIVE_ID); - return sqlFail(SELECT_NODE_BY_NODEIDDRIVE_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_BY_PARENTNODEID_AND_NAMELOCAL_REQUEST_ID)); - if (!queryPrepare(SELECT_NODE_BY_PARENTNODEID_AND_NAMELOCAL_REQUEST_ID, SELECT_NODE_BY_PARENTNODEID_AND_NAMELOCAL_REQUEST, - false, errId, error)) { - queryFree(SELECT_NODE_BY_PARENTNODEID_AND_NAMELOCAL_REQUEST_ID); - return sqlFail(SELECT_NODE_BY_PARENTNODEID_AND_NAMELOCAL_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_BY_PARENTNODEID_AND_NAMEDRIVE_REQUEST_ID)); - if (!queryPrepare(SELECT_NODE_BY_PARENTNODEID_AND_NAMEDRIVE_REQUEST_ID, SELECT_NODE_BY_PARENTNODEID_AND_NAMEDRIVE_REQUEST, - false, errId, error)) { - queryFree(SELECT_NODE_BY_PARENTNODEID_AND_NAMEDRIVE_REQUEST_ID); - return sqlFail(SELECT_NODE_BY_PARENTNODEID_AND_NAMEDRIVE_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_BY_PARENTNODEID_REQUEST_ID)); - if (!queryPrepare(SELECT_NODE_BY_PARENTNODEID_REQUEST_ID, SELECT_NODE_BY_PARENTNODEID_REQUEST, false, errId, error)) { - queryFree(SELECT_NODE_BY_PARENTNODEID_REQUEST_ID); - return sqlFail(SELECT_NODE_BY_PARENTNODEID_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_BY_PARENTNODEID_ROOT_REQUEST_ID)); - if (!queryPrepare(SELECT_NODE_BY_PARENTNODEID_ROOT_REQUEST_ID, SELECT_NODE_BY_PARENTNODEID_ROOT_REQUEST, false, errId, - error)) { - queryFree(SELECT_NODE_BY_PARENTNODEID_ROOT_REQUEST_ID); - return sqlFail(SELECT_NODE_BY_PARENTNODEID_ROOT_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_STATUS_BY_NODEID_REQUEST_ID)); - if (!queryPrepare(SELECT_NODE_STATUS_BY_NODEID_REQUEST_ID, SELECT_NODE_STATUS_BY_NODEID_REQUEST, false, errId, error)) { - queryFree(SELECT_NODE_STATUS_BY_NODEID_REQUEST_ID); - return sqlFail(SELECT_NODE_STATUS_BY_NODEID_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_NODE_SYNCING_BY_NODEID_REQUEST_ID)); - if (!queryPrepare(SELECT_NODE_SYNCING_BY_NODEID_REQUEST_ID, SELECT_NODE_SYNCING_BY_NODEID_REQUEST, false, errId, error)) { - queryFree(SELECT_NODE_SYNCING_BY_NODEID_REQUEST_ID); - return sqlFail(SELECT_NODE_SYNCING_BY_NODEID_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_ALL_RENAMED_COLON_NODES_REQUEST_ID)); - if (!queryPrepare(SELECT_ALL_RENAMED_COLON_NODES_REQUEST_ID, SELECT_ALL_RENAMED_COLON_NODES_REQUEST, false, errId, error)) { - queryFree(SELECT_ALL_RENAMED_COLON_NODES_REQUEST_ID); - return sqlFail(SELECT_ALL_RENAMED_COLON_NODES_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_ALL_RENAMED_NODES_REQUEST_ID)); - if (!queryPrepare(SELECT_ALL_RENAMED_NODES_REQUEST_ID, SELECT_ALL_RENAMED_NODES_REQUEST, false, errId, error)) { - queryFree(SELECT_ALL_RENAMED_NODES_REQUEST_ID); - return sqlFail(SELECT_ALL_RENAMED_NODES_REQUEST_ID, error); - } + if (!createAndPrepareRequest(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_LITE)) return false; + if (!createAndPrepareRequest(SELECT_NODE_BY_NODEID_FULL_ID, SELECT_NODE_BY_NODEID_FULL)) return false; + if (!createAndPrepareRequest(SELECT_NODE_BY_NODEIDLOCAL_ID, SELECT_NODE_BY_NODEIDLOCAL)) return false; + if (!createAndPrepareRequest(SELECT_NODE_BY_NODEIDDRIVE_ID, SELECT_NODE_BY_NODEIDDRIVE)) return false; + if (!createAndPrepareRequest(SELECT_NODE_BY_PARENTNODEID_AND_NAMELOCAL_REQUEST_ID, + SELECT_NODE_BY_PARENTNODEID_AND_NAMELOCAL_REQUEST)) + return false; + if (!createAndPrepareRequest(SELECT_NODE_BY_PARENTNODEID_AND_NAMEDRIVE_REQUEST_ID, + SELECT_NODE_BY_PARENTNODEID_AND_NAMEDRIVE_REQUEST)) + return false; + if (!createAndPrepareRequest(SELECT_NODE_BY_PARENTNODEID_REQUEST_ID, SELECT_NODE_BY_PARENTNODEID_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_NODE_BY_PARENTNODEID_ROOT_REQUEST_ID, SELECT_NODE_BY_PARENTNODEID_ROOT_REQUEST)) + return false; + if (!createAndPrepareRequest(SELECT_NODE_STATUS_BY_NODEID_REQUEST_ID, SELECT_NODE_STATUS_BY_NODEID_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_NODE_SYNCING_BY_NODEID_REQUEST_ID, SELECT_NODE_SYNCING_BY_NODEID_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_RENAMED_COLON_NODES_REQUEST_ID, SELECT_ALL_RENAMED_COLON_NODES_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_RENAMED_NODES_REQUEST_ID, SELECT_ALL_RENAMED_NODES_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ANCESTORS_NODES_REQUEST_ID, SELECT_ANCESTORS_NODES_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ANCESTORS_NODES_LOCAL_REQUEST_ID, SELECT_ANCESTORS_NODES_LOCAL_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ANCESTORS_NODES_DRIVE_REQUEST_ID, SELECT_ANCESTORS_NODES_DRIVE_REQUEST)) return false; // Sync Node - ASSERT(queryCreate(INSERT_SYNC_NODE_REQUEST_ID)); - if (!queryPrepare(INSERT_SYNC_NODE_REQUEST_ID, INSERT_SYNC_NODE_REQUEST, false, errId, error)) { - queryFree(INSERT_SYNC_NODE_REQUEST_ID); - return sqlFail(INSERT_SYNC_NODE_REQUEST_ID, error); - } - - ASSERT(queryCreate(DELETE_ALL_SYNC_NODE_BY_TYPE_REQUEST_ID)); - if (!queryPrepare(DELETE_ALL_SYNC_NODE_BY_TYPE_REQUEST_ID, DELETE_ALL_SYNC_NODE_BY_TYPE_REQUEST, false, errId, error)) { - queryFree(DELETE_ALL_SYNC_NODE_BY_TYPE_REQUEST_ID); - return sqlFail(DELETE_ALL_SYNC_NODE_BY_TYPE_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_ALL_SYNC_NODE_REQUEST_ID)); - if (!queryPrepare(SELECT_ALL_SYNC_NODE_REQUEST_ID, SELECT_ALL_SYNC_NODE_REQUEST, false, errId, error)) { - queryFree(SELECT_ALL_SYNC_NODE_REQUEST_ID); - return sqlFail(SELECT_ALL_SYNC_NODE_REQUEST_ID, error); - } + if (!createAndPrepareRequest(INSERT_SYNC_NODE_REQUEST_ID, INSERT_SYNC_NODE_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_ALL_SYNC_NODE_BY_TYPE_REQUEST_ID, DELETE_ALL_SYNC_NODE_BY_TYPE_REQUEST)) return false; + if (!createAndPrepareRequest(SELECT_ALL_SYNC_NODE_REQUEST_ID, SELECT_ALL_SYNC_NODE_REQUEST)) return false; // Upload session token table - ASSERT(queryCreate(INSERT_UPLOAD_SESSION_TOKEN_REQUEST_ID)); - if (!queryPrepare(INSERT_UPLOAD_SESSION_TOKEN_REQUEST_ID, INSERT_UPLOAD_SESSION_TOKEN_REQUEST, false, errId, error)) { - queryFree(INSERT_UPLOAD_SESSION_TOKEN_REQUEST_ID); - return sqlFail(INSERT_UPLOAD_SESSION_TOKEN_REQUEST_ID, error); - } - - ASSERT(queryCreate(SELECT_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID)); - if (!queryPrepare(SELECT_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID, SELECT_ALL_UPLOAD_SESSION_TOKEN_REQUEST, false, errId, error)) { - queryFree(SELECT_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID); - return sqlFail(SELECT_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID, error); - } - - ASSERT(queryCreate(DELETE_UPLOAD_SESSION_TOKEN_BY_DBID_REQUEST_ID)); - if (!queryPrepare(DELETE_UPLOAD_SESSION_TOKEN_BY_DBID_REQUEST_ID, DELETE_UPLOAD_SESSION_TOKEN_BY_DBID_REQUEST, false, errId, - error)) { - queryFree(DELETE_UPLOAD_SESSION_TOKEN_BY_DBID_REQUEST_ID); - return sqlFail(DELETE_UPLOAD_SESSION_TOKEN_BY_DBID_REQUEST_ID, error); - } - - ASSERT(queryCreate(DELETE_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID)); - if (!queryPrepare(DELETE_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID, DELETE_ALL_UPLOAD_SESSION_TOKEN_REQUEST, false, errId, error)) { - queryFree(DELETE_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID); - return sqlFail(DELETE_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID, error); - } + if (!createAndPrepareRequest(INSERT_UPLOAD_SESSION_TOKEN_REQUEST_ID, INSERT_UPLOAD_SESSION_TOKEN_REQUEST)) return false; + if (!createAndPrepareRequest(DELETE_UPLOAD_SESSION_TOKEN_BY_DBID_REQUEST_ID, DELETE_UPLOAD_SESSION_TOKEN_BY_DBID_REQUEST)) + return false; + if (!createAndPrepareRequest(DELETE_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID, DELETE_ALL_UPLOAD_SESSION_TOKEN_REQUEST)) + return false; + if (!createAndPrepareRequest(SELECT_ALL_UPLOAD_SESSION_TOKEN_REQUEST_ID, SELECT_ALL_UPLOAD_SESSION_TOKEN_REQUEST)) + return false; if (!initData()) { LOG_WARN(_logger, "Error in initParameters"); @@ -529,21 +429,6 @@ bool SyncDb::prepare() { return true; } -bool SyncDb::createAndPrepareRequest(const char *requestId, const char *query) { - int errId = 0; - std::string error; - - if (!queryCreate(requestId)) { - LOG_FATAL(_logger, "ENFORCE: \"queryCreate(" << requestId << ")\"."); - } - if (!queryPrepare(requestId, query, false, errId, error)) { - queryFree(requestId); - return sqlFail(requestId, error); - } - - return true; -} - bool SyncDb::upgrade(const std::string &fromVersion, const std::string & /*toVersion*/) { const std::string dbFromVersionNumber = CommonUtility::dbVersionNumber(fromVersion); @@ -554,11 +439,7 @@ bool SyncDb::upgrade(const std::string &fromVersion, const std::string & /*toVer LOG_DEBUG(_logger, "Upgrade 3.4.0 Sync DB"); // Upload session token table - ASSERT(queryCreate(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID)); - if (!queryPrepare(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, CREATE_UPLOAD_SESSION_TOKEN_TABLE, false, errId, error)) { - queryFree(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID); - return sqlFail(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, error); - } + if (!createAndPrepareRequest(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, CREATE_UPLOAD_SESSION_TOKEN_TABLE)) return false; if (!queryExec(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, errId, error)) { queryFree(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID); return sqlFail(CREATE_UPLOAD_SESSION_TOKEN_TABLE_ID, error); @@ -569,11 +450,7 @@ bool SyncDb::upgrade(const std::string &fromVersion, const std::string & /*toVer if (CommonUtility::isVersionLower(dbFromVersionNumber, "3.4.4")) { LOG_DEBUG(_logger, "Upgrade < 3.4.4 Sync DB"); - ASSERT(queryCreate(PRAGMA_WRITABLE_SCHEMA_ID)); - if (!queryPrepare(PRAGMA_WRITABLE_SCHEMA_ID, PRAGMA_WRITABLE_SCHEMA, false, errId, error)) { - queryFree(PRAGMA_WRITABLE_SCHEMA_ID); - return sqlFail(PRAGMA_WRITABLE_SCHEMA_ID, error); - } + if (!createAndPrepareRequest(PRAGMA_WRITABLE_SCHEMA_ID, PRAGMA_WRITABLE_SCHEMA)) return false; bool hasData = false; if (!queryNext(PRAGMA_WRITABLE_SCHEMA_ID, hasData)) { queryFree(PRAGMA_WRITABLE_SCHEMA_ID); @@ -581,11 +458,7 @@ bool SyncDb::upgrade(const std::string &fromVersion, const std::string & /*toVer } queryFree(PRAGMA_WRITABLE_SCHEMA_ID); - ASSERT(queryCreate(ALTER_NODE_TABLE_FK_ID)); - if (!queryPrepare(ALTER_NODE_TABLE_FK_ID, ALTER_NODE_TABLE_FK, false, errId, error)) { - queryFree(ALTER_NODE_TABLE_FK_ID); - return sqlFail(ALTER_NODE_TABLE_FK_ID, error); - } + if (!createAndPrepareRequest(ALTER_NODE_TABLE_FK_ID, ALTER_NODE_TABLE_FK)) return false; if (!queryExec(ALTER_NODE_TABLE_FK_ID, errId, error)) { queryFree(ALTER_NODE_TABLE_FK_ID); return sqlFail(ALTER_NODE_TABLE_FK_ID, error); @@ -985,9 +858,8 @@ bool SyncDb::node(ReplicaSide side, const NodeId &nodeId, DbNode &dbNode, bool & ASSERT(queryResetAndClearBindings(id)); ASSERT(queryBindValue(id, 1, nodeId)); if (!queryNext(id, found)) { - LOG_WARN(_logger, "Error getting query result: " << id.c_str() - << (side == ReplicaSide::Local ? " - nodeIdLocal=" : " - nodeIdDrive=") - << nodeId.c_str()); + LOG_WARN(_logger, "Error getting query result: " + << id << (side == ReplicaSide::Local ? " - nodeIdLocal=" : " - nodeIdDrive=") << nodeId); return false; } if (!found) { @@ -1111,58 +983,41 @@ bool SyncDb::dbIds(std::unordered_set &ids, bool &found) { bool SyncDb::path(DbNodeId dbNodeId, SyncPath &localPath, SyncPath &remotePath, bool &found) { const std::lock_guard lock(_mutex); - ASSERT(queryResetAndClearBindings(SELECT_NODE_BY_NODEID_LITE_ID)); - ASSERT(queryBindValue(SELECT_NODE_BY_NODEID_LITE_ID, 1, dbNodeId)); - if (!queryNext(SELECT_NODE_BY_NODEID_LITE_ID, found)) { - LOG_WARN(_logger, "Error getting query result: " << SELECT_NODE_BY_NODEID_LITE_ID << " - nodeId=" << dbNodeId); - return false; - } - if (!found) { - return true; - } + localPath.clear(); + remotePath.clear(); + found = false; - // Fill names' vector + // Local and remote name vector. std::vector> names; // first: local names, second: drive names - bool parentNodeDbIdIsNull; - DbNodeId parentNodeDbId; - ASSERT(queryIsNullValue(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_PARENTID, parentNodeDbIdIsNull)); - if (!parentNodeDbIdIsNull) { - ASSERT(queryInt64Value(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_PARENTID, parentNodeDbId)); - - SyncName localName; - ASSERT(querySyncNameValue(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_NAMELOCAL, localName)); - SyncName driveName; - ASSERT(querySyncNameValue(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_NAMEDRIVE, driveName)); - names.push_back({localName, driveName}); - - while (!parentNodeDbIdIsNull) { - ASSERT(queryResetAndClearBindings(SELECT_NODE_BY_NODEID_LITE_ID)); - ASSERT(queryBindValue(SELECT_NODE_BY_NODEID_LITE_ID, 1, parentNodeDbId)); - if (!queryNext(SELECT_NODE_BY_NODEID_LITE_ID, found)) { - LOG_WARN(_logger, "Error getting query result: " << SELECT_NODE_BY_NODEID_LITE_ID << " - nodeId=" << dbNodeId); - return false; - } - if (!found) { - return true; - } + std::string requestId = SELECT_ANCESTORS_NODES_REQUEST_ID; - ASSERT(queryIsNullValue(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_PARENTID, parentNodeDbIdIsNull)); - if (!parentNodeDbIdIsNull) { - ASSERT(queryInt64Value(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_PARENTID, parentNodeDbId)); + ASSERT(queryResetAndClearBindings(requestId)); + ASSERT(queryBindValue(requestId, 1, dbNodeId)); - ASSERT(querySyncNameValue(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_NAMELOCAL, localName)); - ASSERT(querySyncNameValue(SELECT_NODE_BY_NODEID_LITE_ID, SELECT_NODE_BY_NODEID_NAMEDRIVE, driveName)); - names.push_back({localName, driveName}); - } + for (;;) { + bool hasNext = false; + if (!queryNext(requestId, hasNext)) { + LOG_WARN(_logger, "Error getting query result: " << requestId); + return false; } + if (!hasNext) { + break; + } + + SyncName nameLocal; + ASSERT(querySyncNameValue(requestId, 0, nameLocal)); + SyncName nameDrive; + ASSERT(querySyncNameValue(requestId, 1, nameDrive)); // Name on the remote drive. + + names.emplace_back(nameLocal, nameDrive); } - ASSERT(queryResetAndClearBindings(SELECT_NODE_BY_NODEID_LITE_ID)); + ASSERT(queryResetAndClearBindings(requestId)); + + found = !names.empty(); // Construct path from names' vector - localPath.clear(); - remotePath.clear(); for (auto nameIt = names.rbegin(); nameIt != names.rend(); ++nameIt) { localPath.append(nameIt->first); remotePath.append(nameIt->second); @@ -1178,7 +1033,7 @@ bool SyncDb::node(DbNodeId dbNodeId, DbNode &dbNode, bool &found) { ASSERT(queryResetAndClearBindings(id)); ASSERT(queryBindValue(id, 1, dbNodeId)); if (!queryNext(id, found)) { - LOG_WARN(_logger, "Error getting query result: " << id.c_str() << " - dbNodeId=" << dbNodeId); + LOG_WARN(_logger, "Error getting query result: " << id << " - dbNodeId=" << dbNodeId); return false; } if (!found) { @@ -1288,7 +1143,7 @@ bool SyncDb::dbId(ReplicaSide side, const SyncPath &path, DbNodeId &dbNodeId, bo std::vector names; SyncPath pathTmp(path); while (pathTmp != pathTmp.root_path()) { - names.push_back(pathTmp.filename().native()); + names.emplace_back(pathTmp.filename().native()); pathTmp = pathTmp.parent_path(); } @@ -1315,9 +1170,9 @@ bool SyncDb::dbId(ReplicaSide side, const SyncPath &path, DbNodeId &dbNodeId, bo ASSERT(queryBindValue(id, 1, dbNodeId)); ASSERT(queryBindValue(id, 2, *nameIt)); if (!queryNext(id, found)) { - LOGW_WARN(_logger, L"Error getting query result: " << Utility::s2ws(id).c_str() << L" - parentNodeId=" - << std::to_wstring(dbNodeId).c_str() << L" and name=" - << (SyncName2WStr(*nameIt)).c_str()); + LOGW_WARN(_logger, L"Error getting query result: " << Utility::s2ws(id) << L" - parentNodeId=" + << std::to_wstring(dbNodeId) << L" and name=" + << (SyncName2WStr(*nameIt))); return false; } if (!found) { @@ -1358,7 +1213,7 @@ bool SyncDb::id(ReplicaSide side, const SyncPath &path, std::optional &n std::vector names; SyncPath pathTmp(path); while (pathTmp != pathTmp.root_path()) { - names.push_back(pathTmp.filename()); + names.emplace_back(pathTmp.filename()); pathTmp = pathTmp.parent_path(); } @@ -1385,9 +1240,8 @@ bool SyncDb::id(ReplicaSide side, const SyncPath &path, std::optional &n ASSERT(queryBindValue(queryId, 1, nodeDbId)); ASSERT(queryBindValue(queryId, 2, *nameIt)); if (!queryNext(queryId, found)) { - LOGW_WARN(_logger, L"Error getting query result: " << Utility::s2ws(queryId.c_str()) << L" - parentNodeId=" - << nodeDbId << L" and name=" - << (SyncName2WStr(*nameIt)).c_str()); + LOGW_WARN(_logger, L"Error getting query result: " << Utility::s2ws(queryId) << L" - parentNodeId=" << nodeDbId + << L" and name=" << Utility::formatSyncName(*nameIt)); return false; } if (!found) { @@ -1432,9 +1286,8 @@ bool SyncDb::type(ReplicaSide side, const NodeId &nodeId, NodeType &type, bool & ASSERT(queryResetAndClearBindings(id)); ASSERT(queryBindValue(id, 1, nodeId)); if (!queryNext(id, found)) { - LOG_WARN(_logger, "Error getting query result: " << id.c_str() - << (side == ReplicaSide::Local ? " - nodeIdLocal=" : " - nodeIdDrive=") - << nodeId.c_str()); + LOG_WARN(_logger, "Error getting query result: " + << id << (side == ReplicaSide::Local ? " - nodeIdLocal=" : " - nodeIdDrive=") << nodeId); return false; } if (!found) { @@ -1456,9 +1309,8 @@ bool SyncDb::size(ReplicaSide side, const NodeId &nodeId, int64_t &size, bool &f ASSERT(queryResetAndClearBindings(id)); ASSERT(queryBindValue(id, 1, nodeId)); if (!queryNext(id, found)) { - LOG_WARN(_logger, "Error getting query result: " << id.c_str() - << (side == ReplicaSide::Local ? " - nodeIdLocal=" : " - nodeIdDrive=") - << nodeId.c_str()); + LOG_WARN(_logger, "Error getting query result: " + << id << (side == ReplicaSide::Local ? " - nodeIdLocal=" : " - nodeIdDrive=") << nodeId); return false; } if (!found) { @@ -1478,9 +1330,8 @@ bool SyncDb::created(ReplicaSide side, const NodeId &nodeId, std::optionalvfsMode() != VirtualFileMode::Off) { sentry::pTraces::scoped::ResetStatus perfMonitor1(syncDbId()); diff --git a/src/server/appserver.cpp b/src/server/appserver.cpp index 0ad7c7eb6..4e5a83cca 100644 --- a/src/server/appserver.cpp +++ b/src/server/appserver.cpp @@ -1917,6 +1917,7 @@ void AppServer::onRequestReceived(int id, RequestNum num, const QByteArray ¶ } case RequestNum::UTILITY_DISPLAY_CLIENT_REPORT: { sentry::pTraces::basic::AppStart().stop(); + break; } case RequestNum::SYNC_SETSUPPORTSVIRTUALFILES: { int syncDbId = 0;