main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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-2014 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. static char *substitute_variables(dbConnection *);
  25. int main(int argc, char *argv[])
  26. {
  27. char *databaseName;
  28. dbConnection conn;
  29. struct Flag *print, *shell, *check_set_default, *def;
  30. /* struct Option *driver, *database, *user, *password, *keycol; */
  31. struct Option *driver, *database, *schema, *group;
  32. struct GModule *module;
  33. /* Initialize the GIS calls */
  34. G_gisinit(argv[0]);
  35. /* Set description */
  36. module = G_define_module();
  37. G_add_keyword(_("database"));
  38. G_add_keyword(_("attribute table"));
  39. G_add_keyword(_("connection settings"));
  40. module->description =
  41. _("Prints/sets general DB connection for current mapset.");
  42. print = G_define_flag();
  43. print->key = 'p';
  44. print->label = _("Print current connection parameters and exit");
  45. print->description = _("Substitute variables in database settings");
  46. print->guisection = _("Print");
  47. shell = G_define_flag();
  48. shell->key = 'g';
  49. shell->description = _("Print current connection parameters using shell style and exit");
  50. shell->guisection = _("Print");
  51. check_set_default = G_define_flag();
  52. check_set_default->key = 'c';
  53. check_set_default->description =
  54. _("Check connection parameters, set if uninitialized, and exit");
  55. check_set_default->guisection = _("Set");
  56. def = G_define_flag();
  57. def->key = 'd';
  58. def->label = _("Set from default settings and exit");
  59. def->description = _("Overwrite current settings if already initialized");
  60. def->guisection = _("Set");
  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 = _("Set");
  65. database = G_define_standard_option(G_OPT_DB_DATABASE);
  66. database->answer = (char *) db_get_default_database_name();
  67. database->guisection = _("Set");
  68. schema = G_define_standard_option(G_OPT_DB_SCHEMA);
  69. schema->answer = (char *) db_get_default_schema_name();
  70. schema->guisection = _("Set");
  71. group = G_define_option();
  72. group->key = "group";
  73. group->type = TYPE_STRING;
  74. group->required = NO;
  75. group->multiple = NO;
  76. group->answer = (char*) db_get_default_group_name();
  77. group->description = _("Default group of database users to which "
  78. "select privilege is granted");
  79. group->guisection = _("Set");
  80. /* commented due to new mechanism - see db.login
  81. user = G_define_option() ;
  82. user->key = "user" ;
  83. user->type = TYPE_STRING ;
  84. user->required = NO ;
  85. user->multiple = NO ;
  86. user->description= "User:" ;
  87. password = G_define_option() ;
  88. password->key = "password" ;
  89. password->type = TYPE_STRING ;
  90. password->required = NO ;
  91. password->multiple = NO ;
  92. password->description= "Password:" ;
  93. */
  94. if (G_parser(argc, argv))
  95. exit(EXIT_FAILURE);
  96. if (print->answer || shell->answer) {
  97. /* get and print connection in shell style */
  98. if (db_get_connection(&conn) == DB_OK) {
  99. if (shell->answer) {
  100. fprintf(stdout, "driver=%s\n",
  101. conn.driverName ? conn.driverName : "");
  102. fprintf(stdout, "database=%s\n",
  103. conn.databaseName ? conn.databaseName : "");
  104. fprintf(stdout, "schema=%s\n",
  105. conn.schemaName ? conn.schemaName : "");
  106. fprintf(stdout, "group=%s\n", conn.group ? conn.group : "");
  107. }
  108. else {
  109. databaseName = substitute_variables(&conn);
  110. fprintf(stdout, "driver: %s\n",
  111. conn.driverName ? conn.driverName : "");
  112. /* substitute variables */
  113. fprintf(stdout, "database: %s\n", databaseName);
  114. G_free(database);
  115. fprintf(stdout, "schema: %s\n",
  116. conn.schemaName ? conn.schemaName : "");
  117. fprintf(stdout, "group: %s\n", conn.group ? conn.group : "");
  118. }
  119. }
  120. else
  121. G_fatal_error(_("Database connection not defined. "
  122. "Run db.connect."));
  123. exit(EXIT_SUCCESS);
  124. }
  125. if (check_set_default->answer) {
  126. /* check connection and set to system-wide default in required */
  127. /*
  128. * TODO: improve db_{get,set}_connection() to not return DB_OK on error
  129. * (thus currently there is no point in checking for that here)
  130. */
  131. db_get_connection(&conn);
  132. if (!conn.driverName && !conn.databaseName) {
  133. db_set_default_connection();
  134. db_get_connection(&conn);
  135. databaseName = substitute_variables(&conn);
  136. G_important_message(_("Default driver / database set to:\n"
  137. "driver: %s\ndatabase: %s"), conn.driverName,
  138. databaseName);
  139. }
  140. else {
  141. G_important_message(_("DB settings already defined, nothing to do"));
  142. }
  143. /* they must be a matched pair, so if one is set but not the other
  144. then give up and let the user figure it out */
  145. if (!conn.driverName) {
  146. G_fatal_error(_("Default driver is not set"));
  147. }
  148. if (!conn.databaseName) {
  149. G_fatal_error(_("Default database is not set"));
  150. }
  151. /* connection either already existed or now exists */
  152. exit(EXIT_SUCCESS);
  153. }
  154. if (def->answer) {
  155. db_set_default_connection();
  156. db_get_connection(&conn);
  157. databaseName = substitute_variables(&conn);
  158. G_important_message(_("Default driver / database set to:\n"
  159. "driver: %s\ndatabase: %s"), conn.driverName,
  160. databaseName);
  161. exit(EXIT_SUCCESS);
  162. }
  163. /* set connection */
  164. db_get_connection(&conn); /* read current */
  165. if (driver->answer)
  166. conn.driverName = driver->answer;
  167. if (database->answer)
  168. conn.databaseName = database->answer;
  169. if (schema->answer)
  170. conn.schemaName = schema->answer;
  171. if (group->answer)
  172. conn.group = group->answer;
  173. db_set_connection(&conn);
  174. exit(EXIT_SUCCESS);
  175. }
  176. char *substitute_variables(dbConnection *conn)
  177. {
  178. char *database, *c, buf[GPATH_MAX];
  179. if (!conn->databaseName)
  180. return NULL;
  181. database = (char *) G_malloc(GPATH_MAX);
  182. strcpy(database, conn->databaseName);
  183. strcpy(buf, database);
  184. c = (char *)strstr(buf, "$GISDBASE");
  185. if (c != NULL) {
  186. *c = '\0';
  187. sprintf(database, "%s%s%s", buf, G_gisdbase(), c + 9);
  188. }
  189. strcpy(buf, database);
  190. c = (char *)strstr(buf, "$LOCATION_NAME");
  191. if (c != NULL) {
  192. *c = '\0';
  193. sprintf(database, "%s%s%s", buf, G_location(), c + 14);
  194. }
  195. strcpy(buf, database);
  196. c = (char *)strstr(buf, "$MAPSET");
  197. if (c != NULL) {
  198. *c = '\0';
  199. sprintf(database, "%s%s%s", buf, G_mapset(), c + 7);
  200. }
  201. #ifdef __MINGW32__
  202. if (strcmp(conn->driverName, "sqlite") == 0 ||
  203. strcmp(conn->driverName, "dbf") == 0) {
  204. char *p;
  205. p = database;
  206. while(*p) {
  207. if (*p == '/')
  208. *p = HOST_DIRSEP;
  209. p++;
  210. }
  211. }
  212. #endif
  213. return database;
  214. }