mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
Added sqlite-utils views command, closes #105
This commit is contained in:
parent
147b52622d
commit
344e9573ca
3 changed files with 78 additions and 2 deletions
20
docs/cli.rst
20
docs/cli.rst
|
|
@ -202,6 +202,26 @@ Use ``--schema`` to include the schema of each table::
|
|||
|
||||
The ``--nl``, ``--csv`` and ``--table`` options are all available.
|
||||
|
||||
Listing views
|
||||
=============
|
||||
|
||||
The `views` command shows any views defined in the database::
|
||||
|
||||
$ sqlite-utils views sf-trees.db --table --counts --columns --schema
|
||||
view count columns schema
|
||||
--------- ------- -------------------- --------------------------------------------------------------
|
||||
demo_view 189144 ['qSpecies'] CREATE VIEW demo_view AS select qSpecies from Street_Tree_List
|
||||
hello 1 ['sqlite_version()'] CREATE VIEW hello as select sqlite_version()
|
||||
|
||||
It takes the same options as the ``tables`` command:
|
||||
|
||||
* ``--columns``
|
||||
* ``--schema``
|
||||
* ``--counts``
|
||||
* ``--nl``
|
||||
* ``--csv``
|
||||
* ``--table``
|
||||
|
||||
.. _cli_inserting_data:
|
||||
|
||||
Inserting JSON data
|
||||
|
|
|
|||
|
|
@ -94,10 +94,11 @@ def tables(
|
|||
json_cols,
|
||||
columns,
|
||||
schema,
|
||||
views=False,
|
||||
):
|
||||
"""List the tables in the database"""
|
||||
db = sqlite_utils.Database(path)
|
||||
headers = ["table"]
|
||||
headers = ["view" if views else "table"]
|
||||
if counts:
|
||||
headers.append("count")
|
||||
if columns:
|
||||
|
|
@ -106,7 +107,11 @@ def tables(
|
|||
headers.append("schema")
|
||||
|
||||
def _iter():
|
||||
for name in db.table_names(fts4=fts4, fts5=fts5):
|
||||
if views:
|
||||
items = db.view_names()
|
||||
else:
|
||||
items = db.table_names(fts4=fts4, fts5=fts5)
|
||||
for name in items:
|
||||
row = [name]
|
||||
if counts:
|
||||
row.append(db[name].count)
|
||||
|
|
@ -133,6 +138,47 @@ def tables(
|
|||
click.echo(line)
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument(
|
||||
"path",
|
||||
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
|
||||
required=True,
|
||||
)
|
||||
@click.option(
|
||||
"--counts", help="Include row counts per view", default=False, is_flag=True
|
||||
)
|
||||
@output_options
|
||||
@click.option(
|
||||
"--columns",
|
||||
help="Include list of columns for each view",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--schema", help="Include schema for each view", is_flag=True, default=False,
|
||||
)
|
||||
def views(
|
||||
path, counts, nl, arrays, csv, no_headers, table, fmt, json_cols, columns, schema,
|
||||
):
|
||||
"""List the views in the database"""
|
||||
tables.callback(
|
||||
path=path,
|
||||
fts4=False,
|
||||
fts5=False,
|
||||
counts=counts,
|
||||
nl=nl,
|
||||
arrays=arrays,
|
||||
csv=csv,
|
||||
no_headers=no_headers,
|
||||
table=table,
|
||||
fmt=fmt,
|
||||
json_cols=json_cols,
|
||||
columns=columns,
|
||||
schema=schema,
|
||||
views=True,
|
||||
)
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument(
|
||||
"path",
|
||||
|
|
|
|||
|
|
@ -28,6 +28,16 @@ def test_tables(db_path):
|
|||
assert '[{"table": "Gosh"},\n {"table": "Gosh2"}]' == result.output.strip()
|
||||
|
||||
|
||||
def test_views(db_path):
|
||||
Database(db_path).create_view("hello", "select sqlite_version()")
|
||||
result = CliRunner().invoke(cli.cli, ["views", db_path, "--table", "--schema"])
|
||||
assert (
|
||||
"view schema\n"
|
||||
"------ --------------------------------------------\n"
|
||||
"hello CREATE VIEW hello AS select sqlite_version()"
|
||||
) == result.output.strip()
|
||||
|
||||
|
||||
def test_tables_fts4(db_path):
|
||||
Database(db_path)["Gosh"].enable_fts(["c2"], fts_version="FTS4")
|
||||
result = CliRunner().invoke(cli.cli, ["tables", "--fts4", db_path])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue