Skip to content

Commit

Permalink
Add option to use in-memory database
Browse files Browse the repository at this point in the history
This is unused but can be handy.
  • Loading branch information
victorlin committed Dec 8, 2022
1 parent 3503daf commit a7f322a
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions augur/io/sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@ class Sqlite3Database:
"""Represents a SQLite3 database.
Provides useful methods to manage connections and general I/O.
If a file is not provided, an in-memory database will be used.
"""

def __init__(self, file:str):
def __init__(self, file:str=''):

self.file = file
"""Database file."""
"""Database file. Empty string if using an in-memory database."""

self.use_in_memory_db = not self.file
"""Whether to use an in-memory database."""

if self.use_in_memory_db:
# More info: https://www.sqlite.org/inmemorydb.html
self.in_memory_db_connection = sqlite3.connect(":memory:")
"""Persistent connection to an in-memory database."""

def connect(self, **connect_kwargs) -> sqlite3.Connection:
"""Return a new connection to the SQLite database."""
"""Return a connection to the SQLite database.
If using an in-memory database, the same connection is returned for all calls to this method.
Otherwise, a new connection is returned every time.
"""
if self.use_in_memory_db:
return self.in_memory_db_connection
return sqlite3.connect(self.file, **connect_kwargs)

def execute(self, *args):
Expand Down

0 comments on commit a7f322a

Please sign in to comment.