GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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(const 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,
53  "Displaying column names for database connection of layer %d:",
54  field);
55  if ((fi = Vect_get_field(Map, field)) == NULL)
56  return (NULL);
57  driver = db_start_driver(fi->driver);
58  if (driver == NULL)
59  return (NULL);
60  db_init_handle(&handle);
61  db_set_handle(&handle, fi->database, NULL);
62  if (db_open_database(driver, &handle) != DB_OK)
63  return (NULL);
64  db_init_string(&table_name);
65  db_set_string(&table_name, fi->table);
66  if (db_describe_table(driver, &table_name, &table) != DB_OK)
67  return (NULL);
68 
69  ncols = db_get_table_number_of_columns(table);
70  col_names = G_malloc(ncols * sizeof(char *));
71  for (col = 0; col < ncols; col++)
72  col_names[col] = db_get_column_name(db_get_table_column(table, col));
73  if ((list = G_str_concat(col_names, ncols, ",", BUFF_MAX)) == NULL)
74  list = G_store("");
75  G_free(col_names);
76  G_debug(3, "%s", list);
77 
78  db_close_database(driver);
79  db_shutdown_driver(driver);
80 
81  return list;
82 }
83 
84 /*!
85  \brief Fetches list of DB column types of vector map attribute table
86 
87  \param Map vector map
88  \param field layer number
89 
90  \return list of column(s) types on success
91  \return NULL on error
92  */
93 const char *Vect_get_column_types(const struct Map_info *Map, int field)
94 {
95  int num_dblinks, ncols, col;
96  struct field_info *fi;
97  dbDriver *driver = NULL;
98  dbHandle handle;
99  dbString table_name;
100  dbTable *table;
101  const char **sqltype_names;
102  char *list;
103 
104  num_dblinks = Vect_get_num_dblinks(Map);
105  if (num_dblinks <= 0)
106  return (NULL);
107 
108  G_debug(3,
109  "Displaying column types for database connection of layer %d:",
110  field);
111  if ((fi = Vect_get_field(Map, field)) == NULL)
112  return (NULL);
113  driver = db_start_driver(fi->driver);
114  if (driver == NULL)
115  return (NULL);
116  db_init_handle(&handle);
117  db_set_handle(&handle, fi->database, NULL);
118  if (db_open_database(driver, &handle) != DB_OK)
119  return (NULL);
120  db_init_string(&table_name);
121  db_set_string(&table_name, fi->table);
122  if (db_describe_table(driver, &table_name, &table) != DB_OK)
123  return (NULL);
124 
125  ncols = db_get_table_number_of_columns(table);
126  sqltype_names = G_malloc(ncols * sizeof(char *));
127  for (col = 0; col < ncols; col++)
128  sqltype_names[col] = db_sqltype_name(db_get_column_sqltype
130  (table, col)));
131  if ((list = G_str_concat(sqltype_names, ncols, ",", BUFF_MAX)) == NULL)
132  list = G_store("");
133  G_free(sqltype_names);
134  G_debug(3, "%s", list);
135 
136  db_close_database(driver);
137  db_shutdown_driver(driver);
138 
139  return list;
140 }
141 
142 
143 /*!
144  \brief Fetches list of DB column names and types of vector map attribute table
145 
146  \param Map vector map
147  \param field layer number
148 
149  \return list of column(s) types on success
150  \return NULL on error
151  */
152 const char *Vect_get_column_names_types(const struct Map_info *Map, int field)
153 {
154  int num_dblinks, ncols, col;
155  struct field_info *fi;
156  dbDriver *driver = NULL;
157  dbHandle handle;
158  dbString table_name;
159  dbTable *table;
160  const char **col_type_names;
161  char *list;
162 
163  num_dblinks = Vect_get_num_dblinks(Map);
164  if (num_dblinks <= 0)
165  return (NULL);
166 
167  G_debug(3,
168  "Displaying column types for database connection of layer %d:",
169  field);
170  if ((fi = Vect_get_field(Map, field)) == NULL)
171  return (NULL);
172  driver = db_start_driver(fi->driver);
173  if (driver == NULL)
174  return (NULL);
175  db_init_handle(&handle);
176  db_set_handle(&handle, fi->database, NULL);
177  if (db_open_database(driver, &handle) != DB_OK)
178  return (NULL);
179  db_init_string(&table_name);
180  db_set_string(&table_name, fi->table);
181  if (db_describe_table(driver, &table_name, &table) != DB_OK)
182  return (NULL);
183 
184  ncols = db_get_table_number_of_columns(table);
185  col_type_names = G_malloc(ncols * sizeof(char *));
186  for (col = 0; col < ncols; col++) {
187  char buf[256];
188 
189  sprintf(buf, "%s(%s)",
192  (db_get_table_column(table, col))));
193  col_type_names[col] = buf;
194  }
195  if ((list = G_str_concat(col_type_names, ncols, ",", BUFF_MAX)) == NULL)
196  list = G_store("");
197  G_free(col_type_names);
198  G_debug(3, "%s", list);
199 
200  db_close_database(driver);
201  db_shutdown_driver(driver);
202 
203  return list;
204 }
#define G_malloc(n)
Definition: defs/gis.h:112
int db_describe_table(dbDriver *, dbString *, dbTable **)
Describe table.
Definition: c_desc_table.c:28
char * G_str_concat(const char **, int, const char *, int)
String concatenation.
Definition: strings.c:268
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
Definition: start.c:50
const char * Vect_get_column_names_types(const struct Map_info *Map, int field)
Fetches list of DB column names and types of vector map attribute table.
Definition: dbcolumns.c:152
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
char * table
Name of DB table.
Definition: dig_structs.h:155
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
Definition: handle.c:39
#define NULL
Definition: ccmath.h:32
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
struct field_info * Vect_get_field(const struct Map_info *, int)
Get information about link to database (by layer number)
Definition: field.c:506
int Vect_get_num_dblinks(const struct Map_info *)
Get number of defined dblinks.
Definition: level_two.c:163
char * database
Definition: dig_structs.h:151
Layer (old: field) information.
Definition: dig_structs.h:134
const char * Vect_get_column_types(const struct Map_info *Map, int field)
Fetches list of DB column types of vector map attribute table.
Definition: dbcolumns.c:93
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
const char * db_sqltype_name(int)
Get SQL data type description.
Definition: sqltype.c:25
struct list * list
Definition: read_list.c:24
const char * Vect_get_column_names(const struct Map_info *Map, int field)
Fetches list of DB column names of vector map attribute table.
Definition: dbcolumns.c:37
int db_close_database(dbDriver *)
Close database connection.
Definition: c_closedb.c:26
int db_shutdown_driver(dbDriver *)
Closedown the driver, and free the driver structure.
Definition: shutdown.c:36
#define BUFF_MAX
Definition: dbcolumns.c:26
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
Definition: handle.c:23
Vector map info.
Definition: dig_structs.h:1259
char * driver
Name of DB driver (&#39;sqlite&#39;, &#39;dbf&#39;, ...)
Definition: dig_structs.h:147
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
Definition: c_opendb.c:27
Definition: driver.h:22
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
int G_debug(int, const char *,...) __attribute__((format(printf
#define DB_OK
Definition: dbmi.h:71