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,93 +945,99 @@ 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])) {
ast_debug(1, "Found handle %s\n", query->readhandle[dsn]); continue;
if ((obj = ast_odbc_request_obj(query->readhandle[dsn], 0))) { }
ast_debug(1, "Got obj\n"); ast_debug(1, "Found handle %s\n", query->readhandle[dsn]);
if ((stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str))) { if (!(obj = ast_odbc_request_obj(query->readhandle[dsn], 0))) {
struct ast_str *coldata = ast_str_thread_get(&coldata_buf, 16); continue;
char colname[256]; }
SQLULEN maxcol;
ast_debug(1, "Got obj\n");
executed = 1; if (!(stmt = ast_odbc_direct_execute(obj, generic_execute, sql->str))) {
ast_odbc_release_obj(obj);
res = SQLNumResultCols(stmt, &colcount); continue;
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { }
ast_cli(a->fd, "SQL Column Count error!\n[%s]\n\n", sql->str);
SQLCloseCursor(stmt); executed = 1;
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); res = SQLNumResultCols(stmt, &colcount);
AST_RWLIST_UNLOCK(&queries); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
} ast_cli(a->fd, "SQL Column Count error!\n[%s]\n\n", sql->str);
SQLCloseCursor(stmt);
res = SQLFetch(stmt); SQLFreeHandle (SQL_HANDLE_STMT, stmt);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_odbc_release_obj(obj);
SQLCloseCursor(stmt); AST_RWLIST_UNLOCK(&queries);
SQLFreeHandle(SQL_HANDLE_STMT, stmt); return CLI_SUCCESS;
ast_odbc_release_obj(obj); }
if (res == SQL_NO_DATA) {
ast_cli(a->fd, "Returned %d rows. Query executed on handle %d:%s [%s]\n", rows, dsn, query->readhandle[dsn], sql->str); res = SQLFetch(stmt);
break; if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
} else { SQLCloseCursor(stmt);
ast_cli(a->fd, "Error %d in FETCH [%s]\n", res, sql->str); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
} ast_odbc_release_obj(obj);
AST_RWLIST_UNLOCK(&queries); if (res == SQL_NO_DATA) {
return CLI_SUCCESS; ast_cli(a->fd, "Returned %d rows. Query executed on handle %d:%s [%s]\n", rows, dsn, query->readhandle[dsn], sql->str);
} break;
for (;;) { } else {
for (x = 0; x < colcount; x++) { ast_cli(a->fd, "Error %d in FETCH [%s]\n", res, sql->str);
res = SQLDescribeCol(stmt, x + 1, (unsigned char *)colname, sizeof(colname), &collength, NULL, &maxcol, NULL, NULL); }
if (((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) || collength == 0) { AST_RWLIST_UNLOCK(&queries);
snprintf(colname, sizeof(colname), "field%d", x); return CLI_SUCCESS;
} }
for (;;) {
if (coldata->len < maxcol + 1) { for (x = 0; x < colcount; x++) {
ast_str_make_space(&coldata, maxcol + 1); res = SQLDescribeCol(stmt, x + 1, (unsigned char *)colname, sizeof(colname), &collength, NULL, &maxcol, NULL, NULL);
} if (((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) || collength == 0) {
snprintf(colname, sizeof(colname), "field%d", x);
res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata->str, coldata->len, &indicator); }
if (indicator == SQL_NULL_DATA) {
ast_str_set(&coldata, 0, "(nil)"); if (coldata->len < maxcol + 1) {
res = SQL_SUCCESS; ast_str_make_space(&coldata, maxcol + 1);
} }
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata->str, coldata->len, &indicator);
ast_cli(a->fd, "SQL Get Data error %d!\n[%s]\n\n", res, sql->str); if (indicator == SQL_NULL_DATA) {
SQLCloseCursor(stmt); ast_str_set(&coldata, 0, "(nil)");
SQLFreeHandle(SQL_HANDLE_STMT, stmt); res = SQL_SUCCESS;
ast_odbc_release_obj(obj); }
AST_RWLIST_UNLOCK(&queries);
return CLI_SUCCESS; if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
} ast_cli(a->fd, "SQL Get Data error %d!\n[%s]\n\n", res, sql->str);
ast_cli(a->fd, "%-20.20s %s\n", colname, coldata->str);
}
/* Get next row */
res = SQLFetch(stmt);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
break;
}
ast_cli(a->fd, "%-20.20s %s\n", "----------", "----------");
rows++;
}
SQLCloseCursor(stmt); SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj); ast_odbc_release_obj(obj);
ast_cli(a->fd, "Returned %d rows. Query executed on handle %d [%s]\n", rows, dsn, query->readhandle[dsn]); AST_RWLIST_UNLOCK(&queries);
break; return CLI_SUCCESS;
} }
ast_odbc_release_obj(obj);
ast_cli(a->fd, "%-20.20s %s\n", colname, coldata->str);
}
/* Get next row */
res = SQLFetch(stmt);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
break;
} }
ast_cli(a->fd, "%-20.20s %s\n", "----------", "----------");
rows++;
} }
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj);
ast_cli(a->fd, "Returned %d rows. Query executed on handle %d [%s]\n", rows, dsn, query->readhandle[dsn]);
break;
} }
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,26 +1148,30 @@ 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))) {
SQLRowCount(stmt, &rows);
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj);
ast_cli(a->fd, "Affected %d rows. Query executed on handle %d [%s]\n", (int)rows, dsn, query->writehandle[dsn]);
executed = 1;
break;
}
ast_odbc_release_obj(obj);
}
} }
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);
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ast_odbc_release_obj(obj);
ast_cli(a->fd, "Affected %d rows. Query executed on handle %d [%s]\n", (int)rows, dsn, query->writehandle[dsn]);
executed = 1;
break;
} }
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