-
Notifications
You must be signed in to change notification settings - Fork 2k
/
db.py
462 lines (380 loc) · 14.1 KB
/
db.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
import sqlalchemy
import ckan.plugins as p
import json
import datetime
_pg_types = {}
_type_names = set()
_engines = {}
_iso_formats = ['%Y-%m-%d',
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%dT%H:%M:%S']
def _is_valid_field_name(name):
'''
Check that field name is valid:
* can't start with underscore
* can't contain double quote (")
'''
if name.startswith('_') or '"' in name:
return False
return True
def _get_engine(context, data_dict):
'Get either read or write engine.'
connection_url = data_dict['connection_url']
engine = _engines.get(connection_url)
if not engine:
engine = sqlalchemy.create_engine(connection_url, echo=True)
_engines[connection_url] = engine
return engine
def _cache_types(context):
if not _pg_types:
connection = context['connection']
results = connection.execute(
'select oid, typname from pg_type;'
)
for result in results:
_pg_types[result[0]] = result[1]
_type_names.add(result[1])
def _get_type(context, oid):
_cache_types(context)
return _pg_types[oid]
def _guess_type(field):
'Simple guess type of field, only allowed are integer, numeric and text'
data_types = set([int, float])
for data_type in list(data_types):
try:
data_type(field)
except (TypeError, ValueError):
data_types.discard(data_type)
if not data_types:
break
if int in data_types:
return 'integer'
elif float in data_types:
return 'numeric'
##try iso dates
for format in _iso_formats:
try:
datetime.datetime.strptime(field, format)
return 'timestamp'
except ValueError:
continue
return 'text'
def _get_fields(context, data_dict):
fields = []
all_fields = context['connection'].execute(
'select * from "{0}" limit 1'.format(data_dict['resource_id'])
)
for field in all_fields.cursor.description:
if not field[0].startswith('_'):
fields.append({
'id': field[0],
'type': _get_type(context, field[1])
})
return fields
def json_get_values(obj, current_list=None):
if current_list is None:
current_list = []
if isinstance(obj, basestring):
current_list.append(obj)
if isinstance(obj, list):
for item in obj:
json_get_values(item, current_list)
if isinstance(obj, dict):
for item in dict.values():
json_get_values(item, current_list)
return current_list
def check_fields(context, fields):
'Check if field types are valid.'
_cache_types(context)
for field in fields:
if field.get('type') and not field['type'] in _type_names:
raise p.toolkit.ValidationError({
'fields': '{0} is not a valid field type'.format(field['type'])
})
elif not _is_valid_field_name(field['id']):
raise p.toolkit.ValidationError({
'fields': '{0} is not a valid field name'.format(field['id'])
})
def create_table(context, data_dict):
'Create table from combination of fields and first row of data.'
datastore_fields = [
{'id': '_id', 'type': 'serial primary key'},
{'id': '_full_text', 'type': 'tsvector'},
]
# check first row of data for additional fields
extra_fields = []
supplied_fields = data_dict.get('fields', [])
check_fields(context, supplied_fields)
field_ids = [field['id'] for field in data_dict.get('fields', [])]
records = data_dict.get('records')
# if type is field is not given try and guess or throw an error
for field in supplied_fields:
if 'type' not in field:
if not records or field['id'] not in records[0]:
raise p.toolkit.ValidationError({
'fields': '{} type not guessable'.format(field['id'])
})
field['type'] = _guess_type(records[0][field['id']])
if records:
# check record for sanity
if not isinstance(records[0], dict):
raise p.toolkit.ValidationError({
'records': 'The first row is not a json object'
})
supplied_field_ids = records[0].keys()
for field_id in supplied_field_ids:
if not field_id in field_ids:
extra_fields.append({
'id': field_id,
'type': _guess_type(records[0][field_id])
})
fields = datastore_fields + supplied_fields + extra_fields
sql_fields = ", ".join(['"{0}" {1}'.format(f['id'], f['type'])
for f in fields])
sql_string = 'create table "{0}" ({1});'.format(
data_dict['resource_id'],
sql_fields
)
context['connection'].execute(sql_string)
def alter_table(context, data_dict):
'''alter table from combination of fields and first row of data'''
supplied_fields = data_dict.get('fields', [])
current_fields = _get_fields(context, data_dict)
if not supplied_fields:
supplied_fields = current_fields
check_fields(context, supplied_fields)
field_ids = [field['id'] for field in supplied_fields]
records = data_dict.get('records')
new_fields = []
for num, field in enumerate(supplied_fields):
# check to see if field definition is the same or an
# extension of current fields
if num < len(current_fields):
if field['id'] != current_fields[num]['id']:
raise p.toolkit.ValidationError({
'fields': ('Supplied field "{}" not '
'present or in wrong order').format(field['id'])
})
## no need to check type as field already defined.
continue
if 'type' not in field:
if not records or field['id'] not in records[0]:
raise p.toolkit.ValidationError({
'fields': '{} type not guessable'.format(field['id'])
})
field['type'] = _guess_type(records[0][field['id']])
new_fields.append(field)
if records:
# check record for sanity
if not isinstance(records[0], dict):
raise p.toolkit.ValidationError({
'records': 'The first row is not a json object'
})
supplied_field_ids = records[0].keys()
for field_id in supplied_field_ids:
if not field_id in field_ids:
new_fields.append({
'id': field_id,
'type': _guess_type(records[0][field_id])
})
for field in new_fields:
sql = 'alter table "{}" add "{}" {}'.format(
data_dict['resource_id'],
field['id'],
field['type'])
context['connection'].execute(sql)
def insert_data(context, data_dict):
'''insert all data from records'''
if not data_dict.get('records'):
return
fields = _get_fields(context, data_dict)
field_names = [field['id'] for field in fields] + ['_full_text']
sql_columns = ", ".join(['"%s"' % name for name in field_names])
rows = []
## clean up and validate data
for num, record in enumerate(data_dict['records']):
# check record for sanity
if not isinstance(record, dict):
raise p.toolkit.ValidationError({
'records': 'row {} is not a json object'.format(num)
})
## check for extra fields in data
extra_keys = set(record.keys()) - set(field_names)
if extra_keys:
raise p.toolkit.ValidationError({
'records': 'row {} has extra keys "{}"'.format(
num,
', '.join(list(extra_keys))
)
})
full_text = []
row = []
for field in fields:
value = record.get(field['id'])
if isinstance(value, (dict, list)):
full_text.extend(json_get_values(value))
value = json.dumps(value)
elif field['type'].lower() == 'text' and value:
full_text.append(value)
row.append(value)
row.append(' '.join(full_text))
rows.append(row)
sql_string = 'insert into "{0}" ({1}) values ({2});'.format(
data_dict['resource_id'],
sql_columns,
', '.join(['%s' for field in field_names])
)
context['connection'].execute(sql_string, rows)
def _where(field_ids, data_dict):
'Return a SQL WHERE clause from data_dict filters and q'
filters = data_dict.get('filters', {})
if not isinstance(filters, dict):
raise p.toolkit.ValidationError({
'filters': 'Not a json object'}
)
where_clauses = []
values = []
for field, value in filters.iteritems():
if field not in field_ids:
raise p.toolkit.ValidationError({
'filters': 'field "{}" not in table'}
)
where_clauses.append('"{}" = %s'.format(field))
values.append(value)
q = data_dict.get('q')
if q:
where_clauses.append('_full_text @@ to_tsquery(\'{}\')'.format(q))
where_clause = ' and '.join(where_clauses)
if where_clause:
where_clause = 'where ' + where_clause
return where_clause, values
def delete_data(context, data_dict):
fields = _get_fields(context, data_dict)
field_ids = set([field['id'] for field in fields])
where_clause, where_values = _where(field_ids, data_dict)
context['connection'].execute(
'delete from "{}" {}'.format(
data_dict['resource_id'],
where_clause
),
where_values
)
def search_data(context, data_dict):
all_fields = _get_fields(context, data_dict)
all_field_ids = set([field['id'] for field in all_fields])
fields = data_dict.get('fields')
if fields:
check_fields(context, fields)
field_ids = set([field['id'] for field in fields])
for field in field_ids:
if not field in all_field_ids:
raise p.toolkit.ValidationError({
'fields': 'field "{}" not in table'.format(field)}
)
else:
fields = all_fields
field_ids = all_field_ids
select_columns = ', '.join(field_ids)
where_clause, where_values = _where(all_field_ids, data_dict)
limit = data_dict.get('limit', 100)
offset = data_dict.get('offset', 0)
if data_dict.get('sort'):
sort = 'order by {}'.format(data_dict['sort'])
else:
sort = ''
sql_string = '''select {}, count(*) over() as full_count
from "{}" {} {} limit {} offset {}'''\
.format(select_columns, data_dict['resource_id'], where_clause,
sort, limit, offset)
results = context['connection'].execute(sql_string, where_values)
results = [r for r in results]
if results:
data_dict['total'] = results[0]['full_count']
else:
data_dict['total'] = 0
records = [(dict((f, r[f]) for f in field_ids)) for r in results]
data_dict['records'] = records
return data_dict
def create(context, data_dict):
'''
The first row will be used to guess types not in the fields and the
guessed types will be added to the headers permanently.
Consecutive rows have to conform to the field definitions.
rows can be empty so that you can just set the fields.
fields are optional but needed if you want to do type hinting or
add extra information for certain columns or to explicitly
define ordering.
eg: [{"id": "dob", "type": "timestamp"},
{"id": "name", "type": "text"}]
A header items values can not be changed after it has been defined
nor can the ordering of them be changed. They can be extended though.
Any error results in total failure! For now pass back the actual error.
Should be transactional.
'''
engine = _get_engine(context, data_dict)
context['connection'] = engine.connect()
# close connection at all cost.
try:
# check if table already existes
trans = context['connection'].begin()
result = context['connection'].execute(
'select * from pg_tables where tablename = %s',
data_dict['resource_id']
).fetchone()
if not result:
create_table(context, data_dict)
else:
alter_table(context, data_dict)
insert_data(context, data_dict)
trans.commit()
return data_dict
except:
trans.rollback()
raise
finally:
context['connection'].close()
def delete(context, data_dict):
engine = _get_engine(context, data_dict)
context['connection'] = engine.connect()
try:
# check if table existes
trans = context['connection'].begin()
result = context['connection'].execute(
'select * from pg_tables where tablename = %s',
data_dict['resource_id']
).fetchone()
if not result:
raise p.toolkit.ValidationError({
'resource_id': 'table for resource {0} does not exist'.format(
data_dict['resource_id'])
})
if not 'filters' in data_dict:
context['connection'].execute(
'drop table "{}"'.format(data_dict['resource_id'])
)
else:
delete_data(context, data_dict)
trans.commit()
return data_dict
except:
trans.rollback()
raise
finally:
context['connection'].close()
def search(context, data_dict):
engine = _get_engine(context, data_dict)
context['connection'] = engine.connect()
try:
# check if table existes
result = context['connection'].execute(
'select * from pg_tables where tablename = %s',
data_dict['resource_id']
).fetchone()
if not result:
raise p.toolkit.ValidationError({
'resource_id': 'table for resource {0} does not exist'.format(
data_dict['resource_id'])
})
return search_data(context, data_dict)
finally:
context['connection'].close()