mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
expand_foreign_keys() no longer uses inspect, refs #420
This commit is contained in:
parent
97331f3435
commit
53bf875483
7 changed files with 52 additions and 63 deletions
|
|
@ -29,6 +29,7 @@ from .utils import (
|
|||
Results,
|
||||
escape_css_string,
|
||||
escape_sqlite,
|
||||
get_outbound_foreign_keys,
|
||||
get_plugins,
|
||||
module_from_path,
|
||||
sqlite3,
|
||||
|
|
@ -464,11 +465,39 @@ class Datasette:
|
|||
for p in ps
|
||||
]
|
||||
|
||||
def table_metadata(self, database, table):
|
||||
"Fetch table-specific metadata."
|
||||
return (self.metadata("databases") or {}).get(database, {}).get(
|
||||
"tables", {}
|
||||
).get(
|
||||
table, {}
|
||||
)
|
||||
|
||||
async def table_columns(self, db_name, table):
|
||||
return await self.execute_against_connection_in_thread(
|
||||
db_name, lambda conn: table_columns(conn, table)
|
||||
)
|
||||
|
||||
async def foreign_keys_for_table(self, database, table):
|
||||
return await self.execute_against_connection_in_thread(
|
||||
database, lambda conn: get_outbound_foreign_keys(conn, table)
|
||||
)
|
||||
|
||||
async def label_column_for_table(self, db_name, table):
|
||||
explicit_label_column = (
|
||||
self.table_metadata(
|
||||
db_name, table
|
||||
).get("label_column")
|
||||
)
|
||||
if explicit_label_column:
|
||||
return explicit_label_column
|
||||
# If a table has two columns, one of which is ID, then label_column is the other one
|
||||
column_names = await self.table_columns(db_name, table)
|
||||
if (column_names and len(column_names) == 2 and "id" in column_names):
|
||||
return [c for c in column_names if c != "id"][0]
|
||||
# Couldn't find a label:
|
||||
return None
|
||||
|
||||
async def execute_against_connection_in_thread(self, db_name, fn):
|
||||
def in_thread():
|
||||
conn = getattr(connections, db_name, None)
|
||||
|
|
|
|||
|
|
@ -31,17 +31,6 @@ def inspect_views(conn):
|
|||
return [v[0] for v in conn.execute('select name from sqlite_master where type = "view"')]
|
||||
|
||||
|
||||
def detect_label_column(column_names):
|
||||
""" Detect the label column - which we display as the label for a joined column.
|
||||
|
||||
If a table has two columns, one of which is ID, then label_column is the other one.
|
||||
"""
|
||||
if (column_names and len(column_names) == 2 and "id" in column_names):
|
||||
return [c for c in column_names if c != "id"][0]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def detect_primary_keys(conn, table):
|
||||
" Figure out primary keys for a table. "
|
||||
table_info_rows = [
|
||||
|
|
@ -86,7 +75,6 @@ def inspect_tables(conn, database_metadata):
|
|||
"columns": column_names,
|
||||
"primary_keys": detect_primary_keys(conn, table),
|
||||
"count": count,
|
||||
"label_column": detect_label_column(column_names),
|
||||
"hidden": table_metadata.get("hidden") or False,
|
||||
"fts_table": detect_fts(conn, table),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -475,6 +475,22 @@ def temporary_heroku_directory(
|
|||
os.chdir(saved_cwd)
|
||||
|
||||
|
||||
def get_outbound_foreign_keys(conn, table):
|
||||
infos = conn.execute(
|
||||
'PRAGMA foreign_key_list([{}])'.format(table)
|
||||
).fetchall()
|
||||
fks = []
|
||||
for info in infos:
|
||||
if info is not None:
|
||||
id, seq, table_name, from_, to_, on_update, on_delete, match = info
|
||||
fks.append({
|
||||
'other_table': table_name,
|
||||
'column': from_,
|
||||
'other_column': to_
|
||||
})
|
||||
return fks
|
||||
|
||||
|
||||
def get_all_foreign_keys(conn):
|
||||
tables = [r[0] for r in conn.execute('select name from sqlite_master where type="table"')]
|
||||
table_to_foreign_keys = {}
|
||||
|
|
|
|||
|
|
@ -131,14 +131,6 @@ class BaseView(RenderMixin):
|
|||
def __init__(self, datasette):
|
||||
self.ds = datasette
|
||||
|
||||
def table_metadata(self, database, table):
|
||||
"Fetch table-specific metadata."
|
||||
return (self.ds.metadata("databases") or {}).get(database, {}).get(
|
||||
"tables", {}
|
||||
).get(
|
||||
table, {}
|
||||
)
|
||||
|
||||
def options(self, request, *args, **kwargs):
|
||||
r = response.text("ok")
|
||||
if self.ds.cors:
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ LINK_WITH_VALUE = '<a href="/{database}/{table}/{link_id}">{id}</a>'
|
|||
class RowTableShared(BaseView):
|
||||
|
||||
async def sortable_columns_for_table(self, database, table, use_rowid):
|
||||
table_metadata = self.table_metadata(database, table)
|
||||
table_metadata = self.ds.table_metadata(database, table)
|
||||
if "sortable_columns" in table_metadata:
|
||||
sortable_columns = set(table_metadata["sortable_columns"])
|
||||
else:
|
||||
|
|
@ -51,7 +51,7 @@ class RowTableShared(BaseView):
|
|||
expandables = []
|
||||
for fk in table_info["foreign_keys"]["outgoing"]:
|
||||
label_column = (
|
||||
self.table_metadata(
|
||||
self.ds.table_metadata(
|
||||
database, fk["other_table"]
|
||||
).get("label_column")
|
||||
or tables.get(fk["other_table"], {}).get("label_column")
|
||||
|
|
@ -62,11 +62,7 @@ class RowTableShared(BaseView):
|
|||
async def expand_foreign_keys(self, database, table, column, values):
|
||||
"Returns dict mapping (column, value) -> label"
|
||||
labeled_fks = {}
|
||||
tables_info = self.ds.inspect()[database]["tables"]
|
||||
table_info = tables_info.get(table) or {}
|
||||
if not table_info:
|
||||
return {}
|
||||
foreign_keys = table_info["foreign_keys"]["outgoing"]
|
||||
foreign_keys = await self.ds.foreign_keys_for_table(database, table)
|
||||
# Find the foreign_key for this column
|
||||
try:
|
||||
fk = [
|
||||
|
|
@ -75,13 +71,7 @@ class RowTableShared(BaseView):
|
|||
][0]
|
||||
except IndexError:
|
||||
return {}
|
||||
label_column = (
|
||||
# First look in metadata.json for this foreign key table:
|
||||
self.table_metadata(
|
||||
database, fk["other_table"]
|
||||
).get("label_column")
|
||||
or tables_info.get(fk["other_table"], {}).get("label_column")
|
||||
)
|
||||
label_column = await self.ds.label_column_for_table(database, fk["other_table"])
|
||||
if not label_column:
|
||||
return {
|
||||
(fk["column"], value): str(value)
|
||||
|
|
@ -119,7 +109,7 @@ class RowTableShared(BaseView):
|
|||
truncate_cells=0,
|
||||
):
|
||||
"Returns columns, rows for specified table - including fancy foreign key treatment"
|
||||
table_metadata = self.table_metadata(database, table)
|
||||
table_metadata = self.ds.table_metadata(database, table)
|
||||
info = self.ds.inspect()[database]
|
||||
sortable_columns = await self.sortable_columns_for_table(database, table, True)
|
||||
columns = [
|
||||
|
|
@ -309,7 +299,7 @@ class TableView(RowTableShared):
|
|||
forward_querystring=False,
|
||||
)
|
||||
|
||||
table_metadata = self.table_metadata(database, table)
|
||||
table_metadata = self.ds.table_metadata(database, table)
|
||||
units = table_metadata.get("units", {})
|
||||
filters = Filters(sorted(other_args.items()), units, ureg)
|
||||
where_clauses, params = filters.build_where_clauses()
|
||||
|
|
@ -880,7 +870,7 @@ class RowView(RowTableShared):
|
|||
"columns": columns,
|
||||
"primary_keys": pks,
|
||||
"primary_key_values": pk_values,
|
||||
"units": self.table_metadata(database, table).get("units", {}),
|
||||
"units": self.ds.table_metadata(database, table).get("units", {}),
|
||||
}
|
||||
|
||||
if "foreign_key_tables" in (request.raw_args.get("_extras") or "").split(","):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue