|
3 | 3 | import re |
4 | 4 | import simplejson as json |
5 | 5 | from markupsafe import Markup |
6 | | -import sqlalchemy as sa |
| 6 | +from sqlalchemy import text as sql_text |
7 | 7 |
|
8 | 8 | from typing import Union, Dict, Tuple, Any |
9 | 9 | from ckan.types import Response |
|
50 | 50 |
|
51 | 51 | from io import BytesIO |
52 | 52 |
|
| 53 | +# use import incase of ckan.datastore.sqlsearch.enabled = False |
| 54 | +from ckanext.datastore.logic.action import datastore_search_sql |
53 | 55 | from ckanext.datastore.backend import DatastoreBackend |
54 | | -from ckanext.datastore.backend.postgres import DatastorePostgresqlBackend |
| 56 | +from ckanext.datastore.backend.postgres import ( |
| 57 | + DatastorePostgresqlBackend, |
| 58 | + identifier as sql_identifier |
| 59 | +) |
55 | 60 |
|
56 | 61 | from ckanapi import NotFound, LocalCKAN |
57 | 62 |
|
@@ -306,34 +311,43 @@ def template(dataset_type: str, lang: str, owner_org: str) -> Response: |
306 | 311 |
|
307 | 312 | append_data(book, record_data, chromo) |
308 | 313 |
|
| 314 | + resource_names = dict((r['id'], r['name']) for r in dataset['resources']) |
309 | 315 | ds_info = lc.action.datastore_info(id=resource['id']) |
310 | 316 | if 'foreignkeys' in ds_info['meta']: |
311 | 317 | for fk in ds_info['meta']['foreignkeys']: |
| 318 | + f_chromo = None |
312 | 319 | foreign_constraints_sql = None |
313 | 320 | if resource['id'] == fk['child_table']: |
314 | | - foreign_constraints_sql = sa.text(''' |
315 | | - SELECT * FROM "{0}" parent |
316 | | - JOIN "{1}" child ON {2} |
317 | | - ORDER BY parent._id; |
318 | | - '''.format(fk['parent_table'], |
319 | | - fk['child_table'], |
320 | | - ' AND '.join(['parent.{0} = child.{1}'.format(fk_c, fk['child_columns'][fk_i]) |
321 | | - for fk_i, fk_c in enumerate(fk['parent_columns'])]))) |
| 321 | + f_chromo = get_chromo(resource_names[fk['parent_table']]) |
| 322 | + foreign_constraints_sql = sql_text(''' |
| 323 | + SELECT parent.* FROM {0} parent |
| 324 | + JOIN {1} child ON {2} |
| 325 | + ORDER BY parent._id |
| 326 | + '''.format(sql_identifier(fk['parent_table']), |
| 327 | + sql_identifier(fk['child_table']), |
| 328 | + ' AND '.join( |
| 329 | + ['parent.{0} = child.{1}'.format( |
| 330 | + fk_c, fk['child_columns'][fk_i]) |
| 331 | + for fk_i, fk_c in enumerate( |
| 332 | + fk['parent_columns'])]))) |
322 | 333 | elif resource['id'] == fk['parent_table']: |
323 | | - foreign_constraints_sql = sa.text(''' |
324 | | - SELECT * FROM "{0}" child |
325 | | - JOIN "{1}" parent ON {2} |
326 | | - ORDER BY child._id; |
327 | | - '''.format(fk['child_table'], |
328 | | - fk['parent_table'], |
329 | | - ' AND '.join(['child.{0} = parent.{1}'.format(fk_c, fk['parent_columns'][fk_i]) |
330 | | - for fk_i, fk_c in enumerate(fk['child_columns'])]))) |
331 | | - if foreign_constraints_sql is not None: |
332 | | - # TODO: get results and chromo and put them into the workbook |
333 | | - continue |
334 | | - # foreign_constraints_results = connection.execute(foreign_constraints_sql) |
335 | | - for result in foreign_constraints_results.fetchall(): |
336 | | - continue |
| 334 | + f_chromo = get_chromo(resource_names[fk['child_table']]) |
| 335 | + foreign_constraints_sql = sql_text(''' |
| 336 | + SELECT child.* FROM {0} child |
| 337 | + JOIN {1} parent ON {2} |
| 338 | + ORDER BY child._id |
| 339 | + '''.format(sql_identifier(fk['child_table']), |
| 340 | + sql_identifier(fk['parent_table']), |
| 341 | + ' AND '.join( |
| 342 | + ['child.{0} = parent.{1}'.format( |
| 343 | + fk_c, fk['parent_columns'][fk_i]) |
| 344 | + for fk_i, fk_c in enumerate( |
| 345 | + fk['child_columns'])]))) |
| 346 | + if foreign_constraints_sql is not None and f_chromo is not None: |
| 347 | + results = datastore_search_sql( |
| 348 | + {'ignore_auth': True}, {'sql': str(foreign_constraints_sql)}) |
| 349 | + if results: |
| 350 | + append_data(book, results['records'], f_chromo) |
337 | 351 |
|
338 | 352 | blob = BytesIO() |
339 | 353 | book.save(blob) |
|
0 commit comments