GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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;
41  dbDriver *driver = NULL;
42  dbHandle handle;
43  dbString table_name;
44  dbTable *table;
45  const char **col_names;
46  char *list;
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)
58  return (NULL);
59  db_init_handle(&handle);
60  db_set_handle(&handle, fi->database, NULL);
61  if (db_open_database(driver, &handle) != DB_OK)
62  return (NULL);
63  db_init_string(&table_name);
64  db_set_string(&table_name, fi->table);
65  if (db_describe_table(driver, &table_name, &table) != DB_OK)
66  return (NULL);
67 
69  col_names = G_malloc(ncols * sizeof(char *));
70  for (col = 0; col < ncols; col++)
71  col_names[col] = db_get_column_name(db_get_table_column(table, col));
72  if ((list = G_str_concat(col_names, ncols, ",", BUFF_MAX)) == NULL)
73  list = G_store("");
74  G_free(col_names);
75  G_debug(3, "%s", list);
76 
79 
80  return list;
81 }
82 
83 /*!
84  \brief Fetches list of DB column types of vector map attribute table
85 
86  \param Map vector map
87  \param field layer number
88 
89  \return list of column(s) types on success
90  \return NULL on error
91  */
92 const char *Vect_get_column_types(struct Map_info *Map, int field)
93 {
94  int num_dblinks, ncols, col;
95  struct field_info *fi;
96  dbDriver *driver = NULL;
97  dbHandle handle;
98  dbString table_name;
99  dbTable *table;
100  const char **sqltype_names;
101  char *list;
102 
103  num_dblinks = Vect_get_num_dblinks(Map);
104  if (num_dblinks <= 0)
105  return (NULL);
106 
107  G_debug(3, "Displaying column types for database connection of layer %d:",
108  field);
109  if ((fi = Vect_get_field(Map, field)) == NULL)
110  return (NULL);
112  if (driver == NULL)
113  return (NULL);
114  db_init_handle(&handle);
115  db_set_handle(&handle, fi->database, NULL);
116  if (db_open_database(driver, &handle) != DB_OK)
117  return (NULL);
118  db_init_string(&table_name);
119  db_set_string(&table_name, fi->table);
120  if (db_describe_table(driver, &table_name, &table) != DB_OK)
121  return (NULL);
122 
124  sqltype_names = G_malloc(ncols * sizeof(char *));
125  for (col = 0; col < ncols; col++)
126  sqltype_names[col] = db_sqltype_name(
128  if ((list = G_str_concat(sqltype_names, ncols, ",", BUFF_MAX)) == NULL)
129  list = G_store("");
130  G_free(sqltype_names);
131  G_debug(3, "%s", list);
132 
135 
136  return list;
137 }
138 
139 /*!
140  \brief Fetches list of DB column names and types of vector map attribute
141  table
142 
143  \param Map vector map
144  \param field layer number
145 
146  \return list of column(s) types on success
147  \return NULL on error
148  */
149 const char *Vect_get_column_names_types(struct Map_info *Map, int field)
150 {
151  int num_dblinks, ncols, col;
152  struct field_info *fi;
153  dbDriver *driver = NULL;
154  dbHandle handle;
155  dbString table_name;
156  dbTable *table;
157  const char **col_type_names;
158  char *list;
159 
160  num_dblinks = Vect_get_num_dblinks(Map);
161  if (num_dblinks <= 0)
162  return (NULL);
163 
164  G_debug(3, "Displaying column types for database connection of layer %d:",
165  field);
166  if ((fi = Vect_get_field(Map, field)) == NULL)
167  return (NULL);
169  if (driver == NULL)
170  return (NULL);
171  db_init_handle(&handle);
172  db_set_handle(&handle, fi->database, NULL);
173  if (db_open_database(driver, &handle) != DB_OK)
174  return (NULL);
175  db_init_string(&table_name);
176  db_set_string(&table_name, fi->table);
177  if (db_describe_table(driver, &table_name, &table) != DB_OK)
178  return (NULL);
179 
181  col_type_names = G_malloc(ncols * sizeof(char *));
182  for (col = 0; col < ncols; col++) {
183  char buf[256];
184 
185  sprintf(buf, "%s(%s)",
189  col_type_names[col] = buf;
190  }
191  if ((list = G_str_concat(col_type_names, ncols, ",", BUFF_MAX)) == NULL)
192  list = G_store("");
193  G_free(col_type_names);
194  G_debug(3, "%s", list);
195 
198 
199  return list;
200 }
#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:92
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:149
#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_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
int db_close_database(dbDriver *)
Close database connection.
Definition: c_closedb.c:26
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:150
#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
struct list * list
Definition: read_list.c:24
Vector map info.
Definition: dig_structs.h:1243
Definition: driver.h:21
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