listdb.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <grass/dbmi.h>
  2. #include <grass/glocale.h>
  3. #include "odbc.h"
  4. #include "globals.h"
  5. #include "proto.h"
  6. int db__driver_list_databases(dbString *dbpath, int npaths, dbHandle **dblist, int *dbcount)
  7. {
  8. int i, count = 0;
  9. dbHandle *list;
  10. char dsn[SQL_MAX_DSN_LENGTH], desc[100];
  11. SQLUSMALLINT next;
  12. *dblist = NULL;
  13. *dbcount = 0;
  14. if (open_connection() != DB_OK)
  15. return DB_FAILED;
  16. next = SQL_FETCH_FIRST;
  17. while (SQLDataSources(ODenvi, next, dsn, sizeof(dsn),
  18. NULL, desc, sizeof(desc), NULL) == SQL_SUCCESS) {
  19. next = SQL_FETCH_NEXT;
  20. count++;
  21. }
  22. list = db_alloc_handle_array(count);
  23. if (list == NULL) {
  24. db_d_append_error(_("Unable to allocate handle."));
  25. db_d_report_error();
  26. return DB_FAILED;
  27. }
  28. i = 0;
  29. next = SQL_FETCH_FIRST;
  30. while (SQLDataSources(ODenvi, next, dsn, sizeof(dsn),
  31. NULL, desc, sizeof(desc), NULL) == SQL_SUCCESS) {
  32. db_init_handle(&list[i]);
  33. if (db_set_handle(&list[i], dsn, desc) != DB_OK) {
  34. db_d_append_error(_("Unable to set handle"));
  35. db_d_report_error();
  36. db_free_handle_array(list, count);
  37. return DB_FAILED;
  38. }
  39. next = SQL_FETCH_NEXT;
  40. i++;
  41. }
  42. *dblist = list;
  43. *dbcount = count;
  44. close_connection();
  45. return DB_OK;
  46. }