mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
display_columns_and_rows() no longer uses inspect, refs #420
This commit is contained in:
parent
53bf875483
commit
c882e9262f
3 changed files with 20 additions and 21 deletions
|
|
@ -3,6 +3,7 @@ import hashlib
|
||||||
from .utils import (
|
from .utils import (
|
||||||
detect_spatialite,
|
detect_spatialite,
|
||||||
detect_fts,
|
detect_fts,
|
||||||
|
detect_primary_keys,
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
get_all_foreign_keys,
|
get_all_foreign_keys,
|
||||||
table_columns,
|
table_columns,
|
||||||
|
|
@ -31,19 +32,6 @@ def inspect_views(conn):
|
||||||
return [v[0] for v in conn.execute('select name from sqlite_master where type = "view"')]
|
return [v[0] for v in conn.execute('select name from sqlite_master where type = "view"')]
|
||||||
|
|
||||||
|
|
||||||
def detect_primary_keys(conn, table):
|
|
||||||
" Figure out primary keys for a table. "
|
|
||||||
table_info_rows = [
|
|
||||||
row
|
|
||||||
for row in conn.execute(
|
|
||||||
'PRAGMA table_info("{}")'.format(table)
|
|
||||||
).fetchall()
|
|
||||||
if row[-1]
|
|
||||||
]
|
|
||||||
table_info_rows.sort(key=lambda row: row[-1])
|
|
||||||
return [str(r[1]) for r in table_info_rows]
|
|
||||||
|
|
||||||
|
|
||||||
def inspect_tables(conn, database_metadata):
|
def inspect_tables(conn, database_metadata):
|
||||||
" List tables and their row counts, excluding uninteresting tables. "
|
" List tables and their row counts, excluding uninteresting tables. "
|
||||||
tables = {}
|
tables = {}
|
||||||
|
|
|
||||||
|
|
@ -475,6 +475,19 @@ def temporary_heroku_directory(
|
||||||
os.chdir(saved_cwd)
|
os.chdir(saved_cwd)
|
||||||
|
|
||||||
|
|
||||||
|
def detect_primary_keys(conn, table):
|
||||||
|
" Figure out primary keys for a table. "
|
||||||
|
table_info_rows = [
|
||||||
|
row
|
||||||
|
for row in conn.execute(
|
||||||
|
'PRAGMA table_info("{}")'.format(table)
|
||||||
|
).fetchall()
|
||||||
|
if row[-1]
|
||||||
|
]
|
||||||
|
table_info_rows.sort(key=lambda row: row[-1])
|
||||||
|
return [str(r[1]) for r in table_info_rows]
|
||||||
|
|
||||||
|
|
||||||
def get_outbound_foreign_keys(conn, table):
|
def get_outbound_foreign_keys(conn, table):
|
||||||
infos = conn.execute(
|
infos = conn.execute(
|
||||||
'PRAGMA foreign_key_list([{}])'.format(table)
|
'PRAGMA foreign_key_list([{}])'.format(table)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ from datasette.utils import (
|
||||||
InterruptedError,
|
InterruptedError,
|
||||||
append_querystring,
|
append_querystring,
|
||||||
compound_keys_after_sql,
|
compound_keys_after_sql,
|
||||||
|
detect_primary_keys,
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
filters_should_redirect,
|
filters_should_redirect,
|
||||||
is_url,
|
is_url,
|
||||||
|
|
@ -110,19 +111,16 @@ class RowTableShared(BaseView):
|
||||||
):
|
):
|
||||||
"Returns columns, rows for specified table - including fancy foreign key treatment"
|
"Returns columns, rows for specified table - including fancy foreign key treatment"
|
||||||
table_metadata = self.ds.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)
|
sortable_columns = await self.sortable_columns_for_table(database, table, True)
|
||||||
columns = [
|
columns = [
|
||||||
{"name": r[0], "sortable": r[0] in sortable_columns} for r in description
|
{"name": r[0], "sortable": r[0] in sortable_columns} for r in description
|
||||||
]
|
]
|
||||||
tables = info["tables"]
|
pks = await self.ds.execute_against_connection_in_thread(
|
||||||
table_info = tables.get(table) or {}
|
database, lambda conn: detect_primary_keys(conn, table)
|
||||||
pks = table_info.get("primary_keys") or []
|
)
|
||||||
column_to_foreign_key_table = {
|
column_to_foreign_key_table = {
|
||||||
fk["column"]: fk["other_table"]
|
fk["column"]: fk["other_table"]
|
||||||
for fk in table_info.get(
|
for fk in await self.ds.foreign_keys_for_table(database, table)
|
||||||
"foreign_keys", {}
|
|
||||||
).get("outgoing", None) or []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cell_rows = []
|
cell_rows = []
|
||||||
|
|
@ -600,7 +598,7 @@ class TableView(RowTableShared):
|
||||||
|
|
||||||
if columns_to_expand:
|
if columns_to_expand:
|
||||||
expanded_labels = {}
|
expanded_labels = {}
|
||||||
for fk, label_column in expandable_columns:
|
for fk, _ in expandable_columns:
|
||||||
column = fk["column"]
|
column = fk["column"]
|
||||||
if column not in columns_to_expand:
|
if column not in columns_to_expand:
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue