mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Use rowid if no primary key available
Allows us to link to individual records even for tables that do not have a primary key. Refs #5
This commit is contained in:
parent
b2dee11fcd
commit
d9fa2bf7ba
2 changed files with 33 additions and 16 deletions
|
|
@ -248,20 +248,27 @@ class TableView(BaseView):
|
||||||
|
|
||||||
async def data(self, request, name, hash, table):
|
async def data(self, request, name, hash, table):
|
||||||
table = urllib.parse.unquote_plus(table)
|
table = urllib.parse.unquote_plus(table)
|
||||||
|
pks = await self.pks_for_table(name, table)
|
||||||
|
use_rowid = not pks
|
||||||
|
select = '*'
|
||||||
|
if use_rowid:
|
||||||
|
select = 'rowid, *'
|
||||||
if request.args:
|
if request.args:
|
||||||
where_clause, params = build_where_clause(request.args)
|
where_clause, params = build_where_clause(request.args)
|
||||||
sql = 'select * from "{}" where {} limit 50'.format(
|
sql = 'select {} from "{}" where {} limit 50'.format(
|
||||||
table, where_clause
|
select, table, where_clause
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
sql = 'select * from "{}" limit 50'.format(table)
|
sql = 'select {} from "{}" limit 50'.format(select, table)
|
||||||
params = []
|
params = []
|
||||||
|
|
||||||
rows = await self.execute(name, sql)
|
rows = await self.execute(name, sql)
|
||||||
|
|
||||||
columns = [r[0] for r in rows.description]
|
columns = [r[0] for r in rows.description]
|
||||||
|
display_columns = columns
|
||||||
|
if use_rowid:
|
||||||
|
display_columns = display_columns[1:]
|
||||||
rows = list(rows)
|
rows = list(rows)
|
||||||
pks = await self.pks_for_table(name, table)
|
|
||||||
info = ensure_build_metadata(self.files)
|
info = ensure_build_metadata(self.files)
|
||||||
total_rows = info[name]['tables'].get(table)
|
total_rows = info[name]['tables'].get(table)
|
||||||
return {
|
return {
|
||||||
|
|
@ -273,7 +280,9 @@ class TableView(BaseView):
|
||||||
'primary_keys': pks,
|
'primary_keys': pks,
|
||||||
}, lambda: {
|
}, lambda: {
|
||||||
'database_hash': hash,
|
'database_hash': hash,
|
||||||
'row_link': lambda row: path_from_row_pks(row, pks),
|
'use_rowid': use_rowid,
|
||||||
|
'row_link': lambda row: path_from_row_pks(row, pks, use_rowid),
|
||||||
|
'display_columns': display_columns,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -284,12 +293,17 @@ class RowView(BaseView):
|
||||||
table = urllib.parse.unquote_plus(table)
|
table = urllib.parse.unquote_plus(table)
|
||||||
pk_values = compound_pks_from_path(pk_path)
|
pk_values = compound_pks_from_path(pk_path)
|
||||||
pks = await self.pks_for_table(name, table)
|
pks = await self.pks_for_table(name, table)
|
||||||
|
use_rowid = not pks
|
||||||
|
select = '*'
|
||||||
|
if use_rowid:
|
||||||
|
select = 'rowid, *'
|
||||||
|
pks = ['rowid']
|
||||||
wheres = [
|
wheres = [
|
||||||
'"{}"=:p{}'.format(pk, i)
|
'"{}"=:p{}'.format(pk, i)
|
||||||
for i, pk in enumerate(pks)
|
for i, pk in enumerate(pks)
|
||||||
]
|
]
|
||||||
sql = 'select * from "{}" where {}'.format(
|
sql = 'select {} from "{}" where {}'.format(
|
||||||
table, ' AND '.join(wheres)
|
select, table, ' AND '.join(wheres)
|
||||||
)
|
)
|
||||||
params = {}
|
params = {}
|
||||||
for i, pk_value in enumerate(pk_values):
|
for i, pk_value in enumerate(pk_values):
|
||||||
|
|
@ -351,9 +365,9 @@ def compound_pks_from_path(path):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def path_from_row_pks(row, pks):
|
def path_from_row_pks(row, pks, use_rowid):
|
||||||
if not pks:
|
if use_rowid:
|
||||||
return ''
|
return urllib.parse.quote_plus(str(row['rowid']))
|
||||||
bits = []
|
bits = []
|
||||||
for pk in pks:
|
for pk in pks:
|
||||||
bits.append(
|
bits.append(
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@
|
||||||
<h2>{{ table }}{% if total_rows != None %} ({{ "{:,}".format(total_rows) }} total row{% if total_rows == 1 %}{% else %}s{% endif %} in this table){% endif %}</h2>
|
<h2>{{ table }}{% if total_rows != None %} ({{ "{:,}".format(total_rows) }} total row{% if total_rows == 1 %}{% else %}s{% endif %} in this table){% endif %}</h2>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
th {
|
||||||
|
padding-right: 1em;
|
||||||
|
}
|
||||||
td {
|
td {
|
||||||
white-space: pre;
|
white-space: pre;
|
||||||
vertical-align: top;
|
vertical-align: top;
|
||||||
|
|
@ -17,16 +20,16 @@ td {
|
||||||
</style>
|
</style>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
{% if primary_keys and row_link %}<th scope="col">Link</th>{% endif %}
|
<th scope="col">{% if use_rowid %}rowid{% else %}Link{% endif %}</th>
|
||||||
{% for column in columns %}<th scope="col">{{ column }}</th>{% endfor %}
|
{% for column in display_columns %}<th scope="col">{{ column }}</th>{% endfor %}
|
||||||
</tr>
|
</tr>
|
||||||
{% for row in rows %}
|
{% for row in rows %}
|
||||||
<tr>
|
<tr>
|
||||||
{% if primary_keys and row_link %}
|
<td><a href="/{{ database }}-{{ database_hash }}/{{ table }}/{{ row_link(row) }}">{{ row_link(row) }}</a></td>
|
||||||
<td><a href="/{{ database }}-{{ database_hash }}/{{ table }}/{{ row_link(row) }}">{{ row_link(row) }}</a></td>
|
|
||||||
{% endif %}
|
|
||||||
{% for td in row %}
|
{% for td in row %}
|
||||||
<td>{{ td }}</td>
|
{% if not use_rowid or (use_rowid and not loop.first) %}
|
||||||
|
<td>{{ td }}</td>
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue