Skip to content

Commit 4bc06a2

Browse files
committed
memory_name= feature, closes #405
1 parent 8f528ed commit 4bc06a2

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

docs/python-api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ If you want to create an in-memory database, you can do so like this:
9898
9999
db = Database(memory=True)
100100
101+
You can also create a named in-memory database. Unlike regular memory databases these can be accessed by multiple threads, provided at least one reference to the database still exists. `del db` will clear the database from memory.
102+
103+
.. code-block:: python
104+
105+
db = Database(memory_name="my_shared_database")
106+
101107
Connections use ``PRAGMA recursive_triggers=on`` by default. If you don't want to use `recursive triggers <https://www.sqlite.org/pragma.html#pragma_recursive_triggers>`__ you can turn them off using:
102108

103109
.. code-block:: python

sqlite_utils/db.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ class Database:
278278
- ``filename_or_conn`` - String path to a file, or a ``pathlib.Path`` object, or a
279279
``sqlite3`` connection
280280
- ``memory`` - set to ``True`` to create an in-memory database
281+
- ``memory_name`` - creates a named in-memory database that can be shared across multiple connections.
281282
- ``recreate`` - set to ``True`` to delete and recreate a file database (**dangerous**)
282283
- ``recursive_triggers`` - defaults to ``True``, which sets ``PRAGMA recursive_triggers=on;`` -
283284
set to ``False`` to avoid setting this pragma
@@ -294,15 +295,23 @@ def __init__(
294295
self,
295296
filename_or_conn: Union[str, pathlib.Path, sqlite3.Connection] = None,
296297
memory: bool = False,
298+
memory_name: str = None,
297299
recreate: bool = False,
298300
recursive_triggers: bool = True,
299301
tracer: Callable = None,
300302
use_counts_table: bool = False,
301303
):
302-
assert (filename_or_conn is not None and not memory) or (
303-
filename_or_conn is None and memory
304+
assert (filename_or_conn is not None and (not memory and not memory_name)) or (
305+
filename_or_conn is None and (memory or memory_name)
304306
), "Either specify a filename_or_conn or pass memory=True"
305-
if memory or filename_or_conn == ":memory:":
307+
if memory_name:
308+
uri = "file:{}?mode=memory&cache=shared".format(memory_name)
309+
self.conn = sqlite3.connect(
310+
uri,
311+
uri=True,
312+
check_same_thread=False,
313+
)
314+
elif memory or filename_or_conn == ":memory:":
306315
self.conn = sqlite3.connect(":memory:")
307316
elif isinstance(filename_or_conn, (str, pathlib.Path)):
308317
if recreate and os.path.exists(filename_or_conn):

tests/test_constructor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ def test_recursive_triggers():
99
def test_recursive_triggers_off():
1010
db = Database(memory=True, recursive_triggers=False)
1111
assert not db.execute("PRAGMA recursive_triggers").fetchone()[0]
12+
13+
14+
def test_memory_name():
15+
db1 = Database(memory_name="shared")
16+
db2 = Database(memory_name="shared")
17+
db1["dogs"].insert({"name": "Cleo"})
18+
assert list(db2["dogs"].rows) == [{"name": "Cleo"}]

0 commit comments

Comments
 (0)