/
db.py
320 lines (266 loc) · 10 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
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 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 [datetime.datetime.strptime(field, format){"id": "dob", "label": ""Date of Birth",
"type": "timestamp" ,"concept": "day"},
{"name": "some_stuff": ..].
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()