GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dbcolumns.c
Go to the documentation of this file.
1 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <grass/glocale.h>
27 #include <grass/gis.h>
28 #include <grass/Vect.h>
29 #include <grass/dbmi.h>
30 
40 const char *Vect_get_column_names(struct Map_info *Map, int field)
41 {
42  int num_dblinks, ncols, col;
43  struct field_info *fi;
44  dbDriver *driver = NULL;
45  dbHandle handle;
46  dbString table_name;
47  dbTable *table;
48  char buf[2000], temp_buf[2000];
49 
50 
51  num_dblinks = Vect_get_num_dblinks(Map);
52  if (num_dblinks <= 0)
53  return (NULL);
54 
55  G_debug(3,
56  "Displaying column names for database connection of layer %d:",
57  field);
58  if ((fi = Vect_get_field(Map, field)) == NULL)
59  return (NULL);
60  driver = db_start_driver(fi->driver);
61  if (driver == NULL)
62  return (NULL);
63  db_init_handle(&handle);
64  db_set_handle(&handle, fi->database, NULL);
65  if (db_open_database(driver, &handle) != DB_OK)
66  return (NULL);
67  db_init_string(&table_name);
68  db_set_string(&table_name, fi->table);
69  if (db_describe_table(driver, &table_name, &table) != DB_OK)
70  return (NULL);
71 
72  ncols = db_get_table_number_of_columns(table);
73  sprintf(buf, " ");
74  for (col = 0; col < ncols; col++) {
75  if (col == 0)
76  sprintf(buf, "%s",
78  else {
79  sprintf(temp_buf, ",%s",
81  strcat(buf, temp_buf);
82  }
83  }
84  G_debug(3, "%s", buf);
85 
86  db_close_database(driver);
87  db_shutdown_driver(driver);
88 
89  return G_store(G_chop(buf));
90 }
91 
101 const char *Vect_get_column_types(struct Map_info *Map, int field)
102 {
103  int num_dblinks, ncols, col;
104  struct field_info *fi;
105  dbDriver *driver = NULL;
106  dbHandle handle;
107  dbString table_name;
108  dbTable *table;
109  char buf[2000], temp_buf[2000];
110 
111 
112  num_dblinks = Vect_get_num_dblinks(Map);
113  if (num_dblinks <= 0)
114  return (NULL);
115 
116  G_debug(3,
117  "Displaying column types for database connection of layer %d:",
118  field);
119  if ((fi = Vect_get_field(Map, field)) == NULL)
120  return (NULL);
121  driver = db_start_driver(fi->driver);
122  if (driver == NULL)
123  return (NULL);
124  db_init_handle(&handle);
125  db_set_handle(&handle, fi->database, NULL);
126  if (db_open_database(driver, &handle) != DB_OK)
127  return (NULL);
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  return (NULL);
132 
133  ncols = db_get_table_number_of_columns(table);
134  sprintf(buf, " ");
135  for (col = 0; col < ncols; col++) {
136  if (col == 0)
137  sprintf(buf, "%s",
139  (db_get_table_column(table, col))));
140  else {
141  sprintf(temp_buf, ",%s",
143  (db_get_table_column(table, col))));
144  strcat(buf, temp_buf);
145  }
146  }
147  G_debug(3, "%s", buf);
148 
149  db_close_database(driver);
150  db_shutdown_driver(driver);
151 
152  return G_store(G_chop(buf));
153 }
154 
155 
165 const char *Vect_get_column_names_types(struct Map_info *Map, int field)
166 {
167  int num_dblinks, ncols, col;
168  struct field_info *fi;
169  dbDriver *driver = NULL;
170  dbHandle handle;
171  dbString table_name;
172  dbTable *table;
173  char buf[2000], temp_buf[2000];
174 
175 
176  num_dblinks = Vect_get_num_dblinks(Map);
177  if (num_dblinks <= 0)
178  return (NULL);
179 
180  G_debug(3,
181  "Displaying column types for database connection of layer %d:",
182  field);
183  if ((fi = Vect_get_field(Map, field)) == NULL)
184  return (NULL);
185  driver = db_start_driver(fi->driver);
186  if (driver == NULL)
187  return (NULL);
188  db_init_handle(&handle);
189  db_set_handle(&handle, fi->database, NULL);
190  if (db_open_database(driver, &handle) != DB_OK)
191  return (NULL);
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  return (NULL);
196 
197  ncols = db_get_table_number_of_columns(table);
198  sprintf(buf, " ");
199  for (col = 0; col < ncols; col++) {
200  if (col == 0)
201  sprintf(buf, "%s(%s)",
204  (db_get_table_column(table, col))));
205  else {
206  sprintf(temp_buf, ",%s(%s)",
209  (db_get_table_column(table, col))));
210  strcat(buf, temp_buf);
211  }
212  }
213  G_debug(3, "%s", buf);
214 
215  db_close_database(driver);
216  db_shutdown_driver(driver);
217 
218  return G_store(G_chop(buf));
219 }
dbColumn * db_get_table_column(dbTable *table, int n)
returns column structure for given table and column number
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
const char * db_sqltype_name(int sqltype)
returns sqltype description
Definition: sqltype.c:9
struct field_info * Vect_get_field(struct Map_info *Map, int field)
Get information about link to database.
Definition: field.c:404
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
const char * db_get_column_name(dbColumn *column)
returns column name for given column
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 * G_chop(char *line)
Chop leading and trailing white spaces:
Definition: strings.c:418
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:165
int db_get_column_sqltype(dbColumn *column)
returns column sqltype for column (the function db_sqltype_name() returns sqltype description) ...
int db_get_table_number_of_columns(dbTable *table)
int Vect_get_num_dblinks(struct Map_info *map)
Get number of defined dblinks.
Definition: level_two.c:158
int db_set_handle(dbHandle *handle, const char *dbName, const char *dbSchema)
Definition: handle.c:22
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
Definition: driver.h:25
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void db_init_handle(dbHandle *handle)
Definition: handle.c:10
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:40
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
int db_open_database(dbDriver *driver, dbHandle *handle)
Open database connection.
Definition: c_opendb.c:27
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:101
dbDriver * db_start_driver(const char *name)
Initialize a new dbDriver for db transaction.
Definition: start.c:43
void db_init_string(dbString *x)
Definition: string.c:11