app_voicemail: Cleanup ODBC connection handling
The primary focus of this patch is adding a missing call to ast_odbc_release_obj(), but is also a general cleanup of the ODBC related code in app_voicemail. ASTERISK-27093 #close Change-Id: I8e285142eaeb3146b4287a928276b70db76c902b
This commit is contained in:
parent
b62a3f0a67
commit
950b39a4f5
|
@ -3779,12 +3779,12 @@ static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
|
|||
SQLHSTMT stmt;
|
||||
|
||||
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Alloc Handle failed!\n");
|
||||
return NULL;
|
||||
}
|
||||
res = SQLPrepare(stmt, (unsigned char *) gps->sql, SQL_NTS);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Prepare failed![%s]\n", gps->sql);
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
return NULL;
|
||||
|
@ -3862,16 +3862,21 @@ static int retrieve_file(char *dir, int msgnum)
|
|||
char msgnums[80];
|
||||
char *argv[] = { dir, msgnums };
|
||||
struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
|
||||
|
||||
struct odbc_obj *obj;
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ast_copy_string(fmt, vmfmts, sizeof(fmt));
|
||||
c = strchr(fmt, '|');
|
||||
if (c)
|
||||
*c = '\0';
|
||||
if (!strcasecmp(fmt, "wav49"))
|
||||
strcpy(fmt, "WAV");
|
||||
|
||||
snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
|
||||
if (msgnum > -1)
|
||||
make_file(fn, sizeof(fn), dir, msgnum);
|
||||
|
@ -3883,43 +3888,38 @@ static int retrieve_file(char *dir, int msgnum)
|
|||
|
||||
if (!(f = fopen(full_fn, "w+"))) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to open/create '%s'\n", full_fn);
|
||||
goto yuck;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
snprintf(full_fn, sizeof(full_fn), "%s.%s", fn, fmt);
|
||||
snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE dir=? AND msgnum=?", odbc_table);
|
||||
|
||||
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
|
||||
if (!stmt) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
res = SQLFetch(stmt);
|
||||
if (res == SQL_NO_DATA) {
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
} else if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
if (res != SQL_NO_DATA) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
}
|
||||
goto bail_with_handle;
|
||||
}
|
||||
|
||||
fd = open(full_fn, O_RDWR | O_CREAT | O_TRUNC, VOICEMAIL_FILE_MODE);
|
||||
if (fd < 0) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to write '%s': %s\n", full_fn, strerror(errno));
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
|
||||
res = SQLNumResultCols(stmt, &colcount);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
if (f)
|
||||
|
||||
fprintf(f, "[message]\n");
|
||||
for (x = 0; x < colcount; x++) {
|
||||
rowdata[0] = '\0';
|
||||
|
@ -3927,11 +3927,9 @@ static int retrieve_file(char *dir, int msgnum)
|
|||
collen = sizeof(coltitle);
|
||||
res = SQLDescribeCol(stmt, x + 1, (unsigned char *) coltitle, sizeof(coltitle), &collen,
|
||||
&datatype, &colsize, &decimaldigits, &nullable);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
if (!strcasecmp(coltitle, "recording")) {
|
||||
off_t offset;
|
||||
|
@ -3949,19 +3947,14 @@ static int retrieve_file(char *dir, int msgnum)
|
|||
for (offset = 0; offset < colsize2; offset += CHUNKSIZE) {
|
||||
if ((fdm = mmap(NULL, CHUNKSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset)) == MAP_FAILED) {
|
||||
ast_log(AST_LOG_WARNING, "Could not mmap the output file: %s (%d)\n", strerror(errno), errno);
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
} else {
|
||||
goto bail_with_handle;
|
||||
}
|
||||
res = SQLGetData(stmt, x + 1, SQL_BINARY, fdm, CHUNKSIZE, NULL);
|
||||
munmap(fdm, CHUNKSIZE);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
unlink(full_fn);
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
}
|
||||
goto bail_with_handle;
|
||||
}
|
||||
}
|
||||
if (truncate(full_fn, fdlen) < 0) {
|
||||
|
@ -3970,30 +3963,32 @@ static int retrieve_file(char *dir, int msgnum)
|
|||
}
|
||||
} else {
|
||||
res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res == SQL_NULL_DATA) && (!strcasecmp(coltitle, "msg_id"))) {
|
||||
if (res == SQL_NULL_DATA && !strcasecmp(coltitle, "msg_id")) {
|
||||
char msg_id[MSG_ID_LEN];
|
||||
generate_msg_id(msg_id);
|
||||
snprintf(rowdata, sizeof(rowdata), "%s", msg_id);
|
||||
odbc_update_msg_id(dir, msgnum, msg_id);
|
||||
} else if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
} else if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error! coltitle=%s\n[%s]\n\n", coltitle, sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
if (strcasecmp(coltitle, "msgnum") && strcasecmp(coltitle, "dir") && f)
|
||||
if (strcasecmp(coltitle, "msgnum") && strcasecmp(coltitle, "dir")) {
|
||||
fprintf(f, "%s=%s\n", coltitle, rowdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bail_with_handle:
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
yuck:
|
||||
|
||||
bail:
|
||||
if (f)
|
||||
fclose(f);
|
||||
if (fd > -1)
|
||||
close(fd);
|
||||
|
||||
ast_odbc_release_obj(obj);
|
||||
|
||||
return x - 1;
|
||||
}
|
||||
|
||||
|
@ -4010,53 +4005,56 @@ yuck:
|
|||
*/
|
||||
static int last_message_index(struct ast_vm_user *vmu, char *dir)
|
||||
{
|
||||
int x = 0;
|
||||
int x = -1;
|
||||
int res;
|
||||
SQLHSTMT stmt;
|
||||
char sql[PATH_MAX];
|
||||
char rowdata[20];
|
||||
char *argv[] = { dir };
|
||||
struct generic_prepare_struct gps = { .sql = sql, .argc = 1, .argv = argv };
|
||||
|
||||
struct odbc_obj *obj;
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return -1;
|
||||
}
|
||||
|
||||
snprintf(sql, sizeof(sql), "SELECT msgnum FROM %s WHERE dir=? order by msgnum desc", odbc_table);
|
||||
|
||||
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
|
||||
if (!stmt) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
res = SQLFetch(stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
if (res == SQL_NO_DATA) {
|
||||
ast_log(AST_LOG_DEBUG, "Directory '%s' has no messages and therefore no index was retrieved.\n", dir);
|
||||
} else {
|
||||
ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
}
|
||||
goto bail_with_handle;
|
||||
}
|
||||
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
}
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
if (sscanf(rowdata, "%30d", &x) != 1)
|
||||
|
||||
if (sscanf(rowdata, "%30d", &x) != 1) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to read message index!\n");
|
||||
}
|
||||
|
||||
bail_with_handle:
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
|
||||
bail:
|
||||
ast_odbc_release_obj(obj);
|
||||
|
||||
return x;
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
yuck:
|
||||
return x - 1;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -4078,39 +4076,43 @@ static int message_exists(char *dir, int msgnum)
|
|||
char msgnums[20];
|
||||
char *argv[] = { dir, msgnums };
|
||||
struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
|
||||
|
||||
struct odbc_obj *obj;
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return 0;
|
||||
}
|
||||
|
||||
snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=? AND msgnum=?", odbc_table);
|
||||
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
|
||||
if (!stmt) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
res = SQLFetch(stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
if (sscanf(rowdata, "%30d", &x) != 1)
|
||||
|
||||
if (sscanf(rowdata, "%30d", &x) != 1) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to read message count!\n");
|
||||
}
|
||||
|
||||
bail_with_handle:
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
|
||||
bail:
|
||||
ast_odbc_release_obj(obj);
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
yuck:
|
||||
return x;
|
||||
}
|
||||
|
||||
|
@ -4125,48 +4127,50 @@ yuck:
|
|||
*/
|
||||
static int count_messages(struct ast_vm_user *vmu, char *dir)
|
||||
{
|
||||
int x = 0;
|
||||
int x = -1;
|
||||
int res;
|
||||
SQLHSTMT stmt;
|
||||
char sql[PATH_MAX];
|
||||
char rowdata[20];
|
||||
char *argv[] = { dir };
|
||||
struct generic_prepare_struct gps = { .sql = sql, .argc = 1, .argv = argv };
|
||||
|
||||
struct odbc_obj *obj;
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return -1;
|
||||
}
|
||||
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=?", odbc_table);
|
||||
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
|
||||
if (!stmt) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
res = SQLFetch(stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
if (sscanf(rowdata, "%30d", &x) != 1)
|
||||
|
||||
if (sscanf(rowdata, "%30d", &x) != 1) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to read message count!\n");
|
||||
}
|
||||
|
||||
bail_with_handle:
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
|
||||
bail:
|
||||
ast_odbc_release_obj(obj);
|
||||
return x;
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
yuck:
|
||||
return x - 1;
|
||||
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -4188,20 +4192,24 @@ static void delete_file(const char *sdir, int smsg)
|
|||
struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
|
||||
struct odbc_obj *obj;
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return;
|
||||
}
|
||||
|
||||
argv[0] = ast_strdupa(sdir);
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
snprintf(msgnums, sizeof(msgnums), "%d", smsg);
|
||||
snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE dir=? AND msgnum=?", odbc_table);
|
||||
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
|
||||
if (!stmt)
|
||||
if (!stmt) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
else
|
||||
} else {
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
}
|
||||
ast_odbc_release_obj(obj);
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -4230,7 +4238,11 @@ static void copy_file(char *sdir, int smsg, char *ddir, int dmsg, char *dmailbox
|
|||
generate_msg_id(msg_id);
|
||||
delete_file(ddir, dmsg);
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(msgnums, sizeof(msgnums), "%d", smsg);
|
||||
snprintf(msgnumd, sizeof(msgnumd), "%d", dmsg);
|
||||
snprintf(sql, sizeof(sql), "INSERT INTO %s (dir, msgnum, msg_id, context, macrocontext, callerid, origtime, duration, recording, flag, mailboxuser, mailboxcontext) SELECT ?,?,?,context,macrocontext,callerid,origtime,duration,recording,flag,?,? FROM %s WHERE dir=? AND msgnum=?", odbc_table, odbc_table);
|
||||
|
@ -4240,8 +4252,7 @@ static void copy_file(char *sdir, int smsg, char *ddir, int dmsg, char *dmailbox
|
|||
else
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -4271,9 +4282,8 @@ static SQLHSTMT insert_data_cb(struct odbc_obj *obj, void *vdata)
|
|||
SQLHSTMT stmt;
|
||||
|
||||
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Alloc Handle failed!\n");
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -4293,7 +4303,7 @@ static SQLHSTMT insert_data_cb(struct odbc_obj *obj, void *vdata)
|
|||
SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(data->category), 0, (void *) data->category, 0, NULL);
|
||||
}
|
||||
res = SQLExecDirect(stmt, (unsigned char *) data->sql, SQL_NTS);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Direct Execute failed!\n");
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
return NULL;
|
||||
|
@ -4335,7 +4345,9 @@ static int store_file(const char *dir, const char *mailboxuser, const char *mail
|
|||
struct ast_flags config_flags = { CONFIG_FLAG_NOCACHE };
|
||||
|
||||
delete_file(dir, msgnum);
|
||||
if (!(obj = ast_odbc_request_obj(odbc_database, 0))) {
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return -1;
|
||||
}
|
||||
|
@ -4414,9 +4426,9 @@ static int store_file(const char *dir, const char *mailboxuser, const char *mail
|
|||
res = -1;
|
||||
}
|
||||
} while (0);
|
||||
if (obj) {
|
||||
|
||||
ast_odbc_release_obj(obj);
|
||||
}
|
||||
|
||||
if (valid_config(cfg))
|
||||
ast_config_destroy(cfg);
|
||||
if (fdm != MAP_FAILED)
|
||||
|
@ -4450,8 +4462,13 @@ static void rename_file(char *sdir, int smsg, char *mailboxuser, char *mailboxco
|
|||
struct generic_prepare_struct gps = { .sql = sql, .argc = 6, .argv = argv };
|
||||
|
||||
delete_file(ddir, dmsg);
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(msgnums, sizeof(msgnums), "%d", smsg);
|
||||
snprintf(msgnumd, sizeof(msgnumd), "%d", dmsg);
|
||||
snprintf(sql, sizeof(sql), "UPDATE %s SET dir=?, msgnum=?, mailboxuser=?, mailboxcontext=? WHERE dir=? AND msgnum=?", odbc_table);
|
||||
|
@ -4461,8 +4478,6 @@ static void rename_file(char *sdir, int smsg, char *mailboxuser, char *mailboxco
|
|||
else
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
ast_odbc_release_obj(obj);
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -5663,18 +5678,49 @@ static void free_zone(struct vm_zone *z)
|
|||
}
|
||||
|
||||
#ifdef ODBC_STORAGE
|
||||
static int inboxcount2(const char *mailbox, int *urgentmsgs, int *newmsgs, int *oldmsgs)
|
||||
|
||||
static int count_messages_in_folder(struct odbc_obj *odbc, const char *context, const char *mailbox, const char *folder, int *messages)
|
||||
{
|
||||
int x = -1;
|
||||
int res;
|
||||
SQLHSTMT stmt = NULL;
|
||||
char sql[PATH_MAX];
|
||||
char rowdata[20];
|
||||
char tmp[PATH_MAX] = "";
|
||||
struct odbc_obj *obj = NULL;
|
||||
char *context;
|
||||
SQLHSTMT stmt = NULL;
|
||||
struct generic_prepare_struct gps = { .sql = sql, .argc = 0 };
|
||||
|
||||
if (!messages) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/%s'", odbc_table, VM_SPOOL_DIR, context, mailbox, folder);
|
||||
if (!(stmt = ast_odbc_prepare_and_execute(odbc, generic_prepare, &gps))) {
|
||||
ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
return 1;
|
||||
}
|
||||
res = SQLFetch(stmt);
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
return 1;
|
||||
}
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
*messages = atoi(rowdata);
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int inboxcount2(const char *mailbox, int *urgentmsgs, int *newmsgs, int *oldmsgs)
|
||||
{
|
||||
char tmp[PATH_MAX] = "";
|
||||
struct odbc_obj *obj;
|
||||
char *context;
|
||||
|
||||
if (newmsgs)
|
||||
*newmsgs = 0;
|
||||
if (oldmsgs)
|
||||
|
@ -5715,80 +5761,21 @@ static int inboxcount2(const char *mailbox, int *urgentmsgs, int *newmsgs, int *
|
|||
} else
|
||||
context = "default";
|
||||
|
||||
if ((obj = ast_odbc_request_obj(odbc_database, 0))) {
|
||||
do {
|
||||
if (newmsgs) {
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/%s'", odbc_table, VM_SPOOL_DIR, context, tmp, "INBOX");
|
||||
if (!(stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps))) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
res = SQLFetch(stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
*newmsgs = atoi(rowdata);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
}
|
||||
|
||||
if (oldmsgs) {
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/%s'", odbc_table, VM_SPOOL_DIR, context, tmp, "Old");
|
||||
if (!(stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps))) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
res = SQLFetch(stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
*oldmsgs = atoi(rowdata);
|
||||
}
|
||||
|
||||
if (urgentmsgs) {
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/%s'", odbc_table, VM_SPOOL_DIR, context, tmp, "Urgent");
|
||||
if (!(stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps))) {
|
||||
ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
res = SQLFetch(stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
break;
|
||||
}
|
||||
*urgentmsgs = atoi(rowdata);
|
||||
}
|
||||
|
||||
x = 0;
|
||||
} while (0);
|
||||
} else {
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (stmt) {
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
if (count_messages_in_folder(obj, context, tmp, "INBOX", newmsgs)
|
||||
|| count_messages_in_folder(obj, context, tmp, "Old", oldmsgs)
|
||||
|| count_messages_in_folder(obj, context, tmp, "Urgent", urgentmsgs)) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain message count for mailbox %s@%s\n",
|
||||
tmp, context);
|
||||
}
|
||||
if (obj) {
|
||||
|
||||
ast_odbc_release_obj(obj);
|
||||
}
|
||||
return x;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -5822,36 +5809,38 @@ static int messagecount(const char *mailbox_id, const char *folder)
|
|||
}
|
||||
|
||||
obj = ast_odbc_request_obj(odbc_database, 0);
|
||||
if (obj) {
|
||||
if (!obj) {
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(folder, "INBOX")) {
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/INBOX' OR dir = '%s%s/%s/Urgent'", odbc_table, VM_SPOOL_DIR, context, mailbox, VM_SPOOL_DIR, context, mailbox);
|
||||
} else {
|
||||
snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/%s'", odbc_table, VM_SPOOL_DIR, context, mailbox, folder);
|
||||
}
|
||||
|
||||
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
|
||||
if (!stmt) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
|
||||
goto yuck;
|
||||
goto bail;
|
||||
}
|
||||
res = SQLFetch(stmt);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
|
||||
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
|
||||
if (!SQL_SUCCEEDED(res)) {
|
||||
ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
goto yuck;
|
||||
goto bail_with_handle;
|
||||
}
|
||||
nummsgs = atoi(rowdata);
|
||||
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
|
||||
} else
|
||||
ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
|
||||
|
||||
yuck:
|
||||
if (obj)
|
||||
bail_with_handle:
|
||||
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
|
||||
|
||||
bail:
|
||||
ast_odbc_release_obj(obj);
|
||||
return nummsgs;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue