2
/****************************************************************************
5
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
6
* Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
7
* PURPOSE: list the column names for a table
8
* COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
10
* This program is free software under the GNU General Public
11
* License (>=v2). Read the file COPYING that comes with GRASS
14
*****************************************************************************/
16
#include <grass/gis.h>
17
#include <grass/dbmi.h>
18
#include <grass/codes.h>
20
#include <grass/glocale.h>
25
char *driver, *database, *table;
29
/* function prototypes */
30
static void parse_command_line(int, char **);
33
int main(int argc, char **argv)
41
parse_command_line(argc, argv);
43
driver = db_start_driver(parms.driver);
45
G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
47
db_init_handle(&handle);
48
db_set_handle(&handle, parms.database, NULL);
49
if (db_open_database(driver, &handle) != DB_OK)
52
db_init_string(&table_name);
53
db_set_string(&table_name, parms.table);
54
if (db_describe_table(driver, &table_name, &table) != DB_OK)
57
db_close_database(driver);
58
db_shutdown_driver(driver);
60
ncols = db_get_table_number_of_columns(table);
61
for (col = 0; col < ncols; col++)
62
fprintf(stdout, "%s\n",
63
db_get_column_name(db_get_table_column(table, col)));
69
static void parse_command_line(int argc, char **argv)
71
struct Option *driver, *database, *table;
72
struct GModule *module;
75
/* Initialize the GIS calls */
78
table = G_define_standard_option(G_OPT_TABLE);
79
table->required = YES;
81
driver = G_define_standard_option(G_OPT_DRIVER);
82
driver->options = db_list_drivers();
83
if ((drv = db_get_default_driver_name()))
84
driver->answer = (char *) drv;
86
database = G_define_standard_option(G_OPT_DATABASE);
87
if ((db = db_get_default_database_name()))
88
database->answer = (char *) db;
91
module = G_define_module();
92
module->keywords = _("database, attribute table");
93
module->description = _("List all columns for a given table.");
95
if (G_parser(argc, argv))
98
parms.driver = driver->answer;
99
parms.database = database->answer;
100
parms.table = table->answer;