From 344e9573ca1cf7c59482af21a0a517bdae70f7d5 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 1 May 2020 13:38:28 -0700 Subject: [PATCH] Added sqlite-utils views command, closes #105 --- docs/cli.rst | 20 ++++++++++++++++++ sqlite_utils/cli.py | 50 +++++++++++++++++++++++++++++++++++++++++++-- tests/test_cli.py | 10 +++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index be9970f..eca3df3 100644 --- a/docs/cli.rst +++ b/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 diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index f53bcd0..14aa338 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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", diff --git a/tests/test_cli.py b/tests/test_cli.py index 0bcefff..fe6bbb2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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])