Skip to content

Commit

Permalink
Add property for ThreediDatabase.path to make handling of string and …
Browse files Browse the repository at this point in the history
…pathlib paths more consistent
  • Loading branch information
margrietpalm committed Feb 13, 2024
1 parent 21b0e85 commit 2637028
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions threedi_schema/application/threedi_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def set_sqlite_pragma(dbapi_connection, connection_record):

class ThreediDatabase:
def __init__(self, path, echo=False):
self.path = path
self._path = path
self.echo = echo

self._engine = None
Expand All @@ -59,22 +59,33 @@ def engine(self):
def base_path(self):
return Path(self.path).absolute().parent

@property
def path(self):
return Path(self._path)

@path.setter
def path(self, value):
if isinstance(value, Path):
self._path = str(value)
else:
self._path = value

def get_engine(self, get_seperate_engine=False):
if self._engine is None or get_seperate_engine:
if self.path == "":
if self._path == "":
# Special case in-memory SQLite:
# https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#threading-pooling-behavior
poolclass = None
else:
poolclass = NullPool
if str(self.path).endswith(".gpkg"):
engine = create_engine(
"gpkg:///{0}".format(self.path), echo=self.echo, poolclass=poolclass
"gpkg:///{0}".format(self._path), echo=self.echo, poolclass=poolclass
)
listen(engine, "connect", load_spatialite_gpkg)
else:
engine = create_engine(
"sqlite:///{0}".format(self.path),
"sqlite:///{0}".format(self._path),
echo=self.echo,
poolclass=poolclass,
)
Expand Down

0 comments on commit 2637028

Please sign in to comment.