sqlite-utils memory handles files with same filename, closes #325

This commit is contained in:
Simon Willison 2021-09-22 13:45:37 -07:00
commit c1b26eed03
3 changed files with 35 additions and 1 deletions

View file

@ -281,6 +281,10 @@ The in-memory tables will be named after the files without their extensions. The
$ sqlite-utils memory example.csv "select * from t"
If two files have the same name they will be assigned a numeric suffix::
$ sqlite-utils memory foo/data.csv bar/data.csv "select * from data_2"
To read from standard input, use either ``-`` or ``stdin`` as the filename - then use ``stdin`` or ``t`` or ``t1`` as the table name::
$ cat example.csv | sqlite-utils memory - "select * from stdin"

View file

@ -1275,6 +1275,7 @@ def memory(
if (dump or save or schema or analyze) and not paths:
paths = [sql]
sql = None
stem_counts = {}
for i, path in enumerate(paths):
# Path may have a :format suffix
if ":" in path and path.rsplit(":", 1)[-1].upper() in Format.__members__:
@ -1287,7 +1288,12 @@ def memory(
csv_table = "stdin"
else:
csv_path = pathlib.Path(path)
csv_table = csv_path.stem
stem = csv_path.stem
if stem_counts.get(stem):
csv_table = "{}_{}".format(stem, stem_counts[stem])
else:
csv_table = stem
stem_counts[stem] = stem_counts.get(stem, 1) + 1
csv_fp = csv_path.open("rb")
rows, format_used = rows_from_file(csv_fp, format=format, encoding=encoding)
tracker = None

View file

@ -241,3 +241,27 @@ def test_memory_analyze():
" Blank rows: 0\n\n"
" Distinct values: 2\n\n"
)
def test_memory_two_files_with_same_stem(tmpdir):
(tmpdir / "one").mkdir()
(tmpdir / "two").mkdir()
one = tmpdir / "one" / "data.csv"
two = tmpdir / "two" / "data.csv"
one.write_text("id,name\n1,Cleo\n2,Bants", encoding="utf-8")
two.write_text("id,name\n3,Blue\n4,Lila", encoding="utf-8")
result = CliRunner().invoke(cli.cli, ["memory", str(one), str(two), "", "--schema"])
assert result.exit_code == 0
assert result.output == (
'CREATE TABLE "data" (\n'
" [id] INTEGER,\n"
" [name] TEXT\n"
");\n"
"CREATE VIEW t1 AS select * from [data];\n"
"CREATE VIEW t AS select * from [data];\n"
'CREATE TABLE "data_2" (\n'
" [id] INTEGER,\n"
" [name] TEXT\n"
");\n"
"CREATE VIEW t2 AS select * from [data_2];\n"
)