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