From 515d362ad60c3dc16272c4fdca932cf0a0e9dafa Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 31 Jul 2018 17:35:36 -0700 Subject: [PATCH] .table_names and .tables properties plus expanded docs --- docs/table.rst | 26 ++++++++++++++++++++++---- sqlite_utils/db.py | 16 ++++++++++++++-- tests/test_create.py | 6 +++--- tests/test_enable_fts.py | 4 ++-- tests/test_introspect.py | 13 +++++++++++++ 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/docs/table.rst b/docs/table.rst index 0ae25d3..e29ad69 100644 --- a/docs/table.rst +++ b/docs/table.rst @@ -1,8 +1,8 @@ -======= - Table -======= +====================== + Databases and Tables +====================== -Tables are accessed using the indexing operator, like so: +Database objects are constructed by passing in a SQLite3 database connection: .. code-block:: python @@ -10,10 +10,28 @@ Tables are accessed using the indexing operator, like so: import sqlite3 db = Database(sqlite3.connect("my_database.db")) + +Tables are accessed using the indexing operator, like so: + +.. code-block:: python + table = db["my_table"] If the table does not yet exist, it will be created the first time you attempt to insert or upsert data into it. +Listing tables +============== + +You can list the names of tables in a database using the ``.table_names`` property:: + + >>> db.table_names + ['dogs'] + +You can also iterate through the table objects themselves using ``.tables``:: + + >>> db.tables + [] + Creating tables =============== diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index b5fbf26..2d6c30a 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -20,8 +20,11 @@ class Database: def __getitem__(self, table_name): return Table(self, table_name) + def __repr__(self): + return "".format(self.conn) + @property - def tables(self): + def table_names(self): return [ r[0] for r in self.conn.execute( @@ -29,6 +32,10 @@ class Database: ).fetchall() ] + @property + def tables(self): + return [self[name] for name in self.table_names] + def create_table(self, name, columns, pk=None, foreign_keys=None): foreign_keys = foreign_keys or [] foreign_keys_by_name = {fk[0]: fk for fk in foreign_keys} @@ -69,7 +76,12 @@ class Table: def __init__(self, db, name): self.db = db self.name = name - self.exists = self.name in self.db.tables + self.exists = self.name in self.db.table_names + + def __repr__(self): + return "
".format( + self.name, " (does not exist yet)" if not self.exists else "" + ) @property def count(self): diff --git a/tests/test_create.py b/tests/test_create.py index 3fe6f5b..3ae5fef 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -4,12 +4,12 @@ import json def test_create_table(fresh_db): - assert [] == fresh_db.tables + assert [] == fresh_db.table_names table = fresh_db.create_table( "test_table", {"text_col": str, "float_col": float, "int_col": int, "bool_col": bool}, ) - assert ["test_table"] == fresh_db.tables + assert ["test_table"] == fresh_db.table_names assert [ {"name": "text_col", "type": "TEXT"}, {"name": "float_col", "type": "FLOAT"}, @@ -29,7 +29,7 @@ def test_create_table(fresh_db): ) def test_create_table_from_example(fresh_db, example, expected_columns): fresh_db["people"].insert(example) - assert ["people"] == fresh_db.tables + assert ["people"] == fresh_db.table_names assert expected_columns == [ {"name": col.name, "type": col.type} for col in fresh_db["people"].columns ] diff --git a/tests/test_enable_fts.py b/tests/test_enable_fts.py index 8a95042..8b823a2 100644 --- a/tests/test_enable_fts.py +++ b/tests/test_enable_fts.py @@ -10,7 +10,7 @@ search_records = [ def test_enable_fts(fresh_db): table = fresh_db["searchable"] table.insert_all(search_records) - assert ["searchable"] == fresh_db.tables + assert ["searchable"] == fresh_db.table_names table.enable_fts(["text", "country"]) assert [ "searchable", @@ -19,7 +19,7 @@ def test_enable_fts(fresh_db): "searchable_fts_segdir", "searchable_fts_docsize", "searchable_fts_stat", - ] == fresh_db.tables + ] == fresh_db.table_names assert [("tanuki are tricksters", "Japan", "foo")] == table.search("tanuki") assert [("racoons are trash pandas", "USA", "bar")] == table.search("usa") assert [] == table.search("bar") diff --git a/tests/test_introspect.py b/tests/test_introspect.py index de2d7df..fc1f226 100644 --- a/tests/test_introspect.py +++ b/tests/test_introspect.py @@ -1,6 +1,15 @@ from .fixtures import existing_db +def test_table_names(existing_db): + assert ["foo"] == existing_db.table_names + + +def test_tables(existing_db): + assert 1 == len(existing_db.tables) + assert "foo" == existing_db.tables[0].name + + def test_count(existing_db): assert 3 == existing_db["foo"].count @@ -14,3 +23,7 @@ def test_columns(existing_db): def test_schema(existing_db): assert "CREATE TABLE foo (text TEXT)" == existing_db["foo"].schema + + +def test_table_repr(existing_db): + assert "
" == repr(existing_db["foo"])