Skip to content

Commit

Permalink
Use read-only SQLite connection (#165)
Browse files Browse the repository at this point in the history
* Use read-only SQLite connection

* suggestion from ruff

* Update db.py

* Update db.py

* Update db.py

* fix interface

* ruff format
  • Loading branch information
jan-janssen authored Jun 5, 2024
1 parent 393692c commit 55c2244
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mendeleev/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ def get_package_dbpath() -> str:
return os.path.join(os.path.abspath(os.path.dirname(__file__)), DBNAME)


def get_engine(dbpath: str = None) -> Engine:
def get_engine(dbpath: str = None, read_only: bool = True) -> Engine:
"""Return the db engine"""
if not dbpath:
dbpath = get_package_dbpath()
return create_engine("sqlite:///{path:s}".format(path=dbpath), echo=False)
if read_only:
connectstr = "sqlite:///file:{path:s}?mode=ro&nolock=1&uri=true".format(
path=dbpath
)
else:
connectstr = "sqlite:///{path:s}".format(path=dbpath)
return create_engine(connectstr, echo=False)


def get_session(dbpath: str = None) -> Session:
def get_session(dbpath: str = None, read_only: bool = True) -> Session:
"""Return the database session connection."""
engine = get_engine(dbpath=dbpath)
engine = get_engine(dbpath=dbpath, read_only=read_only)
db_session = sessionmaker(bind=engine, autoflush=False, autocommit=False)
return db_session()

0 comments on commit 55c2244

Please # to comment.