GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
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 SPDX-FileCopyrightText: 2005-2009 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Markus Neteler
12 */
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <string.h>
17#include <unistd.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <grass/glocale.h>
21#include <grass/vector.h>
22#include <grass/dbmi.h>
23
24#define BUFF_MAX 2000
25
26/*!
27 \brief Fetches list of DB column names of vector map attribute table
28
29 \param Map vector map
30 \param field layer number
31
32 \return list of column(s) names on success
33 \return NULL on error
34 */
35const char *Vect_get_column_names(struct Map_info *Map, int field)
36{
37 int num_dblinks, ncols, col;
38 struct field_info *fi = NULL;
40 dbHandle handle;
41 dbString table_name;
43 const char **col_names;
44 char *list = NULL;
45
47 if (num_dblinks <= 0)
48 return (NULL);
49
50 G_debug(3, "Displaying column names for database connection of layer %d:",
51 field);
52 if ((fi = Vect_get_field(Map, field)) == NULL)
53 return (NULL);
55 if (driver == NULL) {
57 return (NULL);
58 }
59 db_init_handle(&handle);
60 db_set_handle(&handle, fi->database, NULL);
61 if (db_open_database(driver, &handle) != DB_OK) {
64 return (NULL);
65 }
66 db_init_string(&table_name);
67 db_set_string(&table_name, fi->table);
68 if (db_describe_table(driver, &table_name, &table) != DB_OK) {
69 goto cleanup_exit;
70 }
71
73 col_names = G_malloc(ncols * sizeof(char *));
74 for (col = 0; col < ncols; col++)
76 if ((list = G_str_concat(col_names, ncols, ",", BUFF_MAX)) == NULL)
77 list = G_store("");
79 G_debug(3, "%s", list);
80
84
85 return list;
86}
87
88/*!
89 \brief Fetches list of DB column types of vector map attribute table
90
91 \param Map vector map
92 \param field layer number
93
94 \return list of column(s) types on success
95 \return NULL on error
96 */
97const char *Vect_get_column_types(struct Map_info *Map, int field)
98{
99 int num_dblinks, ncols, col;
100 struct field_info *fi = NULL;
102 dbHandle handle;
103 dbString table_name;
104 dbTable *table;
105 const char **sqltype_names;
106 char *list = NULL;
107
109 if (num_dblinks <= 0)
110 return (NULL);
111
112 G_debug(3, "Displaying column types for database connection of layer %d:",
113 field);
114 if ((fi = Vect_get_field(Map, field)) == NULL)
115 return (NULL);
117 if (driver == NULL) {
119 return (NULL);
120 }
121 db_init_handle(&handle);
122 db_set_handle(&handle, fi->database, NULL);
123 if (db_open_database(driver, &handle) != DB_OK) {
126 return (NULL);
127 }
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 goto cleanup_exit;
132 }
133
135 sqltype_names = G_malloc(ncols * sizeof(char *));
136 for (col = 0; col < ncols; col++)
139 if ((list = G_str_concat(sqltype_names, ncols, ",", BUFF_MAX)) == NULL)
140 list = G_store("");
142 G_debug(3, "%s", list);
143
147
148 return list;
149}
150
151/*!
152 \brief Fetches list of DB column names and types of vector map attribute
153 table
154
155 \param Map vector map
156 \param field layer number
157
158 \return list of column(s) types on success
159 \return NULL on error
160 */
161const char *Vect_get_column_names_types(struct Map_info *Map, int field)
162{
163 int num_dblinks, ncols, col;
164 struct field_info *fi = NULL;
166 dbHandle handle;
167 dbString table_name;
168 dbTable *table;
169 char **col_type_names;
170 char *list = NULL;
171
173 if (num_dblinks <= 0)
174 return (NULL);
175
176 G_debug(3, "Displaying column types for database connection of layer %d:",
177 field);
178 if ((fi = Vect_get_field(Map, field)) == NULL)
179 return (NULL);
181 if (driver == NULL) {
183 return (NULL);
184 }
185 db_init_handle(&handle);
186 db_set_handle(&handle, fi->database, NULL);
187 if (db_open_database(driver, &handle) != DB_OK) {
190 return (NULL);
191 }
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 goto cleanup_exit;
196 }
197
199 col_type_names = G_malloc(ncols * sizeof(char *));
200 for (col = 0; col < ncols; col++) {
201 col_type_names[col] = (char *)G_calloc(256, sizeof(char));
202
203 snprintf(col_type_names[col], 256, "%s(%s)",
207 }
208
209 if ((list = G_str_concat((const char **)col_type_names, ncols, ",",
210 BUFF_MAX)) == NULL)
211 list = G_store("");
212
213 for (col = 0; col < ncols; col++) {
215 }
217 G_debug(3, "%s", list);
218
222
223 return list;
224}
#define NULL
Definition ccmath.h:32
#define BUFF_MAX
Definition dbcolumns.c:24
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:97
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:35
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:161
Main header of GRASS DataBase Management Interface.
#define DB_OK
Definition dbmi.h:69
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:33
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
Definition c_opendb.c:24
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition db.c:58
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition string.c:39
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:36
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
Definition start.c:48
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
Definition handle.c:20
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:23
const char * db_sqltype_name(int)
Get SQL data type description.
Definition sqltype.c:23
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:145
#define G_calloc(m, n)
Definition defs/gis.h:137
#define G_malloc(n)
Definition defs/gis.h:136
char * G_str_concat(const char **, int, const char *, int)
String concatenation.
Definition strings.c:265
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
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:508
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
Definition level_two.c:157
void Vect_destroy_field_info(struct field_info *)
Free a struct field_info and all memory associated with it.
Definition field.c:626
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