main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /****************************************************************************
  2. *
  3. * MODULE: db.databases
  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: lists all databases for a given driver
  7. * COPYRIGHT: (C) 2002-2006, 2012 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General
  10. * Public License (>=v2). Read the file COPYING that
  11. * comes with GRASS for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <grass/dbmi.h>
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. struct
  19. {
  20. char *driver;
  21. char *location;
  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 *handles;
  29. dbString locations;
  30. int nlocs = 0;
  31. int count, i;
  32. db_init_string(&locations);
  33. parse_command_line(argc, argv);
  34. if (parms.location) {
  35. db_set_string(&locations, parms.location);
  36. nlocs = 1;
  37. }
  38. driver = db_start_driver(parms.driver);
  39. if (driver == NULL)
  40. G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
  41. if (db_list_databases(driver, &locations,
  42. nlocs, &handles, &count) != DB_OK) {
  43. db_shutdown_driver(driver);
  44. G_fatal_error(_("Unable to list databases. "
  45. "Try to define correct connection settings by db.login."));
  46. }
  47. db_shutdown_driver(driver);
  48. for (i = 0; i < count; i++) {
  49. fprintf(stdout, "%s\n", db_get_handle_dbname(&handles[i]));
  50. }
  51. if (count < 1)
  52. G_important_message(_("No databases found"));
  53. exit(EXIT_SUCCESS);
  54. }
  55. static void parse_command_line(int argc, char **argv)
  56. {
  57. struct Option *driver, *location;
  58. struct GModule *module;
  59. /* Initialize the GIS calls */
  60. G_gisinit(argv[0]);
  61. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  62. driver->options = db_list_drivers();
  63. driver->answer = (char *) db_get_default_driver_name();
  64. driver->guisection = _("Connection");
  65. location = G_define_option();
  66. location->key = "location";
  67. location->type = TYPE_STRING;
  68. location->required = NO;
  69. /* location->multiple = YES; ? */
  70. location->label = _("Location");
  71. location->description = _("Path for SQLite driver, or connection string "
  72. "for PostgreSQL driver");
  73. location->key_desc = "name";
  74. location->guisection = _("Connection");
  75. /* Set description */
  76. module = G_define_module();
  77. G_add_keyword(_("database"));
  78. G_add_keyword(_("attribute table"));
  79. G_add_keyword(_("SQL"));
  80. module->description =
  81. _("Lists all databases for a given driver and location.");
  82. if (G_parser(argc, argv))
  83. exit(EXIT_FAILURE);
  84. parms.driver = driver->answer;
  85. parms.location = location->answer ? location->answer : "";
  86. }