make compatible with aolserver 4.0+
| 1 | /* |
| 2 | * The contents of this file are subject to the AOLserver Public License |
| 3 | * Version 1.1 (the "License"); you may not use this file except in |
| 4 | * compliance with the License. You may obtain a copy of the License at |
| 5 | * http://aolserver.com/. |
| 6 | * |
| 7 | * Software distributed under the License is distributed on an "AS IS" |
| 8 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 9 | * the License for the specific language governing rights and limitations |
| 10 | * under the License. |
| 11 | * |
| 12 | * The Original Code is AOLserver Code and related documentation |
| 13 | * distributed by AOL. |
| 14 | * |
| 15 | * The Initial Developer of the Original Code is America Online, |
| 16 | * Inc. Portions created by AOL are Copyright (C) 1999 America Online, |
| 17 | * Inc. All Rights Reserved. |
| 18 | * |
| 19 | * Alternatively, the contents of this file may be used under the terms |
| 20 | * of the GNU General Public License (the "GPL"), in which case the |
| 21 | * provisions of GPL are applicable instead of those above. If you wish |
| 22 | * to allow use of your version of this file only under the terms of the |
| 23 | * GPL and not to allow others to use your version of this file under the |
| 24 | * License, indicate your decision by deleting the provisions above and |
| 25 | * replace them with the notice and other provisions required by the GPL. |
| 26 | * If you do not delete the provisions above, a recipient may use your |
| 27 | * version of this file under either the License or the GPL. |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | static const char *RCSID = "@(#) $Header: /cvsroot-fuse/aolserver/nsodbc/nsodbc.c,v 1.4 2003/07/23 18:52:38 elizthom Exp $, compiled: " __DATE__ " " __TIME__; |
| 32 | |
| 33 | #include "ns.h" |
| 34 | #include "nsdb.h" |
| 35 | |
| 36 | #ifndef REDBRICK |
| 37 | #include <sql.h> |
| 38 | #include <sqlext.h> |
| 39 | #else |
| 40 | #include <rbsql.h> |
| 41 | #include <rbsqlext.h> |
| 42 | #endif |
| 43 | |
| 44 | #define RC_OK(rc) (!((rc)>>1)) |
| 45 | #define MAX_ERROR_MSG 500 |
| 46 | #define MAX_IDENTIFIER 256 |
| 47 | |
| 48 | static char *<a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCName">ODBCName</a>(void); |
| 49 | static int <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCServerInit">ODBCServerInit</a>(char *server, char *hModule, char *driver); |
| 50 | static int <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCOpenDb">ODBCOpenDb</a>(Ns_DbHandle *handle); |
| 51 | static int <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCloseDb">ODBCCloseDb</a>(Ns_DbHandle *handle); |
| 52 | static int <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCGetRow">ODBCGetRow</a>(Ns_DbHandle *handle, Ns_Set *row); |
| 53 | static int <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCancel">ODBCCancel</a>(Ns_DbHandle *handle); |
| 54 | static int <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCExec">ODBCExec</a>(Ns_DbHandle *handle, char *sql); |
| 55 | static Ns_Set *<a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCBindRow">ODBCBindRow</a>(Ns_DbHandle *handle); |
| 56 | static int <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a>(Ns_DbHandle *handle); |
| 57 | static void <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(RETCODE rc, Ns_DbHandle *handle); |
| 58 | static char *odbcName = "ODBC"; |
| 59 | static HENV odbcenv; |
| 60 | static Tcl_CmdProc <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCmd">ODBCCmd</a>; |
| 61 | |
| 62 | static Ns_DbProc odbcProcs[] = { |
| 63 | {DbFn_Name, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCName">ODBCName</a>}, |
| 64 | {DbFn_ServerInit, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCServerInit">ODBCServerInit</a>}, |
| 65 | {DbFn_OpenDb, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCOpenDb">ODBCOpenDb</a>}, |
| 66 | {DbFn_CloseDb, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCloseDb">ODBCCloseDb</a>}, |
| 67 | {DbFn_GetRow, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCGetRow">ODBCGetRow</a>}, |
| 68 | {DbFn_Flush, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCancel">ODBCCancel</a>}, |
| 69 | {DbFn_Cancel, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCancel">ODBCCancel</a>}, |
| 70 | {DbFn_Exec, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCExec">ODBCExec</a>}, |
| 71 | {DbFn_BindRow, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCBindRow">ODBCBindRow</a>}, |
| 72 | {0, NULL} |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | *---------------------------------------------------------------------- |
| 78 | * |
| 79 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_Ns_DbDriverInit">Ns_DbDriverInit</a> - |
| 80 | * |
| 81 | * ODBC module load routine. |
| 82 | * |
| 83 | * Results: |
| 84 | * NS_OK or NS_ERROR. |
| 85 | * |
| 86 | * Side effects: |
| 87 | * Initializes ODBC. |
| 88 | * |
| 89 | *---------------------------------------------------------------------- |
| 90 | */ |
| 91 | |
| 92 | NS_EXPORT int Ns_ModuleVersion = 1; |
| 93 | |
| 94 | NS_EXPORT int |
| 95 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_Ns_DbDriverInit">Ns_DbDriverInit</a>(char *driver, char *configPath) |
| 96 | { |
| 97 | RETCODE rc; |
| 98 | |
| 99 | rc = SQLAllocEnv(&odbcenv); |
| 100 | if (rc != SQL_SUCCESS) { |
| 101 | <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(Error, "%s: failed to allocate odbc", driver); |
| 102 | return NS_ERROR; |
| 103 | } |
| 104 | if (<a href="/cvs/aolserver/aolserver/nsdb/dbdrv.c#A_Ns_DbRegisterDriver">Ns_DbRegisterDriver</a>(driver, odbcProcs) != NS_OK) { |
| 105 | <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(Error, "%s: failed to register driver", driver); |
| 106 | return NS_ERROR; |
| 107 | } |
| 108 | return NS_OK; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /* |
| 113 | *---------------------------------------------------------------------- |
| 114 | * |
| 115 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCServerInit">ODBCServerInit</a> - |
| 116 | * |
| 117 | * Initialize the server when nsdb is ready. |
| 118 | * |
| 119 | * Results: |
| 120 | * NS_OK or NS_ERROR. |
| 121 | * |
| 122 | * Side effects: |
| 123 | * Adds ns_odbc command. |
| 124 | * |
| 125 | *---------------------------------------------------------------------- |
| 126 | */ |
| 127 | |
| 128 | static int |
| 129 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_AddCmd">AddCmd</a>(Tcl_Interp *interp, void *ignored) |
| 130 | { |
| 131 | Tcl_CreateCommand(interp, "ns_odbc", <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCmd">ODBCCmd</a>, NULL, NULL); |
| 132 | return NS_OK; |
| 133 | } |
| 134 | |
| 135 | static int |
| 136 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCServerInit">ODBCServerInit</a>(char *server, char *hModule, char *driver) |
| 137 | { |
| 138 | return <a href="/cvs/aolserver/aolserver/nsd/tclinit.c#A_Ns_TclInitInterps">Ns_TclInitInterps</a>(server, <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_AddCmd">AddCmd</a>, NULL); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /* |
| 143 | *---------------------------------------------------------------------- |
| 144 | * |
| 145 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCName">ODBCName</a> - |
| 146 | * |
| 147 | * Return the ODBC driver name. |
| 148 | * |
| 149 | * Results: |
| 150 | * Pointer to static string name. |
| 151 | * |
| 152 | * Side effects: |
| 153 | * None. |
| 154 | * |
| 155 | *---------------------------------------------------------------------- |
| 156 | */ |
| 157 | |
| 158 | static char * |
| 159 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCName">ODBCName</a>(void) |
| 160 | { |
| 161 | return odbcName; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | *---------------------------------------------------------------------- |
| 167 | * |
| 168 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCOpenDb">ODBCOpenDb</a> - |
| 169 | * |
| 170 | * Open an ODBC datasource. |
| 171 | * |
| 172 | * Results: |
| 173 | * NS_OK or NS_ERROR. |
| 174 | * |
| 175 | * Side effects: |
| 176 | * Performs both SQLAllocConnect and SQLConnect. |
| 177 | * |
| 178 | *---------------------------------------------------------------------- |
| 179 | */ |
| 180 | |
| 181 | static int |
| 182 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCOpenDb">ODBCOpenDb</a>(Ns_DbHandle *handle) |
| 183 | { |
| 184 | HDBC hdbc; |
| 185 | RETCODE rc; |
| 186 | int userlen, passwordlen; |
| 187 | |
| 188 | handle->connected = NS_FALSE; |
| 189 | handle->connection = NULL; |
| 190 | handle->statement = NULL; |
| 191 | rc = SQLAllocConnect(odbcenv, &hdbc); |
| 192 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 193 | if (!RC_OK(rc)) { |
| 194 | return NS_ERROR; |
| 195 | } |
| 196 | if (handle->user != NULL) { |
| 197 | userlen = strlen(handle->user); |
| 198 | } else { |
| 199 | userlen = 0; |
| 200 | } |
| 201 | if (handle->password != NULL) { |
| 202 | passwordlen = strlen(handle->password); |
| 203 | } else { |
| 204 | passwordlen = 0; |
| 205 | } |
| 206 | <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(Notice, "%s[%s]: attemping to open '%s'", |
| 207 | handle->driver, handle->poolname, handle->datasource); |
| 208 | rc = SQLConnect(hdbc, handle->datasource, |
| 209 | (SWORD) strlen(handle->datasource), handle->user, |
| 210 | (SWORD) userlen, handle->password, (SWORD) passwordlen); |
| 211 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 212 | if (!RC_OK(rc)) { |
| 213 | return NS_ERROR; |
| 214 | } |
| 215 | handle->connection = (void *) hdbc; |
| 216 | handle->connected = NS_TRUE; |
| 217 | return NS_OK; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /* |
| 222 | *---------------------------------------------------------------------- |
| 223 | * |
| 224 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCloseDb">ODBCCloseDb</a> - |
| 225 | * |
| 226 | * Close an ODBC datasource. |
| 227 | * |
| 228 | * Results: |
| 229 | * NS_OK or NS_ERROR. |
| 230 | * |
| 231 | * Side effects: |
| 232 | * Performs both SQLDisconnect and SQLFreeConnect. |
| 233 | * |
| 234 | *---------------------------------------------------------------------- |
| 235 | */ |
| 236 | |
| 237 | static int |
| 238 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCloseDb">ODBCCloseDb</a>(Ns_DbHandle *handle) |
| 239 | { |
| 240 | RETCODE rc; |
| 241 | |
| 242 | rc = SQLDisconnect((HDBC) handle->connection); |
| 243 | if (!RC_OK(rc)) { |
| 244 | return NS_ERROR; |
| 245 | } |
| 246 | rc = SQLFreeConnect((HDBC) handle->connection); |
| 247 | if (!RC_OK(rc)) { |
| 248 | return NS_ERROR; |
| 249 | } |
| 250 | handle->connection = NULL; |
| 251 | handle->connected = NS_FALSE; |
| 252 | return NS_OK; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* |
| 257 | *---------------------------------------------------------------------- |
| 258 | * |
| 259 | * ODBExec - |
| 260 | * |
| 261 | * Send an SQL statement. |
| 262 | * |
| 263 | * Results: |
| 264 | * NS_DML, NS_ROWS, or NS_ERROR. |
| 265 | * |
| 266 | * Side effects: |
| 267 | * Database may be modified or rows may be waiting. |
| 268 | * |
| 269 | *---------------------------------------------------------------------- |
| 270 | */ |
| 271 | |
| 272 | static int |
| 273 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCExec">ODBCExec</a>(Ns_DbHandle *handle, char *sql) |
| 274 | { |
| 275 | HSTMT hstmt; |
| 276 | RETCODE rc; |
| 277 | int status; |
| 278 | short numcols; |
| 279 | |
| 280 | /* |
| 281 | * Allocate a new statement. |
| 282 | */ |
| 283 | |
| 284 | rc = SQLAllocStmt((HDBC) handle->connection, &hstmt); |
| 285 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 286 | if (!RC_OK(rc)) { |
| 287 | return NS_ERROR; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Send the SQL and determine if rows are available. |
| 292 | */ |
| 293 | |
| 294 | handle->statement = hstmt; |
| 295 | rc = SQLExecDirect(hstmt, sql, SQL_NTS); |
| 296 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 297 | if (RC_OK(rc)) { |
| 298 | rc = SQLNumResultCols(hstmt, &numcols); |
| 299 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 300 | if (RC_OK(rc)) { |
| 301 | if (numcols != 0) { |
| 302 | handle->fetchingRows = 1; |
| 303 | status = NS_ROWS; |
| 304 | } else { |
| 305 | status = NS_DML; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Free the statement unless rows are waiting. |
| 312 | */ |
| 313 | |
| 314 | if (!RC_OK(rc)) { |
| 315 | status = NS_ERROR; |
| 316 | } |
| 317 | if (status != NS_ROWS && <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a>(handle) != NS_OK) { |
| 318 | status = NS_ERROR; |
| 319 | } |
| 320 | return status; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /* |
| 325 | *---------------------------------------------------------------------- |
| 326 | * |
| 327 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCBindRow">ODBCBindRow</a> - |
| 328 | * |
| 329 | * Bind row after an <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCExec">ODBCExec</a> which returned NS_ROWS. |
| 330 | * |
| 331 | * Results: |
| 332 | * NS_OK or NS_ERROR. |
| 333 | * |
| 334 | * Side effects: |
| 335 | * Given Ns_Set is modified with column names. |
| 336 | * |
| 337 | *---------------------------------------------------------------------- |
| 338 | */ |
| 339 | |
| 340 | static Ns_Set * |
| 341 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCBindRow">ODBCBindRow</a>(Ns_DbHandle *handle) |
| 342 | { |
| 343 | HSTMT hstmt; |
| 344 | RETCODE rc; |
| 345 | Ns_Set *row; |
| 346 | short i, numcols; |
| 347 | char colname[100]; |
| 348 | short cbcolname; |
| 349 | SWORD sqltype, ibscale, nullable; |
| 350 | UDWORD cbcoldef; |
| 351 | |
| 352 | if (!handle->fetchingRows) { |
| 353 | <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(Error, "%s[%s]: no waiting rows", |
| 354 | handle->driver, handle->poolname); |
| 355 | return NULL; |
| 356 | } |
| 357 | row = handle->row; |
| 358 | hstmt = (HSTMT) handle->statement; |
| 359 | rc = SQLNumResultCols(hstmt, &numcols); |
| 360 | for (i = 1; RC_OK(rc) && i <= numcols; i++) { |
| 361 | rc = SQLDescribeCol(hstmt, i, colname, sizeof(colname), |
| 362 | &cbcolname, &sqltype, &cbcoldef, &ibscale, &nullable); |
| 363 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 364 | if (RC_OK(rc)) { |
| 365 | <a href="/cvs/aolserver/aolserver/nsd/set.c#A_Ns_SetPut">Ns_SetPut</a>(row, colname, NULL); |
| 366 | } |
| 367 | } |
| 368 | if (!RC_OK(rc)) { |
| 369 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a>(handle); |
| 370 | row = NULL; |
| 371 | } |
| 372 | return row; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* |
| 377 | *---------------------------------------------------------------------- |
| 378 | * |
| 379 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCGetRow">ODBCGetRow</a> - |
| 380 | * |
| 381 | * Fetch the next row. |
| 382 | * |
| 383 | * Results: |
| 384 | * NS_OK or NS_ERROR. |
| 385 | * |
| 386 | * Side effects: |
| 387 | * Given Ns_Set is modified with new values. |
| 388 | * |
| 389 | *---------------------------------------------------------------------- |
| 390 | */ |
| 391 | |
| 392 | static int |
| 393 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCGetRow">ODBCGetRow</a>(Ns_DbHandle *handle, Ns_Set *row) |
| 394 | { |
| 395 | RETCODE rc; |
| 396 | short i; |
| 397 | HSTMT hstmt; |
| 398 | short numcols; |
| 399 | |
| 400 | if (!handle->fetchingRows) { |
| 401 | <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(Error, "%s[%s]: no waiting rows", |
| 402 | handle->driver, handle->poolname); |
| 403 | return NS_ERROR; |
| 404 | } |
| 405 | hstmt = (HSTMT) handle->statement; |
| 406 | rc = SQLNumResultCols(hstmt, &numcols); |
| 407 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 408 | if (!RC_OK(rc)) { |
| 409 | goto error; |
| 410 | } |
| 411 | if (numcols != Ns_SetSize(row)) { |
| 412 | <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(Error, "%s[%s]: mismatched number of rows", |
| 413 | handle->driver, handle->poolname); |
| 414 | goto error; |
| 415 | } |
| 416 | rc = SQLFetch(hstmt); |
| 417 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 418 | if (rc == SQL_NO_DATA_FOUND) { |
| 419 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a>(handle); |
| 420 | return NS_END_DATA; |
| 421 | } |
| 422 | for (i = 1; RC_OK(rc) && i <= numcols; i++) { |
| 423 | char datum[8192]; |
| 424 | SDWORD cbvalue; |
| 425 | |
| 426 | rc = SQLGetData(hstmt, i, SQL_C_CHAR, datum, sizeof(datum), &cbvalue); |
| 427 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 428 | if (RC_OK(rc)) { |
| 429 | <a href="/cvs/aolserver/aolserver/nsd/set.c#A_Ns_SetPutValue">Ns_SetPutValue</a>(row, i - 1, cbvalue == SQL_NULL_DATA ? "" : datum); |
| 430 | } |
| 431 | } |
| 432 | if (!RC_OK(rc)) { |
| 433 | error: |
| 434 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a>(handle); |
| 435 | return NS_ERROR; |
| 436 | } |
| 437 | return NS_OK; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /* |
| 442 | *---------------------------------------------------------------------- |
| 443 | * |
| 444 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCancel">ODBCCancel</a> - |
| 445 | * |
| 446 | * Cancel the active select. |
| 447 | * |
| 448 | * Results: |
| 449 | * NS_OK or NS_ERROR. |
| 450 | * |
| 451 | * Side effects: |
| 452 | * Rows are flushed. |
| 453 | * |
| 454 | *---------------------------------------------------------------------- |
| 455 | */ |
| 456 | |
| 457 | static int |
| 458 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCancel">ODBCCancel</a>(Ns_DbHandle *handle) |
| 459 | { |
| 460 | RETCODE rc; |
| 461 | int status; |
| 462 | |
| 463 | status = NS_OK; |
| 464 | if (handle->fetchingRows) { |
| 465 | rc = SQLCancel((HSTMT) handle->statement); |
| 466 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 467 | status = <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a>(handle); |
| 468 | } |
| 469 | return status; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* |
| 474 | *---------------------------------------------------------------------- |
| 475 | * |
| 476 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCmd">ODBCCmd</a> - |
| 477 | * |
| 478 | * Process the ns_odbc command. |
| 479 | * |
| 480 | * Results: |
| 481 | * Standard Tcl result. |
| 482 | * |
| 483 | * Side effects: |
| 484 | * None. |
| 485 | * |
| 486 | *---------------------------------------------------------------------- |
| 487 | */ |
| 488 | |
| 489 | static int |
| 490 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCCmd">ODBCCmd</a>(ClientData dummy, Tcl_Interp *interp, int argc, char **argv) |
| 491 | { |
| 492 | Ns_DbHandle *handle; |
| 493 | RETCODE rc; |
| 494 | char buf[MAX_IDENTIFIER]; |
| 495 | SWORD FAR cbInfoValue; |
| 496 | |
| 497 | if (argc != 3) { |
| 498 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 499 | argv[0], " cmd handle\"", NULL); |
| 500 | return TCL_ERROR; |
| 501 | } |
| 502 | if (<a href="/cvs/aolserver/aolserver/nsdb/dbtcl.c#A_Ns_TclDbGetHandle">Ns_TclDbGetHandle</a>(interp, argv[2], &handle) != TCL_OK) { |
| 503 | return TCL_ERROR; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Make sure this is an open ODBC handle. |
| 508 | */ |
| 509 | |
| 510 | if (<a href="/cvs/aolserver/aolserver/nsdb/dbdrv.c#A_Ns_DbDriverName">Ns_DbDriverName</a>(handle) != odbcName) { |
| 511 | Tcl_AppendResult(interp, "handle \"", argv[1], "\" is not of type \"", |
| 512 | odbcName, "\"", NULL); |
| 513 | return TCL_ERROR; |
| 514 | } |
| 515 | |
| 516 | if (handle->connection == NULL) { |
| 517 | Tcl_AppendResult(interp, "handle \"", argv[1], "\" not connected", NULL); |
| 518 | return TCL_ERROR; |
| 519 | } |
| 520 | |
| 521 | if (STREQ(argv[1], "dbmsname")) { |
| 522 | rc = SQLGetInfo((HDBC) handle->connection, SQL_DBMS_NAME, buf, sizeof(buf), &cbInfoValue); |
| 523 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 524 | if (!RC_OK(rc)) { |
| 525 | Tcl_SetResult(interp, "could not determine dbmsname", TCL_STATIC); |
| 526 | return TCL_ERROR; |
| 527 | } |
| 528 | } else if (STREQ(argv[1], "dbmsver")) { |
| 529 | rc = SQLGetInfo((HDBC) handle->connection, SQL_DBMS_VER, buf, sizeof(buf), &cbInfoValue); |
| 530 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(rc, handle); |
| 531 | if (!RC_OK(rc)) { |
| 532 | Tcl_SetResult(interp, "could not determine dbmsver", TCL_STATIC); |
| 533 | return TCL_ERROR; |
| 534 | } |
| 535 | } else { |
| 536 | Tcl_AppendResult(interp, "unknown command \"", argv[1], |
| 537 | "\": should be dbmsname or dbmsver.", NULL); |
| 538 | return TCL_ERROR; |
| 539 | } |
| 540 | |
| 541 | Tcl_SetResult(interp, buf, TCL_VOLATILE); |
| 542 | return TCL_OK; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | /* |
| 547 | *---------------------------------------------------------------------- |
| 548 | * |
| 549 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a> - |
| 550 | * |
| 551 | * <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Log">Log</a> a possible error. |
| 552 | * |
| 553 | * Results: |
| 554 | * None. |
| 555 | * |
| 556 | * Side effects: |
| 557 | * May add log entry. |
| 558 | * |
| 559 | *---------------------------------------------------------------------- |
| 560 | */ |
| 561 | |
| 562 | static void |
| 563 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCLog">ODBCLog</a>(RETCODE rc, Ns_DbHandle *handle) |
| 564 | { |
| 565 | HDBC hdbc; |
| 566 | HSTMT hstmt; |
| 567 | Ns_LogSeverity severity; |
| 568 | char szSQLSTATE[6]; |
| 569 | SDWORD nErr; |
| 570 | char msg[MAX_ERROR_MSG + 1]; |
| 571 | SWORD cbmsg; |
| 572 | |
| 573 | if (rc == SQL_SUCCESS_WITH_INFO) { |
| 574 | severity = Warning; |
| 575 | } else if (rc == SQL_ERROR) { |
| 576 | severity = Error; |
| 577 | } else { |
| 578 | return; |
| 579 | } |
| 580 | hdbc = (HDBC) handle->connection; |
| 581 | hstmt = (HSTMT) handle->statement; |
| 582 | while (SQLError(odbcenv, hdbc, hstmt, szSQLSTATE, &nErr, msg, |
| 583 | sizeof(msg), &cbmsg) == SQL_SUCCESS) { |
| 584 | <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(severity, "%s[%s]: odbc message: " |
| 585 | "SQLSTATE = %s, Native err = %ld, msg = '%s'", |
| 586 | handle->driver, handle->poolname, szSQLSTATE, nErr, msg); |
| 587 | strcpy(handle->cExceptionCode, szSQLSTATE); |
| 588 | <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringFree">Ns_DStringFree</a>(&(handle->dsExceptionMsg)); |
| 589 | <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringAppend">Ns_DStringAppend</a>(&(handle->dsExceptionMsg), msg); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | *---------------------------------------------------------------------- |
| 595 | * |
| 596 | * <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a> - |
| 597 | * |
| 598 | * Free the handle's current statement. |
| 599 | * |
| 600 | * Results: |
| 601 | * NS_OK or NS_ERROR. |
| 602 | * |
| 603 | * Side effects: |
| 604 | * None. |
| 605 | * |
| 606 | *---------------------------------------------------------------------- |
| 607 | */ |
| 608 | |
| 609 | static int |
| 610 | <a href="/cvs/aolserver/nsodbc/nsodbc.c#A_ODBCFreeStmt">ODBCFreeStmt</a>(Ns_DbHandle *handle) |
| 611 | { |
| 612 | RETCODE rc; |
| 613 | |
| 614 | rc = SQLFreeStmt((HSTMT) handle->statement, SQL_DROP); |
| 615 | handle->statement = NULL; |
| 616 | handle->fetchingRows = 0; |
| 617 | if (!RC_OK(rc)) { |
| 618 | return NS_ERROR; |
| 619 | } |
| 620 | return NS_OK; |
| 621 | } |
Copyright © 2010 Geeknet, Inc. All rights reserved. Terms of Use