GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
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 */
37const char *Vect_get_column_names(struct Map_info *Map, int field)
38{
39 int num_dblinks, ncols, col;
40 struct field_info *fi = NULL;
42 dbHandle handle;
43 dbString table_name;
45 const char **col_names;
46 char *list = NULL;
47
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++)
78 if ((list = G_str_concat(col_names, ncols, ",", BUFF_MAX)) == NULL)
79 list = G_store("");
81 G_debug(3, "%s", list);
82
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 */
99const char *Vect_get_column_types(struct Map_info *Map, int field)
100{
101 int num_dblinks, ncols, col;
102 struct field_info *fi = NULL;
104 dbHandle handle;
105 dbString table_name;
106 dbTable *table;
107 const char **sqltype_names;
108 char *list = NULL;
109
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++)
141 if ((list = G_str_concat(sqltype_names, ncols, ",", BUFF_MAX)) == NULL)
142 list = G_store("");
144 G_debug(3, "%s", list);
145
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 */
163const 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;
168 dbHandle handle;
169 dbString table_name;
170 dbTable *table;
171 char **col_type_names;
172 char *list = NULL;
173
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++) {
217 }
219 G_debug(3, "%s", list);
220
224
225 return list;
226}
#define NULL
Definition ccmath.h:32
#define BUFF_MAX
Definition dbcolumns.c:26
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(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_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
int db_describe_table(dbDriver *, dbString *, dbTable **)
Describe table.
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
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
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
Definition handle.c:39
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
Definition start.c:51
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_sqltype_name(int)
Get SQL data type description.
Definition sqltype.c:25
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:140
#define G_malloc(n)
Definition defs/gis.h:139
char * G_str_concat(const char **, int, const char *, int)
String concatenation.
Definition strings.c:267
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:87
int G_debug(int, const char *,...) __attribute__((format(printf
struct field_info * Vect_get_field(struct Map_info *, int)
Get information about link to database (by layer number)
Definition field.c:510
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
Definition level_two.c:159
void Vect_destroy_field_info(struct field_info *)
Free a struct field_info and all memory associated with it.
Definition field.c:628
Vector map info.
Layer (old: field) information.
char * table
Name of DB table.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
char * database
Definition manage.h:4