From 742aa272604cc4c5aebac4d659cba840396c1b65 Mon Sep 17 00:00:00 2001 From: SJ Date: Sun, 28 Apr 2013 14:18:09 +0200 Subject: [PATCH] preparing for database consolidation --- src/mysql.c | 25 +++++++++++++++++++++++++ src/piler.c | 6 ++---- src/piler.h | 2 ++ src/pilerexport.c | 8 ++------ src/pilerget.c | 12 ++---------- src/pilerimport.c | 14 ++++---------- src/pilerpurge.c | 11 ++--------- src/reindex.c | 8 ++------ src/retr.c | 14 +++----------- src/session.c | 13 +++---------- src/test.c | 12 ++---------- 11 files changed, 49 insertions(+), 76 deletions(-) diff --git a/src/mysql.c b/src/mysql.c index d1517f0d..5b61981f 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -9,6 +9,31 @@ #include +int open_database(struct session_data *sdata, struct __config *cfg){ + int rc=1; + + mysql_init(&(sdata->mysql)); + + mysql_options(&(sdata->mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg->mysql_connect_timeout); + mysql_options(&(sdata->mysql), MYSQL_OPT_RECONNECT, (const char*)&rc); + + if(mysql_real_connect(&(sdata->mysql), cfg->mysqlhost, cfg->mysqluser, cfg->mysqlpwd, cfg->mysqldb, cfg->mysqlport, cfg->mysqlsocket, 0) == 0){ + printf("cant connect to mysql server\n"); + return ERR; + } + + mysql_real_query(&(sdata->mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); + mysql_real_query(&(sdata->mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); + + return OK; +} + + +void close_database(struct session_data *sdata){ + mysql_close(&(sdata->mysql)); +} + + int prepare_a_mysql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s){ *stmt = mysql_stmt_init(&(sdata->mysql)); diff --git a/src/piler.c b/src/piler.c index 85136f3c..ed02f756 100644 --- a/src/piler.c +++ b/src/piler.c @@ -317,9 +317,7 @@ void initialise_configuration(){ } #endif - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout); - if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){ + if(open_database(&sdata, &cfg) == ERR){ syslog(LOG_PRIORITY, "cannot connect to mysql server"); return; } @@ -331,7 +329,7 @@ void initialise_configuration(){ if(cfg.server_id > 0) insert_offset(&sdata, cfg.server_id); - mysql_close(&(sdata.mysql)); + close_database(&sdata); syslog(LOG_PRIORITY, "reloaded config: %s", configfile); diff --git a/src/piler.h b/src/piler.h index e9e53c7f..28ae52ac 100644 --- a/src/piler.h +++ b/src/piler.h @@ -53,6 +53,8 @@ void update_counters(struct session_data *sdata, struct __data *data, struct __c int retrieve_email_from_archive(struct session_data *sdata, struct __data *data, FILE *dest, struct __config *cfg); int file_from_archive_to_network(char *filename, int sd, int tls_enable, struct __data *data, struct __config *cfg); +int open_database(struct session_data *sdata, struct __config *cfg); +void close_database(struct session_data *sdata); 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); diff --git a/src/pilerexport.c b/src/pilerexport.c index b318da06..95d6a46a 100644 --- a/src/pilerexport.c +++ b/src/pilerexport.c @@ -405,14 +405,10 @@ GO: init_session_data(&sdata, &cfg); - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout); - if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){ + if(open_database(&sdata, &cfg) == ERR){ p_clean_exit("cannot connect to mysql server", 1); } - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); rc = export_emails_matching_to_query(&sdata, &data, query, &cfg); @@ -420,7 +416,7 @@ GO: free(query); - mysql_close(&(sdata.mysql)); + close_database(&sdata); return 0; } diff --git a/src/pilerget.c b/src/pilerget.c index af885d3d..0d166fde 100644 --- a/src/pilerget.c +++ b/src/pilerget.c @@ -40,15 +40,7 @@ int main(int argc, char **argv){ } - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout); - if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){ - printf("cannot connect to mysql server\n"); - return 0; - } - - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); + if(open_database(&sdata, &cfg) == ERR) return 0; snprintf(sdata.ttmpfile, SMALLBUFSIZE-1, "%s", argv[1]); @@ -56,7 +48,7 @@ int main(int argc, char **argv){ retrieve_email_from_archive(&sdata, &data, stdout, &cfg); - mysql_close(&(sdata.mysql)); + close_database(&sdata); return 0; } diff --git a/src/pilerimport.c b/src/pilerimport.c index 32355a7f..16f99afd 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -523,15 +523,9 @@ int main(int argc, char **argv){ return ERR; } - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout); - if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){ - printf("error: cant connect to mysql server\n"); - return ERR; - } - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); + if(open_database(&sdata, &cfg) == ERR) return 0; + if(create_prepared_statements(&sdata, &data) == ERR){ rc = ERR; @@ -555,7 +549,7 @@ int main(int argc, char **argv){ if(data.folder == 0){ printf("error: cannot get/add folder '%s'\n", folder); - mysql_close(&(sdata.mysql)); + close_database(&sdata); return 0; } @@ -585,7 +579,7 @@ int main(int argc, char **argv){ ENDE: close_prepared_statements(&data); - mysql_close(&(sdata.mysql)); + close_database(&sdata); if(quiet == 0) printf("\n"); diff --git a/src/pilerpurge.c b/src/pilerpurge.c index 31d36330..ae419fb2 100644 --- a/src/pilerpurge.c +++ b/src/pilerpurge.c @@ -336,15 +336,8 @@ int main(int argc, char **argv){ cfg = read_config(configfile); - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout); - if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){ - printf("cannot connect to mysql server\n"); - return 0; - } + if(open_database(&sdata, &cfg) == ERR) return 0; - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); setlocale(LC_CTYPE, cfg.locale); @@ -359,7 +352,7 @@ int main(int argc, char **argv){ } else printf("purge is not allowed by configuration, enable_purge=%d\n", i); - mysql_close(&(sdata.mysql)); + close_database(&sdata); return 0; } diff --git a/src/reindex.c b/src/reindex.c index 7fe98830..64bfb9e7 100644 --- a/src/reindex.c +++ b/src/reindex.c @@ -215,14 +215,10 @@ int main(int argc, char **argv){ init_session_data(&sdata, &cfg); - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout); - if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){ + if(open_database(&sdata, &cfg) == ERR){ p_clean_exit("cannot connect to mysql server", 1); } - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); load_mydomains(&sdata, &data, &cfg); @@ -235,7 +231,7 @@ int main(int argc, char **argv){ printf("put %llu messages to %s table for reindexing\n", n, SQL_SPHINX_TABLE); - mysql_close(&(sdata.mysql)); + close_database(&sdata); return 0; } diff --git a/src/retr.c b/src/retr.c index f7bf3fca..95e996f3 100644 --- a/src/retr.c +++ b/src/retr.c @@ -110,7 +110,6 @@ int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cf char *q, buf[MAXBUFSIZE], puf[MAXBUFSIZE], muf[TINYBUFSIZE], resp[MAXBUFSIZE]; struct session_data sdata; int db_conn=0; - int rc; struct __counters counters; struct timezone tz; @@ -140,15 +139,8 @@ int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cf db_conn = 0; #ifdef NEED_MYSQL - rc = 1; - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg->mysql_connect_timeout); - mysql_options(&(sdata.mysql), MYSQL_OPT_RECONNECT, (const char*)&rc); - - if(mysql_real_connect(&(sdata.mysql), cfg->mysqlhost, cfg->mysqluser, cfg->mysqlpwd, cfg->mysqldb, cfg->mysqlport, cfg->mysqlsocket, 0)){ + if(open_database(&sdata, cfg) == OK){ db_conn = 1; - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); } else syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT); @@ -156,7 +148,7 @@ int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cf if(db_conn == 1 && create_prepared_statements(&sdata, data) == ERR){ close_prepared_statements(data); - mysql_close(&(sdata.mysql)); + close_database(&sdata); db_conn = 0; } @@ -273,7 +265,7 @@ QUITTING: #ifdef NEED_MYSQL close_prepared_statements(data); - mysql_close(&(sdata.mysql)); + close_database(&sdata); #endif SSL_shutdown(data->ssl); diff --git a/src/session.c b/src/session.c index 1cc59908..29fc555f 100644 --- a/src/session.c +++ b/src/session.c @@ -65,15 +65,8 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ db_conn = 0; #ifdef NEED_MYSQL - rc = 1; - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg->mysql_connect_timeout); - mysql_options(&(sdata.mysql), MYSQL_OPT_RECONNECT, (const char*)&rc); - - if(mysql_real_connect(&(sdata.mysql), cfg->mysqlhost, cfg->mysqluser, cfg->mysqlpwd, cfg->mysqldb, cfg->mysqlport, cfg->mysqlsocket, 0)){ + if(open_database(&sdata, cfg) == OK){ db_conn = 1; - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); } else syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT); @@ -81,7 +74,7 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ if(db_conn == 1 && create_prepared_statements(&sdata, data) == ERR){ close_prepared_statements(data); - mysql_close(&(sdata.mysql)); + close_database(&sdata); db_conn = 0; } @@ -567,7 +560,7 @@ QUITTING: #ifdef NEED_MYSQL close_prepared_statements(data); - mysql_close(&(sdata.mysql)); + close_database(&sdata); #endif #ifdef HAVE_STARTTLS diff --git a/src/test.c b/src/test.c index 5cb99968..2c43f71e 100644 --- a/src/test.c +++ b/src/test.c @@ -37,15 +37,7 @@ int main(int argc, char **argv){ cfg = read_config(CONFIG_FILE); - mysql_init(&(sdata.mysql)); - mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout); - if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){ - printf("cant connect to mysql server\n"); - return 0; - } - - mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8")); - mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8")); + if(open_database(&sdata, &cfg) == ERR) return 0; printf("locale: %s\n", setlocale(LC_MESSAGES, cfg.locale)); setlocale(LC_CTYPE, cfg.locale); @@ -119,7 +111,7 @@ int main(int argc, char **argv){ printf("\n\n"); - mysql_close(&(sdata.mysql)); + close_database(&sdata); return 0; }