GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
db/dbmi_base/index.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdlib.h>
3 #include <grass/dbmi.h>
4 
11 void db_init_index(dbIndex * index)
12 {
13  db_init_string(&index->indexName);
14  db_init_string(&index->tableName);
15  index->numColumns = 0;
16  index->columnNames = NULL;
17  index->unique = 0;
18 }
19 
26 void db_free_index(dbIndex * index)
27 {
28  db_free_string(&index->indexName);
29  db_free_string(&index->tableName);
30  if (index->numColumns > 0)
31  db_free_string_array(index->columnNames, index->numColumns);
32  db_init_index(index);
33 }
34 
41 int db_alloc_index_columns(dbIndex * index, int ncols)
42 {
43  index->columnNames = db_alloc_string_array(ncols);
44  if (index->columnNames == NULL)
45  return db_get_error_code();
46  index->numColumns = ncols;
47 
48  return DB_OK;
49 }
50 
58 {
59  dbIndex *list;
60  int i;
61 
62  list = (dbIndex *) db_calloc(count, sizeof(dbIndex));
63  if (list) {
64  for (i = 0; i < count; i++)
65  db_init_index(&list[i]);
66  }
67  return list;
68 }
69 
76 void db_free_index_array(dbIndex * list, int count)
77 {
78  int i;
79 
80  if (list) {
81  for (i = 0; i < count; i++)
82  db_free_index(&list[i]);
83  db_free(list);
84  }
85 }
86 
93 int db_set_index_name(dbIndex * index, const char *name)
94 {
95  return db_set_string(&index->indexName, name);
96 }
97 
104 const char *db_get_index_name(dbIndex * index)
105 {
106  return db_get_string(&index->indexName);
107 }
108 
115 int db_set_index_table_name(dbIndex * index, const char *name)
116 {
117  return db_set_string(&index->tableName, name);
118 }
119 
126 const char *db_get_index_table_name(dbIndex * index)
127 {
128  return db_get_string(&index->tableName);
129 }
130 
137 int db_get_index_number_of_columns(dbIndex * index)
138 {
139  return index->numColumns;
140 }
141 
148 int
149 db_set_index_column_name(dbIndex * index, int column_num, const char *name)
150 {
151  if (column_num < 0 || column_num >= index->numColumns) {
152  db_error("db_set_index_column_name(): invalid column number");
153  return db_get_error_code();
154  }
155  return db_set_string(&index->columnNames[column_num], name);
156 }
157 
164 const char *db_get_index_column_name(dbIndex * index, int column_num)
165 {
166  if (column_num < 0 || column_num >= index->numColumns) {
167  db_error("db_get_index_column_name(): invalid column number");
168  return ((const char *)NULL);
169  }
170  return db_get_string(&index->columnNames[column_num]);
171 }
172 
179 int db_set_index_type_unique(dbIndex * index)
180 {
181  index->unique = 1;
182 
183  return 0;
184 }
185 
192 int db_set_index_type_non_unique(dbIndex * index)
193 {
194  index->unique = 0;
195 
196  return 0;
197 }
198 
205 int db_test_index_type_unique(dbIndex * index)
206 {
207  return index->unique != 0;
208 }
209 
216 void db_print_index(FILE * fd, dbIndex * index)
217 {
218  int i, nCols;
219 
220  fprintf(fd, "Name: %s\n", db_get_index_name(index));
221  if (db_test_index_type_unique(index))
222  fprintf(fd, "Unique: true\n");
223  else
224  fprintf(fd, "Unique: false\n");
225  fprintf(fd, "Table: %s\n", db_get_index_table_name(index));
226  nCols = db_get_index_number_of_columns(index);
227  fprintf(fd, "Number of columns: %d\nColumns:\n", nCols);
228 
229  for (i = 0; i < nCols; i++) {
230  fprintf(fd, " %s\n", db_get_index_column_name(index, i));
231  }
232 }
int db_set_index_type_unique(dbIndex *index)
int db_get_index_number_of_columns(dbIndex *index)
int db_set_index_type_non_unique(dbIndex *index)
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
void db_free_index_array(dbIndex *list, int count)
void db_init_index(dbIndex *index)
void * db_calloc(int n, int m)
void db_free_index(dbIndex *index)
int db_alloc_index_columns(dbIndex *index, int ncols)
int count
const char * db_get_index_name(dbIndex *index)
void db_error(const char *s)
int db_set_index_name(dbIndex *index, const char *name)
int db_set_index_column_name(dbIndex *index, int column_num, const char *name)
int db_get_error_code(void)
void db_print_index(FILE *fd, dbIndex *index)
const char * db_get_index_table_name(dbIndex *index)
const char * db_get_index_column_name(dbIndex *index, int column_num)
void db_free_string_array(dbString *a, int n)
Definition: string.c:155
int db_test_index_type_unique(dbIndex *index)
return NULL
Definition: dbfopen.c:1394
dbString * db_alloc_string_array(int count)
Definition: string.c:172
char * db_get_string(dbString *x)
Definition: string.c:131
int db_set_index_table_name(dbIndex *index, const char *name)
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
void db_free_string(dbString *x)
Definition: string.c:142
dbIndex * db_alloc_index_array(int count)
void db_init_string(dbString *x)
Definition: string.c:11
void db_free(void *s)