GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dbcolumns.c
Go to the documentation of this file.
1 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <grass/glocale.h>
27 #include <grass/gis.h>
28 #include <grass/Vect.h>
29 #include <grass/dbmi.h>
30 
40 const char *Vect_get_column_names(struct Map_info *Map, int field)
41 {
42  int num_dblinks, ncols, col;
43  struct field_info *fi;
44  dbDriver *driver = NULL;
45  dbHandle handle;
46  dbString table_name;
47  dbTable *table;
48  char buf[2000], temp_buf[2000];
49 
50 
51  num_dblinks = Vect_get_num_dblinks(Map);
52  if (num_dblinks <= 0)
53  return (NULL);
54 
55  G_debug(3,
56  "Displaying column names for database connection of layer %d:",
57  field);
58  if ((fi = Vect_get_field(Map, field)) == NULL)
59  return (NULL);
60  driver = db_start_driver(fi->driver);
61  if (driver == NULL)
62  return (NULL);
63  db_init_handle(&handle);
64  db_set_handle(&handle, fi->database, NULL);
65  if (db_open_database(driver, &handle) != DB_OK)
66  return (NULL);
67  db_init_string(&table_name);
68  db_set_string(&table_name, fi->table);
69  if (db_describe_table(driver, &table_name, &table) != DB_OK)
70  return (NULL);
71 
72  ncols = db_get_table_number_of_columns(table);
73  sprintf(buf, " ");
74  for (col = 0; col < ncols; col++) {
75  if (col == 0)
76  sprintf(buf, "%s",
78  else {
79  sprintf(temp_buf, ",%s",
81  strcat(buf, temp_buf);
82  }
83  }
84  G_debug(3, "%s", buf);
85 
86  db_close_database(driver);
87  db_shutdown_driver(driver);
88 
89  return G_store(G_chop(buf));
90 }
91 
101 const char *Vect_get_column_types(struct Map_info *Map, int field)
102 {
103  int num_dblinks, ncols, col;
104  struct field_info *fi;
105  dbDriver *driver = NULL;
106  dbHandle handle;
107  dbString table_name;
108  dbTable *table;
109  char buf[2000], temp_buf[2000];
110 
111 
112  num_dblinks = Vect_get_num_dblinks(Map);
113  if (num_dblinks <= 0)
114  return (NULL);
115 
116  G_debug(3,
117  "Displaying column types for database connection of layer %d:",
118  field);
119  if ((fi = Vect_get_field(Map, field)) == NULL)
120  return (NULL);
121  driver = db_start_driver(fi->driver);
122  if (driver == NULL)
123  return (NULL);
124  db_init_handle(&handle);
125  db_set_handle(&handle, fi->database, NULL);
126  if (db_open_database(driver, &handle) != DB_OK)
127  return (NULL);
128  db_init_string(&table_name);
129  db_set_string(&table_name, fi->table);
130  if (db_describe_table(driver, &table_name, &table) != DB_OK)
131  return (NULL);
132 
133  ncols = db_get_table_number_of_columns(table);
134  sprintf(buf, " ");
135  for (col = 0; col < ncols; col++) {
136  if (col == 0)
137  sprintf(buf, "%s",
139  (db_get_table_column(table, col))));
140  else {
141  sprintf(temp_buf, ",%s",
143  (db_get_table_column(table, col))));
144  strcat(buf, temp_buf);
145  }
146  }
147  G_debug(3, "%s", buf);
148 
149  db_close_database(driver);
150  db_shutdown_driver(driver);
151 
152  return G_store(G_chop(buf));
153 }
154 
155 
165 const char *Vect_get_column_names_types(struct Map_info *Map, int field)
166 {
167  int num_dblinks, ncols, col;
168  struct field_info *fi;
169  dbDriver *driver = NULL;
170  dbHandle handle;
171  dbString table_name;
172  dbTable *table;
173  char buf[2000], temp_buf[2000];
174 
175 
176  num_dblinks = Vect_get_num_dblinks(Map);
177  if (num_dblinks <= 0)
178  return (NULL);
179 
180  G_debug(3,
181  "Displaying column types for database connection of layer %d:",
182  field);
183  if ((fi = Vect_get_field(Map, field)) == NULL)
184  return (NULL);
185  driver = db_start_driver(fi->driver);
186  if (driver == NULL)
187  return (NULL);
188  db_init_handle(&handle);
189  db_set_handle(&handle, fi->database, NULL);
190  if (db_open_database(driver, &handle) != DB_OK)
191  return (NULL);
192  db_init_string(&table_name);
193  db_set_string(&table_name, fi->table);
194  if (db_describe_table(driver, &table_name, &table) != DB_OK)
195  return (NULL);
196 
197  ncols = db_get_table_number_of_columns(table);
198  sprintf(buf, " ");
199  for (col = 0; col < ncols; col++) {
200  if (col == 0)
201  sprintf(buf, "%s(%s)",
204  (db_get_table_column(table, col))));
205  else {
206  sprintf(temp_buf, ",%s(%s)",
209  (db_get_table_column(table, col))));
210  strcat(buf, temp_buf);
211  }
212  }
213  G_debug(3, "%s", buf);
214 
215  db_close_database(driver);
216  db_shutdown_driver(driver);
217 
218  return G_store(G_chop(buf));
219 }