select.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <grass/dbmi.h>
  2. #include "odbc.h"
  3. #include "globals.h"
  4. #include "proto.h"
  5. #include <stdio.h>
  6. int db__driver_open_select_cursor(dbString *sel, dbCursor *dbc, int mode)
  7. {
  8. cursor *c;
  9. SQLRETURN ret;
  10. SQLINTEGER err;
  11. char *sql, msg[OD_MSG];
  12. dbTable *table;
  13. int nrows;
  14. /* allocate cursor */
  15. c = alloc_cursor();
  16. if (c == NULL)
  17. return DB_FAILED;
  18. db_set_cursor_mode(dbc, mode);
  19. db_set_cursor_type_readonly(dbc);
  20. sql = db_get_string(sel);
  21. ret = SQLExecDirect(c->stmt, sql, SQL_NTS);
  22. if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
  23. SQLGetDiagRec(SQL_HANDLE_STMT, c->stmt, 1, NULL, &err, msg,
  24. sizeof(msg), NULL);
  25. db_d_append_error("SQLExecDirect():\n%s\n%s (%d)", sql, msg, (int)err);
  26. db_d_report_error();
  27. return DB_FAILED;
  28. }
  29. describe_table(c->stmt, &table);
  30. db_set_cursor_table(dbc, table);
  31. /* record table with dbCursor */
  32. db_set_cursor_table(dbc, table);
  33. /* set dbCursor's token for my cursor */
  34. db_set_cursor_token(dbc, c->token);
  35. /* It seems that there is no function in ODBC to get number of selected rows.
  36. * SQLRowCount() works for insert, update, delete. */
  37. nrows = 0;
  38. while (1) {
  39. ret = SQLFetchScroll(c->stmt, SQL_FETCH_NEXT, 0);
  40. if (ret == SQL_NO_DATA) {
  41. break;
  42. }
  43. if (!SQL_SUCCEEDED(ret)) {
  44. return DB_FAILED;
  45. }
  46. nrows++;
  47. }
  48. c->nrows = nrows;
  49. SQLFetchScroll(c->stmt, SQL_FETCH_FIRST, 0);
  50. SQLFetchScroll(c->stmt, SQL_FETCH_PRIOR, 0);
  51. return DB_OK;
  52. }