kopia lustrzana https://github.com/simonw/datasette
Implemented new database view and template
Closes #53 - see comments there for screenshots.pull/81/head
rodzic
2366a016f2
commit
fd3a33989a
|
@ -243,27 +243,26 @@ class DatabaseView(BaseView):
|
|||
template = 'database.html'
|
||||
|
||||
async def data(self, request, name, hash):
|
||||
sql = 'select * from sqlite_master'
|
||||
custom_sql = False
|
||||
params = {}
|
||||
if request.args.get('sql'):
|
||||
params = request.raw_args
|
||||
sql = params.pop('sql')
|
||||
validate_sql_select(sql)
|
||||
custom_sql = True
|
||||
rows = await self.execute(name, sql, params)
|
||||
columns = [r[0] for r in rows.description]
|
||||
tables = []
|
||||
table_metadata = self.ds.metadata()[name]['tables']
|
||||
for table_name, table_rows in table_metadata.items():
|
||||
rows = await self.execute(
|
||||
name,
|
||||
'PRAGMA table_info({});'.format(table_name)
|
||||
)
|
||||
tables.append({
|
||||
'name': table_name,
|
||||
'columns': [r[1] for r in rows],
|
||||
'table_rows': table_rows,
|
||||
})
|
||||
tables.sort(key=lambda t: t['name'])
|
||||
views = await self.execute(name, 'select name from sqlite_master where type = "view"')
|
||||
return {
|
||||
'database': name,
|
||||
'rows': rows,
|
||||
'columns': columns,
|
||||
'query': {
|
||||
'sql': sql,
|
||||
'params': params,
|
||||
}
|
||||
'tables': tables,
|
||||
'views': [v[0] for v in views],
|
||||
}, {
|
||||
'database_hash': hash,
|
||||
'custom_sql': custom_sql,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -64,3 +64,12 @@ th {
|
|||
.hd :link {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.db-table p {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0.3em;
|
||||
}
|
||||
.db-table h2 {
|
||||
margin-top: 1em;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,23 @@
|
|||
<p><a href="/{{ database }}-{{ database_hash }}.db">download {{ database }}.db</a></p>
|
||||
{% endif %}
|
||||
|
||||
{% for table in tables %}
|
||||
<div class="db-table">
|
||||
<h2><a href="/{{ database }}-{{ database_hash }}/{{ table.name }}">{{ table.name }}</a></h2>
|
||||
<p><em>{% for column in table.columns[:9] %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}{% if table.columns|length > 9 %}...{% endif %}</em></p>
|
||||
<p>{{ "{:,}".format(table.table_rows) }} row{% if table.table_rows == 1 %}{% else %}s{% endif %}</p>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% if views %}
|
||||
<h2>Views</h2>
|
||||
<ul>
|
||||
{% for view in views %}
|
||||
<li><a href="/{{ database }}-{{ database_hash }}/{{ view }}">{{ view }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
|
|
Ładowanie…
Reference in New Issue