Skip to content

Commit

Permalink
Documentation for derive_named_parameters()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 27, 2024
1 parent dfd4ad5 commit c863443
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import types
import secrets
import shutil
from typing import Iterable, Tuple
from typing import Iterable, List, Tuple
import urllib
import yaml
from .shutil_backport import copytree
from .sqlite import sqlite3, supports_table_xinfo

if typing.TYPE_CHECKING:
from datasette.database import Database

# From https://www.sqlite.org/lang_keywords.html
reserved_words = set(
Expand Down Expand Up @@ -1130,7 +1132,13 @@ class StartupError(Exception):
_re_named_parameter = re.compile(":([a-zA-Z0-9_]+)")


async def derive_named_parameters(db, sql):
@documented
async def derive_named_parameters(db: "Database", sql: str) -> List[str]:
"""
Given a SQL statement, return a list of named parameters that are used in the statement
e.g. for ``select * from foo where id=:id`` this would return ``["id"]``
"""
explain = "explain {}".format(sql.strip().rstrip(";"))
possible_params = _re_named_parameter.findall(sql)
try:
Expand Down
9 changes: 9 additions & 0 deletions docs/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,15 @@ Utility function for calling ``await`` on a return value if it is awaitable, oth

.. autofunction:: datasette.utils.await_me_maybe

.. _internals_utils_derive_named_parameters:

derive_named_parameters(db, sql)
--------------------------------

Derive the list of named parameters referenced in a SQL query, using an ``explain`` query executed against the provided database.

.. autofunction:: datasette.utils.derive_named_parameters

.. _internals_tilde_encoding:

Tilde encoding
Expand Down

0 comments on commit c863443

Please sign in to comment.