listtab.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <grass/dbmi.h>
  2. #include "odbc.h"
  3. #include "globals.h"
  4. #include "proto.h"
  5. int db__driver_list_tables(tlist, tcount, system)
  6. dbString **tlist;
  7. int *tcount;
  8. int system;
  9. {
  10. cursor *c;
  11. dbString *list;
  12. int count = 0;
  13. SQLCHAR tableName[SQL_MAX_TABLE_NAME_LEN];
  14. SQLINTEGER indi, nrow = 0;
  15. SQLRETURN ret;
  16. char ttype[50];
  17. *tlist = NULL;
  18. *tcount = 0;
  19. /* allocate cursor */
  20. c = alloc_cursor();
  21. if (c == NULL)
  22. return DB_FAILED;
  23. /* Execute SQL */
  24. if (system)
  25. sprintf(ttype, "SYSTEM TABLE");
  26. else
  27. sprintf(ttype, "TABLE, VIEW");
  28. ret = SQLTables(c->stmt, NULL, 0, NULL, 0, NULL, 0, ttype, sizeof(ttype));
  29. if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
  30. db_d_append_error("SQLTables()");
  31. db_d_report_error();
  32. return DB_FAILED;
  33. }
  34. SQLBindCol(c->stmt, 3, SQL_C_CHAR, tableName, sizeof(tableName), &indi);
  35. /* Get number of rows */
  36. /* WARNING: after SQLTables(), SQLRowCount() doesn't sets number of rows
  37. * to number of tables! ODBC developers said, this is correct. */
  38. nrow = 0;
  39. ret = SQLFetch(c->stmt);
  40. while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
  41. nrow++;
  42. ret = SQLFetch(c->stmt);
  43. }
  44. list = db_alloc_string_array(nrow);
  45. if (list == NULL)
  46. return DB_FAILED;
  47. /* Get table names */
  48. /* ret = SQLFetch( c->stmt ); */
  49. ret = SQLFetchScroll(c->stmt, SQL_FETCH_FIRST, 0);
  50. while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
  51. if (indi == SQL_NULL_DATA) {
  52. if (db_set_string(&list[count], "Unknown") != DB_OK)
  53. return DB_FAILED;
  54. }
  55. else {
  56. if (db_set_string(&list[count], (char *)tableName) != DB_OK)
  57. return DB_FAILED;
  58. }
  59. count++;
  60. ret = SQLFetch(c->stmt);
  61. }
  62. free_cursor(c);
  63. *tlist = list;
  64. *tcount = count;
  65. return DB_OK;
  66. }