sqlite-utils icon indicating copy to clipboard operation
sqlite-utils copied to clipboard

Idea: conversions= could take Python functions

Open simonw opened this issue 5 years ago • 4 comments

Right now you use conversions= like this:

db["example"].insert({
    "name": "The Bigfoot Discovery Museum"
}, conversions={"name": "upper(?)"})

How about if you could optionally provide a Python function (or a lambda) like this?

db["example"].insert({
    "name": "The Bigfoot Discovery Museum"
}, conversions={"name": lambda s: s.upper()})

This would work by creating a random name for that function, registering it (similar to #162), executing the SQL and then un-registering the custom function at the end.

simonw avatar Sep 22 '20 00:09 simonw

This would apply to .transform() in #114 too.

simonw avatar Sep 22 '20 01:09 simonw

This could even be exposed as a feature of the CLI tool - similar to how sqlite-transform works: https://github.com/simonw/sqlite-transform

$ sqlite-transform lambda my.db mytable mycolumn --code='str(value).upper()'

Maybe sqlite-utils should absorb all of the functionality from sqlite-transform - having two separate tools doesn't necessarily make sense.

simonw avatar Oct 14 '20 22:10 simonw

sqlite-utils convert my.db mytable mycolumn could become the new sqlite-transform. The sub-command could take --code but could also take --parsedate and --parsedatetime options.

simonw avatar Oct 14 '20 22:10 simonw

Maybe sqlite-utils should absorb all of the functionality from sqlite-transform - having two separate tools doesn't necessarily make sense.

I implemented that in:

  • #251

simonw avatar Dec 20 '21 00:12 simonw