Browse Source

temporal library: Store the default database connection as shell variable substitude to avoid wrong temporal database path's in cases the location was renamed or the path to the grass database changed. (trunk, https://trac.osgeo.org/grass/changeset/65519)

git-svn-id: https://svn.osgeo.org/grass/grass/branches/releasebranch_7_0@66366 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Neteler 9 years ago
parent
commit
e08f2dc71e
2 changed files with 20 additions and 11 deletions
  1. 18 9
      lib/python/temporal/core.py
  2. 2 2
      lib/temporal/lib/default_name.c

+ 18 - 9
lib/python/temporal/core.py

@@ -847,7 +847,8 @@ class SQLDatabaseInterfaceConnection(object):
             driver,  dbstring = self.tgis_mapsets[mapset]
 
             if dbstring not in self.unique_connections.keys():
-                self.unique_connections[dbstring] = DBConnection(driver)
+                self.unique_connections[dbstring] = DBConnection(backend=driver, 
+                                                                 dbstring=dbstring)
 
             self.connections[mapset] = self.unique_connections[dbstring]
 
@@ -891,7 +892,7 @@ class SQLDatabaseInterfaceConnection(object):
            close all temporal databases that have been opened.
         """
         for key in self.unique_connections.keys():
-            self.unique_connections[key] .close()
+            self.unique_connections[key].close()
 
         self.connected = False
 
@@ -1001,16 +1002,20 @@ class SQLDatabaseInterfaceConnection(object):
 
 class DBConnection(object):
     """This class represents the database interface connection
-       and provides access to the chisen backend modules.
+       and provides access to the chosen backend modules.
 
        The following DBMS are supported:
 
          - sqlite via the sqlite3 standard library
          - postgresql via psycopg2
-
     """
 
-    def __init__(self, backend=None):
+    def __init__(self, backend=None, dbstring=None):
+        """ Constructor of a database connection
+        
+            param backend:The database backend sqlite or pg
+            param dbstring: The database connection string
+        """
         self.connected = False
         if backend is None:
             global tgis_backend
@@ -1024,6 +1029,10 @@ class DBConnection(object):
             else:
                 self.dbmi = psycopg2
 
+        if dbstring is None:
+            global tgis_database_string
+            self.dbstring = tgis_database_string
+
         self.msgr = get_tgis_message_interface()
         self.msgr.debug(1, "SQLDatabaseInterfaceConnection constructor")
 
@@ -1048,13 +1057,13 @@ class DBConnection(object):
     def connect(self,  dbstring=None):
         """Connect to the DBMI to execute SQL statements
 
-           Supported backends are sqlite3 and postgresql
+            Supported backends are sqlite3 and postgresql
+            
+            param dbstring: The database connection string
         """
         # Connection in the current mapset
         if dbstring is None:
-            global tgis_database_string
-            dbstring = tgis_database_string
-
+            dbstring = self.dbstring
         try:
             if self.dbmi.__name__ == "sqlite3":
                 self.connection = self.dbmi.connect(dbstring,

+ 2 - 2
lib/temporal/lib/default_name.c

@@ -40,8 +40,8 @@ char *tgis_get_default_database_name(void)
 {
     char default_connection[2048];
 
-    G_snprintf(default_connection, 2048, "%s/%s/%s/%s", G_gisdbase(), G_location(),
-               G_mapset(), TGISDB_DEFAULT_SQLITE_PATH);
+    G_snprintf(default_connection, 2048, "$GISDBASE/$LOCATION_NAME/$MAPSET/%s", 
+               TGISDB_DEFAULT_SQLITE_PATH);
 
     return G_store(default_connection);
 }