Skip to content

Commit

Permalink
fix mapset support
Browse files Browse the repository at this point in the history
  • Loading branch information
metzm committed Sep 11, 2023
1 parent a7c7c88 commit d0b7c4d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
51 changes: 39 additions & 12 deletions python/grass/temporal/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
get_tgis_dbmi_paramstyle,
SQLDatabaseInterfaceConnection,
get_current_mapset,
get_available_temporal_mapsets,
)

###############################################################################
Expand Down Expand Up @@ -242,13 +243,13 @@ class SQLDatabaseInterface(DictSQLSerializer):
"""

def __init__(self, table=None, ident=None):
def __init__(self, table=None, ident=None, tgis_mapset=None):
"""Constructor of this class
:param table: The name of the table
:param ident: The identifier (primary key) of this
object in the database table
:param tgis_mapset: the mapset of the tgis db to be used
:param tgis_mapset: the mapset to be used for the tgis db
"""
DictSQLSerializer.__init__(self)

Expand All @@ -261,6 +262,7 @@ def __init__(self, table=None, ident=None):
self.data_mapset = self.ident.split("@" "")[1]
else:
self.data_mapset = None
self.tgis_mapset = tgis_mapset

def get_table_name(self):
"""Return the name of the table in which the internal
Expand Down Expand Up @@ -330,17 +332,38 @@ def is_in_db(self, dbif=None, mapset=None):

# determine correct mapset for the temporal database
if mapset is None:
mapset = get_current_mapset()
mapset = self.tgis_mapset

if dbif:
dbif.execute(sql, mapset=mapset)
row = dbif.fetchone(mapset=mapset)
row = None
if mapset is not None:
# search only in the tgis db in the given mapset
if dbif:
dbif.execute(sql, mapset=mapset)
row = dbif.fetchone(mapset=mapset)
else:
dbif = SQLDatabaseInterfaceConnection()
dbif.connect()
dbif.execute(sql, mapset=mapset)
row = dbif.fetchone(mapset=mapset)
dbif.close()
else:
dbif = SQLDatabaseInterfaceConnection()
dbif.connect()
dbif.execute(sql, mapset=mapset)
row = dbif.fetchone(mapset=mapset)
dbif.close()
# search all available datasets
tgis_mapsets = get_available_temporal_mapsets()
for mapset in tgis_mapsets:
if dbif:
dbif.execute(sql, mapset=mapset)
row = dbif.fetchone(mapset=mapset)
else:
dbif = SQLDatabaseInterfaceConnection()
dbif.connect()
dbif.execute(sql, mapset=mapset)
row = dbif.fetchone(mapset=mapset)
dbif.close()
dbif = None
if row is not None:
# set tgis mapset for this instance
self.tgis_mapset = mapset
break

# Nothing found
if row is None:
Expand Down Expand Up @@ -544,10 +567,14 @@ def get_update_all_statement_mogrified(self, dbif=None, ident=None):
:param ident: The identifier to be updated, useful for renaming
:return: The UPDATE string
"""

# use the temporal database in the current mapset
mapset = get_current_mapset()

if not dbif:
dbif = SQLDatabaseInterfaceConnection()

return dbif.mogrify_sql_statement(self.get_update_all_statement(ident))
return dbif.mogrify_sql_statement(self.get_update_all_statement(ident), mapset)

def update_all(self, dbif=None, ident=None):
"""Serialize the content of this object, including None objects,
Expand Down
1 change: 1 addition & 0 deletions python/grass/temporal/open_stds.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def open_old_stds(name, type, dbif=None):
mapset = get_current_mapset()
else:
name, mapset = name.split("@")

semantic_label = None
if name.find(".") > -1:
try:
Expand Down
3 changes: 2 additions & 1 deletion python/grass/temporal/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ def register_map_object_list(
import copy

dbif, connection_state_changed = init_dbif(dbif)
mapset = get_current_mapset()

filename = gscript.tempfile(True)
file = open(filename, "w")
Expand Down Expand Up @@ -651,7 +652,7 @@ def register_map_object_list(
if map.get_type() == "vector":
mod(type="vector", name=map.get_name())
mod.run()
if map.is_in_db(dbif):
if map.is_in_db(dbif, mapset):
map.delete(dbif)

if connection_state_changed:
Expand Down

0 comments on commit d0b7c4d

Please sign in to comment.