diff --git a/datasette/app.py b/datasette/app.py index e36e2e62..0b73d0e1 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -24,7 +24,7 @@ from .utils import ( Filters, CustomJSONEncoder, compound_keys_after_sql, - detect_fts_sql, + detect_fts, detect_spatialite, escape_css_string, escape_sqlite, @@ -726,12 +726,7 @@ class TableView(RowTableShared): where_clauses, params = filters.build_where_clauses() # _search support: - fts_table = None - fts_sql = detect_fts_sql(table) - fts_rows = list(await self.execute(name, fts_sql)) - if fts_rows: - fts_table = fts_rows[0][0] - + fts_table = info[name]['tables'].get(table, {}).get('fts_table') search = special_args.get('_search') search_description = None if search and fts_table: @@ -1252,6 +1247,9 @@ class Datasette: # This can happen when running against a FTS virtual tables # e.g. "select count(*) from some_fts;" count = 0 + # Does this table have a FTS table? + fts_table = detect_fts(conn, table) + # Figure out primary keys table_info_rows = [ row for row in conn.execute( @@ -1276,6 +1274,7 @@ class Datasette: 'count': count, 'label_column': label_column, 'hidden': table_metadata.get('hidden') or False, + 'fts_table': fts_table, } foreign_keys = get_all_foreign_keys(conn) diff --git a/datasette/utils.py b/datasette/utils.py index 863079bf..9cf4724e 100644 --- a/datasette/utils.py +++ b/datasette/utils.py @@ -427,7 +427,7 @@ def detect_spatialite(conn): return len(rows) > 0 -def detect_fts(conn, table, return_sql=False): +def detect_fts(conn, table): "Detect if table has a corresponding FTS virtual table and return it" rows = conn.execute(detect_fts_sql(table)).fetchall() if len(rows) == 0: diff --git a/tests/test_api.py b/tests/test_api.py index 5bd8c15b..40c23d00 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -33,6 +33,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': [], }, { 'columns': ['pk', 'content'], @@ -41,6 +42,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk'], }, { 'columns': ['pk', 'f1', 'f2', 'f3'], @@ -64,6 +66,7 @@ def test_database_page(app_client): }, 'hidden': False, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk'], }, { 'columns': ['pk1', 'pk2', 'content'], @@ -72,6 +75,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk1', 'pk2'], }, { 'columns': ['pk1', 'pk2', 'pk3', 'content'], @@ -80,6 +84,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk1', 'pk2', 'pk3'], }, { 'columns': ['pk', 'foreign_key_with_custom_label'], @@ -95,6 +100,7 @@ def test_database_page(app_client): }], }, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk'], }, { 'columns': ['pk', 'foreign_key_with_label', 'foreign_key_with_no_label'], @@ -114,6 +120,7 @@ def test_database_page(app_client): }], }, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk'], }, { 'columns': ['id', 'content', 'content2'], @@ -129,6 +136,7 @@ def test_database_page(app_client): }, 'hidden': False, 'label_column': None, + 'fts_table': None, 'primary_keys': ['id'] }, { 'columns': ['id', 'content', 'content2'], @@ -144,6 +152,7 @@ def test_database_page(app_client): }, 'hidden': False, 'label_column': None, + 'fts_table': None, 'primary_keys': ['id'] }, { 'columns': ['group', 'having', 'and'], @@ -152,6 +161,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': [], }, { 'columns': ['id', 'content'], @@ -179,6 +189,7 @@ def test_database_page(app_client): 'outgoing': [], }, 'label_column': 'content', + 'fts_table': None, 'primary_keys': ['id'], }, { 'columns': [ @@ -190,6 +201,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk1', 'pk2'], }, { 'columns': ['pk', 'content'], @@ -198,6 +210,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk'], }, { 'columns': ['pk', 'distance', 'frequency'], @@ -206,6 +219,7 @@ def test_database_page(app_client): 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': ['pk'], }, { 'columns': ['content', 'a', 'b', 'c'], @@ -214,6 +228,7 @@ def test_database_page(app_client): 'hidden': True, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, + 'fts_table': None, 'primary_keys': [], }] == data['tables']