Ability to introspect and run queries against views (#55)

* db.views_names() method and and db.views property
* Separate View and Table classes, both subclassing new Queryable class
* view.drop() method
* Updated documentation
This commit is contained in:
Simon Willison 2019-08-23 15:19:41 +03:00 committed by GitHub
commit 9faa982226
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 142 additions and 56 deletions

View file

@ -45,6 +45,8 @@ You can also access tables using the ``.table()`` method like so:
Using this factory function allows you to set :ref:`python_api_table_configuration`.
.. _python_api_tables:
Listing tables
==============
@ -60,6 +62,31 @@ You can also iterate through the table objects themselves using the ``.tables``
>>> db.tables
[<Table dogs>]
.. _python_api_views:
Listing views
=============
``.table_views()`` shows you a list of views in the database::
>>> db.table_views()
['good_dogs']
You can iterate through view objects using the ``.views`` property::
>>> db.views
[<View good_dogs>]
View objects are similar to Table objects, except that any attempts to insert or update data will throw an error. The full list of methods and properties available on a view object is as follows:
* ``columns``
* ``columns_dict``
* ``count``
* ``schema``
* ``rows``
* ``rows_where(where, where_args)``
* ``drop()``
.. _python_api_rows:
Listing rows
@ -682,10 +709,10 @@ If you want to ensure that every foreign key column in your database has a corre
.. _python_api_drop:
Dropping a table
================
Dropping a table or view
========================
You can drop a table by using the ``.drop()`` method:
You can drop a table or view using the ``.drop()`` method:
.. code-block:: python
@ -764,7 +791,7 @@ For example:
Introspection
=============
If you have loaded an existing table, you can use introspection to find out more about it::
If you have loaded an existing table or view, you can use introspection to find out more about it::
>>> db["PlantType"]
<Table PlantType (id, value)>
@ -776,7 +803,7 @@ The ``.count`` property shows the current number of rows (``select count(*) from
>>> db["Street_Tree_List"].count
189144
The ``.columns`` property shows the columns in the table::
The ``.columns`` property shows the columns in the table or view::
>>> db["PlantType"].columns
[Column(cid=0, name='id', type='INTEGER', notnull=0, default_value=None, is_pk=1),
@ -787,7 +814,9 @@ The ``.columns_dict`` property returns a dictionary version of this with just th
>>> db["PlantType"].columns_dict
{'id': <class 'int'>, 'value': <class 'str'>}
The ``.foreign_keys`` property shows if the table has any foreign key relationships::
The ``.foreign_keys`` property shows if the table has any foreign key relationships. It is not available on views.
::
>>> db["Street_Tree_List"].foreign_keys
[ForeignKey(table='Street_Tree_List', column='qLegalStatus', other_table='qLegalStatus', other_column='id'),
@ -827,7 +856,9 @@ The ``.schema`` property outputs the table's schema as a SQL string::
FOREIGN KEY ("qCareAssistant") REFERENCES [qCareAssistant](id),
FOREIGN KEY ("qLegalStatus") REFERENCES [qLegalStatus](id))
The ``.indexes`` property shows you all indexes created for a table::
The ``.indexes`` property shows you all indexes created for a table. It is not available on views.
::
>>> db["Street_Tree_List"].indexes
[Index(seq=0, name='"Street_Tree_List_qLegalStatus"', unique=0, origin='c', partial=0, columns=['qLegalStatus']),