Simplify some nested functions, as suggested by Russell on -dev

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@151732 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.6.2
Tilghman Lesher 17 years ago
parent 9fe75b494a
commit a45c3a8729

@ -145,7 +145,7 @@ static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, co
); );
SQLHSTMT stmt = NULL; SQLHSTMT stmt = NULL;
SQLLEN rows=0; SQLLEN rows=0;
struct ast_str *buf = ast_str_create(16); struct ast_str *buf = ast_str_thread_get(&sql_buf, 16);
if (!buf) { if (!buf) {
return -1; return -1;
@ -275,7 +275,7 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
SQLSMALLINT collength; SQLSMALLINT collength;
struct odbc_datastore *resultset = NULL; struct odbc_datastore *resultset = NULL;
struct odbc_datastore_row *row = NULL; struct odbc_datastore_row *row = NULL;
struct ast_str *sql = ast_str_create(16); struct ast_str *sql = ast_str_thread_get(&sql_buf, 16);
if (!sql) { if (!sql) {
return -1; return -1;
@ -297,12 +297,14 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
} }
if (!chan) { if (!chan) {
if ((chan = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/func_odbc"))) if ((chan = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/func_odbc"))) {
bogus_chan = 1; bogus_chan = 1;
} }
}
if (chan) if (chan) {
ast_autoservice_start(chan); ast_autoservice_start(chan);
}
AST_STANDARD_APP_ARGS(args, s); AST_STANDARD_APP_ARGS(args, s);
for (x = 0; x < args.argc; x++) { for (x = 0; x < args.argc; x++) {
@ -324,32 +326,38 @@ static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, cha
if (ast_test_flag(query, OPT_MULTIROW)) { if (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) {
rowlimit = query->rowlimit; rowlimit = query->rowlimit;
else } else {
rowlimit = INT_MAX; rowlimit = INT_MAX;
} }
}
AST_RWLIST_UNLOCK(&queries); AST_RWLIST_UNLOCK(&queries);
for (dsn = 0; dsn < 5; dsn++) { for (dsn = 0; dsn < 5; dsn++) {
if (!ast_strlen_zero(query->readhandle[dsn])) { if (!ast_strlen_zero(query->readhandle[dsn])) {
obj = ast_odbc_request_obj(query->readhandle[dsn], 0); obj = ast_odbc_request_obj(query->readhandle[dsn], 0);
if (obj) if (obj) {
stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str); stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str);
} }
if (stmt) }
if (stmt) {
break; break;
} }
}
if (!stmt) { if (!stmt) {
ast_log(LOG_ERROR, "Unable to execute query [%s]\n", sql->str); ast_log(LOG_ERROR, "Unable to execute query [%s]\n", sql->str);
if (obj) if (obj) {
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
}
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
if (chan) if (chan) {
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan) }
if (bogus_chan) {
ast_channel_free(chan); ast_channel_free(chan);
}
ast_free(sql); ast_free(sql);
return -1; return -1;
} }
@ -361,10 +369,12 @@ 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);
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
if (chan) if (chan) {
ast_autoservice_stop(chan); ast_autoservice_stop(chan);
if (bogus_chan) }
if (bogus_chan) {
ast_channel_free(chan); ast_channel_free(chan);
}
ast_free(sql); ast_free(sql);
return -1; return -1;
} }
@ -935,16 +945,24 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args
int rows = 0, res, x; int rows = 0, res, x;
SQLSMALLINT colcount = 0, collength; SQLSMALLINT colcount = 0, collength;
SQLLEN indicator; SQLLEN indicator;
struct ast_str *coldata = ast_str_thread_get(&coldata_buf, 16);
char colname[256];
SQLULEN maxcol;
for (dsn = 0; dsn < 5; dsn++) { for (dsn = 0; dsn < 5; dsn++) {
if (!ast_strlen_zero(query->readhandle[dsn])) { if (ast_strlen_zero(query->readhandle[dsn])) {
continue;
}
ast_debug(1, "Found handle %s\n", query->readhandle[dsn]); ast_debug(1, "Found handle %s\n", query->readhandle[dsn]);
if ((obj = ast_odbc_request_obj(query->readhandle[dsn], 0))) { if (!(obj = ast_odbc_request_obj(query->readhandle[dsn], 0))) {
continue;
}
ast_debug(1, "Got obj\n"); ast_debug(1, "Got obj\n");
if ((stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str))) { if (!(stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str))) {
struct ast_str *coldata = ast_str_thread_get(&coldata_buf, 16); ast_odbc_release_obj(obj);
char colname[256]; continue;
SQLULEN maxcol; }
executed = 1; executed = 1;
@ -955,6 +973,7 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args
SQLFreeHandle (SQL_HANDLE_STMT, stmt); SQLFreeHandle (SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
AST_RWLIST_UNLOCK(&queries); AST_RWLIST_UNLOCK(&queries);
return CLI_SUCCESS;
} }
res = SQLFetch(stmt); res = SQLFetch(stmt);
@ -1014,14 +1033,11 @@ static char *cli_odbc_read(struct ast_cli_entry *e, int cmd, struct ast_cli_args
break; break;
} }
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
}
}
}
if (!executed) { if (!executed) {
ast_cli(a->fd, "Failed to execute query. [%s]\n", sql->str); ast_cli(a->fd, "Failed to execute query. [%s]\n", sql->str);
} }
} else { } else { /* No execution, just print out the resulting SQL */
ast_cli(a->fd, "%s\n", sql->str); ast_cli(a->fd, "%s\n", sql->str);
} }
AST_RWLIST_UNLOCK(&queries); AST_RWLIST_UNLOCK(&queries);
@ -1132,9 +1148,17 @@ static char *cli_odbc_write(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
SQLLEN rows = -1; SQLLEN rows = -1;
for (dsn = 0; dsn < 5; dsn++) { for (dsn = 0; dsn < 5; dsn++) {
if (!ast_strlen_zero(query->writehandle[dsn])) { if (ast_strlen_zero(query->writehandle[dsn])) {
if ((obj = ast_odbc_request_obj(query->writehandle[dsn], 0))) { continue;
if ((stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str))) { }
if (!(obj = ast_odbc_request_obj(query->writehandle[dsn], 0))) {
continue;
}
if (!(stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str))) {
ast_odbc_release_obj(obj);
continue;
}
SQLRowCount(stmt, &rows); SQLRowCount(stmt, &rows);
SQLCloseCursor(stmt); SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
@ -1143,15 +1167,11 @@ static char *cli_odbc_write(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
executed = 1; executed = 1;
break; break;
} }
ast_odbc_release_obj(obj);
}
}
}
if (!executed) { if (!executed) {
ast_cli(a->fd, "Failed to execute query.\n"); ast_cli(a->fd, "Failed to execute query.\n");
} }
} else { } else { /* No execution, just print out the resulting SQL */
ast_cli(a->fd, "%s\n", sql->str); ast_cli(a->fd, "%s\n", sql->str);
} }
AST_RWLIST_UNLOCK(&queries); AST_RWLIST_UNLOCK(&queries);

Loading…
Cancel
Save