mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Support repeated calls to Table.convert()
* Test repeated calls to Table.convert() * Register Table.convert() functions under their own `lambda_hash` name * Raise exception on registering identical function names Refs #525
This commit is contained in:
parent
6500fed8b2
commit
02f5c4d69d
3 changed files with 35 additions and 9 deletions
|
|
@ -219,6 +219,11 @@ class AlterError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class FunctionAlreadyRegistered(Exception):
|
||||
"A function with this name and arity was already registered"
|
||||
pass
|
||||
|
||||
|
||||
class NoObviousTable(Exception):
|
||||
"Could not tell which table this operation refers to"
|
||||
pass
|
||||
|
|
@ -409,7 +414,7 @@ class Database:
|
|||
fn_name = name or fn.__name__
|
||||
arity = len(inspect.signature(fn).parameters)
|
||||
if not replace and (fn_name, arity) in self._registered_functions:
|
||||
return fn
|
||||
raise FunctionAlreadyRegistered(f'Already registered function with name "{fn_name}" and identical arity')
|
||||
kwargs = {}
|
||||
registered = False
|
||||
if deterministic:
|
||||
|
|
@ -434,7 +439,7 @@ class Database:
|
|||
|
||||
def register_fts4_bm25(self):
|
||||
"Register the ``rank_bm25(match_info)`` function used for calculating relevance with SQLite FTS4."
|
||||
self.register_function(rank_bm25, deterministic=True)
|
||||
self.register_function(rank_bm25, deterministic=True, replace=True)
|
||||
|
||||
def attach(self, alias: str, filepath: Union[str, pathlib.Path]):
|
||||
"""
|
||||
|
|
@ -2687,13 +2692,16 @@ class Table(Queryable):
|
|||
return v
|
||||
return jsonify_if_needed(fn(v))
|
||||
|
||||
self.db.register_function(convert_value)
|
||||
fn_name = fn.__name__
|
||||
if fn_name == '<lambda>':
|
||||
fn_name = f'lambda_{hash(fn)}'
|
||||
self.db.register_function(convert_value, name=fn_name)
|
||||
sql = "update [{table}] set {sets}{where};".format(
|
||||
table=self.name,
|
||||
sets=", ".join(
|
||||
[
|
||||
"[{output_column}] = convert_value([{column}])".format(
|
||||
output_column=output or column, column=column
|
||||
"[{output_column}] = {fn_name}([{column}])".format(
|
||||
output_column=output or column, column=column, fn_name=fn_name
|
||||
)
|
||||
for column in columns
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue