Skip to content

Commit

Permalink
TGIS: enhance mapset access management (#1924)
Browse files Browse the repository at this point in the history
* tgis: enhance mapset access management
  • Loading branch information
metzm committed Mar 14, 2022
1 parent f0eced2 commit 596b459
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 176 deletions.
76 changes: 25 additions & 51 deletions python/grass/temporal/abstract_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .core import (
get_tgis_message_interface,
init_dbif,
get_enable_mapset_check,
get_current_mapset,
)
from .temporal_topology_dataset_connector import TemporalTopologyDatasetConnector
Expand Down Expand Up @@ -354,7 +353,7 @@ def get_spatial_extent(self):
"""Return the spatial extent"""
return self.spatial_extent

def select(self, dbif=None):
def select(self, dbif=None, mapset=None):
"""Select temporal dataset entry from database and fill
the internal structure
Expand All @@ -363,27 +362,33 @@ def select(self, dbif=None):
from the temporal database.
:param dbif: The database interface to be used
:param mapset: The dbif connection to be used
"""

dbif, connection_state_changed = init_dbif(dbif)

self.base.select(dbif)
self.temporal_extent.select(dbif)
self.spatial_extent.select(dbif)
self.metadata.select(dbif)
# default mapset is mapset of this instance
if mapset is None:
mapset = self.get_mapset()

self.base.select(dbif, mapset=mapset)
self.temporal_extent.select(dbif, mapset=mapset)
self.spatial_extent.select(dbif, mapset=mapset)
self.metadata.select(dbif, mapset=mapset)
if self.is_stds() is False:
self.stds_register.select(dbif)
self.stds_register.select(dbif, mapset=mapset)

if connection_state_changed:
dbif.close()

def is_in_db(self, dbif=None):
def is_in_db(self, dbif=None, mapset=None):
"""Check if the dataset is registered in the database
:param dbif: The database interface to be used
:param mapset: The dbif connection to be used
:return: True if the dataset is registered in the database
"""
return self.base.is_in_db(dbif)
return self.base.is_in_db(dbif, mapset)

@abstractmethod
def delete(self):
Expand All @@ -400,22 +405,17 @@ def insert(self, dbif=None, execute=True):
empty string otherwise
"""

if (
get_enable_mapset_check() is True
and self.get_mapset() != get_current_mapset()
):
self.msgr.fatal(
_(
"Unable to insert dataset <%(ds)s> of type "
"%(type)s in the temporal database. The mapset "
"of the dataset does not match the current "
"mapset"
)
% {"ds": self.get_id(), "type": self.get_type()}
)
# it must be possible to insert a map from a different
# mapset in the temporal database of the current mapset
# the temporal database must be in the current mapset

dbif, connection_state_changed = init_dbif(dbif)

self.msgr.debug(2, "AbstractDataset.insert...")

# only modify database in current mapset
mapset = get_current_mapset()

# Build the INSERT SQL statement
statement = self.base.get_insert_statement_mogrified(dbif)
statement += self.temporal_extent.get_insert_statement_mogrified(dbif)
Expand All @@ -424,8 +424,10 @@ def insert(self, dbif=None, execute=True):
if self.is_stds() is False:
statement += self.stds_register.get_insert_statement_mogrified(dbif)

self.msgr.debug(2, "insert with %s" % statement)
if execute:
dbif.execute_transaction(statement)
# database to be modified must be in the current mapset
dbif.execute_transaction(statement, mapset=mapset)
if connection_state_changed:
dbif.close()
return ""
Expand All @@ -447,20 +449,6 @@ def update(self, dbif=None, execute=True, ident=None):
empty string otherwise
"""

if (
get_enable_mapset_check() is True
and self.get_mapset() != get_current_mapset()
):
self.msgr.fatal(
_(
"Unable to update dataset <%(ds)s> of type "
"%(type)s in the temporal database. The mapset "
"of the dataset does not match the current "
"mapset"
)
% {"ds": self.get_id(), "type": self.get_type()}
)

dbif, connection_state_changed = init_dbif(dbif)

# Build the UPDATE SQL statement
Expand Down Expand Up @@ -495,20 +483,6 @@ def update_all(self, dbif=None, execute=True, ident=None):
empty string otherwise
"""

if (
get_enable_mapset_check() is True
and self.get_mapset() != get_current_mapset()
):
self.msgr.fatal(
_(
"Unable to update dataset <%(ds)s> of type "
"%(type)s in the temporal database. The mapset"
" of the dataset does not match the current "
"mapset"
)
% {"ds": self.get_id(), "type": self.get_type()}
)

dbif, connection_state_changed = init_dbif(dbif)

# Build the UPDATE SQL statement
Expand Down
60 changes: 25 additions & 35 deletions python/grass/temporal/abstract_map_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,24 +919,17 @@ def delete(self, dbif=None, update=True, execute=True):
:return: The SQL statements if execute=False, else an empty string,
None in case of a failure
"""
if (
get_enable_mapset_check() is True
and self.get_mapset() != get_current_mapset()
):
self.msgr.fatal(
_(
"Unable to delete dataset <%(ds)s> of type "
"%(type)s from the temporal database. The mapset"
" of the dataset does not match the current "
"mapset"
)
% {"ds": self.get_id(), "type": self.get_type()}
)

# TODO: it must be possible to delete a map from a temporal
# database even if the map is in a different mapset,
# as long as the temporal database of the current mapset is used

mapset = get_current_mapset()

dbif, connection_state_changed = init_dbif(dbif)
statement = ""

if self.is_in_db(dbif):
if self.is_in_db(dbif, mapset=mapset):

# SELECT all needed information from the database
self.metadata.select(dbif)
Expand All @@ -954,7 +947,7 @@ def delete(self, dbif=None, update=True, execute=True):
statement += self.base.get_delete_statement()

if execute:
dbif.execute_transaction(statement)
dbif.execute_transaction(statement, mapset=mapset)
statement = ""

# Remove the timestamp from the file system
Expand Down Expand Up @@ -1002,25 +995,13 @@ def unregister(self, dbif=None, update=True, execute=True):
% {"type": self.get_type(), "map": self.get_map_id()},
)

if (
get_enable_mapset_check() is True
and self.get_mapset() != get_current_mapset()
):
self.msgr.fatal(
_(
"Unable to unregister dataset <%(ds)s> of type "
"%(type)s from the temporal database. The mapset"
" of the dataset does not match the current "
"mapset"
)
% {"ds": self.get_id(), "type": self.get_type()}
)
mapset = get_current_mapset()

statement = ""
dbif, connection_state_changed = init_dbif(dbif)

# Get all datasets in which this map is registered
datasets = self.get_registered_stds(dbif)
datasets = self.get_registered_stds(dbif, mapset=mapset)

# For each stds in which the map is registered
if datasets is not None:
Expand All @@ -1036,15 +1017,15 @@ def unregister(self, dbif=None, update=True, execute=True):
stds.update_from_registered_maps(dbif)

if execute:
dbif.execute_transaction(statement)
dbif.execute_transaction(statement, mapset=mapset)
statement = ""

if connection_state_changed:
dbif.close()

return statement

def get_registered_stds(self, dbif=None):
def get_registered_stds(self, dbif=None, mapset=None):
"""Return all space time dataset ids in which this map is registered
as as a list of strings, or None if this map is not
registered in any space time dataset.
Expand All @@ -1055,7 +1036,7 @@ def get_registered_stds(self, dbif=None):
"""
dbif, connection_state_changed = init_dbif(dbif)

self.stds_register.select(dbif)
self.stds_register.select(dbif, mapset)
datasets = self.stds_register.get_registered_stds()

if datasets is not None and datasets != "" and datasets.find("@") >= 0:
Expand All @@ -1068,6 +1049,8 @@ def get_registered_stds(self, dbif=None):

return datasets

# this fn should not be in a class for maps,
# but instead in a class for stds: AbstractSpaceTimeDataset ?
def add_stds_to_register(self, stds_id, dbif=None, execute=True):
"""Add a new space time dataset to the register
Expand All @@ -1080,9 +1063,13 @@ def add_stds_to_register(self, stds_id, dbif=None, execute=True):
:return: The SQL statements if execute=False, else an empty string
"""
self.msgr.debug(2, "AbstractMapDataset.add_stds_to_register")

dbif, connection_state_changed = init_dbif(dbif=dbif)

datasets = self.get_registered_stds(dbif=dbif)
# only modify database in current mapset
mapset = get_current_mapset()
datasets = self.get_registered_stds(dbif=dbif, mapset=mapset)

if stds_id is None or stds_id == "":
return ""
Expand All @@ -1104,7 +1091,7 @@ def add_stds_to_register(self, stds_id, dbif=None, execute=True):
statement = ""

if execute is True:
self.stds_register.update(dbif=dbif)
self.stds_register.update(dbif=dbif, mapset=mapset)
else:
statement = self.stds_register.get_update_statement_mogrified(dbif=dbif)

Expand All @@ -1126,9 +1113,12 @@ def remove_stds_from_register(self, stds_id, dbif=None, execute=True):
:return: The SQL statements if execute=False, else an empty string
"""
self.msgr.debug(2, "AbstractMapDataset.remove_stds_from_register")
dbif, connection_state_changed = init_dbif(dbif)

datasets = self.get_registered_stds(dbif=dbif)
# only modify database in current mapset
mapset = get_current_mapset()
datasets = self.get_registered_stds(dbif=dbif, mapset=mapset)

# Check if no datasets are present
if datasets is None:
Expand Down

0 comments on commit 596b459

Please sign in to comment.