mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Database(..., execute_plugins=False) mechanism, refs #575
This commit is contained in:
parent
3f80a02698
commit
374a816c72
3 changed files with 24 additions and 15 deletions
|
|
@ -20,6 +20,12 @@ You can see a JSON list of plugins that have been installed by running this:
|
|||
|
||||
sqlite-utils plugins
|
||||
|
||||
Plugin hooks such as :ref:`plugins_hooks_prepare_connection` affect each instance of the ``Database`` class. You can opt-out of these plugins by creating that class instance like so:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
db = Database(memory=True, execute_plugins=False)
|
||||
|
||||
.. _plugins_building:
|
||||
|
||||
Building a plugin
|
||||
|
|
|
|||
|
|
@ -311,6 +311,7 @@ class Database:
|
|||
recursive_triggers: bool = True,
|
||||
tracer: Optional[Callable] = None,
|
||||
use_counts_table: bool = False,
|
||||
execute_plugins: bool = True,
|
||||
):
|
||||
assert (filename_or_conn is not None and (not memory and not memory_name)) or (
|
||||
filename_or_conn is None and (memory or memory_name)
|
||||
|
|
@ -342,8 +343,8 @@ class Database:
|
|||
self.execute("PRAGMA recursive_triggers=on;")
|
||||
self._registered_functions: set = set()
|
||||
self.use_counts_table = use_counts_table
|
||||
|
||||
pm.hook.prepare_connection(conn=self.conn)
|
||||
if execute_plugins:
|
||||
pm.hook.prepare_connection(conn=self.conn)
|
||||
|
||||
def close(self):
|
||||
"Close the SQLite connection, and the underlying database file"
|
||||
|
|
|
|||
|
|
@ -49,10 +49,16 @@ def test_prepare_connection():
|
|||
conn.create_function("hello", 1, lambda name: f"Hello, {name}!")
|
||||
|
||||
db = Database(memory=True)
|
||||
functions = db.execute(
|
||||
"select distinct name from pragma_function_list order by 1"
|
||||
).fetchall()
|
||||
assert "hello" not in functions
|
||||
|
||||
def _functions(db):
|
||||
return [
|
||||
row[0]
|
||||
for row in db.execute(
|
||||
"select distinct name from pragma_function_list order by 1"
|
||||
).fetchall()
|
||||
]
|
||||
|
||||
assert "hello" not in _functions(db)
|
||||
|
||||
try:
|
||||
plugins.pm.register(HelloFunctionPlugin(), name="HelloFunctionPlugin")
|
||||
|
|
@ -62,18 +68,14 @@ def test_prepare_connection():
|
|||
]
|
||||
|
||||
db = Database(memory=True)
|
||||
|
||||
functions = [
|
||||
row[0]
|
||||
for row in db.execute(
|
||||
"select distinct name from pragma_function_list order by 1"
|
||||
).fetchall()
|
||||
]
|
||||
assert "hello" in functions
|
||||
|
||||
assert "hello" in _functions(db)
|
||||
result = db.execute('select hello("world")').fetchone()[0]
|
||||
assert result == "Hello, world!"
|
||||
|
||||
# Test execute_plugins=False
|
||||
db2 = Database(memory=True, execute_plugins=False)
|
||||
assert "hello" not in _functions(db2)
|
||||
|
||||
finally:
|
||||
plugins.pm.unregister(name="HelloFunctionPlugin")
|
||||
assert plugins.get_plugins() == []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue