handle.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. \file lib/db/dbmi_base/handle.c
  3. \brief DBMI Library (base) - handle management
  4. (C) 1999-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. \author Joel Jones (CERL/UIUC), Radim Blazek
  9. */
  10. #include <stdlib.h>
  11. #include <grass/dbmi.h>
  12. /*!
  13. \brief Initialize handle (i.e database/schema)
  14. \param handle pointer to dbHandle to be initialized
  15. */
  16. void db_init_handle(dbHandle * handle)
  17. {
  18. db_init_string(&handle->dbName);
  19. db_init_string(&handle->dbSchema);
  20. }
  21. /*!
  22. \brief Set handle (database and schema name)
  23. \param handle pointer to dbHandle
  24. \param dbName database name
  25. \param dbSchema schema name
  26. \return DB_OK on success
  27. \return DB_FAILED on failure
  28. */
  29. int db_set_handle(dbHandle * handle, const char *dbName, const char *dbSchema)
  30. {
  31. int stat;
  32. stat = db_set_string(&handle->dbName, dbName);
  33. if (stat != DB_OK)
  34. return stat;
  35. stat = db_set_string(&handle->dbSchema, dbSchema);
  36. return stat;
  37. }
  38. /*!
  39. \brief Get handle database name
  40. \param handle pointer to dbHandle
  41. \return pointer to string with database name
  42. */
  43. const char *db_get_handle_dbname(dbHandle * handle)
  44. {
  45. return db_get_string(&handle->dbName);
  46. }
  47. /*!
  48. \brief Get handle schema name
  49. \param handle pointer to dbHandle
  50. \return pointer to string with schema name
  51. */
  52. const char *db_get_handle_dbschema(dbHandle * handle)
  53. {
  54. return db_get_string(&handle->dbSchema);
  55. }
  56. /*!
  57. \brief Free dbHandle structure
  58. \param handle pointer to dbHandle
  59. */
  60. void db_free_handle(dbHandle * handle)
  61. {
  62. db_free_string(&handle->dbName);
  63. db_free_string(&handle->dbSchema);
  64. }
  65. /*!
  66. \brief Free array of handles
  67. \param handle pointer to first dbHandle in the array
  68. \param count number of handles in the array
  69. */
  70. void db_free_handle_array(dbHandle * handle, int count)
  71. {
  72. int i;
  73. if (handle) {
  74. for (i = 0; i < count; i++)
  75. db_free_handle(&handle[i]);
  76. db_free((void *) handle);
  77. }
  78. }
  79. /*!
  80. \brief Allocate array of handles
  81. \param count number of handles in the array
  82. \return pointer to first dbHandle in the array
  83. */
  84. dbHandle *db_alloc_handle_array(int count)
  85. {
  86. int i;
  87. dbHandle *handle;
  88. handle = (dbHandle *) db_calloc(count, sizeof(dbHandle));
  89. if (handle)
  90. for (i = 0; i < count; i++)
  91. db_init_handle(&handle[i]);
  92. return handle;
  93. }