index.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*!
  2. \file lib/db/dbmi_base/index.c
  3. \brief DBMI Library (base) - index management
  4. (C) 1999-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Joel Jones (CERL/UIUC), Radim Blazek
  8. \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
  9. */
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <grass/dbmi.h>
  13. #include <grass/glocale.h>
  14. /*!
  15. \brief Initialize dbIndex
  16. \param index pointer to dbIndex to be initialized
  17. */
  18. void db_init_index(dbIndex * index)
  19. {
  20. db_init_string(&index->indexName);
  21. db_init_string(&index->tableName);
  22. index->numColumns = 0;
  23. index->columnNames = NULL;
  24. index->unique = 0;
  25. }
  26. /*!
  27. \brief Free allocated dbIndex
  28. \param index pointer to dbIndex to be freed
  29. */
  30. void db_free_index(dbIndex * index)
  31. {
  32. db_free_string(&index->indexName);
  33. db_free_string(&index->tableName);
  34. if (index->numColumns > 0)
  35. db_free_string_array(index->columnNames, index->numColumns);
  36. db_init_index(index);
  37. }
  38. /*!
  39. \brief Allocate index columns
  40. \param index pointer to dbIndex
  41. \param ncols number of columns to be allocated
  42. \return DB_OK
  43. */
  44. int db_alloc_index_columns(dbIndex * index, int ncols)
  45. {
  46. index->columnNames = db_alloc_string_array(ncols);
  47. if (index->columnNames == NULL)
  48. return db_get_error_code();
  49. index->numColumns = ncols;
  50. return DB_OK;
  51. }
  52. /*!
  53. \brief Allocate index array
  54. \param count number of items
  55. \return pointer to allocated dbIndex array
  56. */
  57. dbIndex *db_alloc_index_array(int count)
  58. {
  59. dbIndex *list;
  60. int i;
  61. list = (dbIndex *) db_calloc(count, sizeof(dbIndex));
  62. if (list) {
  63. for (i = 0; i < count; i++)
  64. db_init_index(&list[i]);
  65. }
  66. return list;
  67. }
  68. /*!
  69. \brief Free index array
  70. \param list dbIndex array
  71. \param count number of items in the array
  72. */
  73. void db_free_index_array(dbIndex * list, int count)
  74. {
  75. int i;
  76. if (list) {
  77. for (i = 0; i < count; i++)
  78. db_free_index(&list[i]);
  79. db_free(list);
  80. }
  81. }
  82. /*!
  83. \brief Set index name
  84. \param index pointer to dbIndex
  85. \param name name to be set
  86. \return DB_OK on success
  87. \return DB_FAILED on error
  88. */
  89. int db_set_index_name(dbIndex * index, const char *name)
  90. {
  91. return db_set_string(&index->indexName, name);
  92. }
  93. /*!
  94. \brief Get index name
  95. \param index pointer to dbIndex
  96. \return string buffer with name
  97. */
  98. const char *db_get_index_name(dbIndex * index)
  99. {
  100. return db_get_string(&index->indexName);
  101. }
  102. /*!
  103. \brief Set table name
  104. \param index pointer to dbIndex
  105. \param name name to be set
  106. \return DB_OK on success
  107. \return DB_FAILED on error
  108. */
  109. int db_set_index_table_name(dbIndex * index, const char *name)
  110. {
  111. return db_set_string(&index->tableName, name);
  112. }
  113. /*!
  114. \brief Get table name
  115. \param index pointer to dbIndex
  116. \return string buffer with name
  117. */
  118. const char *db_get_index_table_name(dbIndex * index)
  119. {
  120. return db_get_string(&index->tableName);
  121. }
  122. /*!
  123. \brief Get number of columns
  124. \param index pointer to dbIndex
  125. \return number of columns
  126. */
  127. int db_get_index_number_of_columns(dbIndex * index)
  128. {
  129. return index->numColumns;
  130. }
  131. /*!
  132. \brief Set column name
  133. \param index pointer to dbIndex
  134. \param column_num column number
  135. \param name name to be set
  136. \return DB_OK on success
  137. \return DB_FAILED on error
  138. */
  139. int db_set_index_column_name(dbIndex * index, int column_num, const char *name)
  140. {
  141. if (column_num < 0 || column_num >= index->numColumns) {
  142. db_error(_("db_set_index_column_name(): invalid column number"));
  143. return db_get_error_code();
  144. }
  145. return db_set_string(&index->columnNames[column_num], name);
  146. }
  147. /*!
  148. \brief Get column number
  149. \param index pointer to dbIndex
  150. \param column_num column number
  151. \return string buffer with name
  152. */
  153. const char *db_get_index_column_name(dbIndex * index, int column_num)
  154. {
  155. if (column_num < 0 || column_num >= index->numColumns) {
  156. db_error(_("db_get_index_column_name(): invalid column number"));
  157. return ((const char *)NULL);
  158. }
  159. return db_get_string(&index->columnNames[column_num]);
  160. }
  161. /*!
  162. \brief Set index type to unique
  163. \todo return type void?
  164. \param index pointer to dbIndex
  165. \return 0
  166. */
  167. int db_set_index_type_unique(dbIndex * index)
  168. {
  169. index->unique = 1;
  170. return 0;
  171. }
  172. /*!
  173. \brief Set index type to non-unique
  174. \todo return type void?
  175. \param index pointer to dbIndex
  176. \return 0
  177. */
  178. int db_set_index_type_non_unique(dbIndex * index)
  179. {
  180. index->unique = 0;
  181. return 0;
  182. }
  183. /*!
  184. \brief Test if type is unique
  185. \param index pointer to dbIndex
  186. \return non-zero if True
  187. \return zero if False
  188. */
  189. int db_test_index_type_unique(dbIndex * index)
  190. {
  191. return index->unique != 0;
  192. }
  193. /*!
  194. \brief Report index
  195. \param fd file where to print index info
  196. \param index pointer to dbIndex
  197. */
  198. void db_print_index(FILE * fd, dbIndex * index)
  199. {
  200. int i, nCols;
  201. fprintf(fd, "Name: %s\n", db_get_index_name(index));
  202. if (db_test_index_type_unique(index))
  203. fprintf(fd, "Unique: true\n");
  204. else
  205. fprintf(fd, "Unique: false\n");
  206. fprintf(fd, "Table: %s\n", db_get_index_table_name(index));
  207. nCols = db_get_index_number_of_columns(index);
  208. fprintf(fd, "Number of columns: %d\nColumns:\n", nCols);
  209. for (i = 0; i < nCols; i++) {
  210. fprintf(fd, " %s\n", db_get_index_column_name(index, i));
  211. }
  212. }