main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /****************************************************************************
  2. *
  3. * MODULE: t.connect
  4. * AUTHOR(S): Soeren Gebbert, based on db.connect
  5. *
  6. * PURPOSE: Prints/sets general temporal GIS database connection for current mapset.
  7. * COPYRIGHT: (C) 2002-2010 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 <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <grass/gis.h>
  18. #include <grass/temporal.h>
  19. #include <grass/glocale.h>
  20. int main(int argc, char *argv[])
  21. {
  22. dbConnection conn;
  23. struct Flag *print, *check_set_default, *def, *sh;
  24. struct Option *driver, *database;
  25. struct GModule *module;
  26. /* Initialize the GIS calls */
  27. G_gisinit(argv[0]);
  28. /* Set description */
  29. module = G_define_module();
  30. G_add_keyword(_("database"));
  31. G_add_keyword(_("attribute table"));
  32. G_add_keyword(_("connection settings"));
  33. module->description =
  34. _("Prints/sets general temporal GIS database connection for current mapset.");
  35. print = G_define_flag();
  36. print->key = 'p';
  37. print->description = _("Print current connection parameters and exit");
  38. print->guisection = _("Print");
  39. check_set_default = G_define_flag();
  40. check_set_default->key = 'c';
  41. check_set_default->description =
  42. _("Check connection parameters, set if uninitialized, and exit");
  43. check_set_default->guisection = _("Set");
  44. def = G_define_flag();
  45. def->key = 'd';
  46. def->label = _("Set from default settings and exit");
  47. def->description = _("Overwrite current settings if initialized");
  48. def->guisection = _("Set");
  49. sh = G_define_flag();
  50. sh->key = 'g';
  51. sh->description = _("Print current connection parameter in shell style and exit");
  52. sh->guisection = _("Set");
  53. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  54. driver->options = "sqlite,pg";
  55. driver->answer = (char *) tgis_get_default_driver_name();
  56. driver->guisection = _("Set");
  57. database = G_define_standard_option(G_OPT_DB_DATABASE);
  58. database->answer = (char *) tgis_get_default_database_name();
  59. database->guisection = _("Set");
  60. if (G_parser(argc, argv))
  61. exit(EXIT_FAILURE);
  62. if (print->answer) {
  63. if(sh->answer) {
  64. if (tgis_get_connection(&conn) == DB_OK) {
  65. fprintf(stdout, "driver=%s\n",
  66. conn.driverName ? conn.driverName : "");
  67. fprintf(stdout, "database=%s\n",
  68. conn.databaseName ? conn.databaseName : "");
  69. }
  70. else
  71. G_fatal_error(_("Temporal GIS database connection not defined. "
  72. "Run t.connect."));
  73. } else {
  74. /* get and print connection */
  75. if (tgis_get_connection(&conn) == DB_OK) {
  76. fprintf(stdout, "driver:%s\n",
  77. conn.driverName ? conn.driverName : "");
  78. fprintf(stdout, "database:%s\n",
  79. conn.databaseName ? conn.databaseName : "");
  80. }
  81. else
  82. G_fatal_error(_("Temporal GIS database connection not defined. "
  83. "Run t.connect."));
  84. }
  85. exit(EXIT_SUCCESS);
  86. }
  87. if (check_set_default->answer) {
  88. /* check connection and set to system-wide default in required */
  89. tgis_get_connection(&conn);
  90. if (!conn.driverName && !conn.databaseName) {
  91. tgis_set_default_connection();
  92. tgis_get_connection(&conn);
  93. G_important_message(_("Default TGIS driver / database set to:\n"
  94. "driver: %s\ndatabase: %s"), conn.driverName,
  95. conn.databaseName);
  96. }
  97. /* they must be a matched pair, so if one is set but not the other
  98. then give up and let the user figure it out */
  99. else if (!conn.driverName) {
  100. G_fatal_error(_("Default TGIS driver is not set"));
  101. }
  102. else if (!conn.databaseName) {
  103. G_fatal_error(_("Default TGIS database is not set"));
  104. }
  105. /* connection either already existed or now exists */
  106. exit(EXIT_SUCCESS);
  107. }
  108. if (def->answer) {
  109. tgis_set_default_connection();
  110. tgis_get_connection(&conn);
  111. G_important_message(_("Default driver / database set to:\n"
  112. "driver: %s\ndatabase: %s"), conn.driverName,
  113. conn.databaseName);
  114. exit(EXIT_SUCCESS);
  115. }
  116. /* set connection */
  117. tgis_get_connection(&conn); /* read current */
  118. if (driver->answer)
  119. conn.driverName = driver->answer;
  120. if (database->answer)
  121. conn.databaseName = database->answer;
  122. tgis_set_connection(&conn);
  123. exit(EXIT_SUCCESS);
  124. }