/
utils.py
475 lines (405 loc) · 15.3 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
from base64 import b64decode, b64encode
from collections import namedtuple
from enum import Enum
from datasette.utils.asgi import Request
import graphene
import urllib
import sqlite_utils
import wrapt
TableMetadata = namedtuple(
"TableMetadata", ("columns", "foreign_keys", "fks_back", "pks", "supports_fts")
)
class Bytes(graphene.Scalar):
# Replace with graphene.Base64 after graphene 3.0 is released
@staticmethod
def serialize(value):
if isinstance(value, bytes):
return b64encode(value).decode("utf-8")
return value
@classmethod
def parse_literal(cls, node):
if isinstance(node, ast.StringValue):
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if isinstance(value, bytes):
return value
else:
return b64decode(value)
types = {
str: graphene.String(),
float: graphene.Float(),
int: graphene.Int(),
bytes: Bytes(),
}
class PageInfo(graphene.ObjectType):
endCursor = graphene.String()
hasNextPage = graphene.Boolean()
async def schema_for_database(datasette, database=None, tables=None):
db = datasette.get_database(database)
hidden_tables = await db.hidden_table_names()
# Perform all introspection in a single call to the execute_fn thread
def introspect_tables(conn):
db = sqlite_utils.Database(conn)
table_names = db.table_names()
view_names = db.view_names()
table_metadata = {}
for table in table_names + view_names:
columns = db[table].columns_dict
foreign_keys = []
pks = []
supports_fts = False
fks_back = []
if hasattr(db[table], "foreign_keys"):
# Views don't have this
foreign_keys = db[table].foreign_keys
pks = db[table].pks
supports_fts = bool(db[table].detect_fts())
# Gather all foreign keys pointing back here
collected = []
for t in db.tables:
collected.extend(t.foreign_keys)
fks_back = [f for f in collected if f.other_table == table]
table_metadata[table] = TableMetadata(
columns, foreign_keys, fks_back, pks, supports_fts
)
return table_metadata
table_metadata = await db.execute_fn(introspect_tables)
# Construct the tableFilter classes
table_filters = {
table: make_table_filter_class(table, meta.columns)
for table, meta in table_metadata.items()
}
# And the table_collection_kwargs
table_collection_kwargs = {}
for table, meta in table_metadata.items():
column_names = meta.columns.keys()
options = list(zip(column_names, column_names))
sort_enum = graphene.Enum.from_enum(Enum("{}Sort".format(table), options))
sort_desc_enum = graphene.Enum.from_enum(
Enum("{}SortDesc".format(table), options)
)
kwargs = dict(
filter=graphene.List(
table_filters[table],
description='Filters e.g. {name: {eq: "datasette"}}',
),
where=graphene.String(
description="Extra SQL where clauses, e.g. \"name='datasette'\""
),
first=graphene.Int(description="Number of results to return"),
after=graphene.String(
description="Start at this pagination cursor (from tableInfo { endCursor })"
),
sort=graphene.Argument(sort_enum, description="Sort by this column"),
sort_desc=graphene.Argument(
sort_desc_enum, description="Sort by this column descending"
),
)
if meta.supports_fts:
kwargs["search"] = graphene.String(description="Search for this term")
table_collection_kwargs[table] = kwargs
# For each table, expose a graphene.List
to_add = []
table_classes = {}
table_collection_classes = {}
for (
table,
(columns, foreign_keys, fks_back, pks, supports_fts),
) in table_metadata.items():
if table in hidden_tables:
continue
fks_by_column = {fk.column: fk for fk in foreign_keys}
# Create a node class for this table
table_dict = {}
if pks == ["rowid"]:
table_dict["rowid"] = graphene.Int()
column_names = []
for colname, coltype in columns.items():
column_names.append(colname)
if colname in fks_by_column:
fk = fks_by_column[colname]
table_dict[colname] = graphene.Field(
make_table_getter(table_classes, fk.other_table)
)
table_dict["resolve_{}".format(colname)] = make_fk_resolver(
db, table, table_classes, fk
)
else:
table_dict[colname] = types[coltype]
# Now add the backwards foreign key fields for related items
for fk in fks_back:
meta = table_metadata[fk.table]
fk_table_columns = meta.columns
filter_class = table_filters[fk.table]
table_dict["{}_list".format(fk.table)] = graphene.Field(
make_table_collection_getter(table_collection_classes, fk.table),
**table_collection_kwargs[fk.table]
)
table_dict["resolve_{}_list".format(fk.table)] = make_table_resolver(
datasette,
db.name,
fk.table,
table_classes,
meta.supports_fts,
default_where="[{}] = ".format(fk.column)
+ "{root."
+ fk.other_column
+ "}",
)
table_node_class = type(table, (graphene.ObjectType,), table_dict)
table_classes[table] = table_node_class
# We also need a table collection class - this is the thing with the
# nodes, edges, pageInfo and totalCount fields for that table
table_collection_class = make_table_collection_class(
table, table_node_class, pks
)
table_collection_classes[table] = table_collection_class
to_add.append(
(
table,
graphene.Field(
table_collection_class, **table_collection_kwargs[table]
),
)
)
to_add.append(
(
"resolve_{}".format(table),
make_table_resolver(
datasette, db.name, table, table_classes, supports_fts
),
)
)
# *_get field
table_get_kwargs = dict(table_collection_kwargs[table])
table_get_kwargs.pop("first")
# Add an argument for each primary key
for pk in pks:
if pk == "rowid" and pk not in columns:
pk_column_type = graphene.Int()
else:
pk_column_type = types[columns[pk]]
table_get_kwargs[pk] = pk_column_type
to_add.append(
(
"{}_get".format(table),
graphene.Field(table_node_class, **table_get_kwargs),
)
)
to_add.append(
(
"resolve_{}_get".format(table),
make_table_resolver(
datasette,
db.name,
table,
table_classes,
supports_fts,
pk_args=pks,
return_first_row=True,
),
)
)
Query = type(
"Query", (graphene.ObjectType,), {key: value for key, value in to_add},
)
return graphene.Schema(
query=Query,
auto_camelcase=(datasette.plugin_config("datasette-graphql") or {}).get(
"auto_camelcase", False
),
)
def make_table_collection_class(table, table_class, pks):
class _Edge(graphene.ObjectType):
cursor = graphene.String()
node = graphene.Field(table_class)
class Meta:
name = "{}Edge".format(table)
class _TableCollection(graphene.ObjectType):
totalCount = graphene.Int()
pageInfo = graphene.Field(PageInfo)
nodes = graphene.List(table_class)
edges = graphene.List(_Edge)
def resolve_totalCount(parent, info):
return parent["filtered_table_rows_count"]
def resolve_nodes(parent, info):
return parent["rows"]
def resolve_edges(parent, info):
return [
{"cursor": path_from_row_pks(row, pks, use_rowid=not pks), "node": row}
for row in parent["rows"]
]
def resolve_pageInfo(parent, info):
return {
"endCursor": parent["next"],
"hasNextPage": parent["next"] is not None,
}
class Meta:
name = "{}Collection".format(table)
return _TableCollection
class StringOperations(graphene.InputObjectType):
exact = graphene.String(name="eq", description="Exact match")
not_ = graphene.String(name="not", description="Not exact match")
contains = graphene.String(description="String contains")
endswith = graphene.String(description="String ends with")
startswith = graphene.String(description="String starts with")
gt = graphene.String(description="is greater than")
gte = graphene.String(description="is greater than or equal to")
lt = graphene.String(description="is less than")
lte = graphene.String(description="is less than or equal to")
like = graphene.String(description=r"is like (% for wildcards)")
notlike = graphene.String(description="is not like")
glob = graphene.String(description="glob matches (* for wildcards)")
in_ = graphene.List(graphene.String, name="in", description="in this list")
notin = graphene.List(graphene.String, description="not in this list")
arraycontains = graphene.String(description="JSON array contains this value")
date = graphene.String(description="Value is a datetime on this date")
isnull = graphene.Boolean(description="Value is null")
notnull = graphene.Boolean(description="Value is not null")
isblank = graphene.Boolean(description="Value is null or blank")
notblank = graphene.Boolean(description="Value is not null or blank")
class IntegerOperations(graphene.InputObjectType):
exact = graphene.Int(name="eq", description="Exact match")
not_ = graphene.Int(name="not", description="Not exact match")
gt = graphene.Int(description="is greater than")
gte = graphene.Int(description="is greater than or equal to")
lt = graphene.Int(description="is less than")
lte = graphene.Int(description="is less than or equal to")
in_ = graphene.List(graphene.Int, name="in", description="in this list")
notin = graphene.List(graphene.Int, description="not in this list")
arraycontains = graphene.Int(description="JSON array contains this value")
isnull = graphene.Boolean(description="Value is null")
notnull = graphene.Boolean(description="Value is not null")
isblank = graphene.Boolean(description="Value is null or blank")
notblank = graphene.Boolean(description="Value is not null or blank")
types_to_operations = {
str: StringOperations,
int: IntegerOperations,
float: IntegerOperations,
}
def make_table_filter_class(table, columns):
return type(
"{}Filter".format(table),
(graphene.InputObjectType,),
{
column: (types_to_operations.get(column_type) or StringOperations)()
for column, column_type in columns.items()
},
)
class DatasetteSpecialConfig(wrapt.ObjectProxy):
def config(self, key):
if key == "suggest_facets":
return False
return self.__wrapped__.config(key)
def make_table_resolver(
datasette,
database_name,
table_name,
table_classes,
supports_fts,
default_where=None,
pk_args=None,
return_first_row=False,
):
from datasette.views.table import TableView
async def resolve_table(
root,
info,
filter=None,
where=None,
first=None,
after=None,
search=None,
sort=None,
sort_desc=None,
**kwargs
):
if first is None:
first = 10
if return_first_row:
first = 1
pairs = []
for filter_ in filter or []:
for column_name, operations in filter_.items():
for operation_name, value in operations.items():
if isinstance(value, list):
value = ",".join(value)
pairs.append(
[
"{}__{}".format(column_name, operation_name.rstrip("_")),
value,
]
)
if pk_args is not None:
for pk in pk_args:
if kwargs.get(pk) is not None:
pairs.append([pk, kwargs[pk]])
qs = {}
qs.update(pairs)
if after:
qs["_next"] = after
qs["_size"] = first
if search and supports_fts:
qs["_search"] = search
if where:
qs["_where"] = where
if sort:
qs["_sort"] = sort
elif sort_desc:
qs["_sort_desc"] = sort_desc
if default_where:
qs["_where"] = default_where.format(root=root)
path_with_query_string = "/{}/{}.json?{}".format(
database_name, table_name, urllib.parse.urlencode(qs)
)
request = Request.fake(path_with_query_string)
view = TableView(DatasetteSpecialConfig(datasette))
data, _, _ = await view.data(
request, database=database_name, hash=None, table=table_name, _next=after
)
klass = table_classes[table_name]
data["rows"] = [klass(**dict(r)) for r in data["rows"]]
if return_first_row:
try:
return data["rows"][0]
except IndexError:
return None
else:
return data
return resolve_table
def make_fk_resolver(db, table, table_classes, fk):
async def resolve_foreign_key(parent, info):
# retrieve the correct column from parent
pk = getattr(parent, fk.column)
sql = "select * from [{}] where [{}] = :v".format(
fk.other_table, fk.other_column
)
params = {"v": pk}
results = await db.execute(sql, params)
fk_class = table_classes[fk.other_table]
try:
return [fk_class(**dict(row)) for row in results.rows][0]
except IndexError:
return None
return resolve_foreign_key
def make_table_getter(table_classes, table):
def getter():
return table_classes[table]
return getter
def make_table_collection_getter(table_collection_classes, table):
def getter():
return table_collection_classes[table]
return getter
def path_from_row_pks(row, pks, use_rowid, quote=True):
""" Generate an optionally URL-quoted unique identifier
for a row from its primary keys."""
if use_rowid:
bits = [row.rowid]
else:
bits = [getattr(row, pk) for pk in pks]
if quote:
bits = [urllib.parse.quote_plus(str(bit)) for bit in bits]
else:
bits = [str(bit) for bit in bits]
return ",".join(bits)