fix numRows and add numCols

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9310 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2008-08-16 15:55:49 +00:00
parent 05b01a6c03
commit a3f6e72f35
2 changed files with 67 additions and 44 deletions

View File

@ -260,8 +260,7 @@ static JSBool odbc_exec(JSContext * cx, JSObject * obj, uintN argc, jsval * argv
static JSBool odbc_num_rows(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) static JSBool odbc_num_rows(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval)
{ {
odbc_obj_t *odbc_obj = (odbc_obj_t *) JS_GetPrivate(cx, obj); odbc_obj_t *odbc_obj = (odbc_obj_t *) JS_GetPrivate(cx, obj);
SQLLEN row_count;
SQLSMALLINT rows = 0;
if (!odbc_obj || switch_odbc_handle_get_state(odbc_obj->handle) != SWITCH_ODBC_STATE_CONNECTED) { if (!odbc_obj || switch_odbc_handle_get_state(odbc_obj->handle) != SWITCH_ODBC_STATE_CONNECTED) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database is not connected!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database is not connected!\n");
@ -269,12 +268,36 @@ static JSBool odbc_num_rows(JSContext * cx, JSObject * obj, uintN argc, jsval *
} }
if (odbc_obj->stmt) { if (odbc_obj->stmt) {
SQLNumResultCols(odbc_obj->stmt, &rows); SQLRowCount(odbc_obj->stmt, &row_count);
} }
done: done:
*rval = INT_TO_JSVAL(rows); *rval = INT_TO_JSVAL(row_count);
return JS_TRUE;
}
static JSBool odbc_num_cols(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval)
{
odbc_obj_t *odbc_obj = (odbc_obj_t *) JS_GetPrivate(cx, obj);
SQLSMALLINT cols = 0;
if (!odbc_obj || switch_odbc_handle_get_state(odbc_obj->handle) != SWITCH_ODBC_STATE_CONNECTED) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database is not connected!\n");
goto done;
}
if (odbc_obj->stmt) {
SQLNumResultCols(odbc_obj->stmt, &cols);
}
done:
*rval = INT_TO_JSVAL(cols);
return JS_TRUE; return JS_TRUE;
@ -449,6 +472,7 @@ static JSFunctionSpec odbc_methods[] = {
{"query", odbc_exec, 1}, {"query", odbc_exec, 1},
{"execute", odbc_execute, 1}, {"execute", odbc_execute, 1},
{"numRows", odbc_num_rows, 1}, {"numRows", odbc_num_rows, 1},
{"numCols", odbc_num_cols, 1},
{"nextRow", odbc_next_row, 1}, {"nextRow", odbc_next_row, 1},
{"getData", odbc_get_data, 1}, {"getData", odbc_get_data, 1},
{"close", odbc_close, 1}, {"close", odbc_close, 1},

View File

@ -353,15 +353,15 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec(switch_odb
SQLNumResultCols(stmt, &c); SQLNumResultCols(stmt, &c);
SQLRowCount(stmt, &m); SQLRowCount(stmt, &m);
if (m > 0) {
for (t = 0; t < m; t++) { for (t = 0 ;; t++) {
int name_len = 256; int name_len = 256;
char **names; char **names;
char **vals; char **vals;
int y = 0; int y = 0;
if (!(result = SQLFetch(stmt)) == SQL_SUCCESS) { if (!(result = SQLFetch(stmt)) == SQL_SUCCESS) {
goto error; break;
} }
names = calloc(c, sizeof(*names)); names = calloc(c, sizeof(*names));
@ -395,7 +395,6 @@ SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec(switch_odb
free(names); free(names);
free(vals); free(vals);
} }
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLFreeHandle(SQL_HANDLE_STMT, stmt);