2018-05-13 12:44:22 +00:00
|
|
|
from sanic.views import HTTPMethodView
|
|
|
|
from sanic import response
|
|
|
|
from sanic.exceptions import NotFound
|
|
|
|
from datasette import __version__
|
|
|
|
from datasette.utils import (
|
|
|
|
CustomJSONEncoder,
|
|
|
|
InvalidSql,
|
|
|
|
path_from_row_pks,
|
|
|
|
path_with_added_args,
|
|
|
|
path_with_ext,
|
|
|
|
sqlite_timelimit,
|
|
|
|
to_css_class,
|
|
|
|
)
|
|
|
|
import re
|
|
|
|
import threading
|
|
|
|
import sqlite3
|
|
|
|
import asyncio
|
|
|
|
import time
|
|
|
|
import json
|
|
|
|
import pint
|
|
|
|
|
|
|
|
connections = threading.local()
|
|
|
|
ureg = pint.UnitRegistry()
|
|
|
|
|
|
|
|
HASH_BLOCK_SIZE = 1024 * 1024
|
|
|
|
HASH_LENGTH = 7
|
|
|
|
|
|
|
|
|
|
|
|
class DatasetteError(Exception):
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
def __init__(self, message, title=None, error_dict=None, status=500, template=None):
|
|
|
|
self.message = message
|
|
|
|
self.title = title
|
|
|
|
self.error_dict = error_dict or {}
|
|
|
|
self.status = status
|
|
|
|
|
|
|
|
|
|
|
|
class RenderMixin(HTTPMethodView):
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
def render(self, templates, **context):
|
|
|
|
template = self.jinja_env.select_template(templates)
|
2018-05-13 12:55:15 +00:00
|
|
|
select_templates = [
|
|
|
|
"{}{}".format("*" if template_name == template.name else "", template_name)
|
|
|
|
for template_name in templates
|
|
|
|
]
|
2018-05-13 12:44:22 +00:00
|
|
|
return response.html(
|
2018-05-13 12:55:15 +00:00
|
|
|
template.render(
|
|
|
|
{
|
|
|
|
**context,
|
|
|
|
**{
|
|
|
|
"app_css_hash": self.ds.app_css_hash(),
|
|
|
|
"select_templates": select_templates,
|
|
|
|
"zip": zip,
|
|
|
|
}
|
2018-05-13 12:44:22 +00:00
|
|
|
}
|
2018-05-13 12:55:15 +00:00
|
|
|
)
|
2018-05-13 12:44:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BaseView(RenderMixin):
|
2018-05-13 12:55:15 +00:00
|
|
|
re_named_parameter = re.compile(":([a-zA-Z0-9_]+)")
|
2018-05-13 12:44:22 +00:00
|
|
|
|
|
|
|
def __init__(self, datasette):
|
|
|
|
self.ds = datasette
|
|
|
|
self.files = datasette.files
|
|
|
|
self.jinja_env = datasette.jinja_env
|
|
|
|
self.executor = datasette.executor
|
|
|
|
self.page_size = datasette.page_size
|
|
|
|
self.max_returned_rows = datasette.max_returned_rows
|
|
|
|
|
|
|
|
def table_metadata(self, database, table):
|
|
|
|
"Fetch table-specific metadata."
|
2018-05-13 12:55:15 +00:00
|
|
|
return self.ds.metadata.get("databases", {}).get(database, {}).get(
|
|
|
|
"tables", {}
|
|
|
|
).get(
|
|
|
|
table, {}
|
|
|
|
)
|
2018-05-13 12:44:22 +00:00
|
|
|
|
|
|
|
def options(self, request, *args, **kwargs):
|
2018-05-13 12:55:15 +00:00
|
|
|
r = response.text("ok")
|
2018-05-13 12:44:22 +00:00
|
|
|
if self.ds.cors:
|
2018-05-13 12:55:15 +00:00
|
|
|
r.headers["Access-Control-Allow-Origin"] = "*"
|
2018-05-13 12:44:22 +00:00
|
|
|
return r
|
|
|
|
|
|
|
|
def redirect(self, request, path, forward_querystring=True):
|
2018-05-13 12:55:15 +00:00
|
|
|
if request.query_string and "?" not in path and forward_querystring:
|
|
|
|
path = "{}?{}".format(path, request.query_string)
|
2018-05-13 12:44:22 +00:00
|
|
|
r = response.redirect(path)
|
2018-05-13 12:55:15 +00:00
|
|
|
r.headers["Link"] = "<{}>; rel=preload".format(path)
|
2018-05-13 12:44:22 +00:00
|
|
|
if self.ds.cors:
|
2018-05-13 12:55:15 +00:00
|
|
|
r.headers["Access-Control-Allow-Origin"] = "*"
|
2018-05-13 12:44:22 +00:00
|
|
|
return r
|
|
|
|
|
|
|
|
def resolve_db_name(self, db_name, **kwargs):
|
|
|
|
databases = self.ds.inspect()
|
|
|
|
hash = None
|
|
|
|
name = None
|
2018-05-13 12:55:15 +00:00
|
|
|
if "-" in db_name:
|
2018-05-13 12:44:22 +00:00
|
|
|
# Might be name-and-hash, or might just be
|
|
|
|
# a name with a hyphen in it
|
2018-05-13 12:55:15 +00:00
|
|
|
name, hash = db_name.rsplit("-", 1)
|
2018-05-13 12:44:22 +00:00
|
|
|
if name not in databases:
|
|
|
|
# Try the whole name
|
|
|
|
name = db_name
|
|
|
|
hash = None
|
|
|
|
else:
|
|
|
|
name = db_name
|
|
|
|
# Verify the hash
|
|
|
|
try:
|
|
|
|
info = databases[name]
|
|
|
|
except KeyError:
|
2018-05-13 12:55:15 +00:00
|
|
|
raise NotFound("Database not found: {}".format(name))
|
|
|
|
|
|
|
|
expected = info["hash"][:HASH_LENGTH]
|
2018-05-13 12:44:22 +00:00
|
|
|
if expected != hash:
|
2018-05-13 12:55:15 +00:00
|
|
|
should_redirect = "/{}-{}".format(name, expected)
|
|
|
|
if "table" in kwargs:
|
|
|
|
should_redirect += "/" + kwargs["table"]
|
|
|
|
if "pk_path" in kwargs:
|
|
|
|
should_redirect += "/" + kwargs["pk_path"]
|
|
|
|
if "as_json" in kwargs:
|
|
|
|
should_redirect += kwargs["as_json"]
|
|
|
|
if "as_db" in kwargs:
|
|
|
|
should_redirect += kwargs["as_db"]
|
2018-05-13 12:44:22 +00:00
|
|
|
return name, expected, should_redirect
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
return name, expected, None
|
|
|
|
|
2018-05-13 12:55:15 +00:00
|
|
|
async def execute(
|
|
|
|
self,
|
|
|
|
db_name,
|
|
|
|
sql,
|
|
|
|
params=None,
|
|
|
|
truncate=False,
|
|
|
|
custom_time_limit=None,
|
|
|
|
page_size=None,
|
|
|
|
):
|
2018-05-13 12:44:22 +00:00
|
|
|
"""Executes sql against db_name in a thread"""
|
|
|
|
page_size = page_size or self.page_size
|
|
|
|
|
|
|
|
def sql_operation_in_thread():
|
|
|
|
conn = getattr(connections, db_name, None)
|
|
|
|
if not conn:
|
|
|
|
info = self.ds.inspect()[db_name]
|
|
|
|
conn = sqlite3.connect(
|
2018-05-13 12:55:15 +00:00
|
|
|
"file:{}?immutable=1".format(info["file"]),
|
2018-05-13 12:44:22 +00:00
|
|
|
uri=True,
|
|
|
|
check_same_thread=False,
|
|
|
|
)
|
|
|
|
self.ds.prepare_connection(conn)
|
|
|
|
setattr(connections, db_name, conn)
|
|
|
|
|
|
|
|
time_limit_ms = self.ds.sql_time_limit_ms
|
|
|
|
if custom_time_limit and custom_time_limit < self.ds.sql_time_limit_ms:
|
|
|
|
time_limit_ms = custom_time_limit
|
|
|
|
|
|
|
|
with sqlite_timelimit(conn, time_limit_ms):
|
|
|
|
try:
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute(sql, params or {})
|
|
|
|
max_returned_rows = self.max_returned_rows
|
|
|
|
if max_returned_rows == page_size:
|
|
|
|
max_returned_rows += 1
|
|
|
|
if max_returned_rows and truncate:
|
|
|
|
rows = cursor.fetchmany(max_returned_rows + 1)
|
|
|
|
truncated = len(rows) > max_returned_rows
|
|
|
|
rows = rows[:max_returned_rows]
|
|
|
|
else:
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
truncated = False
|
|
|
|
except Exception as e:
|
2018-05-13 12:55:15 +00:00
|
|
|
print(
|
|
|
|
"ERROR: conn={}, sql = {}, params = {}: {}".format(
|
|
|
|
conn, repr(sql), params, e
|
|
|
|
)
|
|
|
|
)
|
2018-05-13 12:44:22 +00:00
|
|
|
raise
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
if truncate:
|
|
|
|
return rows, truncated, cursor.description
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
else:
|
|
|
|
return rows
|
|
|
|
|
|
|
|
return await asyncio.get_event_loop().run_in_executor(
|
|
|
|
self.executor, sql_operation_in_thread
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_templates(self, database, table=None):
|
|
|
|
assert NotImplemented
|
|
|
|
|
|
|
|
async def get(self, request, db_name, **kwargs):
|
|
|
|
name, hash, should_redirect = self.resolve_db_name(db_name, **kwargs)
|
|
|
|
if should_redirect:
|
|
|
|
return self.redirect(request, should_redirect)
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
return await self.view_get(request, name, hash, **kwargs)
|
|
|
|
|
|
|
|
async def view_get(self, request, name, hash, **kwargs):
|
|
|
|
try:
|
2018-05-13 12:55:15 +00:00
|
|
|
as_json = kwargs.pop("as_json")
|
2018-05-13 12:44:22 +00:00
|
|
|
except KeyError:
|
|
|
|
as_json = False
|
|
|
|
extra_template_data = {}
|
|
|
|
start = time.time()
|
|
|
|
status_code = 200
|
|
|
|
templates = []
|
|
|
|
try:
|
|
|
|
response_or_template_contexts = await self.data(
|
|
|
|
request, name, hash, **kwargs
|
|
|
|
)
|
|
|
|
if isinstance(response_or_template_contexts, response.HTTPResponse):
|
|
|
|
return response_or_template_contexts
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
else:
|
|
|
|
data, extra_template_data, templates = response_or_template_contexts
|
|
|
|
except (sqlite3.OperationalError, InvalidSql) as e:
|
2018-05-13 12:55:15 +00:00
|
|
|
raise DatasetteError(str(e), title="Invalid SQL", status=400)
|
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
except (sqlite3.OperationalError) as e:
|
|
|
|
raise DatasetteError(str(e))
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
except DatasetteError:
|
|
|
|
raise
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
end = time.time()
|
2018-05-13 12:55:15 +00:00
|
|
|
data["query_ms"] = (end - start) * 1000
|
|
|
|
for key in ("source", "source_url", "license", "license_url"):
|
2018-05-13 12:44:22 +00:00
|
|
|
value = self.ds.metadata.get(key)
|
|
|
|
if value:
|
|
|
|
data[key] = value
|
|
|
|
if as_json:
|
|
|
|
# Special case for .jsono extension - redirect to _shape=objects
|
2018-05-13 12:55:15 +00:00
|
|
|
if as_json == ".jsono":
|
2018-05-13 12:44:22 +00:00
|
|
|
return self.redirect(
|
|
|
|
request,
|
|
|
|
path_with_added_args(
|
|
|
|
request,
|
2018-05-13 12:55:15 +00:00
|
|
|
{"_shape": "objects"},
|
|
|
|
path=request.path.rsplit(".jsono", 1)[0] + ".json",
|
2018-05-13 12:44:22 +00:00
|
|
|
),
|
2018-05-13 12:55:15 +00:00
|
|
|
forward_querystring=False,
|
2018-05-13 12:44:22 +00:00
|
|
|
)
|
2018-05-13 12:55:15 +00:00
|
|
|
|
2018-05-13 12:44:22 +00:00
|
|
|
# Deal with the _shape option
|
2018-05-13 12:55:15 +00:00
|
|
|
shape = request.args.get("_shape", "arrays")
|
|
|
|
if shape in ("objects", "object", "array"):
|
|
|
|
columns = data.get("columns")
|
|
|
|
rows = data.get("rows")
|
2018-05-13 12:44:22 +00:00
|
|
|
if rows and columns:
|
2018-05-13 12:55:15 +00:00
|
|
|
data["rows"] = [dict(zip(columns, row)) for row in rows]
|
|
|
|
if shape == "object":
|
2018-05-13 12:44:22 +00:00
|
|
|
error = None
|
2018-05-13 12:55:15 +00:00
|
|
|
if "primary_keys" not in data:
|
|
|
|
error = "_shape=object is only available on tables"
|
2018-05-13 12:44:22 +00:00
|
|
|
else:
|
2018-05-13 12:55:15 +00:00
|
|
|
pks = data["primary_keys"]
|
2018-05-13 12:44:22 +00:00
|
|
|
if not pks:
|
2018-05-13 12:55:15 +00:00
|
|
|
error = "_shape=object not available for tables with no primary keys"
|
2018-05-13 12:44:22 +00:00
|
|
|
else:
|
|
|
|
object_rows = {}
|
2018-05-13 12:55:15 +00:00
|
|
|
for row in data["rows"]:
|
2018-05-13 12:44:22 +00:00
|
|
|
pk_string = path_from_row_pks(row, pks, not pks)
|
|
|
|
object_rows[pk_string] = row
|
|
|
|
data = object_rows
|
|
|
|
if error:
|
|
|
|
data = {
|
2018-05-13 12:55:15 +00:00
|
|
|
"ok": False,
|
|
|
|
"error": error,
|
|
|
|
"database": name,
|
|
|
|
"database_hash": hash,
|
2018-05-13 12:44:22 +00:00
|
|
|
}
|
2018-05-13 12:55:15 +00:00
|
|
|
elif shape == "array":
|
|
|
|
data = data["rows"]
|
|
|
|
elif shape == "arrays":
|
2018-05-13 12:44:22 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
status_code = 400
|
|
|
|
data = {
|
2018-05-13 12:55:15 +00:00
|
|
|
"ok": False,
|
|
|
|
"error": "Invalid _shape: {}".format(shape),
|
|
|
|
"status": 400,
|
|
|
|
"title": None,
|
2018-05-13 12:44:22 +00:00
|
|
|
}
|
|
|
|
headers = {}
|
|
|
|
if self.ds.cors:
|
2018-05-13 12:55:15 +00:00
|
|
|
headers["Access-Control-Allow-Origin"] = "*"
|
2018-05-13 12:44:22 +00:00
|
|
|
r = response.HTTPResponse(
|
2018-05-13 12:55:15 +00:00
|
|
|
json.dumps(data, cls=CustomJSONEncoder),
|
2018-05-13 12:44:22 +00:00
|
|
|
status=status_code,
|
2018-05-13 12:55:15 +00:00
|
|
|
content_type="application/json",
|
2018-05-13 12:44:22 +00:00
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
extras = {}
|
|
|
|
if callable(extra_template_data):
|
|
|
|
extras = extra_template_data()
|
|
|
|
if asyncio.iscoroutine(extras):
|
|
|
|
extras = await extras
|
|
|
|
else:
|
|
|
|
extras = extra_template_data
|
|
|
|
context = {
|
|
|
|
**data,
|
|
|
|
**extras,
|
|
|
|
**{
|
2018-05-13 12:55:15 +00:00
|
|
|
"url_json": path_with_ext(request, ".json"),
|
|
|
|
"url_jsono": path_with_ext(request, ".jsono"),
|
|
|
|
"extra_css_urls": self.ds.extra_css_urls(),
|
|
|
|
"extra_js_urls": self.ds.extra_js_urls(),
|
|
|
|
"datasette_version": __version__,
|
2018-05-13 12:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-13 12:55:15 +00:00
|
|
|
if "metadata" not in context:
|
|
|
|
context["metadata"] = self.ds.metadata
|
|
|
|
r = self.render(templates, **context)
|
2018-05-13 12:44:22 +00:00
|
|
|
r.status = status_code
|
|
|
|
# Set far-future cache expiry
|
|
|
|
if self.ds.cache_headers:
|
2018-05-13 12:55:15 +00:00
|
|
|
r.headers["Cache-Control"] = "max-age={}".format(365 * 24 * 60 * 60)
|
2018-05-13 12:44:22 +00:00
|
|
|
return r
|
|
|
|
|
2018-05-13 12:55:15 +00:00
|
|
|
async def custom_sql(
|
|
|
|
self, request, name, hash, sql, editable=True, canned_query=None
|
|
|
|
):
|
2018-05-13 12:44:22 +00:00
|
|
|
params = request.raw_args
|
2018-05-13 12:55:15 +00:00
|
|
|
if "sql" in params:
|
|
|
|
params.pop("sql")
|
|
|
|
if "_shape" in params:
|
|
|
|
params.pop("_shape")
|
2018-05-13 12:44:22 +00:00
|
|
|
# Extract any :named parameters
|
|
|
|
named_parameters = self.re_named_parameter.findall(sql)
|
|
|
|
named_parameter_values = {
|
2018-05-13 12:55:15 +00:00
|
|
|
named_parameter: params.get(named_parameter) or ""
|
2018-05-13 12:44:22 +00:00
|
|
|
for named_parameter in named_parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set to blank string if missing from params
|
|
|
|
for named_parameter in named_parameters:
|
|
|
|
if named_parameter not in params:
|
2018-05-13 12:55:15 +00:00
|
|
|
params[named_parameter] = ""
|
2018-05-13 12:44:22 +00:00
|
|
|
|
|
|
|
extra_args = {}
|
2018-05-13 12:55:15 +00:00
|
|
|
if params.get("_timelimit"):
|
|
|
|
extra_args["custom_time_limit"] = int(params["_timelimit"])
|
2018-05-13 12:44:22 +00:00
|
|
|
rows, truncated, description = await self.execute(
|
|
|
|
name, sql, params, truncate=True, **extra_args
|
|
|
|
)
|
|
|
|
columns = [r[0] for r in description]
|
|
|
|
|
2018-05-13 12:55:15 +00:00
|
|
|
templates = ["query-{}.html".format(to_css_class(name)), "query.html"]
|
2018-05-13 12:44:22 +00:00
|
|
|
if canned_query:
|
2018-05-13 12:55:15 +00:00
|
|
|
templates.insert(
|
|
|
|
0,
|
|
|
|
"query-{}-{}.html".format(
|
|
|
|
to_css_class(name), to_css_class(canned_query)
|
|
|
|
),
|
|
|
|
)
|
2018-05-13 12:44:22 +00:00
|
|
|
|
|
|
|
return {
|
2018-05-13 12:55:15 +00:00
|
|
|
"database": name,
|
|
|
|
"rows": rows,
|
|
|
|
"truncated": truncated,
|
|
|
|
"columns": columns,
|
|
|
|
"query": {"sql": sql, "params": params},
|
2018-05-13 12:44:22 +00:00
|
|
|
}, {
|
2018-05-13 12:55:15 +00:00
|
|
|
"database_hash": hash,
|
|
|
|
"custom_sql": True,
|
|
|
|
"named_parameter_values": named_parameter_values,
|
|
|
|
"editable": editable,
|
|
|
|
"canned_query": canned_query,
|
2018-05-13 12:44:22 +00:00
|
|
|
}, templates
|