From ed0a8bafd542c8443a79fb15a6c1ed84112a64e6 Mon Sep 17 00:00:00 2001 From: SJ Date: Fri, 24 Jul 2015 14:28:31 +0200 Subject: [PATCH] logging option to the prepared statement --- src/attachment.c | 8 ++++---- src/counters.c | 2 +- src/import.c | 8 ++++---- src/message.c | 10 +++++----- src/mydomains.c | 2 +- src/mysql.c | 4 +++- src/piler.c | 4 ++-- src/piler.h | 4 ++-- src/pilerexport.c | 2 +- src/pilerimport.c | 18 +++++++++--------- src/pilerpurge.c | 8 ++++---- src/reindex.c | 10 +++++----- src/rules.c | 4 ++-- src/rules.h | 2 +- src/sql.h | 2 +- src/test.c | 4 ++-- 16 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/attachment.c b/src/attachment.c index 323f2ecc..ae77c6ba 100644 --- a/src/attachment.c +++ b/src/attachment.c @@ -21,8 +21,8 @@ int store_attachments(struct session_data *sdata, struct _state *state, struct _ int i, rc=1, found, affected_rows; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_attachment_table), SQL_PREPARED_STMT_INSERT_INTO_ATTACHMENT_TABLE) == ERR) return rc; - if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_id_by_signature), SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE) == ERR) return rc; + if(prepare_sql_statement(sdata, &(data->stmt_insert_into_attachment_table), SQL_PREPARED_STMT_INSERT_INTO_ATTACHMENT_TABLE, cfg) == ERR) return rc; + if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_id_by_signature), SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE, cfg) == ERR) return rc; for(i=1; i<=state->n_attachments; i++){ @@ -96,7 +96,7 @@ CLOSE: int query_attachment_pointers(struct session_data *sdata, struct __data *data, uint64 ptr, char *piler_id, int *id, struct __config *cfg){ int rc=0; - if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_pointer), SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER) == ERR) return rc; + if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_pointer), SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER, cfg) == ERR) return rc; p_bind_init(data); @@ -130,7 +130,7 @@ int query_attachments(struct session_data *sdata, struct __data *data, struct pt for(i=0; istmt_query_attachment), SQL_PREPARED_STMT_QUERY_ATTACHMENT) == ERR) goto ENDE; + if(prepare_sql_statement(sdata, &(data->stmt_query_attachment), SQL_PREPARED_STMT_QUERY_ATTACHMENT, cfg) == ERR) goto ENDE; p_bind_init(data); diff --git a/src/counters.c b/src/counters.c index 20fc429d..de0e6d68 100644 --- a/src/counters.c +++ b/src/counters.c @@ -20,7 +20,7 @@ struct __counters load_counters(struct session_data *sdata, struct __data *data, snprintf(buf, SMALLBUFSIZE-1, "SELECT `rcvd`, `virus`, `duplicate`, `ignore`, `size`, `stored_size` FROM `%s`", SQL_COUNTER_TABLE); - if(prepare_sql_statement(sdata, &(data->stmt_generic), buf) == ERR) return counters; + if(prepare_sql_statement(sdata, &(data->stmt_generic), buf, cfg) == ERR) return counters; p_bind_init(data); diff --git a/src/import.c b/src/import.c index e5948edc..647d695a 100644 --- a/src/import.c +++ b/src/import.c @@ -142,10 +142,10 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da } -int get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){ +int get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id, struct __config *cfg){ int id=ERR_FOLDER; - if(prepare_sql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) return id; + if(prepare_sql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID, cfg) == ERR) return id; p_bind_init(data); data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; @@ -167,12 +167,12 @@ int get_folder_id(struct session_data *sdata, struct __data *data, char *foldern } -int add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){ +int add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id, struct __config *cfg){ int id=ERR_FOLDER; if(foldername == NULL) return id; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) return id; + if(prepare_sql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE, cfg) == ERR) return id; p_bind_init(data); data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; diff --git a/src/message.c b/src/message.c index 97986b98..b6eb523e 100644 --- a/src/message.c +++ b/src/message.c @@ -25,7 +25,7 @@ int store_index_data(struct session_data *sdata, struct _state *state, struct __ if(*subj == ' ') subj++; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_sphinx_table), SQL_PREPARED_STMT_INSERT_INTO_SPHINX_TABLE) == ERR) return rc; + if(prepare_sql_statement(sdata, &(data->stmt_insert_into_sphinx_table), SQL_PREPARED_STMT_INSERT_INTO_SPHINX_TABLE, cfg) == ERR) return rc; fix_email_address_for_sphinx(state->b_from); @@ -63,7 +63,7 @@ int store_index_data(struct session_data *sdata, struct _state *state, struct __ uint64 get_metaid_by_messageid(struct session_data *sdata, struct __data *data, char *message_id, struct __config *cfg){ uint64 id=0; - if(prepare_sql_statement(sdata, &(data->stmt_get_meta_id_by_message_id), SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID) == ERR) return id; + if(prepare_sql_statement(sdata, &(data->stmt_get_meta_id_by_message_id), SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID, cfg) == ERR) return id; p_bind_init(data); data->sql[data->pos] = message_id; data->type[data->pos] = TYPE_STRING; data->pos++; @@ -91,7 +91,7 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to, int ret=OK, n=0; char *p, *q, puf[SMALLBUFSIZE]; - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) return ret; + if(prepare_sql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE, cfg) == ERR) return ret; p = to; do { @@ -128,7 +128,7 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to, int update_metadata_reference(struct session_data *sdata, struct _state *state, struct __data *data, char *ref, struct __config *cfg){ int ret = ERR; - if(prepare_sql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) return ret; + if(prepare_sql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE, cfg) == ERR) return ret; p_bind_init(data); @@ -165,7 +165,7 @@ int store_meta_data(struct session_data *sdata, struct _state *state, struct __d } - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_meta_table), SQL_PREPARED_STMT_INSERT_INTO_META_TABLE) == ERR) return ERR; + if(prepare_sql_statement(sdata, &(data->stmt_insert_into_meta_table), SQL_PREPARED_STMT_INSERT_INTO_META_TABLE, cfg) == ERR) return ERR; memset(s2, 0, sizeof(s2)); diff --git a/src/mydomains.c b/src/mydomains.c index efa62a11..8bcb93cd 100644 --- a/src/mydomains.c +++ b/src/mydomains.c @@ -17,7 +17,7 @@ void load_mydomains(struct session_data *sdata, struct __data *data, struct __co memset(s, 0, sizeof(s)); - if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_DOMAINS) == ERR) return; + if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_DOMAINS, cfg) == ERR) return; p_bind_init(data); diff --git a/src/mysql.c b/src/mysql.c index 256efff6..2f9ccc74 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -208,7 +208,9 @@ int p_get_affected_rows(MYSQL_STMT *stmt){ } -int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s){ +int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s, struct __config *cfg){ + + if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: sql=%s", sdata->ttmpfile, s); *stmt = mysql_stmt_init(&(sdata->mysql)); if(!*stmt){ diff --git a/src/piler.c b/src/piler.c index 2b127361..0711cf99 100644 --- a/src/piler.c +++ b/src/piler.c @@ -333,8 +333,8 @@ void initialise_configuration(){ return; } - load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE); - load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE); + load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE, &cfg); + load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE, &cfg); load_mydomains(&sdata, &data, &cfg); diff --git a/src/piler.h b/src/piler.h index 8d098aed..7c16f208 100644 --- a/src/piler.h +++ b/src/piler.h @@ -54,8 +54,8 @@ int retrieve_email_from_archive(struct session_data *sdata, struct __data *data, int file_from_archive_to_network(char *filename, int sd, int tls_enable, struct __data *data, struct __config *cfg); int import_message(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg); -int get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id); -int add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id); +int get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id, struct __config *cfg); +int add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id, struct __config *cfg); int store_index_data(struct session_data *sdata, struct _state *state, struct __data *data, uint64 id, struct __config *cfg); diff --git a/src/pilerexport.c b/src/pilerexport.c index de997459..806019d6 100644 --- a/src/pilerexport.c +++ b/src/pilerexport.c @@ -144,7 +144,7 @@ int export_emails_matching_to_query(struct session_data *sdata, struct __data *d int rc=0; - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return ERR; + if(prepare_sql_statement(sdata, &(data->stmt_generic), s, cfg) == ERR) return ERR; p_bind_init(data); diff --git a/src/pilerimport.c b/src/pilerimport.c index c5fd53fa..212eec8e 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -134,9 +134,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, data, fname, data->folder); + folder = get_folder_id(sdata, data, fname, data->folder, cfg); if(folder == ERR_FOLDER){ - folder = add_new_folder(sdata, data, fname, data->folder); + folder = add_new_folder(sdata, data, fname, data->folder, cfg); if(folder == ERR_FOLDER){ printf("error: cannot get/add folder '%s' to parent id: %d\n", fname, data->folder); @@ -210,9 +210,9 @@ int import_from_maildir(char *directory, struct session_data *sdata, struct __da return ERR; } - folder = get_folder_id(sdata, data, p, data->folder); + folder = get_folder_id(sdata, data, p, data->folder, cfg); if(folder == ERR_FOLDER){ - folder = add_new_folder(sdata, data, p, data->folder); + folder = add_new_folder(sdata, data, p, data->folder, cfg); if(folder == ERR_FOLDER){ printf("error: cannot get/add folder '%s' to parent id: %d\n", p, data->folder); @@ -415,7 +415,7 @@ int read_gui_import_data(struct session_data *sdata, struct __data *data, char * memset(s_password, 0, sizeof(s_password)); memset(s_server, 0, sizeof(s_server)); - if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_GUI_IMPORT_JOBS) == ERR) return ERR; + if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_GUI_IMPORT_JOBS, cfg) == ERR) return ERR; p_bind_init(data); @@ -689,10 +689,10 @@ int main(int argc, char **argv){ #endif if(folder){ - data.folder = get_folder_id(&sdata, &data, folder, 0); + data.folder = get_folder_id(&sdata, &data, folder, 0, &cfg); if(data.folder == ERR_FOLDER){ - data.folder = add_new_folder(&sdata, &data, folder, 0); + data.folder = add_new_folder(&sdata, &data, folder, 0, &cfg); } if(data.folder == ERR_FOLDER){ @@ -703,8 +703,8 @@ int main(int argc, char **argv){ } - load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE); - load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE); + load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE, &cfg); + load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE, &cfg); load_mydomains(&sdata, &data, &cfg); diff --git a/src/pilerpurge.c b/src/pilerpurge.c index ae75231f..ff3acff5 100644 --- a/src/pilerpurge.c +++ b/src/pilerpurge.c @@ -33,7 +33,7 @@ unsigned long purged_size=0; int is_purge_allowed(struct session_data *sdata, struct __data *data, struct __config *cfg){ int rc=0; - if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE) == ERR) return rc; + if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE, cfg) == ERR) return rc; p_bind_init(data); @@ -124,7 +124,7 @@ int remove_attachments(char *in, struct session_data *sdata, struct __data *data in[strlen(in)-1] = '\0'; snprintf(a, len-1, "%s%s", SQL_STMT_SELECT_NON_REFERENCED_ATTACHMENTS, in); - if(prepare_sql_statement(sdata, &(data->stmt_select_non_referenced_attachments), a) == ERR){ free(a); return n; } + if(prepare_sql_statement(sdata, &(data->stmt_select_non_referenced_attachments), a, cfg) == ERR){ free(a); return n; } if(dryrun == 1) printf("running sql query: *%s*\n\n", a); @@ -248,7 +248,7 @@ int purge_messages_round1(struct session_data *sdata, struct __data *data, char if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "purge sql: *%s*", s); - if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s) == ERR) return purged; + if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s, cfg) == ERR) return purged; p_bind_init(data); @@ -308,7 +308,7 @@ int purge_messages_with_attachments(struct session_data *sdata, struct __data *d if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "purge sql: *%s*", s); - if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s) == ERR) return purged; + if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s, cfg) == ERR) return purged; p_bind_init(data); if(p_exec_query(sdata, data->stmt_select_from_meta_table, data) == ERR) goto ENDE; diff --git a/src/reindex.c b/src/reindex.c index 1c183275..3af3cf68 100644 --- a/src/reindex.c +++ b/src/reindex.c @@ -42,14 +42,14 @@ void p_clean_exit(char *msg, int rc){ } -uint64 get_max_meta_id(struct session_data *sdata, struct __data *data){ +uint64 get_max_meta_id(struct session_data *sdata, struct __data *data, struct __config *cfg){ char s[SMALLBUFSIZE]; uint64 id=0; snprintf(s, sizeof(s)-1, "SELECT MAX(`id`) FROM %s", SQL_METADATA_TABLE); - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return id; + if(prepare_sql_statement(sdata, &(data->stmt_generic), s, cfg) == ERR) return id; p_bind_init(data); @@ -85,7 +85,7 @@ uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct __data * snprintf(s, sizeof(s)-1, "SELECT `id`, `piler_id`, `arrived`, `sent` FROM %s WHERE (id BETWEEN %llu AND %llu) AND `deleted`=0", SQL_METADATA_TABLE, from_id, to_id); - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return reindexed; + if(prepare_sql_statement(sdata, &(data->stmt_generic), s, cfg) == ERR) return reindexed; p_bind_init(data); @@ -233,7 +233,7 @@ int main(int argc, char **argv){ } if(folder){ - data.folder = get_folder_id(&sdata, &data, folder, 0); + data.folder = get_folder_id(&sdata, &data, folder, 0, &cfg); if(data.folder == 0){ printf("error: could not get folder id for '%s'\n", folder); return 0; @@ -245,7 +245,7 @@ int main(int argc, char **argv){ if(all == 1){ from_id = 1; - to_id = get_max_meta_id(&sdata, &data); + to_id = get_max_meta_id(&sdata, &data, &cfg); } n = retrieve_email_by_metadata_id(&sdata, &data, from_id, to_id, &cfg); diff --git a/src/rules.c b/src/rules.c index 2e3c7705..b7e48f1d 100644 --- a/src/rules.c +++ b/src/rules.c @@ -10,7 +10,7 @@ #include "rules.h" -void load_rules(struct session_data *sdata, struct __data *data, struct node *xhash[], char *table){ +void load_rules(struct session_data *sdata, struct __data *data, struct node *xhash[], char *table, struct __config *cfg){ char s[SMALLBUFSIZE]; char domain[SMALLBUFSIZE], from[SMALLBUFSIZE], to[SMALLBUFSIZE], subject[SMALLBUFSIZE], body[SMALLBUFSIZE], _size[SMALLBUFSIZE], attachment_name[SMALLBUFSIZE], attachment_type[SMALLBUFSIZE], _attachment_size[SMALLBUFSIZE]; int size=0, attachment_size=0, spam=0, days=0; @@ -28,7 +28,7 @@ void load_rules(struct session_data *sdata, struct __data *data, struct node *xh snprintf(s, sizeof(s)-1, "SELECT `domain`, `from`, `to`, `subject`, `body`, `_size`, `size`, `attachment_name`, `attachment_type`, `_attachment_size`, `attachment_size`, `spam`, `days` FROM `%s`", table); - if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return; + if(prepare_sql_statement(sdata, &(data->stmt_generic), s, cfg) == ERR) return; p_bind_init(data); diff --git a/src/rules.h b/src/rules.h index f0d63116..799d5ef5 100644 --- a/src/rules.h +++ b/src/rules.h @@ -7,7 +7,7 @@ #include "defs.h" -void load_rules(struct session_data *sdata, struct __data *data, struct node *xhash[], char *table); +void load_rules(struct session_data *sdata, struct __data *data, struct node *xhash[], char *table, struct __config *cfg); int append_rule(struct node *xhash[], char *domain, char *from, char *to, char *subject, char *body, char *_size, int size, char *attachment_name, char *attachment_type, char *_attachment_size, int attachment_size, int spam, int days, struct __data *data); struct rule *create_rule_item(char *domain, char *from, char *to, char *subject, char *body, char *_size, int size, char *attachment_name, char *attachment_type, char *_attachment_size, int attachment_size, int spam, int days, struct __data *data); char *check_againt_ruleset(struct node *xhash[], struct _state *state, int size, int spam); diff --git a/src/sql.h b/src/sql.h index d1d3dbb4..07c214ce 100644 --- a/src/sql.h +++ b/src/sql.h @@ -8,7 +8,7 @@ int open_database(struct session_data *sdata, struct __config *cfg); void close_database(struct session_data *sdata); -int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s); +int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s, struct __config *cfg); void p_query(struct session_data *sdata, char *s); int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct __data *data); int p_store_results(struct session_data *sdata, MYSQL_STMT *stmt, struct __data *data); diff --git a/src/test.c b/src/test.c index 1d70ba3c..756f0e19 100644 --- a/src/test.c +++ b/src/test.c @@ -66,8 +66,8 @@ int main(int argc, char **argv){ load_mydomains(&sdata, &data, &cfg); - load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE); - load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE); + load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE, &cfg); + load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE, &cfg); init_session_data(&sdata, &cfg);