mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-13 20:14:24 +02:00
feat: Implement a prepare_connection plugin hook
This commit is contained in:
parent
86a352f8b7
commit
ae973e794b
4 changed files with 78 additions and 2 deletions
|
|
@ -84,7 +84,7 @@ Your command will also be listed in the output of ``sqlite-utils --help``.
|
|||
Plugin hooks
|
||||
------------
|
||||
|
||||
Plugin hooks allow ``sqlite-utils`` to be customized. There is currently one hook.
|
||||
Plugin hooks allow ``sqlite-utils`` to be customized.
|
||||
|
||||
.. _plugins_hooks_register_commands:
|
||||
|
||||
|
|
@ -107,3 +107,29 @@ Example implementation:
|
|||
"Say hello world"
|
||||
click.echo("Hello world!")
|
||||
|
||||
prepare_connection(conncl)
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
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>`_,
|
||||
aggregates and collations. For example:
|
||||
|
||||
|
||||
Example implementation:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import click
|
||||
import sqlite_utils
|
||||
|
||||
@sqlite_utils.hookimpl
|
||||
def prepare_connection(self, conn):
|
||||
conn.create_function(
|
||||
"hello", 1, lambda name: f"Hello, {name}!"
|
||||
)
|
||||
|
||||
This registers a SQL function called ``hello`` which takes a single
|
||||
argument and can be called like this::
|
||||
|
||||
.. code-block:: sql
|
||||
select hello("world"); -- "Hello, world!"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue