Database.get_outbound_foreign_keys() refactor

Following this, the only module that ever makes calls to the low-level
execute_against_connection_in_thread() method is datasette/database.py
pull/497/head
Simon Willison 2019-05-27 11:41:44 -07:00
rodzic 20f98c3e20
commit edb36629e7
3 zmienionych plików z 13 dodań i 16 usunięć

Wyświetl plik

@ -205,6 +205,11 @@ class Database:
self.name, get_all_foreign_keys self.name, get_all_foreign_keys
) )
async def get_outbound_foreign_keys(self, table):
return await self.ds.execute_against_connection_in_thread(
self.name, lambda conn: get_outbound_foreign_keys(conn, table)
)
async def get_table_definition(self, table, type_="table"): async def get_table_definition(self, table, type_="table"):
table_definition_rows = list( table_definition_rows = list(
await self.ds.execute( await self.ds.execute(

Wyświetl plik

@ -4,7 +4,6 @@ import re
from datasette import hookimpl from datasette import hookimpl
from datasette.utils import ( from datasette.utils import (
escape_sqlite, escape_sqlite,
get_all_foreign_keys,
path_with_added_args, path_with_added_args,
path_with_removed_args, path_with_removed_args,
detect_json1, detect_json1,
@ -486,9 +485,8 @@ class ManyToManyFacet(Facet):
# This is calculated based on foreign key relationships to this table # This is calculated based on foreign key relationships to this table
# Are there any many-to-many tables pointing here? # Are there any many-to-many tables pointing here?
suggested_facets = [] suggested_facets = []
all_foreign_keys = await self.ds.execute_against_connection_in_thread( db = self.ds.databases[self.database]
self.database, get_all_foreign_keys all_foreign_keys = await db.get_all_foreign_keys()
)
if not all_foreign_keys.get(self.table): if not all_foreign_keys.get(self.table):
# It's probably a view # It's probably a view
return [] return []
@ -528,9 +526,8 @@ class ManyToManyFacet(Facet):
facets_timed_out = [] facets_timed_out = []
args = set(self.get_querystring_pairs()) args = set(self.get_querystring_pairs())
facet_size = self.ds.config("default_facet_size") facet_size = self.ds.config("default_facet_size")
all_foreign_keys = await self.ds.execute_against_connection_in_thread( db = self.ds.databases[self.database]
self.database, get_all_foreign_keys all_foreign_keys = await db.get_all_foreign_keys()
)
if not all_foreign_keys.get(self.table): if not all_foreign_keys.get(self.table):
return [], [] return [], []
# We care about three tables: self.table, middle_table and destination_table # We care about three tables: self.table, middle_table and destination_table

Wyświetl plik

@ -14,8 +14,6 @@ from datasette.utils import (
compound_keys_after_sql, compound_keys_after_sql,
escape_sqlite, escape_sqlite,
filters_should_redirect, filters_should_redirect,
get_all_foreign_keys,
get_outbound_foreign_keys,
is_url, is_url,
path_from_row_pks, path_from_row_pks,
path_with_added_args, path_with_added_args,
@ -293,9 +291,8 @@ class TableView(RowTableShared):
through_table = through_data["table"] through_table = through_data["table"]
other_column = through_data["column"] other_column = through_data["column"]
value = through_data["value"] value = through_data["value"]
outgoing_foreign_keys = await self.ds.execute_against_connection_in_thread( outgoing_foreign_keys = await db.get_outbound_foreign_keys(
database, through_table
lambda conn: get_outbound_foreign_keys(conn, through_table),
) )
try: try:
fk_to_us = [ fk_to_us = [
@ -843,10 +840,8 @@ class RowView(RowTableShared):
async def foreign_key_tables(self, database, table, pk_values): async def foreign_key_tables(self, database, table, pk_values):
if len(pk_values) != 1: if len(pk_values) != 1:
return [] return []
db = self.ds.databases[database]
all_foreign_keys = await self.ds.execute_against_connection_in_thread( all_foreign_keys = await db.get_all_foreign_keys()
database, get_all_foreign_keys
)
foreign_keys = all_foreign_keys[table]["incoming"] foreign_keys = all_foreign_keys[table]["incoming"]
if len(foreign_keys) == 0: if len(foreign_keys) == 0:
return [] return []