mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
sqlite-utils convert db table column -, refs #353
This commit is contained in:
parent
e328db8eba
commit
a3df483c80
3 changed files with 48 additions and 0 deletions
10
docs/cli.rst
10
docs/cli.rst
|
|
@ -1023,6 +1023,16 @@ You can specify Python modules that should be imported and made available to you
|
|||
'"\n".join(textwrap.wrap(value, 100))' \
|
||||
--import=textwrap
|
||||
|
||||
Use a CODE value of `-` to read from standard input:
|
||||
|
||||
$ cat mycode.py | sqlite-utils convert content.db articles headline -
|
||||
|
||||
Where `mycode.py` contains a fragment of Python code that looks like this:
|
||||
|
||||
```python
|
||||
return value.upper()
|
||||
```
|
||||
|
||||
The transformation will be applied to every row in the specified table. You can limit that to just rows that match a ``WHERE`` clause using ``--where``::
|
||||
|
||||
$ sqlite-utils convert content.db articles headline 'value.upper()' \
|
||||
|
|
|
|||
|
|
@ -2038,6 +2038,8 @@ def _generate_convert_help():
|
|||
|
||||
"value" is a variable with the column value to be converted.
|
||||
|
||||
Use "-" for CODE to read Python code from standard input.
|
||||
|
||||
The following common operations are available as recipe functions:
|
||||
"""
|
||||
).strip()
|
||||
|
|
@ -2120,6 +2122,9 @@ def convert(
|
|||
raise click.ClickException("Cannot use --multi with more than one column")
|
||||
if drop and not (output or multi):
|
||||
raise click.ClickException("--drop can only be used with --output or --multi")
|
||||
if code == "-":
|
||||
# Read code from standard input
|
||||
code = sys.stdin.read()
|
||||
# If single line and no 'return', add the return
|
||||
if "\n" not in code and not code.strip().startswith("return "):
|
||||
code = "return {}".format(code)
|
||||
|
|
|
|||
|
|
@ -515,3 +515,36 @@ def test_convert_where_multi(fresh_db_and_path):
|
|||
{"id": 1, "name": "Cleo", "upper": None},
|
||||
{"id": 2, "name": "Bants", "upper": "BANTS"},
|
||||
]
|
||||
|
||||
|
||||
def test_convert_code_standard_input(fresh_db_and_path):
|
||||
db, db_path = fresh_db_and_path
|
||||
db["names"].insert_all([{"id": 1, "name": "Cleo"}], pk="id")
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
[
|
||||
"convert",
|
||||
db_path,
|
||||
"names",
|
||||
"name",
|
||||
"-",
|
||||
],
|
||||
input="value.upper()",
|
||||
)
|
||||
assert 0 == result.exit_code, result.output
|
||||
assert list(db["names"].rows) == [
|
||||
{"id": 1, "name": "CLEO"},
|
||||
]
|
||||
|
||||
|
||||
def test_convert_hyphen_workaround(fresh_db_and_path):
|
||||
db, db_path = fresh_db_and_path
|
||||
db["names"].insert_all([{"id": 1, "name": "Cleo"}], pk="id")
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["convert", db_path, "names", "name", '"-"'],
|
||||
)
|
||||
assert 0 == result.exit_code, result.output
|
||||
assert list(db["names"].rows) == [
|
||||
{"id": 1, "name": "-"},
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue