default_name.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*!
  2. \file lib/db/dbmi_base/default_name.c
  3. \brief DBMI Library (base) - default settings
  4. (C) 1999-2010 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Joel Jones (CERL/UIUC)
  8. \author Upgraded to GRASS 5.7 by Radim Blazek
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/glocale.h>
  15. /*!
  16. \brief Get default driver name
  17. \return pointer to default driver name
  18. \return NULL if not set
  19. */
  20. const char *db_get_default_driver_name(void)
  21. {
  22. const char *drv;
  23. if ((drv = G__getenv2("DB_DRIVER", G_VAR_MAPSET)))
  24. return G_store(drv);
  25. return NULL;
  26. }
  27. /*!
  28. \brief Get default database name
  29. \return pointer to default database name
  30. \return NULL if not set
  31. */
  32. const char *db_get_default_database_name(void)
  33. {
  34. const char *drv;
  35. if ((drv = G__getenv2("DB_DATABASE", G_VAR_MAPSET)))
  36. return G_store(drv);
  37. return NULL;
  38. }
  39. /*!
  40. \brief Get default schema name
  41. \return pointer to default schema name
  42. \return NULL if not set
  43. */
  44. const char *db_get_default_schema_name(void)
  45. {
  46. const char *sch;
  47. if ((sch = G__getenv2("DB_SCHEMA", G_VAR_MAPSET)))
  48. return G_store(sch);
  49. return NULL;
  50. }
  51. /*!
  52. \brief Get default group name
  53. \return pointer to default group name
  54. \return NULL if not set
  55. */
  56. const char *db_get_default_group_name(void)
  57. {
  58. const char *gr;
  59. if ((gr = G__getenv2("DB_GROUP", G_VAR_MAPSET)))
  60. return G_store(gr);
  61. return NULL;
  62. }
  63. /*!
  64. \brief Sets up database connection settings using GRASS default from dbmi.h
  65. \todo DB_OK on success, DB_* error code on fail
  66. \return returns DB_OK
  67. */
  68. int db_set_default_connection(void)
  69. {
  70. dbConnection connection;
  71. char buf[GPATH_MAX];
  72. G_debug(1,
  73. "Creating new default DB params with db_set_default_connection()");
  74. /* is this really needed ? */
  75. db_get_connection(&connection);
  76. if (strcmp(DB_DEFAULT_DRIVER, "dbf") == 0) {
  77. /* Set default values and create dbf db dir */
  78. connection.driverName = "dbf";
  79. connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
  80. db_set_connection(&connection);
  81. sprintf(buf, "%s/%s/dbf", G_location_path(), G_mapset());
  82. G__make_mapset_element("dbf");
  83. }
  84. else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) {
  85. /* Set default values and create sqlite db dir */
  86. connection.driverName = "sqlite";
  87. /*
  88. * TODO: Use one DB for entire mapset (LFS problems?)
  89. * or per-map DBs in $MASPET/vector/mapname/sqlite.db (how to set that here?)
  90. * or $MAPSET/sqlite/mapname.sql as with dbf?
  91. */
  92. /* http://www.sqlite.org/lockingv3.html
  93. * When SQLite creates a journal file on Unix, it opens the
  94. * directory that contains that file and calls fsync() on the
  95. * directory, in an effort to push the directory information to disk.
  96. *
  97. * -> have sqlite.db in a separate directory
  98. */
  99. connection.databaseName =
  100. "$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db";
  101. G__make_mapset_element("sqlite");
  102. db_set_connection(&connection);
  103. }
  104. else
  105. G_fatal_error(_("Programmer error"));
  106. return DB_OK;
  107. }