|
From: <do...@us...> - 2008-04-18 01:38:12
|
Update of /cvsroot/aolserver/nsfreetds In directory sc8-pr-cvs11.sourceforge.net:/tmp/cvs-serv9565 Modified Files: nsfreetds.c Log Message: Updated nsfreetds to work with FreeTDS 0.64. Discovered that in order to specify client character set requires TDS 8.0 with SQL Server 2005. Index: nsfreetds.c =================================================================== RCS file: /cvsroot/aolserver/nsfreetds/nsfreetds.c,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** nsfreetds.c 27 Aug 2004 19:39:44 -0000 1.7 --- nsfreetds.c 18 Apr 2008 01:38:18 -0000 1.8 *************** *** 10,13 **** --- 10,14 ---- * - AOLserver 4.0.8a, FreeTDS 0.61, Linux 2.6.7 glibc 2.3. * - AOLserver 4.1.0a, FreeTDS 0.61, Linux 2.6.7 glibc 2.3. + * - AOLserver 4.0.10, FreeTDS 0.64, Debian 4.0 (etch). * * This driver was derived from the nsmysql driver. *************** *** 60,64 **** #define GET_TDS(handle) ((TDSSOCKET *) handle->connection) ! #define GET_TDS_RESULTS(handle) ((TDSRESULTINFO *) GET_TDS(handle)->res_info) --- 61,65 ---- #define GET_TDS(handle) ((TDSSOCKET *) handle->connection) ! #define GET_TDS_RESULTS(handle) ((TDSRESULTINFO *) GET_TDS(handle)->current_results) *************** *** 92,97 **** long Ns_FreeTDS_Rows_Affected(Ns_DbHandle *handle); ! int Ns_FreeTDS_Msg_Handler(TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMSGINFO *msg); ! int Ns_FreeTDS_Err_Handler(TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMSGINFO *msg); static Ns_DbProc dbProcs[] = { --- 93,98 ---- long Ns_FreeTDS_Rows_Affected(Ns_DbHandle *handle); ! int Ns_FreeTDS_Msg_Handler(const TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMESSAGE *msg); ! int Ns_FreeTDS_Err_Handler(const TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMESSAGE *msg); static Ns_DbProc dbProcs[] = { *************** *** 158,165 **** TDSCONTEXT *context; TDSSOCKET *tds; ! TDSCONNECTINFO *connect_info; const char *locale = NULL; char *charset = NULL; ! void *ctxArray[2]; assert(handle != NULL); --- 159,166 ---- TDSCONTEXT *context; TDSSOCKET *tds; ! TDSCONNECTION *connect_info; const char *locale = NULL; char *charset = NULL; ! void **ctxArray; assert(handle != NULL); *************** *** 178,182 **** } ! context = tds_alloc_context(); if (!context) { Ns_Log(Notice, "nsfreetds: DbOpen(%s): tds_alloc_context() failed.", --- 179,183 ---- } ! context = tds_alloc_context(NULL); if (!context) { Ns_Log(Notice, "nsfreetds: DbOpen(%s): tds_alloc_context() failed.", *************** *** 203,206 **** --- 204,208 ---- } + tds_set_version(login, 8, 0); /* FIXME: Use ns_param tdsversion */ tds_set_app(login, DbName()); tds_set_library(login, "TDS-Library"); *************** *** 211,215 **** tds_set_host(login, "myhost"); /* FIXME: aolserver instance name? */ ! tds_set_charset(login, charset); tds_set_language(login, "us_english"); tds_set_packet(login, 512); --- 213,217 ---- tds_set_host(login, "myhost"); /* FIXME: aolserver instance name? */ ! tds_set_client_charset(login, charset); tds_set_language(login, "us_english"); tds_set_packet(login, 512); *************** *** 227,242 **** connect_info = tds_read_config_info(NULL, login, context->locale); if (!connect_info || tds_connect(tds, connect_info) == TDS_FAIL) { ! tds_free_connect(connect_info); Ns_Log(Notice, "nsfreetds: DbOpen(%s): tds_connect() failed.", handle->datasource); return NS_ERROR; } ! tds_free_connect(connect_info); ctxArray[0] = login; ctxArray[1] = context; handle->connection = (void *) tds; ! handle->context = (void *) ctxArray; handle->connected = NS_TRUE; --- 229,245 ---- connect_info = tds_read_config_info(NULL, login, context->locale); if (!connect_info || tds_connect(tds, connect_info) == TDS_FAIL) { ! tds_free_connection(connect_info); Ns_Log(Notice, "nsfreetds: DbOpen(%s): tds_connect() failed.", handle->datasource); return NS_ERROR; } ! tds_free_connection(connect_info); + ctxArray = (void **) calloc(2, sizeof(void *)); ctxArray[0] = login; ctxArray[1] = context; handle->connection = (void *) tds; ! handle->context = ctxArray; handle->connected = NS_TRUE; *************** *** 247,251 **** DbClose(Ns_DbHandle *handle) { ! void **ctxArray; if (handle->verbose) { --- 250,254 ---- DbClose(Ns_DbHandle *handle) { ! void **ctxArray = (void **) handle->context; if (handle->verbose) { *************** *** 253,263 **** } - DbCancel(handle); - - ctxArray = (void **) handle->context; - tds_free_socket(GET_TDS(handle)); tds_free_login((TDSLOGIN *) ctxArray[0]); tds_free_context((TDSCONTEXT *) ctxArray[1]); handle->connected = NS_FALSE; --- 256,263 ---- } tds_free_socket(GET_TDS(handle)); tds_free_login((TDSLOGIN *) ctxArray[0]); tds_free_context((TDSCONTEXT *) ctxArray[1]); + free(ctxArray); handle->connected = NS_FALSE; *************** *** 273,281 **** TDS_INT rowtype; TDS_INT computeid; ! TDSCOLINFO *col; int ctype; CONV_RESULT dres; ! unsigned char *src; ! TDS_INT srclen; if (handle->verbose) --- 273,281 ---- TDS_INT rowtype; TDS_INT computeid; ! TDSCOLUMN *col; int ctype; CONV_RESULT dres; ! unsigned char *src, *dest; ! TDS_INT srclen, destlen; if (handle->verbose) *************** *** 283,287 **** if (! handle->fetchingRows) { ! Ns_Log(Error, "nsfreetds: DbGetRow(%s): No rows waiting to fetch.", handle->datasource); Ns_DbSetException(handle, "NSDB", "no rows waiting to fetch."); --- 283,287 ---- if (! handle->fetchingRows) { ! Ns_Log(Error, "nsfreetds: DbGetRow(%s): No rows waiting to fetch.", handle->datasource); Ns_DbSetException(handle, "NSDB", "no rows waiting to fetch."); *************** *** 305,343 **** } ! rc = tds_process_row_tokens(GET_TDS(handle), &rowtype, &computeid); if (rc == TDS_FAIL) { ! Ns_Log(Error, "nsfreetds: DbGetRow(%s): tds_process_row_tokens() returned TDS_FAIL", handle->datasource); goto error; ! } else if (rc != TDS_SUCCEED && rc != TDS_NO_MORE_ROWS) { ! Ns_Log(Error, "nsfreetds: DbGetRow(%s): tds_process_row_tokens() unexpected return %d", handle->datasource, rc); goto error; } ! ! if (rc == TDS_NO_MORE_ROWS || !GET_TDS(handle)->res_info) { handle->statement = NULL; handle->fetchingRows = 0; return NS_END_DATA; } ! for (i = 0; i < numcols; i++) { ! if (tds_get_null(GET_TDS_RESULTS(handle)->current_row, i)) { Ns_SetPutValue(row, i, ""); continue; } - col = GET_TDS_RESULTS(handle)->columns[i]; ctype = tds_get_conversion_type(col->column_type, col->column_size); - src = &(GET_TDS_RESULTS(handle)->current_row[col->column_offset]); if (is_blob_type(col->column_type)) ! src = (unsigned char*) ((TDSBLOBINFO *) src)->textvalue; srclen = col->column_cur_size; ! ! if (tds_convert(GET_TDS(handle)->tds_ctx, ctype, (TDS_CHAR*) src, (unsigned) srclen, SYBVARCHAR, &dres) < 0) { continue; } ! Ns_SetPutValue(row, i, dres.c); ! free(dres.c); } --- 305,355 ---- } ! rc = tds_process_tokens(GET_TDS(handle), &rowtype, &computeid, ! TDS_STOPAT_ROWFMT | TDS_RETURN_DONE | TDS_RETURN_ROW | ! TDS_RETURN_COMPUTE); ! if (rc == TDS_FAIL) { ! Ns_Log(Error, "nsfreetds: DbGetRow(%s): tds_process_row_tokens() returned TDS_FAIL.", handle->datasource); goto error; ! } else if (rc != TDS_SUCCEED && rc != TDS_NO_MORE_RESULTS) { ! Ns_Log(Error, "nsfreetds: DbGetRow(%s): tds_process_row_tokens() unexpected return: %d", handle->datasource, rc); goto error; } ! if (rc == TDS_NO_MORE_RESULTS || !GET_TDS_RESULTS(handle) || ! (rowtype != TDS_ROW_RESULT && rowtype != TDS_COMPUTE_RESULT)) { handle->statement = NULL; handle->fetchingRows = 0; return NS_END_DATA; } ! for (i = 0; i < numcols; i++) { ! col = GET_TDS_RESULTS(handle)->columns[i]; ! if (col->column_cur_size < 0) { Ns_SetPutValue(row, i, ""); continue; } ctype = tds_get_conversion_type(col->column_type, col->column_size); src = &(GET_TDS_RESULTS(handle)->current_row[col->column_offset]); if (is_blob_type(col->column_type)) ! src = (unsigned char *) ((TDSBLOB *) src)->textvalue; srclen = col->column_cur_size; ! ! // Ns_Log(Notice, "nsfreetds: DbGetRow(%s): src = {%s}", handle->datasource, src); ! if ((destlen = tds_convert(GET_TDS(handle)->tds_ctx, ctype, ! (const TDS_CHAR *) src, (unsigned) srclen, ! SYBVARCHAR, &dres)) < 0) { ! // Ns_Log(Notice, "nsfreetds: DbGetRow(%s): tds_convert(column_type = 0x%x, ctype = 0x%x) < 0 (%d)", handle->datasource, col->column_type, ctype, destlen); continue; } ! dest = (unsigned char *) malloc(destlen + 1); ! memcpy(dest, src, destlen); ! dest[destlen] = '\0'; ! ! // Ns_Log(Notice, "nsfreetds: DbGetRow(%s): tds_convert(ctype = 0x%x) = {%s}", handle->datasource, ctype, dest); ! Ns_SetPutValue(row, i, (char *) dest); ! free(dest); ! // free(dres.c); } *************** *** 348,351 **** --- 360,364 ---- } + static int DbFlush(Ns_DbHandle *handle) *************** *** 379,384 **** DbExec(Ns_DbHandle *handle, char *sql) { ! int rc, status; ! TDS_INT resulttype; assert(handle != NULL); --- 392,398 ---- DbExec(Ns_DbHandle *handle, char *sql) { ! int rc, status; ! TDS_INT resulttype; ! TDS_INT computeid; assert(handle != NULL); *************** *** 396,401 **** --- 410,420 ---- * Flush previous query in case there was one. */ + DbFlush(handle); + /* + * Execute SQL query. + */ + rc = tds_submit_query(GET_TDS(handle), sql); if (rc != TDS_SUCCEED) { *************** *** 406,421 **** /* * FIXME: There can be multiple result sets. Currently, we * are fetching only the first. */ ! rc = tds_process_result_tokens(GET_TDS(handle), &resulttype); if (rc == TDS_FAIL) { ! Ns_Log(Error, "nsfreetds: DbExec(%s): tds_process_result_tokens() returned TDS_FAIL.", handle->datasource); return NS_ERROR; } else if (rc != TDS_SUCCEED && rc != TDS_NO_MORE_RESULTS) { ! Ns_Log(Error, "nsfreetds: DbExec(%s): tds_process_result_tokens() unexpected return.", handle->datasource); return NS_ERROR; } - if (rc == TDS_NO_MORE_RESULTS) { handle->statement = NULL; --- 425,444 ---- /* + * Process query response. + * * FIXME: There can be multiple result sets. Currently, we * are fetching only the first. */ ! ! rc = tds_process_tokens(GET_TDS(handle), &resulttype, &computeid, ! TDS_TOKEN_RESULTS); ! if (rc == TDS_FAIL) { ! Ns_Log(Error, "nsfreetds: DbExec(%s): tds_process_tokens() returned TDS_FAIL.", handle->datasource); return NS_ERROR; } else if (rc != TDS_SUCCEED && rc != TDS_NO_MORE_RESULTS) { ! Ns_Log(Error, "nsfreetds: DbExec(%s): tds_process_tokens() unexpected return: %d", handle->datasource, rc); return NS_ERROR; } if (rc == TDS_NO_MORE_RESULTS) { handle->statement = NULL; *************** *** 437,460 **** } ! static Ns_Set * DbBindRow(Ns_DbHandle *handle) { ! unsigned int i, numcols; if (handle->verbose) Ns_Log(Notice, "nsfreetds: DbBindRow(%s) called.", handle->datasource); numcols = GET_TDS_RESULTS(handle)->num_cols; if (handle->verbose) Ns_Log(Notice, "nsfreetds: DbBindRow(%s, numcols) = %u", ! handle->datasource, numcols); for (i = 0; i < numcols; i++) { ! Ns_SetPut((Ns_Set *) handle->row, ! GET_TDS_RESULTS(handle)->columns[i]->column_name, NULL); } ! return (Ns_Set *) handle->row; } --- 460,490 ---- } ! static Ns_Set * DbBindRow(Ns_DbHandle *handle) { ! Ns_Set *row = (Ns_Set *) handle->row; ! unsigned int i, numcols = 0; if (handle->verbose) Ns_Log(Notice, "nsfreetds: DbBindRow(%s) called.", handle->datasource); + if (!GET_TDS_RESULTS(handle)) { + Ns_Log(Error, "nsfreetds: DbBindRow(%s): No result data for row.", + handle->datasource); + Ns_DbSetException(handle, "NSDB", "no result data for row"); + return NULL; + } + numcols = GET_TDS_RESULTS(handle)->num_cols; if (handle->verbose) Ns_Log(Notice, "nsfreetds: DbBindRow(%s, numcols) = %u", ! handle->datasource, numcols); for (i = 0; i < numcols; i++) { ! Ns_SetPut(row, GET_TDS_RESULTS(handle)->columns[i]->column_name, NULL); } ! return row; } *************** *** 492,503 **** int ! Ns_FreeTDS_Err_Handler(TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMSGINFO *msg) { Ns_DbHandle *handle = (Ns_DbHandle *) tds->parent; ! Ns_Log(Error, "nsfreetds: Ns_FreeTDS_Err_Handler(%s): ERR(%u:%u) %s", ! handle->datasource, msg->msg_number, msg->line_number, msg->message); ! ! Ns_DbSetException(handle, "NSDB", msg->message); return 0; --- 522,531 ---- int ! Ns_FreeTDS_Msg_Handler(const TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMESSAGE *msg) { Ns_DbHandle *handle = (Ns_DbHandle *) tds->parent; ! Ns_Log(Notice, "nsfreetds: Ns_FreeTDS_Msg_Handler(%s): %s", ! handle->datasource, msg->message); return 0; *************** *** 505,514 **** int ! Ns_FreeTDS_Msg_Handler(TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMSGINFO *msg) { Ns_DbHandle *handle = (Ns_DbHandle *) tds->parent; ! Ns_Log(Notice, "nsfreetds: Ns_FreeTDS_Msg_Handler(%s): %s", ! handle->datasource, msg->message); return 0; --- 533,544 ---- int ! Ns_FreeTDS_Err_Handler(const TDSCONTEXT *ctx, TDSSOCKET *tds, TDSMESSAGE *msg) { Ns_DbHandle *handle = (Ns_DbHandle *) tds->parent; ! Ns_Log(Error, "nsfreetds: Ns_FreeTDS_Err_Handler(%s): ERR(%u:%u) %s", ! handle->datasource, NULL, msg->line_number, msg->message); ! ! Ns_DbSetException(handle, "NSDB", msg->message); return 0; |