preparing for database consolidation

This commit is contained in:
SJ 2013-04-28 14:18:09 +02:00
parent 8e27aea28d
commit 742aa27260
11 changed files with 49 additions and 76 deletions

View File

@ -9,6 +9,31 @@
#include <piler.h>
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));

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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");

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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

View File

@ -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;
}