Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A decorator for registering custom SQL functions #162

Closed
simonw opened this issue Sep 22, 2020 · 2 comments
Closed

A decorator for registering custom SQL functions #162

simonw opened this issue Sep 22, 2020 · 2 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Sep 22, 2020

Syntactic sugar for db.conn.create_function - it would work something like this:

db = sqlite_utils.Database("mydb.db")

@db.register_function
def scramble(text):
    chars = list(text)
    random.shuffle(chars)
    return "".join(chars)

The decorator would inspect the function to find its name and arity (number of arguments). Having run the above you could then do:

db.execute("select scramble('hello')").fetchall()
@simonw
Copy link
Owner Author

simonw commented Sep 22, 2020

Inspired by the idea of adding conversions= to #114 - since this would make it easy to register custom Python functions that can be used to convert the values in a table.

@simonw simonw changed the title Idea: a decorator for registering custom SQL functions A decorator for registering custom SQL functions Sep 22, 2020
@simonw simonw closed this as completed in 4824775 Sep 22, 2020
@simonw
Copy link
Owner Author

simonw commented Sep 22, 2020

simonw added a commit that referenced this issue Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant