prepare_connection() now takes datasette and database args, refs #678

prepare-connection-datasette
Simon Willison 2020-02-21 17:32:40 -08:00
rodzic d3f2fade88
commit 6303ea5048
5 zmienionych plików z 16 dodań i 8 usunięć

Wyświetl plik

@ -316,7 +316,7 @@ class Datasette:
} }
) )
def prepare_connection(self, conn): def prepare_connection(self, conn, database):
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
conn.text_factory = lambda x: str(x, "utf-8", "replace") conn.text_factory = lambda x: str(x, "utf-8", "replace")
for name, num_args, func in self.sqlite_functions: for name, num_args, func in self.sqlite_functions:
@ -328,7 +328,7 @@ class Datasette:
if self.config("cache_size_kb"): if self.config("cache_size_kb"):
conn.execute("PRAGMA cache_size=-{}".format(self.config("cache_size_kb"))) conn.execute("PRAGMA cache_size=-{}".format(self.config("cache_size_kb")))
# pylint: disable=no-member # pylint: disable=no-member
pm.hook.prepare_connection(conn=conn) pm.hook.prepare_connection(conn=conn, database=database, datasette=self)
async def execute( async def execute(
self, self,
@ -412,7 +412,7 @@ class Datasette:
def versions(self): def versions(self):
conn = sqlite3.connect(":memory:") conn = sqlite3.connect(":memory:")
self.prepare_connection(conn) self.prepare_connection(conn, ":memory:")
sqlite_version = conn.execute("select sqlite_version()").fetchone()[0] sqlite_version = conn.execute("select sqlite_version()").fetchone()[0]
sqlite_extensions = {} sqlite_extensions = {}
for extension, testsql, hasversion in ( for extension, testsql, hasversion in (

Wyświetl plik

@ -392,5 +392,7 @@ async def check_databases(ds):
) )
except ConnectionProblem as e: except ConnectionProblem as e:
raise click.UsageError( raise click.UsageError(
"Connection to {} failed check: {}".format(database.path, str(e.args[0])) "Connection to {} failed check: {}".format(
database.path, str(e.args[0])
)
) )

Wyświetl plik

@ -58,7 +58,7 @@ class Database:
conn = getattr(connections, self.name, None) conn = getattr(connections, self.name, None)
if not conn: if not conn:
conn = self.connect() conn = self.connect()
self.ds.prepare_connection(conn) self.ds.prepare_connection(conn, self.name)
setattr(connections, self.name, conn) setattr(connections, self.name, conn)
return fn(conn) return fn(conn)

Wyświetl plik

@ -11,7 +11,7 @@ def asgi_wrapper(datasette):
@hookspec @hookspec
def prepare_connection(conn): def prepare_connection(conn, database, datasette):
"Modify SQLite connection in some way e.g. register custom SQL functions" "Modify SQLite connection in some way e.g. register custom SQL functions"

Wyświetl plik

@ -340,12 +340,18 @@ The full list of available plugin hooks is as follows.
.. _plugin_hook_prepare_connection: .. _plugin_hook_prepare_connection:
prepare_connection(conn) prepare_connection(conn, database, datasette)
~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``conn`` - sqlite3 connection object ``conn`` - sqlite3 connection object
The connection that is being opened The connection that is being opened
``database`` - string
The name of the database
``datasette`` - :ref:`datasette`
You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``
This hook is called when a new SQLite database connection is created. You can This hook is called when a new SQLite database connection is created. You can
use it to `register custom SQL functions <https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.create_function>`_, use it to `register custom SQL functions <https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.create_function>`_,
aggregates and collations. For example: aggregates and collations. For example: