From d08a13314081ae2ce0313a17d3c07c1a7f2d94d5 Mon Sep 17 00:00:00 2001 From: Russ Garrett Date: Wed, 11 Apr 2018 22:20:25 +0100 Subject: [PATCH] Hide Spatialite system tables They were getting on my nerves. --- datasette/app.py | 16 +++++++++++++--- datasette/utils.py | 5 +++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 4fc9dd9d..50794954 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -21,6 +21,7 @@ from .utils import ( CustomJSONEncoder, compound_keys_after_sql, detect_fts_sql, + detect_spatialite, escape_css_string, escape_sqlite, filters_should_redirect, @@ -1121,7 +1122,7 @@ class Datasette: tables[table]['foreign_keys'] = info # Mark tables 'hidden' if they relate to FTS virtual tables - fts_tables = [ + hidden_tables = [ r['name'] for r in conn.execute( ''' @@ -1131,9 +1132,18 @@ class Datasette: ''' ) ] + + if detect_spatialite(conn): + # Also hide Spatialite internal tables + hidden_tables += [ + 'ElementaryGeometries', 'SpatialIndex', 'geometry_columns', + 'spatial_ref_sys', 'spatialite_history', 'sql_statements_log', + 'sqlite_sequence', 'views_geometry_columns', 'virts_geometry_columns' + ] + for t in tables.keys(): - for fts_table in fts_tables: - if t == fts_table or t.startswith(fts_table): + for hidden_table in hidden_tables: + if t == hidden_table or t.startswith(hidden_table): tables[t]['hidden'] = True continue diff --git a/datasette/utils.py b/datasette/utils.py index b0861428..cd1f08cf 100644 --- a/datasette/utils.py +++ b/datasette/utils.py @@ -372,6 +372,11 @@ def get_all_foreign_keys(conn): return table_to_foreign_keys +def detect_spatialite(conn): + rows = conn.execute('select 1 from sqlite_master where tbl_name = "geometry_columns"').fetchall() + return len(rows) > 0 + + def detect_fts(conn, table, return_sql=False): "Detect if table has a corresponding FTS virtual table and return it" rows = conn.execute(detect_fts_sql(table)).fetchall()