main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/glocale.h>
  18. struct
  19. {
  20. char *driver, *database;
  21. int s;
  22. } parms;
  23. /* function prototypes */
  24. static void parse_command_line(int, char **);
  25. int main(int argc, char **argv)
  26. {
  27. dbDriver *driver;
  28. dbHandle handle;
  29. dbString *names;
  30. int i, count;
  31. int system_tables;
  32. parse_command_line(argc, argv);
  33. driver = db_start_driver(parms.driver);
  34. if (driver == NULL)
  35. G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
  36. db_init_handle(&handle);
  37. db_set_handle(&handle, parms.database, NULL);
  38. if (db_open_database(driver, &handle) != DB_OK)
  39. G_fatal_error(_("Unable to open database <%s>"), parms.database);
  40. system_tables = parms.s;
  41. if (db_list_tables(driver, &names, &count, system_tables) != DB_OK)
  42. G_fatal_error(_("Unable to list tables from database <%s>"), parms.database);
  43. for (i = 0; i < count; i++)
  44. fprintf(stdout, "%s\n", db_get_string(&names[i]));
  45. if (count < 1)
  46. G_important_message(_("No tables found"));
  47. db_close_database(driver);
  48. db_shutdown_driver(driver);
  49. exit(EXIT_SUCCESS);
  50. }
  51. static void parse_command_line(int argc, char **argv)
  52. {
  53. struct Option *driver, *database;
  54. struct Flag *p, *s;
  55. struct GModule *module;
  56. const char *drv, *db;
  57. /* Initialize the GIS calls */
  58. G_gisinit(argv[0]);
  59. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  60. driver->options = db_list_drivers();
  61. if ((drv = db_get_default_driver_name()))
  62. driver->answer = (char *) drv;
  63. database = G_define_standard_option(G_OPT_DB_DATABASE);
  64. if ((db = db_get_default_database_name()))
  65. database->answer = (char *) db;
  66. p = G_define_flag();
  67. p->key = 'p';
  68. p->description = _("Print tables and exit");
  69. s = G_define_flag();
  70. s->key = 's';
  71. s->description = _("System tables instead of user tables");
  72. /* Set description */
  73. module = G_define_module();
  74. G_add_keyword(_("database"));
  75. G_add_keyword(_("attribute table"));
  76. module->description = _("Lists all tables for a given database.");
  77. if (G_parser(argc, argv))
  78. exit(EXIT_SUCCESS);
  79. parms.driver = driver->answer;
  80. parms.database = database->answer;
  81. parms.s = s->answer;
  82. }