GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
dbcolumns.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/dbcolumns.c
3 
4  \brief Vector library - DB info on vectors maps
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2005-2009 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Markus Neteler
14  */
15 
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <grass/glocale.h>
23 #include <grass/vector.h>
24 #include <grass/dbmi.h>
25 
26 #define BUFF_MAX 2000
27 
28 /*!
29  \brief Fetches list of DB column names of vector map attribute table
30 
31  \param Map vector map
32  \param field layer number
33 
34  \return list of column(s) names on success
35  \return NULL on error
36  */
37 const char *Vect_get_column_names(struct Map_info *Map, int field)
38 {
39  int num_dblinks, ncols, col;
40  struct field_info *fi = NULL;
41  dbDriver *driver = NULL;
42  dbHandle handle;
43  dbString table_name;
44  dbTable *table;
45  const char **col_names;
46  char *list = NULL;
47 
48  num_dblinks = Vect_get_num_dblinks(Map);
49  if (num_dblinks <= 0)
50  return (NULL);
51 
52  G_debug(3, "Displaying column names for database connection of layer %d:",
53  field);
54  if ((fi = Vect_get_field(Map, field)) == NULL)
55  return (NULL);
57  if (driver == NULL) {
59  return (NULL);
60  }
61  db_init_handle(&handle);
62  db_set_handle(&handle, fi->database, NULL);
63  if (db_open_database(driver, &handle) != DB_OK) {
66  return (NULL);
67  }
68  db_init_string(&table_name);
69  db_set_string(&table_name, fi->table);
70  if (db_describe_table(driver, &table_name, &table) != DB_OK) {
71  goto cleanup_exit;
72  }
73 
75  col_names = G_malloc(ncols * sizeof(char *));
76  for (col = 0; col < ncols; col++)
77  col_names[col] = db_get_column_name(db_get_table_column(table, col));
78  if ((list = G_str_concat(col_names, ncols, ",", BUFF_MAX)) == NULL)
79  list = G_store("");
80  G_free(col_names);
81  G_debug(3, "%s", list);
82 
83 cleanup_exit:
86 
87  return list;
88 }
89 
90 /*!
91  \brief Fetches list of DB column types of vector map attribute table
92 
93  \param Map vector map
94  \param field layer number
95 
96  \return list of column(s) types on success
97  \return NULL on error
98  */
99 const char *Vect_get_column_types(struct Map_info *Map, int field)
100 {
101  int num_dblinks, ncols, col;
102  struct field_info *fi = NULL;
103  dbDriver *driver = NULL;
104  dbHandle handle;
105  dbString table_name;
106  dbTable *table;
107  const char **sqltype_names;
108  char *list = NULL;
109 
110  num_dblinks = Vect_get_num_dblinks(Map);
111  if (num_dblinks <= 0)
112  return (NULL);
113 
114  G_debug(3, "Displaying column types for database connection of layer %d:",
115  field);
116  if ((fi = Vect_get_field(Map, field)) == NULL)
117  return (NULL);
119  if (driver == NULL) {
121  return (NULL);
122  }
123  db_init_handle(&handle);
124  db_set_handle(&handle, fi->database, NULL);
125  if (db_open_database(driver, &handle) != DB_OK) {
128  return (NULL);
129  }
130  db_init_string(&table_name);
131  db_set_string(&table_name, fi->table);
132  if (db_describe_table(driver, &table_name, &table) != DB_OK) {
133  goto cleanup_exit;
134  }
135 
137  sqltype_names = G_malloc(ncols * sizeof(char *));
138  for (col = 0; col < ncols; col++)
139  sqltype_names[col] = db_sqltype_name(
141  if ((list = G_str_concat(sqltype_names, ncols, ",", BUFF_MAX)) == NULL)
142  list = G_store("");
143  G_free(sqltype_names);
144  G_debug(3, "%s", list);
145 
146 cleanup_exit:
149 
150  return list;
151 }
152 
153 /*!
154  \brief Fetches list of DB column names and types of vector map attribute
155  table
156 
157  \param Map vector map
158  \param field layer number
159 
160  \return list of column(s) types on success
161  \return NULL on error
162  */
163 const char *Vect_get_column_names_types(struct Map_info *Map, int field)
164 {
165  int num_dblinks, ncols, col;
166  struct field_info *fi = NULL;
167  dbDriver *driver = NULL;
168  dbHandle handle;
169  dbString table_name;
170  dbTable *table;
171  char **col_type_names;
172  char *list = NULL;
173 
174  num_dblinks = Vect_get_num_dblinks(Map);
175  if (num_dblinks <= 0)
176  return (NULL);
177 
178  G_debug(3, "Displaying column types for database connection of layer %d:",
179  field);
180  if ((fi = Vect_get_field(Map, field)) == NULL)
181  return (NULL);
183  if (driver == NULL) {
185  return (NULL);
186  }
187  db_init_handle(&handle);
188  db_set_handle(&handle, fi->database, NULL);
189  if (db_open_database(driver, &handle) != DB_OK) {
192  return (NULL);
193  }
194  db_init_string(&table_name);
195  db_set_string(&table_name, fi->table);
196  if (db_describe_table(driver, &table_name, &table) != DB_OK) {
197  goto cleanup_exit;
198  }
199 
201  col_type_names = G_malloc(ncols * sizeof(char *));
202  for (col = 0; col < ncols; col++) {
203  col_type_names[col] = (char *)G_calloc(256, sizeof(char));
204 
205  snprintf(col_type_names[col], 256, "%s(%s)",
209  }
210 
211  if ((list = G_str_concat((const char **)col_type_names, ncols, ",",
212  BUFF_MAX)) == NULL)
213  list = G_store("");
214 
215  for (col = 0; col < ncols; col++) {
216  G_free(col_type_names[col]);
217  }
218  G_free(col_type_names);
219  G_debug(3, "%s", list);
220 
221 cleanup_exit:
224 
225  return list;
226 }
#define NULL
Definition: ccmath.h:32
#define BUFF_MAX
Definition: dbcolumns.c:26
const char * Vect_get_column_names(struct Map_info *Map, int field)
Fetches list of DB column names of vector map attribute table.
Definition: dbcolumns.c:37
const char * Vect_get_column_types(struct Map_info *Map, int field)
Fetches list of DB column types of vector map attribute table.
Definition: dbcolumns.c:99
const char * Vect_get_column_names_types(struct Map_info *Map, int field)
Fetches list of DB column names and types of vector map attribute table.
Definition: dbcolumns.c:163
Main header of GRASS DataBase Management Interface.
#define DB_OK
Definition: dbmi.h:71
const char * db_sqltype_name(int)
Get SQL data type description.
Definition: sqltype.c:25
int db_describe_table(dbDriver *, dbString *, dbTable **)
Describe table.
Definition: c_desc_table.c:28
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
int db_shutdown_driver(dbDriver *)
Closedown the driver, and free the driver structure.
Definition: shutdown.c:36
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
Definition: start.c:51
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
Definition: c_opendb.c:27
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition: db.c:61
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
Definition: handle.c:39
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
Definition: handle.c:23
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:147
#define G_calloc(m, n)
Definition: defs/gis.h:95
#define G_malloc(n)
Definition: defs/gis.h:94
char * G_str_concat(const char **, int, const char *, int)
String concatenation.
Definition: strings.c:267
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
Definition: level_two.c:159
struct field_info * Vect_get_field(struct Map_info *, int)
Get information about link to database (by layer number)
Definition: field.c:515
void Vect_destroy_field_info(struct field_info *)
Free a struct field_info and all memory associated with it.
Definition: field.c:633
struct list * list
Definition: read_list.c:24
Vector map info.
Definition: dig_structs.h:1243
Definition: driver.h:27
Layer (old: field) information.
Definition: dig_structs.h:131
char * table
Name of DB table.
Definition: dig_structs.h:151
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
Definition: dig_structs.h:143
char * database
Definition: dig_structs.h:147
Definition: manage.h:4