main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /****************************************************************************
  2. *
  3. * MODULE: db.columns
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
  6. * PURPOSE: list the column names for a table
  7. * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <grass/gis.h>
  15. #include <grass/dbmi.h>
  16. #include <stdlib.h>
  17. #include <grass/glocale.h>
  18. struct
  19. {
  20. char *driver, *database, *table;
  21. } parms;
  22. /* function prototypes */
  23. static void parse_command_line(int, char **);
  24. int main(int argc, char **argv)
  25. {
  26. dbDriver *driver;
  27. dbHandle handle;
  28. dbTable *table;
  29. dbString table_name;
  30. int col, ncols;
  31. parse_command_line(argc, argv);
  32. driver = db_start_driver(parms.driver);
  33. if (driver == NULL)
  34. G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
  35. db_init_handle(&handle);
  36. db_set_handle(&handle, parms.database, NULL);
  37. if (db_open_database(driver, &handle) != DB_OK)
  38. exit(EXIT_FAILURE);
  39. db_init_string(&table_name);
  40. db_set_string(&table_name, parms.table);
  41. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  42. exit(EXIT_FAILURE);
  43. db_close_database(driver);
  44. db_shutdown_driver(driver);
  45. ncols = db_get_table_number_of_columns(table);
  46. for (col = 0; col < ncols; col++)
  47. fprintf(stdout, "%s\n",
  48. db_get_column_name(db_get_table_column(table, col)));
  49. exit(EXIT_SUCCESS);
  50. }
  51. static void parse_command_line(int argc, char **argv)
  52. {
  53. struct Option *driver, *database, *table;
  54. struct GModule *module;
  55. const char *drv, *db;
  56. /* Initialize the GIS calls */
  57. G_gisinit(argv[0]);
  58. table = G_define_standard_option(G_OPT_DB_TABLE);
  59. table->required = YES;
  60. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  61. driver->options = db_list_drivers();
  62. if ((drv = db_get_default_driver_name()))
  63. driver->answer = (char *) drv;
  64. database = G_define_standard_option(G_OPT_DB_DATABASE);
  65. if ((db = db_get_default_database_name()))
  66. database->answer = (char *) db;
  67. /* Set description */
  68. module = G_define_module();
  69. G_add_keyword(_("database"));
  70. G_add_keyword(_("attribute table"));
  71. module->description = _("List all columns for a given table.");
  72. if (G_parser(argc, argv))
  73. exit(EXIT_FAILURE);
  74. parms.driver = driver->answer;
  75. parms.database = database->answer;
  76. parms.table = table->answer;
  77. }