code clean up in folder.c

This commit is contained in:
SJ 2013-01-28 13:40:44 +01:00
parent 0cabb8ce8a
commit 3e64bcd9d5
6 changed files with 28 additions and 33 deletions

View File

@ -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_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_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_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 */ /* Error codes */

View File

@ -255,6 +255,8 @@ struct __data {
MYSQL_STMT *stmt_get_attachment_id_by_signature; MYSQL_STMT *stmt_get_attachment_id_by_signature;
MYSQL_STMT *stmt_get_attachment_pointer; MYSQL_STMT *stmt_get_attachment_pointer;
MYSQL_STMT *stmt_query_attachment; MYSQL_STMT *stmt_query_attachment;
MYSQL_STMT *stmt_get_folder_id;
MYSQL_STMT *stmt_insert_into_folder_table;
#endif #endif
#ifdef HAVE_TRE #ifdef HAVE_TRE

View File

@ -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; unsigned long id=0;
char s[SMALLBUFSIZE];
MYSQL_STMT *stmt;
MYSQL_BIND bind[2]; MYSQL_BIND bind[2];
unsigned long len[2]; unsigned long len[2];
if(prepare_a_mysql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) goto ENDE;
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;
memset(bind, 0, sizeof(bind)); 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].is_null = 0;
bind[1].length = 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; goto CLOSE;
} }
if(mysql_stmt_execute(stmt)){ if(mysql_stmt_execute(data->stmt_get_folder_id)){
goto CLOSE; 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].is_null = 0;
bind[0].length = 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; goto CLOSE;
} }
if(mysql_stmt_store_result(stmt)){ if(mysql_stmt_store_result(data->stmt_get_folder_id)){
goto CLOSE; goto CLOSE;
} }
mysql_stmt_fetch(stmt); mysql_stmt_fetch(data->stmt_get_folder_id);
CLOSE: CLOSE:
mysql_stmt_close(stmt); mysql_stmt_close(data->stmt_get_folder_id);
ENDE: 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; unsigned long id=0;
char s[SMALLBUFSIZE];
MYSQL_STMT *stmt;
MYSQL_BIND bind[2]; MYSQL_BIND bind[2];
unsigned long len[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, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) goto ENDE;
if(prepare_a_mysql_statement(sdata, &stmt, s) == ERR) goto ENDE;
memset(bind, 0, sizeof(bind)); 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].is_null = 0;
bind[1].length = 0; bind[1].length = 0;
if(mysql_stmt_bind_param(stmt, bind)){ 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(stmt)); 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; 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))); syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute error: *%s*", sdata->ttmpfile, SQL_RECIPIENT_TABLE, mysql_error(&(sdata->mysql)));
goto CLOSE; goto CLOSE;
} }
id = mysql_stmt_insert_id(stmt); id = mysql_stmt_insert_id(data->stmt_insert_into_folder_table);
CLOSE: CLOSE:
mysql_stmt_close(stmt); mysql_stmt_close(data->stmt_insert_into_folder_table);
ENDE: ENDE:

View File

@ -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 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); 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 get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id);
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);
int store_index_data(struct session_data *sdata, struct _state *state, struct __data *data, uint64 id, struct __config *cfg); int store_index_data(struct session_data *sdata, struct _state *state, struct __data *data, uint64 id, struct __config *cfg);

View File

@ -128,9 +128,9 @@ int import_mbox_from_dir(char *directory, struct session_data *sdata, struct __d
if(S_ISREG(st.st_mode)){ if(S_ISREG(st.st_mode)){
if(i == 0 && data->recursive_folder_names == 1){ 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){ if(folder == 0){
folder = add_new_folder(sdata, fname, data->folder); folder = add_new_folder(sdata, data, fname, data->folder);
if(folder == 0){ if(folder == 0){
printf("error: cannot get/add folder '%s' to parent id: %d\n", fname, data->folder); 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; return ERR;
} }
folder = get_folder_id(sdata, p, data->folder); folder = get_folder_id(sdata, data, p, data->folder);
if(folder == 0){ if(folder == 0){
folder = add_new_folder(sdata, p, data->folder); folder = add_new_folder(sdata, data, p, data->folder);
if(folder == 0){ if(folder == 0){
printf("error: cannot get/add folder '%s' to parent id: %d\n", p, data->folder); 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); (void) openlog("pilerimport", LOG_PID, LOG_MAIL);
if(folder){ if(folder){
data.folder = get_folder_id(&sdata, folder, 0); data.folder = get_folder_id(&sdata, &data, folder, 0);
if(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){ if(data.folder == 0){

View File

@ -205,7 +205,7 @@ int main(int argc, char **argv){
data.retention_rules = NULL; data.retention_rules = NULL;
if(folder){ if(folder){
data.folder = get_folder_id(&sdata, folder, 0); data.folder = get_folder_id(&sdata, &data, folder, 0);
if(data.folder == 0){ if(data.folder == 0){
printf("error: could not get folder id for '%s'\n", folder); printf("error: could not get folder id for '%s'\n", folder);
return 0; return 0;