driver_state.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*!
  2. * \file db/dbmi_driver/driver_state.c
  3. *
  4. * \brief DBMI Library (driver) - drivers state
  5. *
  6. * (C) 1999-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author Joel Jones (CERL/UIUC), Radim Blazek
  13. */
  14. #include <stdlib.h>
  15. #include <grass/dbmi.h>
  16. #include "dbstubs.h"
  17. static dbDriverState state;
  18. /*!
  19. \brief Initialize driver state
  20. */
  21. void db__init_driver_state(void)
  22. {
  23. db_zero((void *)&state, sizeof(state));
  24. }
  25. /*!
  26. \brief Get driver state
  27. \return pointer to dbDriverState
  28. */
  29. dbDriverState *db__get_driver_state(void)
  30. {
  31. return &state;
  32. }
  33. /*!
  34. \brief Test database connection
  35. \return 1 opened
  36. \return 0 closed
  37. */
  38. int db__test_database_open(void)
  39. {
  40. return state.open ? 1 : 0;
  41. }
  42. /*!
  43. \brief Mark database as opened
  44. \param dbname database name
  45. \param dbschema database schema name
  46. */
  47. void db__mark_database_open(const char *dbname, const char *dbschema)
  48. {
  49. state.dbname = db_store(dbname);
  50. state.dbschema = db_store(dbschema);
  51. state.open = 1;
  52. }
  53. /*!
  54. \brief Mark database as closed
  55. */
  56. void db__mark_database_closed(void)
  57. {
  58. db_free(state.dbname);
  59. db_free(state.dbschema);
  60. state.open = 0;
  61. }
  62. /*!
  63. \brief Add cursor do driver state
  64. \param cursor db cursor to be added
  65. */
  66. void db__add_cursor_to_driver_state(dbCursor * cursor)
  67. {
  68. dbCursor **list;
  69. int i;
  70. /* find an empty slot in the cursor list */
  71. list = state.cursor_list;
  72. for (i = 0; i < state.ncursors; i++)
  73. if (list[i] == NULL)
  74. break;
  75. /* if not found, extend list */
  76. if (i >= state.ncursors) {
  77. list =
  78. (dbCursor **) db_realloc((void *)list,
  79. (i + 1) * sizeof(dbCursor *));
  80. if (list == NULL)
  81. return;
  82. state.cursor_list = list;
  83. state.ncursors = i + 1;
  84. }
  85. /* add it in */
  86. list[i] = cursor;
  87. }
  88. /*!
  89. \brief Drop cursor from driver state
  90. \param cursor db cursor to be dropped
  91. */
  92. void db__drop_cursor_from_driver_state(dbCursor * cursor)
  93. {
  94. int i;
  95. for (i = 0; i < state.ncursors; i++)
  96. if (state.cursor_list[i] == cursor)
  97. state.cursor_list[i] = NULL;
  98. }
  99. /*!
  100. \brief Close all cursors
  101. */
  102. void db__close_all_cursors(void)
  103. {
  104. int i;
  105. for (i = 0; i < state.ncursors; i++)
  106. if (state.cursor_list[i])
  107. db_driver_close_cursor(state.cursor_list[i]);
  108. if (state.cursor_list)
  109. db_free(state.cursor_list);
  110. state.ncursors = 0;
  111. state.cursor_list = NULL;
  112. }