main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /****************************************************************************
  2. *
  3. * MODULE: db.connect
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Alex Shevlakov <sixote yahoo.com>,
  6. * Glynn Clements <glynn gclements.plus.com>,
  7. * Markus Neteler <neteler itc.it>,
  8. * Hamish Bowman <hamish_b yahoo com>
  9. * Martin Landa <landa.martin gmail.com> ('d' flag)
  10. * PURPOSE: set parameters for connection to database
  11. * COPYRIGHT: (C) 2002-2013 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/dbmi.h>
  23. #include <grass/glocale.h>
  24. /* database for DBF can be written with variables:
  25. * database=$GISDBASE/$LOCATION_NAME/$MAPSET/dbf
  26. */
  27. int main(int argc, char *argv[])
  28. {
  29. dbConnection conn;
  30. struct Flag *print, *shell, *check_set_default, *def;
  31. /* struct Option *driver, *database, *user, *password, *keycol; */
  32. struct Option *driver, *database, *schema, *group;
  33. struct GModule *module;
  34. /* Initialize the GIS calls */
  35. G_gisinit(argv[0]);
  36. /* Set description */
  37. module = G_define_module();
  38. G_add_keyword(_("database"));
  39. G_add_keyword(_("attribute table"));
  40. G_add_keyword(_("connection settings"));
  41. module->description =
  42. _("Prints/sets general DB connection for current mapset.");
  43. print = G_define_flag();
  44. print->key = 'p';
  45. print->label = _("Print current connection parameters and exit");
  46. print->description = _("Substitute variables in database settings");
  47. print->guisection = _("Print");
  48. shell = G_define_flag();
  49. shell->key = 'g';
  50. shell->description = _("Print current connection parameters using shell style and exit");
  51. shell->guisection = _("Print");
  52. check_set_default = G_define_flag();
  53. check_set_default->key = 'c';
  54. check_set_default->description =
  55. _("Check connection parameters, set if uninitialized, and exit");
  56. check_set_default->guisection = _("Set");
  57. def = G_define_flag();
  58. def->key = 'd';
  59. def->label = _("Set from default settings and exit");
  60. def->description = _("Overwrite current settings if already initialized");
  61. def->guisection = _("Set");
  62. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  63. driver->options = db_list_drivers();
  64. driver->answer = (char *) db_get_default_driver_name();
  65. driver->guisection = _("Set");
  66. database = G_define_standard_option(G_OPT_DB_DATABASE);
  67. database->answer = (char *) db_get_default_database_name();
  68. database->guisection = _("Set");
  69. schema = G_define_standard_option(G_OPT_DB_SCHEMA);
  70. schema->answer = (char *) db_get_default_schema_name();
  71. schema->guisection = _("Set");
  72. group = G_define_option();
  73. group->key = "group";
  74. group->type = TYPE_STRING;
  75. group->required = NO;
  76. group->multiple = NO;
  77. group->answer = (char*) db_get_default_group_name();
  78. group->description = _("Default group of database users to which "
  79. "select privilege is granted");
  80. group->guisection = _("Set");
  81. /* commented due to new mechanism:
  82. user = G_define_option() ;
  83. user->key = "user" ;
  84. user->type = TYPE_STRING ;
  85. user->required = NO ;
  86. user->multiple = NO ;
  87. user->description= "User:" ;
  88. password = G_define_option() ;
  89. password->key = "password" ;
  90. password->type = TYPE_STRING ;
  91. password->required = NO ;
  92. password->multiple = NO ;
  93. password->description= "Password:" ;
  94. */
  95. if (G_parser(argc, argv))
  96. exit(EXIT_FAILURE);
  97. if (print->answer || shell->answer) {
  98. /* get and print connection in shell style */
  99. if (db_get_connection(&conn) == DB_OK) {
  100. if (shell->answer) {
  101. fprintf(stdout, "driver=%s\n",
  102. conn.driverName ? conn.driverName : "");
  103. fprintf(stdout, "database=%s\n",
  104. conn.databaseName ? conn.databaseName : "");
  105. fprintf(stdout, "schema=%s\n",
  106. conn.schemaName ? conn.schemaName : "");
  107. fprintf(stdout, "group=%s\n", conn.group ? conn.group : "");
  108. }
  109. else {
  110. char database[GPATH_MAX];
  111. if (conn.databaseName) {
  112. char *c, buf[GPATH_MAX];
  113. strcpy(database, conn.databaseName);
  114. strcpy(buf, database);
  115. c = (char *)strstr(buf, "$GISDBASE");
  116. if (c != NULL) {
  117. *c = '\0';
  118. sprintf(database, "%s%s%s", buf, G_gisdbase(), c + 9);
  119. }
  120. strcpy(buf, database);
  121. c = (char *)strstr(buf, "$LOCATION_NAME");
  122. if (c != NULL) {
  123. *c = '\0';
  124. sprintf(database, "%s%s%s", buf, G_location(), c + 14);
  125. }
  126. strcpy(buf, database);
  127. c = (char *)strstr(buf, "$MAPSET");
  128. if (c != NULL) {
  129. *c = '\0';
  130. sprintf(database, "%s%s%s", buf, G_mapset(), c + 7);
  131. }
  132. }
  133. else {
  134. database[0] = '\0';
  135. }
  136. fprintf(stdout, "driver: %s\n",
  137. conn.driverName ? conn.driverName : "");
  138. /* substitute variables */
  139. fprintf(stdout, "database: %s\n", database);
  140. fprintf(stdout, "schema: %s\n",
  141. conn.schemaName ? conn.schemaName : "");
  142. fprintf(stdout, "group: %s\n", conn.group ? conn.group : "");
  143. }
  144. }
  145. else
  146. G_fatal_error(_("Database connection not defined. "
  147. "Run db.connect."));
  148. exit(EXIT_SUCCESS);
  149. }
  150. if (check_set_default->answer) {
  151. /* check connection and set to system-wide default in required */
  152. /*
  153. * TODO: improve db_{get,set}_connection() to not return DB_OK on error
  154. * (thus currently there is no point in checking for that here)
  155. */
  156. db_get_connection(&conn);
  157. if (!conn.driverName && !conn.databaseName) {
  158. db_set_default_connection();
  159. db_get_connection(&conn);
  160. G_important_message(_("Default driver / database set to:\n"
  161. "driver: %s\ndatabase: %s"), conn.driverName,
  162. conn.databaseName);
  163. }
  164. /* they must be a matched pair, so if one is set but not the other
  165. then give up and let the user figure it out */
  166. else if (!conn.driverName) {
  167. G_fatal_error(_("Default driver is not set"));
  168. }
  169. else if (!conn.databaseName) {
  170. G_fatal_error(_("Default database is not set"));
  171. }
  172. /* connection either already existed or now exists */
  173. exit(EXIT_SUCCESS);
  174. }
  175. if (def->answer) {
  176. db_set_default_connection();
  177. db_get_connection(&conn);
  178. G_important_message(_("Default driver / database set to:\n"
  179. "driver: %s\ndatabase: %s"), conn.driverName,
  180. conn.databaseName);
  181. exit(EXIT_SUCCESS);
  182. }
  183. /* set connection */
  184. db_get_connection(&conn); /* read current */
  185. if (driver->answer)
  186. conn.driverName = driver->answer;
  187. if (database->answer)
  188. conn.databaseName = database->answer;
  189. if (schema->answer)
  190. conn.schemaName = schema->answer;
  191. if (group->answer)
  192. conn.group = group->answer;
  193. db_set_connection(&conn);
  194. exit(EXIT_SUCCESS);
  195. }