Skip to content

Commit

Permalink
prepared statement fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuto committed Jun 29, 2013
1 parent 1b693f3 commit a1d11d0
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#define VERSION "0.1.24-master-branch"

#define BUILD 818
#define BUILD 819

#define HOSTID "mailarchiver"

Expand Down
11 changes: 6 additions & 5 deletions src/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
state = parse_message(sdata, 1, data, cfg);
post_parse(sdata, &state, cfg);

if(sdata->hdr_len < 10){
printf("invalid message, hdr_len: %d\n", sdata->hdr_len);
return ERR;
}

if(sdata->sent <= 0 && sdata->delivered > 0) sdata->sent = sdata->delivered;

if(sdata->sent > sdata->now) sdata->sent = sdata->now;
Expand All @@ -94,6 +89,12 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
}
else {
make_digests(sdata, cfg);

if(sdata->hdr_len < 10){
printf("%s: invalid message, hdr_len: %d\n", filename, sdata->hdr_len);
return ERR;
}

rc = process_message(sdata, &state, data, cfg);
}

Expand Down
9 changes: 9 additions & 0 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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;

p_bind_init(data);
data->sql[data->pos] = message_id; data->type[data->pos] = TYPE_STRING; data->pos++;
Expand All @@ -83,6 +84,8 @@ uint64 get_metaid_by_messageid(struct session_data *sdata, struct __data *data,
p_free_results(data->stmt_get_meta_id_by_message_id);

CLOSE:
mysql_stmt_close(data->stmt_get_meta_id_by_message_id);

return id;
}

Expand All @@ -91,6 +94,8 @@ 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;

p = to;
do {
p = split_str(p, " ", puf, sizeof(puf)-1);
Expand All @@ -115,6 +120,7 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to,

} while(p);

mysql_stmt_close(data->stmt_insert_into_rcpt_table);

if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: added %d recipients", sdata->ttmpfile, n);

Expand All @@ -125,6 +131,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;

p_bind_init(data);

Expand All @@ -133,6 +140,8 @@ int update_metadata_reference(struct session_data *sdata, struct _state *state,

if(p_exec_query(sdata, data->stmt_update_metadata_reference, data) == OK) ret = OK;

mysql_stmt_close(data->stmt_update_metadata_reference);

if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: updated meta reference for '%s', rc=%d", sdata->ttmpfile, state->reference, ret);

return ret;
Expand Down
21 changes: 0 additions & 21 deletions src/mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,29 +235,8 @@ void insert_offset(struct session_data *sdata, int server_id){
}


int create_prepared_statements(struct session_data *sdata, struct __data *data){
int rc = OK;

data->stmt_get_meta_id_by_message_id = NULL;
data->stmt_insert_into_rcpt_table = NULL;
data->stmt_update_metadata_reference = NULL;

if(prepare_sql_statement(sdata, &(data->stmt_get_meta_id_by_message_id), SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID) == ERR) rc = ERR;
if(prepare_sql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) rc = ERR;
if(prepare_sql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) rc = ERR;

return rc;
}


void close_prepared_statement(MYSQL_STMT *stmt){
if(stmt) mysql_stmt_close(stmt);
}


void close_prepared_statements(struct __data *data){
if(data->stmt_get_meta_id_by_message_id) mysql_stmt_close(data->stmt_get_meta_id_by_message_id);
if(data->stmt_insert_into_rcpt_table) mysql_stmt_close(data->stmt_insert_into_rcpt_table);
if(data->stmt_update_metadata_reference) mysql_stmt_close(data->stmt_update_metadata_reference);
}

8 changes: 0 additions & 8 deletions src/pilerimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,6 @@ int main(int argc, char **argv){
if(open_database(&sdata, &cfg) == ERR) return 0;


if(create_prepared_statements(&sdata, &data) == ERR){
rc = ERR;
goto ENDE;
}

setlocale(LC_CTYPE, cfg.locale);

(void) openlog("pilerimport", LOG_PID, LOG_MAIL);
Expand Down Expand Up @@ -576,9 +571,6 @@ int main(int argc, char **argv){
free_rule(data.archiving_rules);
free_rule(data.retention_rules);

ENDE:
close_prepared_statements(&data);

close_database(&sdata);

if(quiet == 0) printf("\n");
Expand Down
8 changes: 0 additions & 8 deletions src/retr.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cf
syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT);
#endif

if(db_conn == 1 && create_prepared_statements(&sdata, data) == ERR){
close_prepared_statements(data);
close_database(&sdata);
db_conn = 0;
}


if(db_conn == 0){
send(new_sd, SMTP_RESP_421_ERR_TMP, strlen(SMTP_RESP_421_ERR_TMP), 0);
syslog(LOG_PRIORITY, "cannot make prepared statement");
Expand Down Expand Up @@ -264,7 +257,6 @@ int handle_pilerget_request(int new_sd, struct __data *data, struct __config *cf
QUITTING:

#ifdef NEED_MYSQL
close_prepared_statements(data);
close_database(&sdata);
#endif

Expand Down
8 changes: 0 additions & 8 deletions src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
syslog(LOG_PRIORITY, "%s", ERR_MYSQL_CONNECT);
#endif

if(db_conn == 1 && create_prepared_statements(&sdata, data) == ERR){
close_prepared_statements(data);
close_database(&sdata);
db_conn = 0;
}


if(db_conn == 0){
send(new_sd, SMTP_RESP_421_ERR_TMP, strlen(SMTP_RESP_421_ERR_TMP), 0);
syslog(LOG_PRIORITY, "cannot make prepared statement");
Expand Down Expand Up @@ -563,7 +556,6 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
update_counters(&sdata, data, &counters, cfg);

#ifdef NEED_MYSQL
close_prepared_statements(data);
close_database(&sdata);
#endif

Expand Down
2 changes: 0 additions & 2 deletions src/sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ void p_bind_init(struct __data *data);
uint64 p_get_insert_id(MYSQL_STMT *stmt);
int p_get_affected_rows(MYSQL_STMT *stmt);
void close_prepared_statement(MYSQL_STMT *stmt);
int create_prepared_statements(struct session_data *sdata, struct __data *data);
void close_prepared_statements(struct __data *data);


#endif /* _PILER_H */
Expand Down

0 comments on commit a1d11d0

Please sign in to comment.