Convert func_odbc to use ast_dummy_alloc_channel()

Review: https://reviewboard.asterisk.org/r/290/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@205666 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Matthew Nicholson
2009-07-09 20:04:43 +00:00
parent f3560397be
commit 728fbf077e

View File

@@ -233,17 +233,23 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
if (!query) { if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd); ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
AST_RWLIST_UNLOCK(&queries); AST_RWLIST_UNLOCK(&queries);
if (chan) {
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
return -1; return -1;
} }
if (!chan) { if (!chan) {
if ((chan = ast_channel_alloc(0, AST_STATE_DOWN, "", "", "", "", "", "", 0, "Bogus/func_odbc"))) if (!(chan = ast_dummy_channel_alloc())) {
AST_RWLIST_UNLOCK(&queries);
return -1;
}
bogus_chan = 1; bogus_chan = 1;
} }
if (chan) if (!bogus_chan) {
ast_autoservice_start(chan); ast_autoservice_start(chan);
}
ast_str_make_space(&buf, strlen(query->sql_write) * 2 + 300); ast_str_make_space(&buf, strlen(query->sql_write) * 2 + 300);
ast_str_make_space(&insertbuf, strlen(query->sql_insert) * 2 + 300); ast_str_make_space(&insertbuf, strlen(query->sql_insert) * 2 + 300);
@@ -254,12 +260,11 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
if (!s || !t) { if (!s || !t) {
ast_log(LOG_ERROR, "Out of memory\n"); ast_log(LOG_ERROR, "Out of memory\n");
AST_RWLIST_UNLOCK(&queries); AST_RWLIST_UNLOCK(&queries);
if (chan) if (!bogus_chan) {
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan) {
ast_channel_release(chan);
} else {
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
} else {
ast_channel_release(chan);
} }
return -1; return -1;
} }
@@ -283,6 +288,9 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
ast_str_substitute_variables(&buf, 0, chan, query->sql_write); ast_str_substitute_variables(&buf, 0, chan, query->sql_write);
ast_str_substitute_variables(&insertbuf, 0, chan, query->sql_insert); ast_str_substitute_variables(&insertbuf, 0, chan, query->sql_insert);
if (bogus_chan) {
chan = ast_channel_release(chan);
} else {
/* Restore prior values */ /* Restore prior values */
for (i = 0; i < args.argc; i++) { for (i = 0; i < args.argc; i++) {
snprintf(varname, sizeof(varname), "ARG%d", i + 1); snprintf(varname, sizeof(varname), "ARG%d", i + 1);
@@ -323,6 +331,7 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
} }
} }
}
if (stmt && rows == 0 && ast_str_strlen(insertbuf) != 0) { if (stmt && rows == 0 && ast_str_strlen(insertbuf) != 0) {
SQLCloseCursor(stmt); SQLCloseCursor(stmt);
@@ -351,9 +360,11 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
* flag this as -1 rows. Note that this is different from 0 affected rows * flag this as -1 rows. Note that this is different from 0 affected rows
* which would be the case if we succeeded in our query, but the values did * which would be the case if we succeeded in our query, but the values did
* not change. */ * not change. */
if (!bogus_chan) {
snprintf(varname, sizeof(varname), "%d", (int)rows); snprintf(varname, sizeof(varname), "%d", (int)rows);
pbx_builtin_setvar_helper(chan, "ODBCROWS", varname); pbx_builtin_setvar_helper(chan, "ODBCROWS", varname);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
if (stmt) { if (stmt) {
SQLCloseCursor(stmt); SQLCloseCursor(stmt);
@@ -364,10 +375,9 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
obj = NULL; obj = NULL;
} }
if (chan) if (!bogus_chan) {
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan) }
ast_channel_release(chan);
return 0; return 0;
} }
@@ -392,7 +402,9 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
const char *status = "FAILURE"; const char *status = "FAILURE";
if (!sql) { if (!sql) {
if (chan) {
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
return -1; return -1;
} }
@@ -408,18 +420,22 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
if (!query) { if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd); ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
AST_RWLIST_UNLOCK(&queries); AST_RWLIST_UNLOCK(&queries);
if (chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
return -1; return -1;
} }
if (!chan) { if (!chan) {
if ((chan = ast_channel_alloc(0, AST_STATE_DOWN, "", "", "", "", "", "", 0, "Bogus/func_odbc"))) { if (!(chan = ast_dummy_channel_alloc())) {
AST_RWLIST_UNLOCK(&queries);
return -1;
}
bogus_chan = 1; bogus_chan = 1;
} }
}
if (chan) { if (!bogus_chan) {
ast_autoservice_start(chan); ast_autoservice_start(chan);
} }
@@ -431,15 +447,19 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
ast_str_substitute_variables(&sql, 0, chan, query->sql_read); ast_str_substitute_variables(&sql, 0, chan, query->sql_read);
if (bogus_chan) {
chan = ast_channel_release(chan);
} else {
/* Restore prior values */ /* Restore prior values */
for (x = 0; x < args.argc; x++) { for (x = 0; x < args.argc; x++) {
snprintf(varname, sizeof(varname), "ARG%d", x + 1); snprintf(varname, sizeof(varname), "ARG%d", x + 1);
pbx_builtin_setvar_helper(chan, varname, NULL); pbx_builtin_setvar_helper(chan, varname, NULL);
} }
}
/* Save these flags, so we can release the lock */ /* Save these flags, so we can release the lock */
escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS); escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS);
if (ast_test_flag(query, OPT_MULTIROW)) { if (!bogus_chan && ast_test_flag(query, OPT_MULTIROW)) {
resultset = ast_calloc(1, sizeof(*resultset)); resultset = ast_calloc(1, sizeof(*resultset));
AST_LIST_HEAD_INIT(resultset); AST_LIST_HEAD_INIT(resultset);
if (query->rowlimit) { if (query->rowlimit) {
@@ -468,13 +488,10 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
obj = NULL; obj = NULL;
} }
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
if (chan) {
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
} }
if (bogus_chan) {
ast_channel_release(chan);
}
return -1; return -1;
} }
@@ -485,13 +502,10 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
SQLFreeHandle (SQL_HANDLE_STMT, stmt); SQLFreeHandle (SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
obj = NULL; obj = NULL;
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
if (chan) {
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
} }
if (bogus_chan) {
ast_channel_release(chan);
}
return -1; return -1;
} }
@@ -512,12 +526,11 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
obj = NULL; obj = NULL;
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
if (chan)
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan) }
ast_channel_release(chan);
return res1; return res1;
} }
@@ -558,10 +571,7 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
obj = NULL; obj = NULL;
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "MEMERROR"); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "MEMERROR");
if (chan)
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan)
ast_channel_release(chan);
return -1; return -1;
} }
resultset = tmp; resultset = tmp;
@@ -636,6 +646,7 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
} }
end_acf_read: end_acf_read:
if (!bogus_chan) {
snprintf(rowcount, sizeof(rowcount), "%d", y); snprintf(rowcount, sizeof(rowcount), "%d", y);
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status); pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
@@ -648,29 +659,26 @@ end_acf_read:
odbc_store = ast_datastore_alloc(&odbc_info, buf); odbc_store = ast_datastore_alloc(&odbc_info, buf);
if (!odbc_store) { if (!odbc_store) {
ast_log(LOG_ERROR, "Rows retrieved, but unable to store it in the channel. Results fail.\n"); ast_log(LOG_ERROR, "Rows retrieved, but unable to store it in the channel. Results fail.\n");
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "MEMERROR");
odbc_datastore_free(resultset); odbc_datastore_free(resultset);
SQLCloseCursor(stmt); SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
obj = NULL; obj = NULL;
if (chan) pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "MEMERROR");
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan)
ast_channel_release(chan);
return -1; return -1;
} }
odbc_store->data = resultset; odbc_store->data = resultset;
ast_channel_datastore_add(chan, odbc_store); ast_channel_datastore_add(chan, odbc_store);
} }
}
SQLCloseCursor(stmt); SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
obj = NULL; obj = NULL;
if (chan) if (!bogus_chan) {
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan) }
ast_channel_release(chan);
return 0; return 0;
} }