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