Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temporal dbif for current mapset only #2448

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions python/grass/temporal/abstract_map_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ def delete(self, dbif=None, update=True, execute=True):

mapset = get_current_mapset()

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

if self.is_in_db(dbif, mapset=mapset):
Expand Down Expand Up @@ -1061,7 +1061,7 @@ def unregister(self, dbif=None, update=True, execute=True):
mapset = get_current_mapset()

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

# Get all datasets in which this map is registered
datasets = self.get_registered_stds(dbif, mapset=mapset)
Expand Down
69 changes: 47 additions & 22 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,23 +243,26 @@ 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 to be used for the tgis db
"""
DictSQLSerializer.__init__(self)

self.table = table # Name of the table, set in the subclass
self.ident = ident
self.msgr = get_tgis_message_interface()

# set the mapset where the data are located
if self.ident and self.ident.find("@") >= 0:
self.mapset = self.ident.split("@" "")[1]
self.data_mapset = self.ident.split("@" "")[1]
else:
self.mapset = None
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 @@ -326,19 +330,40 @@ def is_in_db(self, dbif=None, mapset=None):

sql = self.get_is_in_db_statement()

# default: search temporal database in the mapset of the map
# determine correct mapset for the temporal database
if mapset is None:
mapset = self.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 @@ -366,7 +391,7 @@ def get_select_statement_mogrified(self, dbif=None):
dbif = SQLDatabaseInterfaceConnection()

return dbif.mogrify_sql_statement(
self.get_select_statement(), mapset=self.mapset
self.get_select_statement(), mapset=self.tgis_mapset
)

def select(self, dbif=None, mapset=None):
Expand All @@ -377,12 +402,10 @@ def select(self, dbif=None, mapset=None):
if None a temporary connection will be established
"""
sql, args = self.get_select_statement()
# print(sql)
# print(args)

# default: use the temporal database in the mapset of this map
# determine correct mapset for the temporal database
if mapset is None:
mapset = self.mapset
mapset = get_current_mapset()

self.msgr.debug(2, "SQLDatabaseInterface.select() from mapset %s" % mapset)

Expand Down Expand Up @@ -442,8 +465,6 @@ def insert(self, dbif=None):
if None a temporary connection will be established
"""
sql, args = self.get_insert_statement()
# print(sql)
# print(args)

# use the temporal database in the current mapset
mapset = get_current_mapset()
Expand Down Expand Up @@ -546,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 Expand Up @@ -640,7 +665,7 @@ def __init__(
"name@mapset" or "name:layer@mapset"
used as as primary key in the temporal database
:param name: The name of the map or dataset
:param mapset: The name of the mapset
:param mapset: The name of the mapset where data are stored
:param creator: The name of the creator
:param ctime: The creation datetime object
:param ttype: The temporal type
Expand Down
19 changes: 13 additions & 6 deletions python/grass/temporal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,22 @@ def stop_subprocesses():
atexit.register(stop_subprocesses)


def get_available_temporal_mapsets():
def get_available_temporal_mapsets(only_current_mapset=False):
"""Return a list of of mapset names with temporal database driver and names
that are accessible from the current mapset.
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
Optionally, the list can be restricted to the current mapset
to avoid that temporal databases outside the current mapset are used

:returns: A dictionary, mapset names are keys, the tuple (driver,
database) are the values
"""
global c_library_interface
global message_interface

mapsets = c_library_interface.available_mapsets()
if only_current_mapset:
mapsets = [get_current_mapset()]
else:
mapsets = c_library_interface.available_mapsets()

tgis_mapsets = {}

Expand Down Expand Up @@ -1059,8 +1064,10 @@ def _create_tgis_metadata_table(content, dbif=None):


class SQLDatabaseInterfaceConnection:
def __init__(self):
self.tgis_mapsets = get_available_temporal_mapsets()
def __init__(self, only_current_mapset=False):
self.tgis_mapsets = get_available_temporal_mapsets(
only_current_mapset=only_current_mapset
)
self.current_mapset = get_current_mapset()
self.connections = {}
self.connected = False
Expand Down Expand Up @@ -1587,7 +1594,7 @@ def execute_transaction(self, statement, mapset=None):
###############################################################################


def init_dbif(dbif):
def init_dbif(dbif, only_current_mapset=False):
"""This method checks if the database interface connection exists,
if not a new one will be created, connected and True will be returned.
If the database interface exists but is not connected, the connection
Expand All @@ -1612,7 +1619,7 @@ def init_dbif(dbif):
connection_state_changed = False

if dbif is None:
dbif = SQLDatabaseInterfaceConnection()
dbif = SQLDatabaseInterfaceConnection(only_current_mapset=only_current_mapset)
dbif.connect()
connection_state_changed = True
elif dbif.is_connected() is False:
Expand Down
7 changes: 4 additions & 3 deletions python/grass/temporal/list_stds.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_dataset_list(

result = {}

# go through the tgis dbs of all available mapsets
for mapset in mapsets.keys():
if temporal_type == "absolute":
table = sp.get_type() + "_view_abs_time"
Expand All @@ -99,15 +100,15 @@ def get_dataset_list(
else:
sql = "SELECT * FROM " + table

# maps from mapset A can be registered in the tgis db in mapset B:
# do not restrict sql where to mapset = <tgis mapset>
if where:
sql += " WHERE " + where
sql += " AND mapset = '%s'" % (mapset)
else:
sql += " WHERE mapset = '%s'" % (mapset)

if order:
sql += " ORDER BY " + order

# only use the tgis db of mapset 'mapset'
dbif.execute(sql, mapset=mapset)
rows = dbif.fetchall(mapset=mapset)

Expand Down
4 changes: 2 additions & 2 deletions python/grass/temporal/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,8 +1409,8 @@ def get_semantic_labels(self):

dbif = SQLDatabaseInterfaceConnection()
dbif.connect()
dbif.execute(sql, mapset=self.mapset)
rows = dbif.fetchall(mapset=self.mapset)
dbif.execute(sql, mapset=self.tgis_mapset)
rows = dbif.fetchall(mapset=self.tgis_mapset)
dbif.close()

if rows:
Expand Down
3 changes: 2 additions & 1 deletion 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 All @@ -75,7 +76,7 @@ def open_old_stds(name, type, dbif=None):

dbif, connection_state_changed = init_dbif(dbif)

if not sp.is_in_db(dbif):
if not sp.is_in_db(dbif, mapset=mapset):
dbif.close()
msgr.fatal(
_("Space time %(sp)s dataset <%(id)s> not found")
Expand Down
18 changes: 6 additions & 12 deletions python/grass/temporal/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,8 @@ def register_maps_in_space_time_dataset(
msgr.fatal(_("Please specify %s= or %s=") % ("maps", "file"))
# We may need the mapset
mapset = get_current_mapset()
dbif, connection_state_changed = init_dbif(None)

# create new stds only in the current mapset
# remove all connections to any other mapsets
# ugly hack !
currcon = {}
currcon[mapset] = dbif.connections[mapset]
dbif.connections = currcon
# only use the TGIS db of the current mapset
dbif, connection_state_changed = init_dbif(None, only_current_mapset=True)

# The name of the space time dataset is optional
if name:
Expand Down Expand Up @@ -312,7 +306,6 @@ def register_maps_in_space_time_dataset(
map.set_time_to_relative()
else:
map.set_time_to_absolute()

else:
is_in_db = True
# Check the overwrite flag
Expand Down Expand Up @@ -348,10 +341,10 @@ def register_maps_in_space_time_dataset(
continue

# Select information from temporal database
map.select(dbif)
map.select(dbif, mapset)

# Save the datasets that must be updated
datasets = map.get_registered_stds(dbif)
datasets = map.get_registered_stds(dbif, mapset)
if datasets is not None:
for dataset in datasets:
if dataset != "":
Expand Down Expand Up @@ -604,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 @@ -658,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
3 changes: 3 additions & 0 deletions python/grass/temporal/space_time_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ def reset(self, ident):
self.relative_time = STRDSRelativeTime(ident=ident)
self.spatial_extent = STRDSSpatialExtent(ident=ident)
self.metadata = STRDSMetadata(ident=ident)
self.metadata.tgis_mapset = self.get_mapset()


###############################################################################
Expand Down Expand Up @@ -1391,6 +1392,7 @@ def reset(self, ident):
self.relative_time = STR3DSRelativeTime(ident=ident)
self.spatial_extent = STR3DSSpatialExtent(ident=ident)
self.metadata = STR3DSMetadata(ident=ident)
self.metadata.tgis_mapset = self.get_mapset()


###############################################################################
Expand Down Expand Up @@ -1500,6 +1502,7 @@ def reset(self, ident):
self.relative_time = STVDSRelativeTime(ident=ident)
self.spatial_extent = STVDSSpatialExtent(ident=ident)
self.metadata = STVDSMetadata(ident=ident)
self.metadata.tgis_mapset = self.get_mapset()


###############################################################################
Expand Down
Loading
Loading