From 3e64bcd9d58fc90fee8bdaac8cda92f98a9339e4 Mon Sep 17 00:00:00 2001 From: SJ Date: Mon, 28 Jan 2013 13:40:44 +0100 Subject: [PATCH] code clean up in folder.c --- src/config.h | 2 ++ src/defs.h | 2 ++ src/import.c | 39 +++++++++++++++------------------------ src/piler.h | 4 ++-- src/pilerimport.c | 12 ++++++------ src/reindex.c | 2 +- 6 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/config.h b/src/config.h index 18e60a96..58251e1a 100644 --- a/src/config.h +++ b/src/config.h @@ -95,6 +95,8 @@ #define SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE "SELECT `id` FROM `" SQL_ATTACHMENT_TABLE "` WHERE `sig`=?" #define SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER "SELECT `piler_id`, `attachment_id` FROM " SQL_ATTACHMENT_TABLE " WHERE id=?" #define SQL_PREPARED_STMT_QUERY_ATTACHMENT "SELECT `attachment_id`, `ptr` FROM " SQL_ATTACHMENT_TABLE " WHERE piler_id=? ORDER BY attachment_id ASC" +#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(?,?)" /* Error codes */ diff --git a/src/defs.h b/src/defs.h index cccf210f..26da0013 100644 --- a/src/defs.h +++ b/src/defs.h @@ -255,6 +255,8 @@ struct __data { MYSQL_STMT *stmt_get_attachment_id_by_signature; MYSQL_STMT *stmt_get_attachment_pointer; MYSQL_STMT *stmt_query_attachment; + MYSQL_STMT *stmt_get_folder_id; + MYSQL_STMT *stmt_insert_into_folder_table; #endif #ifdef HAVE_TRE diff --git a/src/import.c b/src/import.c index 3efb5480..d64541ba 100644 --- a/src/import.c +++ b/src/import.c @@ -120,17 +120,12 @@ ENDE: } -unsigned long get_folder_id(struct session_data *sdata, char *foldername, int parent_id){ +unsigned long get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){ unsigned long id=0; - char s[SMALLBUFSIZE]; - MYSQL_STMT *stmt; MYSQL_BIND bind[2]; unsigned long len[2]; - - snprintf(s, SMALLBUFSIZE-1, "SELECT `id` FROM %s WHERE `name`=? AND `parent_id`=?", SQL_FOLDER_TABLE); - - if(prepare_a_mysql_statement(sdata, &stmt, s) == ERR) goto ENDE; + if(prepare_a_mysql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) goto ENDE; memset(bind, 0, sizeof(bind)); @@ -144,12 +139,12 @@ unsigned long get_folder_id(struct session_data *sdata, char *foldername, int pa bind[1].is_null = 0; bind[1].length = 0; - if(mysql_stmt_bind_param(stmt, bind)){ + if(mysql_stmt_bind_param(data->stmt_get_folder_id, bind)){ goto CLOSE; } - if(mysql_stmt_execute(stmt)){ + if(mysql_stmt_execute(data->stmt_get_folder_id)){ goto CLOSE; } @@ -160,19 +155,19 @@ unsigned long get_folder_id(struct session_data *sdata, char *foldername, int pa bind[0].is_null = 0; bind[0].length = 0; - if(mysql_stmt_bind_result(stmt, bind)){ + if(mysql_stmt_bind_result(data->stmt_get_folder_id, bind)){ goto CLOSE; } - if(mysql_stmt_store_result(stmt)){ + if(mysql_stmt_store_result(data->stmt_get_folder_id)){ goto CLOSE; } - mysql_stmt_fetch(stmt); + mysql_stmt_fetch(data->stmt_get_folder_id); CLOSE: - mysql_stmt_close(stmt); + mysql_stmt_close(data->stmt_get_folder_id); ENDE: @@ -180,17 +175,13 @@ ENDE: } -unsigned long add_new_folder(struct session_data *sdata, char *foldername, int parent_id){ +unsigned long add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){ unsigned long id=0; - char s[SMALLBUFSIZE]; - MYSQL_STMT *stmt; MYSQL_BIND bind[2]; unsigned long len[2]; - snprintf(s, sizeof(s)-1, "INSERT INTO %s (`name`, `parent_id`) VALUES(?,?)", SQL_FOLDER_TABLE); - - if(prepare_a_mysql_statement(sdata, &stmt, s) == ERR) goto ENDE; + if(prepare_a_mysql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) goto ENDE; memset(bind, 0, sizeof(bind)); @@ -204,21 +195,21 @@ unsigned long add_new_folder(struct session_data *sdata, char *foldername, int p bind[1].is_null = 0; bind[1].length = 0; - if(mysql_stmt_bind_param(stmt, bind)){ - syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_FOLDER_TABLE, mysql_stmt_error(stmt)); + if(mysql_stmt_bind_param(data->stmt_insert_into_folder_table, bind)){ + syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_FOLDER_TABLE, mysql_stmt_error(data->stmt_insert_into_folder_table)); goto CLOSE; } - if(mysql_stmt_execute(stmt)){ + if(mysql_stmt_execute(data->stmt_insert_into_folder_table)){ syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute error: *%s*", sdata->ttmpfile, SQL_RECIPIENT_TABLE, mysql_error(&(sdata->mysql))); goto CLOSE; } - id = mysql_stmt_insert_id(stmt); + id = mysql_stmt_insert_id(data->stmt_insert_into_folder_table); CLOSE: - mysql_stmt_close(stmt); + mysql_stmt_close(data->stmt_insert_into_folder_table); ENDE: diff --git a/src/piler.h b/src/piler.h index 7ca72e54..b81ea4a2 100644 --- a/src/piler.h +++ b/src/piler.h @@ -53,8 +53,8 @@ int retrieve_email_from_archive(struct session_data *sdata, struct __data *data, int prepare_a_mysql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s); int import_message(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg); -unsigned long get_folder_id(struct session_data *sdata, char *foldername, int parent_id); -unsigned long add_new_folder(struct session_data *sdata, char *foldername, int parent_id); +unsigned long get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id); +unsigned long add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id); int store_index_data(struct session_data *sdata, struct _state *state, struct __data *data, uint64 id, struct __config *cfg); diff --git a/src/pilerimport.c b/src/pilerimport.c index 9cac45d7..fbb39ec8 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -128,9 +128,9 @@ int import_mbox_from_dir(char *directory, struct session_data *sdata, struct __d if(S_ISREG(st.st_mode)){ if(i == 0 && data->recursive_folder_names == 1){ - folder = get_folder_id(sdata, fname, data->folder); + folder = get_folder_id(sdata, data, fname, data->folder); if(folder == 0){ - folder = add_new_folder(sdata, fname, data->folder); + folder = add_new_folder(sdata, data, fname, data->folder); if(folder == 0){ printf("error: cannot get/add folder '%s' to parent id: %d\n", fname, data->folder); @@ -204,9 +204,9 @@ int import_from_maildir(char *directory, struct session_data *sdata, struct __da return ERR; } - folder = get_folder_id(sdata, p, data->folder); + folder = get_folder_id(sdata, data, p, data->folder); if(folder == 0){ - folder = add_new_folder(sdata, p, data->folder); + folder = add_new_folder(sdata, data, p, data->folder); if(folder == 0){ printf("error: cannot get/add folder '%s' to parent id: %d\n", p, data->folder); @@ -489,10 +489,10 @@ int main(int argc, char **argv){ (void) openlog("pilerimport", LOG_PID, LOG_MAIL); if(folder){ - data.folder = get_folder_id(&sdata, folder, 0); + data.folder = get_folder_id(&sdata, &data, folder, 0); if(data.folder == 0){ - data.folder = add_new_folder(&sdata, folder, 0); + data.folder = add_new_folder(&sdata, &data, folder, 0); } if(data.folder == 0){ diff --git a/src/reindex.c b/src/reindex.c index 858ebcc8..6d48c0e0 100644 --- a/src/reindex.c +++ b/src/reindex.c @@ -205,7 +205,7 @@ int main(int argc, char **argv){ data.retention_rules = NULL; if(folder){ - data.folder = get_folder_id(&sdata, folder, 0); + data.folder = get_folder_id(&sdata, &data, folder, 0); if(data.folder == 0){ printf("error: could not get folder id for '%s'\n", folder); return 0;