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