main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /****************************************************************************
  2. *
  3. * MODULE: db.tables
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>, Stephan Holl
  6. * PURPOSE: lists all tables for a given database
  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 <stdlib.h>
  15. #include <grass/gis.h>
  16. #include <grass/dbmi.h>
  17. #include <grass/codes.h>
  18. #include <grass/glocale.h>
  19. struct
  20. {
  21. char *driver, *database;
  22. int s;
  23. } parms;
  24. /* function prototypes */
  25. static void parse_command_line(int, char **);
  26. int main(int argc, char **argv)
  27. {
  28. dbDriver *driver;
  29. dbHandle handle;
  30. dbString *names;
  31. int i, count;
  32. int system_tables;
  33. parse_command_line(argc, argv);
  34. driver = db_start_driver(parms.driver);
  35. if (driver == NULL)
  36. G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
  37. db_init_handle(&handle);
  38. db_set_handle(&handle, parms.database, NULL);
  39. if (db_open_database(driver, &handle) != DB_OK)
  40. G_fatal_error(_("Unable to open database <%s>"), parms.database);
  41. system_tables = parms.s;
  42. if (db_list_tables(driver, &names, &count, system_tables) != DB_OK)
  43. exit(ERROR);
  44. for (i = 0; i < count; i++)
  45. fprintf(stdout, "%s\n", db_get_string(&names[i]));
  46. db_close_database(driver);
  47. db_shutdown_driver(driver);
  48. exit(EXIT_SUCCESS);
  49. }
  50. static void parse_command_line(int argc, char **argv)
  51. {
  52. struct Option *driver, *database;
  53. struct Flag *p, *s;
  54. struct GModule *module;
  55. const char *drv, *db;
  56. /* Initialize the GIS calls */
  57. G_gisinit(argv[0]);
  58. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  59. driver->options = db_list_drivers();
  60. if ((drv = db_get_default_driver_name()))
  61. driver->answer = (char *) drv;
  62. database = G_define_standard_option(G_OPT_DB_DATABASE);
  63. if ((db = db_get_default_database_name()))
  64. database->answer = (char *) db;
  65. p = G_define_flag();
  66. p->key = 'p';
  67. p->description = _("Print tables and exit");
  68. s = G_define_flag();
  69. s->key = 's';
  70. s->description = _("System tables instead of user tables");
  71. /* Set description */
  72. module = G_define_module();
  73. G_add_keyword(_("database"));
  74. G_add_keyword(_("attribute table"));
  75. module->description = _("Lists all tables for a given database.");
  76. if (G_parser(argc, argv))
  77. exit(EXIT_SUCCESS);
  78. parms.driver = driver->answer;
  79. parms.database = database->answer;
  80. parms.s = s->answer;
  81. }