main_debug.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*****************************************************************************
  2. * just a test for debugging purpose, imitating dbf driver -a.sh.
  3. *****************************************************************************/
  4. #include <stdlib.h>
  5. #include <grass/dbmi.h>
  6. #include <grass/gis.h>
  7. #include "globals.h"
  8. int main(int argc, char *argv[])
  9. {
  10. dbCursor *cursor;
  11. int stat;
  12. dbToken token;
  13. dbString *select;
  14. int mode = 0;
  15. dbDriver *driver;
  16. dbHandle handle;
  17. driver = db_start_driver("dbf");
  18. if (driver == NULL)
  19. exit(EXIT_FAILURE);
  20. db_init_handle(&handle);
  21. db_set_handle(&handle, "dbf_catalog", NULL);
  22. if (db__test_database_open()) {
  23. db_error("Multiple open databases not allowed");
  24. return DB_OK;
  25. }
  26. /* call the procedure */
  27. stat = db_driver_open_database(&handle);
  28. /* send the return code */
  29. if (stat != DB_OK) {
  30. db_free_handle(&handle);
  31. return DB_OK;
  32. }
  33. /* record the open in the driver state */
  34. db__mark_database_open(db_get_handle_dbname(&handle),
  35. db_get_handle_dbpath(&handle));
  36. /* DO NOT free the handle since we saved the pointers to the name,path */
  37. select = (dbString *) G_malloc(1024);
  38. db_init_string(select);
  39. db_set_string(select,
  40. "select id, quality, flow from river where (flow = 10) or (flow = 20) or (flow = 30) or (flow = 5) or (flow = 7)");
  41. /* create a cursor */
  42. cursor = (dbCursor *) db_malloc(sizeof(dbCursor));
  43. if (cursor == NULL)
  44. return db_get_error_code();
  45. token = db_new_token((dbAddress) cursor);
  46. if (token < 0)
  47. return db_get_error_code();
  48. db_init_cursor(cursor);
  49. cursor->driver = driver;
  50. G_debug(3, "sql is %s", *select);
  51. G_debug(3, "driver is %s", cursor->driver);
  52. /* call the procedure */
  53. stat = db_driver_open_select_cursor(select, cursor, mode);
  54. db_free_string(select);
  55. /* mark this as a readonly cursor */
  56. db_set_cursor_type_readonly(cursor);
  57. /* add this cursor to the cursors managed by the driver state */
  58. db__add_cursor_to_driver_state(cursor);
  59. G_debug(3, "db_d_close_database()");
  60. /* see if a database is open */
  61. if (!db__test_database_open()) {
  62. db_error("no database is open");
  63. G_debug(3, "db_d_close_database(): would sent DB_FAILURE");
  64. return DB_OK;
  65. };
  66. /* make sure all cursors are closed */
  67. db__close_all_cursors();
  68. /* call the procedure */
  69. stat = db_driver_close_database();
  70. G_debug(3, "db_d_close_database(): would have stat = %d", stat);
  71. /* send the return code */
  72. if (stat != DB_OK) {
  73. G_debug(3, "db_d_close_database(): would sent DB_FAILURE");
  74. return DB_OK;
  75. }
  76. G_debug(3, "db_d_close_database(): would sent DB_OK");
  77. /* clear the driver state */
  78. db__mark_database_closed();
  79. db__init_driver_state();
  80. G_debug(3, "main(): ok");
  81. return DB_OK;
  82. }