From 20f98c3e20902c62db610e43155c476508ead4a4 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 27 May 2019 11:27:42 -0700 Subject: [PATCH] Databse.primary_keys(table) / fts_table(table) refactor, closes #488 Also cleaned up some unused imports spotted by the linter. --- datasette/database.py | 12 ++++++++++++ datasette/views/database.py | 15 +++------------ datasette/views/index.py | 17 ++++------------- datasette/views/table.py | 23 +++++------------------ 4 files changed, 24 insertions(+), 43 deletions(-) diff --git a/datasette/database.py b/datasette/database.py index b25765bc..45006cbe 100644 --- a/datasette/database.py +++ b/datasette/database.py @@ -2,6 +2,8 @@ from pathlib import Path from .utils import ( InterruptedError, + detect_fts, + detect_primary_keys, detect_spatialite, get_all_foreign_keys, get_outbound_foreign_keys, @@ -93,6 +95,16 @@ class Database: self.name, lambda conn: table_columns(conn, table) ) + async def primary_keys(self, table): + return await self.ds.execute_against_connection_in_thread( + self.name, lambda conn: detect_primary_keys(conn, table) + ) + + async def fts_table(self, table): + return await self.ds.execute_against_connection_in_thread( + self.name, lambda conn: detect_fts(conn, table) + ) + async def label_column_for_table(self, table): explicit_label_column = self.ds.table_metadata(self.name, table).get( "label_column" diff --git a/datasette/views/database.py b/datasette/views/database.py index 7172b346..859a271f 100644 --- a/datasette/views/database.py +++ b/datasette/views/database.py @@ -2,12 +2,7 @@ import os from sanic import response -from datasette.utils import ( - detect_fts, - detect_primary_keys, - to_css_class, - validate_sql_select, -) +from datasette.utils import to_css_class, validate_sql_select from .base import BaseView, DatasetteError @@ -40,14 +35,10 @@ class DatabaseView(BaseView): { "name": table, "columns": table_columns, - "primary_keys": await self.ds.execute_against_connection_in_thread( - database, lambda conn: detect_primary_keys(conn, table) - ), + "primary_keys": await db.primary_keys(table), "count": table_counts[table], "hidden": table in hidden_table_names, - "fts_table": await self.ds.execute_against_connection_in_thread( - database, lambda conn: detect_fts(conn, table) - ), + "fts_table": await db.fts_table(table), "foreign_keys": all_foreign_keys[table], } ) diff --git a/datasette/views/index.py b/datasette/views/index.py index 276ea1cc..30c77b41 100644 --- a/datasette/views/index.py +++ b/datasette/views/index.py @@ -3,15 +3,10 @@ import json from sanic import response -from datasette.utils import ( - CustomJSONEncoder, - InterruptedError, - detect_primary_keys, - detect_fts, -) +from datasette.utils import CustomJSONEncoder from datasette.version import __version__ -from .base import HASH_LENGTH, RenderMixin +from .base import RenderMixin # Truncate table list on homepage at: @@ -46,14 +41,10 @@ class IndexView(RenderMixin): tables[table] = { "name": table, "columns": table_columns, - "primary_keys": await self.ds.execute_against_connection_in_thread( - name, lambda conn: detect_primary_keys(conn, table) - ), + "primary_keys": await db.primary_keys(table), "count": table_counts.get(table), "hidden": table in hidden_table_names, - "fts_table": await self.ds.execute_against_connection_in_thread( - name, lambda conn: detect_fts(conn, table) - ), + "fts_table": await db.fts_table(table), "num_relationships_for_sorting": 0, } diff --git a/datasette/views/table.py b/datasette/views/table.py index 23777ff8..2d704670 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -6,15 +6,12 @@ import jinja2 from sanic.exceptions import NotFound from sanic.request import RequestParameters -from datasette.facets import load_facet_configs from datasette.plugins import pm from datasette.utils import ( CustomRow, InterruptedError, append_querystring, compound_keys_after_sql, - detect_fts, - detect_primary_keys, escape_sqlite, filters_should_redirect, get_all_foreign_keys, @@ -25,7 +22,6 @@ from datasette.utils import ( path_with_removed_args, path_with_replaced_args, sqlite3, - table_columns, to_css_class, urlsafe_components, value_as_boolean, @@ -70,9 +66,7 @@ class RowTableShared(BaseView): columns = [ {"name": r[0], "sortable": r[0] in sortable_columns} for r in description ] - pks = await self.ds.execute_against_connection_in_thread( - database, lambda conn: detect_primary_keys(conn, table) - ) + pks = await db.primary_keys(table) column_to_foreign_key_table = { fk["column"]: fk["other_table"] for fk in await db.foreign_keys_for_table(table) @@ -213,9 +207,7 @@ class TableView(RowTableShared): if not is_view and not table_exists: raise NotFound("Table not found: {}".format(table)) - pks = await self.ds.execute_against_connection_in_thread( - database, lambda conn: detect_primary_keys(conn, table) - ) + pks = await db.primary_keys(table) use_rowid = not pks and not is_view if use_rowid: select = "rowid, *" @@ -331,9 +323,7 @@ class TableView(RowTableShared): # _search support: fts_table = special_args.get("_fts_table") fts_table = fts_table or table_metadata.get("fts_table") - fts_table = fts_table or await self.ds.execute_against_connection_in_thread( - database, lambda conn: detect_fts(conn, table) - ) + fts_table = fts_table or await db.fts_table(table) fts_pk = special_args.get("_fts_pk", table_metadata.get("fts_pk", "rowid")) search_args = dict( pair for pair in special_args.items() if pair[0].startswith("_search") @@ -668,8 +658,6 @@ class TableView(RowTableShared): and not _next ): for facet in facet_instances: - # TODO: ensure facet is not suggested if it is already active - # used to use 'if facet_column in facets' for this suggested_facets.extend(await facet.suggest()) # human_description_en combines filters AND search, if provided @@ -776,9 +764,8 @@ class RowView(RowTableShared): async def data(self, request, database, hash, table, pk_path, default_labels=False): pk_values = urlsafe_components(pk_path) - pks = await self.ds.execute_against_connection_in_thread( - database, lambda conn: detect_primary_keys(conn, table) - ) + db = self.ds.databases[database] + pks = await db.primary_keys(table) use_rowid = not pks select = "*" if use_rowid: