GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73574
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 /*!
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  */
35 const char *Vect_get_column_names(const struct Map_info *Map, int field)
36 {
37  int num_dblinks, ncols, col;
38  struct field_info *fi;
39  dbDriver *driver = NULL;
40  dbHandle handle;
41  dbString table_name;
42  dbTable *table;
43  char buf[2000];
44 
45 
46  num_dblinks = Vect_get_num_dblinks(Map);
47  if (num_dblinks <= 0)
48  return (NULL);
49 
50  G_debug(3,
51  "Displaying column names for database connection of layer %d:",
52  field);
53  if ((fi = Vect_get_field(Map, field)) == NULL)
54  return (NULL);
55  driver = db_start_driver(fi->driver);
56  if (driver == NULL)
57  return (NULL);
58  db_init_handle(&handle);
59  db_set_handle(&handle, fi->database, NULL);
60  if (db_open_database(driver, &handle) != DB_OK)
61  return (NULL);
62  db_init_string(&table_name);
63  db_set_string(&table_name, fi->table);
64  if (db_describe_table(driver, &table_name, &table) != DB_OK)
65  return (NULL);
66 
67  ncols = db_get_table_number_of_columns(table);
68  sprintf(buf, " ");
69  for (col = 0; col < ncols; col++) {
70  if (col == 0)
71  sprintf(buf, "%s",
73  else
74  sprintf(buf, "%s,%s", buf,
76  }
77  G_debug(3, "%s", buf);
78 
79  db_close_database(driver);
80  db_shutdown_driver(driver);
81 
82  return G_store(G_chop(buf));
83 }
84 
85 /*!
86  \brief Fetches list of DB column types of vector map attribute table
87 
88  \param Map vector map
89  \param field layer number
90 
91  \return list of column(s) types on success
92  \return NULL on error
93  */
94 const char *Vect_get_column_types(const struct Map_info *Map, int field)
95 {
96  int num_dblinks, ncols, col;
97  struct field_info *fi;
98  dbDriver *driver = NULL;
99  dbHandle handle;
100  dbString table_name;
101  dbTable *table;
102  char buf[2000];
103 
104 
105  num_dblinks = Vect_get_num_dblinks(Map);
106  if (num_dblinks <= 0)
107  return (NULL);
108 
109  G_debug(3,
110  "Displaying column types for database connection of layer %d:",
111  field);
112  if ((fi = Vect_get_field(Map, field)) == NULL)
113  return (NULL);
114  driver = db_start_driver(fi->driver);
115  if (driver == NULL)
116  return (NULL);
117  db_init_handle(&handle);
118  db_set_handle(&handle, fi->database, NULL);
119  if (db_open_database(driver, &handle) != DB_OK)
120  return (NULL);
121  db_init_string(&table_name);
122  db_set_string(&table_name, fi->table);
123  if (db_describe_table(driver, &table_name, &table) != DB_OK)
124  return (NULL);
125 
126  ncols = db_get_table_number_of_columns(table);
127  sprintf(buf, " ");
128  for (col = 0; col < ncols; col++) {
129  if (col == 0)
130  sprintf(buf, "%s",
132  (db_get_table_column(table, col))));
133  else
134  sprintf(buf, "%s,%s", buf,
136  (db_get_table_column(table, col))));
137  }
138  G_debug(3, "%s", buf);
139 
140  db_close_database(driver);
141  db_shutdown_driver(driver);
142 
143  return G_store(G_chop(buf));
144 }
145 
146 
147 /*!
148  \brief Fetches list of DB column names and types of vector map attribute table
149 
150  \param Map vector map
151  \param field layer number
152 
153  \return list of column(s) types on success
154  \return NULL on error
155  */
156 const char *Vect_get_column_names_types(const struct Map_info *Map, int field)
157 {
158  int num_dblinks, ncols, col;
159  struct field_info *fi;
160  dbDriver *driver = NULL;
161  dbHandle handle;
162  dbString table_name;
163  dbTable *table;
164  char buf[2000];
165 
166 
167  num_dblinks = Vect_get_num_dblinks(Map);
168  if (num_dblinks <= 0)
169  return (NULL);
170 
171  G_debug(3,
172  "Displaying column types for database connection of layer %d:",
173  field);
174  if ((fi = Vect_get_field(Map, field)) == NULL)
175  return (NULL);
176  driver = db_start_driver(fi->driver);
177  if (driver == NULL)
178  return (NULL);
179  db_init_handle(&handle);
180  db_set_handle(&handle, fi->database, NULL);
181  if (db_open_database(driver, &handle) != DB_OK)
182  return (NULL);
183  db_init_string(&table_name);
184  db_set_string(&table_name, fi->table);
185  if (db_describe_table(driver, &table_name, &table) != DB_OK)
186  return (NULL);
187 
188  ncols = db_get_table_number_of_columns(table);
189  sprintf(buf, " ");
190  for (col = 0; col < ncols; col++) {
191  if (col == 0)
192  sprintf(buf, "%s(%s)",
195  (db_get_table_column(table, col))));
196  else
197  sprintf(buf, "%s,%s(%s)", buf,
200  (db_get_table_column(table, col))));
201  }
202  G_debug(3, "%s", buf);
203 
204  db_close_database(driver);
205  db_shutdown_driver(driver);
206 
207  return G_store(G_chop(buf));
208 }
const char * db_sqltype_name(int sqltype)
Get SQL data type description.
Definition: sqltype.c:25
const char * db_get_column_name(dbColumn *column)
Returns column name for given column.
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:156
int db_describe_table(dbDriver *driver, dbString *name, dbTable **table)
Describe table.
Definition: c_desc_table.c:28
int db_shutdown_driver(dbDriver *driver)
Closedown the driver, and free the driver structure.
Definition: shutdown.c:36
int db_close_database(dbDriver *driver)
Close database connection.
Definition: c_closedb.c:26
char * table
Name of DB table.
Definition: dig_structs.h:155
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int Vect_get_num_dblinks(const struct Map_info *Map)
Get number of defined dblinks.
Definition: level_two.c:163
#define NULL
Definition: ccmath.h:32
char * G_chop(char *line)
Chop leading and trailing white spaces.
Definition: strings.c:286
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:94
int db_get_column_sqltype(dbColumn *column)
Returns column sqltype for column.
int db_get_table_number_of_columns(dbTable *table)
Return the number of columns of the table.
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:35
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
int db_set_handle(dbHandle *handle, const char *dbName, const char *dbSchema)
Set handle (database and schema name)
Definition: handle.c:39
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
Definition: driver.h:22
void db_init_handle(dbHandle *handle)
Initialize handle (i.e database/schema)
Definition: handle.c:23
int db_set_string(dbString *x, const char *s)
Inserts string to dbString (enlarge string)
Definition: string.c:41
int db_open_database(dbDriver *driver, dbHandle *handle)
Open database connection.
Definition: c_opendb.c:27
struct field_info * Vect_get_field(const struct Map_info *Map, int field)
Get information about link to database (by layer number)
Definition: field.c:461
dbColumn * db_get_table_column(dbTable *table, int idx)
Returns column structure for given table and column number.
dbDriver * db_start_driver(const char *name)
Initialize a new dbDriver for db transaction.
Definition: start.c:50
void db_init_string(dbString *x)
Initialize dbString.
Definition: string.c:25
#define DB_OK
Definition: dbmi.h:71