1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/*!
\file lib/vector/Vlib/dbcolumns.c
\brief Vector library - DB info on vectors maps
Higher level functions for reading/writing/manipulating vectors.
(C) 2005-2009 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Markus Neteler
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grass/glocale.h>
#include <grass/vector.h>
#include <grass/dbmi.h>
#define BUFF_MAX 2000
/*!
\brief Fetches list of DB column names of vector map attribute table
\param Map vector map
\param field layer number
\return list of column(s) names on success
\return NULL on error
*/
const char *Vect_get_column_names(struct Map_info *Map, int field)
{
int num_dblinks, ncols, col;
struct field_info *fi;
dbDriver *driver = NULL;
dbHandle handle;
dbString table_name;
dbTable *table;
const char **col_names;
char *list;
num_dblinks = Vect_get_num_dblinks(Map);
if (num_dblinks <= 0)
return (NULL);
G_debug(3, "Displaying column names for database connection of layer %d:",
field);
if ((fi = Vect_get_field(Map, field)) == NULL)
return (NULL);
driver = db_start_driver(fi->driver);
if (driver == NULL)
return (NULL);
db_init_handle(&handle);
db_set_handle(&handle, fi->database, NULL);
if (db_open_database(driver, &handle) != DB_OK)
return (NULL);
db_init_string(&table_name);
db_set_string(&table_name, fi->table);
if (db_describe_table(driver, &table_name, &table) != DB_OK)
return (NULL);
ncols = db_get_table_number_of_columns(table);
col_names = G_malloc(ncols * sizeof(char *));
for (col = 0; col < ncols; col++)
col_names[col] = db_get_column_name(db_get_table_column(table, col));
if ((list = G_str_concat(col_names, ncols, ",", BUFF_MAX)) == NULL)
list = G_store("");
G_free(col_names);
G_debug(3, "%s", list);
db_close_database(driver);
db_shutdown_driver(driver);
return list;
}
/*!
\brief Fetches list of DB column types of vector map attribute table
\param Map vector map
\param field layer number
\return list of column(s) types on success
\return NULL on error
*/
const char *Vect_get_column_types(struct Map_info *Map, int field)
{
int num_dblinks, ncols, col;
struct field_info *fi;
dbDriver *driver = NULL;
dbHandle handle;
dbString table_name;
dbTable *table;
const char **sqltype_names;
char *list;
num_dblinks = Vect_get_num_dblinks(Map);
if (num_dblinks <= 0)
return (NULL);
G_debug(3, "Displaying column types for database connection of layer %d:",
field);
if ((fi = Vect_get_field(Map, field)) == NULL)
return (NULL);
driver = db_start_driver(fi->driver);
if (driver == NULL)
return (NULL);
db_init_handle(&handle);
db_set_handle(&handle, fi->database, NULL);
if (db_open_database(driver, &handle) != DB_OK)
return (NULL);
db_init_string(&table_name);
db_set_string(&table_name, fi->table);
if (db_describe_table(driver, &table_name, &table) != DB_OK)
return (NULL);
ncols = db_get_table_number_of_columns(table);
sqltype_names = G_malloc(ncols * sizeof(char *));
for (col = 0; col < ncols; col++)
sqltype_names[col] = db_sqltype_name(
db_get_column_sqltype(db_get_table_column(table, col)));
if ((list = G_str_concat(sqltype_names, ncols, ",", BUFF_MAX)) == NULL)
list = G_store("");
G_free(sqltype_names);
G_debug(3, "%s", list);
db_close_database(driver);
db_shutdown_driver(driver);
return list;
}
/*!
\brief Fetches list of DB column names and types of vector map attribute
table
\param Map vector map
\param field layer number
\return list of column(s) types on success
\return NULL on error
*/
const char *Vect_get_column_names_types(struct Map_info *Map, int field)
{
int num_dblinks, ncols, col;
struct field_info *fi;
dbDriver *driver = NULL;
dbHandle handle;
dbString table_name;
dbTable *table;
const char **col_type_names;
char *list;
num_dblinks = Vect_get_num_dblinks(Map);
if (num_dblinks <= 0)
return (NULL);
G_debug(3, "Displaying column types for database connection of layer %d:",
field);
if ((fi = Vect_get_field(Map, field)) == NULL)
return (NULL);
driver = db_start_driver(fi->driver);
if (driver == NULL)
return (NULL);
db_init_handle(&handle);
db_set_handle(&handle, fi->database, NULL);
if (db_open_database(driver, &handle) != DB_OK)
return (NULL);
db_init_string(&table_name);
db_set_string(&table_name, fi->table);
if (db_describe_table(driver, &table_name, &table) != DB_OK)
return (NULL);
ncols = db_get_table_number_of_columns(table);
col_type_names = G_malloc(ncols * sizeof(char *));
for (col = 0; col < ncols; col++) {
char buf[256];
sprintf(buf, "%s(%s)",
db_get_column_name(db_get_table_column(table, col)),
db_sqltype_name(
db_get_column_sqltype(db_get_table_column(table, col))));
col_type_names[col] = buf;
}
if ((list = G_str_concat(col_type_names, ncols, ",", BUFF_MAX)) == NULL)
list = G_store("");
G_free(col_type_names);
G_debug(3, "%s", list);
db_close_database(driver);
db_shutdown_driver(driver);
return list;
}
|