From 06c929ce7e13aaff125ad7dec4c395659d0a80d9 Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Thu, 9 Dec 2021 11:32:39 +0100 Subject: [PATCH] Improved reference handling of imported messages Signed-off-by: Janos SUTO --- src/config.h | 1 + src/message.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/config.h b/src/config.h index 78e47f23..610d407b 100644 --- a/src/config.h +++ b/src/config.h @@ -108,6 +108,7 @@ #define SQL_PREPARED_STMT_GET_FOLDER_ID "SELECT `id` FROM " SQL_FOLDER_TABLE " WHERE `name`=? AND `parent_id`=?" #define SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE "INSERT INTO `" SQL_FOLDER_TABLE "` (`name`, `parent_id`) VALUES(?,?)" #define SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE "UPDATE " SQL_METADATA_TABLE " SET reference=? WHERE message_id=? AND reference=''" +#define SQL_PREPARED_STMT_GET_METADATA_REFERENCE "SELECT COUNT(*) AS count FROM " SQL_METADATA_TABLE " WHERE reference=?" #define SQL_PREPARED_STMT_GET_GUI_IMPORT_JOBS "SELECT id, type, username, password, server FROM " SQL_IMPORT_TABLE " WHERE started=0 ORDER BY id LIMIT 0,1" #define SQL_PREPARED_STMT_INSERT_FOLDER_MESSAGE "INSERT INTO " SQL_FOLDER_MESSAGE_TABLE " (`folder_id`, `id`) VALUES(?,?)" #define SQL_PREPARED_STMT_UPDATE_IMPORT_TABLE "UPDATE " SQL_IMPORT_TABLE " SET started=?, status=?, imported=? WHERE id=?" diff --git a/src/message.c b/src/message.c index b0ae85ef..f2bd3038 100644 --- a/src/message.c +++ b/src/message.c @@ -213,6 +213,33 @@ int store_meta_data(struct session_data *sdata, struct parser_state *state, stru digest_string(state->reference, &ref[0]); update_metadata_reference(sdata, state, &ref[0], cfg); } + else if(state->reference[0] == 0){ + // during import, the order of messages is often random + // check if this is a message which is already referenced + uint64 count=0; + + digest_string(state->message_id, &ref[0]); + if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_METADATA_REFERENCE) != ERR){ + p_bind_init(&sql); + + sql.sql[sql.pos] = &ref[0]; sql.type[sql.pos] = TYPE_STRING; sql.pos++; + + if(p_exec_stmt(sdata, &sql) == OK){ + p_bind_init(&sql); + + sql.sql[sql.pos] = (char *)&count; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + p_store_results(&sql); + p_fetch_results(&sql); + p_free_results(&sql); + } + } + + close_prepared_statement(&sql); + + // no reference yet + if(count <= 0) + ref[0] = 0; + } if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_META_TABLE) == ERR) return ERR;