Skip to content

Commit

Permalink
Use rowid if no primary key available
Browse files Browse the repository at this point in the history
Allows us to link to individual records even for tables that do not have a primary key.

Refs #5
  • Loading branch information
Simon Willison committed Nov 9, 2017
1 parent b2dee11 commit d9fa2bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
34 changes: 24 additions & 10 deletions immutabase/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,27 @@ class TableView(BaseView):

async def data(self, request, name, hash, 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:
where_clause, params = build_where_clause(request.args)
sql = 'select * from "{}" where {} limit 50'.format(
table, where_clause
sql = 'select {} from "{}" where {} limit 50'.format(
select, table, where_clause
)
else:
sql = 'select * from "{}" limit 50'.format(table)
sql = 'select {} from "{}" limit 50'.format(select, table)
params = []

rows = await self.execute(name, sql)

columns = [r[0] for r in rows.description]
display_columns = columns
if use_rowid:
display_columns = display_columns[1:]
rows = list(rows)
pks = await self.pks_for_table(name, table)
info = ensure_build_metadata(self.files)
total_rows = info[name]['tables'].get(table)
return {
Expand All @@ -273,7 +280,9 @@ async def data(self, request, name, hash, table):
'primary_keys': pks,
}, lambda: {
'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,
}


Expand All @@ -284,12 +293,17 @@ async def data(self, request, name, hash, table, pk_path):
table = urllib.parse.unquote_plus(table)
pk_values = compound_pks_from_path(pk_path)
pks = await self.pks_for_table(name, table)
use_rowid = not pks
select = '*'
if use_rowid:
select = 'rowid, *'
pks = ['rowid']
wheres = [
'"{}"=:p{}'.format(pk, i)
for i, pk in enumerate(pks)
]
sql = 'select * from "{}" where {}'.format(
table, ' AND '.join(wheres)
sql = 'select {} from "{}" where {}'.format(
select, table, ' AND '.join(wheres)
)
params = {}
for i, pk_value in enumerate(pk_values):
Expand Down Expand Up @@ -351,9 +365,9 @@ def compound_pks_from_path(path):
]


def path_from_row_pks(row, pks):
if not pks:
return ''
def path_from_row_pks(row, pks, use_rowid):
if use_rowid:
return urllib.parse.quote_plus(str(row['rowid']))
bits = []
for pk in pks:
bits.append(
Expand Down
15 changes: 9 additions & 6 deletions immutabase/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ <h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}"
<h2>{{ table }}{% if total_rows != None %} ({{ "{:,}".format(total_rows) }} total row{% if total_rows == 1 %}{% else %}s{% endif %} in this table){% endif %}</h2>

<style>
th {
padding-right: 1em;
}
td {
white-space: pre;
vertical-align: top;
Expand All @@ -17,16 +20,16 @@ <h2>{{ table }}{% if total_rows != None %} ({{ "{:,}".format(total_rows) }} tota
</style>
<table>
<tr>
{% if primary_keys and row_link %}<th scope="col">Link</th>{% endif %}
{% for column in columns %}<th scope="col">{{ column }}</th>{% endfor %}
<th scope="col">{% if use_rowid %}rowid{% else %}Link{% endif %}</th>
{% for column in display_columns %}<th scope="col">{{ column }}</th>{% endfor %}
</tr>
{% for row in rows %}
<tr>
{% if primary_keys and row_link %}
<td><a href="/{{ database }}-{{ database_hash }}/{{ table }}/{{ row_link(row) }}">{{ row_link(row) }}</a></td>
{% endif %}
<td><a href="/{{ database }}-{{ database_hash }}/{{ table }}/{{ row_link(row) }}">{{ row_link(row) }}</a></td>
{% for td in row %}
<td>{{ td }}</td>
{% if not use_rowid or (use_rowid and not loop.first) %}
<td>{{ td }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
Expand Down

0 comments on commit d9fa2bf

Please sign in to comment.