main_debug.c 2.8 KB

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