mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Databse.primary_keys(table) / fts_table(table) refactor, closes #488
Also cleaned up some unused imports spotted by the linter.
This commit is contained in:
parent
3fe21b91bb
commit
20f98c3e20
4 changed files with 24 additions and 43 deletions
|
|
@ -2,6 +2,8 @@ from pathlib import Path
|
||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
InterruptedError,
|
InterruptedError,
|
||||||
|
detect_fts,
|
||||||
|
detect_primary_keys,
|
||||||
detect_spatialite,
|
detect_spatialite,
|
||||||
get_all_foreign_keys,
|
get_all_foreign_keys,
|
||||||
get_outbound_foreign_keys,
|
get_outbound_foreign_keys,
|
||||||
|
|
@ -93,6 +95,16 @@ class Database:
|
||||||
self.name, lambda conn: table_columns(conn, table)
|
self.name, lambda conn: table_columns(conn, table)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def primary_keys(self, table):
|
||||||
|
return await self.ds.execute_against_connection_in_thread(
|
||||||
|
self.name, lambda conn: detect_primary_keys(conn, table)
|
||||||
|
)
|
||||||
|
|
||||||
|
async def fts_table(self, table):
|
||||||
|
return await self.ds.execute_against_connection_in_thread(
|
||||||
|
self.name, lambda conn: detect_fts(conn, table)
|
||||||
|
)
|
||||||
|
|
||||||
async def label_column_for_table(self, table):
|
async def label_column_for_table(self, table):
|
||||||
explicit_label_column = self.ds.table_metadata(self.name, table).get(
|
explicit_label_column = self.ds.table_metadata(self.name, table).get(
|
||||||
"label_column"
|
"label_column"
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,7 @@ import os
|
||||||
|
|
||||||
from sanic import response
|
from sanic import response
|
||||||
|
|
||||||
from datasette.utils import (
|
from datasette.utils import to_css_class, validate_sql_select
|
||||||
detect_fts,
|
|
||||||
detect_primary_keys,
|
|
||||||
to_css_class,
|
|
||||||
validate_sql_select,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .base import BaseView, DatasetteError
|
from .base import BaseView, DatasetteError
|
||||||
|
|
||||||
|
|
@ -40,14 +35,10 @@ class DatabaseView(BaseView):
|
||||||
{
|
{
|
||||||
"name": table,
|
"name": table,
|
||||||
"columns": table_columns,
|
"columns": table_columns,
|
||||||
"primary_keys": await self.ds.execute_against_connection_in_thread(
|
"primary_keys": await db.primary_keys(table),
|
||||||
database, lambda conn: detect_primary_keys(conn, table)
|
|
||||||
),
|
|
||||||
"count": table_counts[table],
|
"count": table_counts[table],
|
||||||
"hidden": table in hidden_table_names,
|
"hidden": table in hidden_table_names,
|
||||||
"fts_table": await self.ds.execute_against_connection_in_thread(
|
"fts_table": await db.fts_table(table),
|
||||||
database, lambda conn: detect_fts(conn, table)
|
|
||||||
),
|
|
||||||
"foreign_keys": all_foreign_keys[table],
|
"foreign_keys": all_foreign_keys[table],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,10 @@ import json
|
||||||
|
|
||||||
from sanic import response
|
from sanic import response
|
||||||
|
|
||||||
from datasette.utils import (
|
from datasette.utils import CustomJSONEncoder
|
||||||
CustomJSONEncoder,
|
|
||||||
InterruptedError,
|
|
||||||
detect_primary_keys,
|
|
||||||
detect_fts,
|
|
||||||
)
|
|
||||||
from datasette.version import __version__
|
from datasette.version import __version__
|
||||||
|
|
||||||
from .base import HASH_LENGTH, RenderMixin
|
from .base import RenderMixin
|
||||||
|
|
||||||
|
|
||||||
# Truncate table list on homepage at:
|
# Truncate table list on homepage at:
|
||||||
|
|
@ -46,14 +41,10 @@ class IndexView(RenderMixin):
|
||||||
tables[table] = {
|
tables[table] = {
|
||||||
"name": table,
|
"name": table,
|
||||||
"columns": table_columns,
|
"columns": table_columns,
|
||||||
"primary_keys": await self.ds.execute_against_connection_in_thread(
|
"primary_keys": await db.primary_keys(table),
|
||||||
name, lambda conn: detect_primary_keys(conn, table)
|
|
||||||
),
|
|
||||||
"count": table_counts.get(table),
|
"count": table_counts.get(table),
|
||||||
"hidden": table in hidden_table_names,
|
"hidden": table in hidden_table_names,
|
||||||
"fts_table": await self.ds.execute_against_connection_in_thread(
|
"fts_table": await db.fts_table(table),
|
||||||
name, lambda conn: detect_fts(conn, table)
|
|
||||||
),
|
|
||||||
"num_relationships_for_sorting": 0,
|
"num_relationships_for_sorting": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,12 @@ import jinja2
|
||||||
from sanic.exceptions import NotFound
|
from sanic.exceptions import NotFound
|
||||||
from sanic.request import RequestParameters
|
from sanic.request import RequestParameters
|
||||||
|
|
||||||
from datasette.facets import load_facet_configs
|
|
||||||
from datasette.plugins import pm
|
from datasette.plugins import pm
|
||||||
from datasette.utils import (
|
from datasette.utils import (
|
||||||
CustomRow,
|
CustomRow,
|
||||||
InterruptedError,
|
InterruptedError,
|
||||||
append_querystring,
|
append_querystring,
|
||||||
compound_keys_after_sql,
|
compound_keys_after_sql,
|
||||||
detect_fts,
|
|
||||||
detect_primary_keys,
|
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
filters_should_redirect,
|
filters_should_redirect,
|
||||||
get_all_foreign_keys,
|
get_all_foreign_keys,
|
||||||
|
|
@ -25,7 +22,6 @@ from datasette.utils import (
|
||||||
path_with_removed_args,
|
path_with_removed_args,
|
||||||
path_with_replaced_args,
|
path_with_replaced_args,
|
||||||
sqlite3,
|
sqlite3,
|
||||||
table_columns,
|
|
||||||
to_css_class,
|
to_css_class,
|
||||||
urlsafe_components,
|
urlsafe_components,
|
||||||
value_as_boolean,
|
value_as_boolean,
|
||||||
|
|
@ -70,9 +66,7 @@ class RowTableShared(BaseView):
|
||||||
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
|
||||||
]
|
]
|
||||||
pks = await self.ds.execute_against_connection_in_thread(
|
pks = await db.primary_keys(table)
|
||||||
database, lambda conn: detect_primary_keys(conn, table)
|
|
||||||
)
|
|
||||||
column_to_foreign_key_table = {
|
column_to_foreign_key_table = {
|
||||||
fk["column"]: fk["other_table"]
|
fk["column"]: fk["other_table"]
|
||||||
for fk in await db.foreign_keys_for_table(table)
|
for fk in await db.foreign_keys_for_table(table)
|
||||||
|
|
@ -213,9 +207,7 @@ class TableView(RowTableShared):
|
||||||
if not is_view and not table_exists:
|
if not is_view and not table_exists:
|
||||||
raise NotFound("Table not found: {}".format(table))
|
raise NotFound("Table not found: {}".format(table))
|
||||||
|
|
||||||
pks = await self.ds.execute_against_connection_in_thread(
|
pks = await db.primary_keys(table)
|
||||||
database, lambda conn: detect_primary_keys(conn, table)
|
|
||||||
)
|
|
||||||
use_rowid = not pks and not is_view
|
use_rowid = not pks and not is_view
|
||||||
if use_rowid:
|
if use_rowid:
|
||||||
select = "rowid, *"
|
select = "rowid, *"
|
||||||
|
|
@ -331,9 +323,7 @@ class TableView(RowTableShared):
|
||||||
# _search support:
|
# _search support:
|
||||||
fts_table = special_args.get("_fts_table")
|
fts_table = special_args.get("_fts_table")
|
||||||
fts_table = fts_table or table_metadata.get("fts_table")
|
fts_table = fts_table or table_metadata.get("fts_table")
|
||||||
fts_table = fts_table or await self.ds.execute_against_connection_in_thread(
|
fts_table = fts_table or await db.fts_table(table)
|
||||||
database, lambda conn: detect_fts(conn, table)
|
|
||||||
)
|
|
||||||
fts_pk = special_args.get("_fts_pk", table_metadata.get("fts_pk", "rowid"))
|
fts_pk = special_args.get("_fts_pk", table_metadata.get("fts_pk", "rowid"))
|
||||||
search_args = dict(
|
search_args = dict(
|
||||||
pair for pair in special_args.items() if pair[0].startswith("_search")
|
pair for pair in special_args.items() if pair[0].startswith("_search")
|
||||||
|
|
@ -668,8 +658,6 @@ class TableView(RowTableShared):
|
||||||
and not _next
|
and not _next
|
||||||
):
|
):
|
||||||
for facet in facet_instances:
|
for facet in facet_instances:
|
||||||
# TODO: ensure facet is not suggested if it is already active
|
|
||||||
# used to use 'if facet_column in facets' for this
|
|
||||||
suggested_facets.extend(await facet.suggest())
|
suggested_facets.extend(await facet.suggest())
|
||||||
|
|
||||||
# human_description_en combines filters AND search, if provided
|
# human_description_en combines filters AND search, if provided
|
||||||
|
|
@ -776,9 +764,8 @@ class RowView(RowTableShared):
|
||||||
|
|
||||||
async def data(self, request, database, hash, table, pk_path, default_labels=False):
|
async def data(self, request, database, hash, table, pk_path, default_labels=False):
|
||||||
pk_values = urlsafe_components(pk_path)
|
pk_values = urlsafe_components(pk_path)
|
||||||
pks = await self.ds.execute_against_connection_in_thread(
|
db = self.ds.databases[database]
|
||||||
database, lambda conn: detect_primary_keys(conn, table)
|
pks = await db.primary_keys(table)
|
||||||
)
|
|
||||||
use_rowid = not pks
|
use_rowid = not pks
|
||||||
select = "*"
|
select = "*"
|
||||||
if use_rowid:
|
if use_rowid:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue