X-Git-Url: https://git.jsancho.org/?p=datasette-pytables.git;a=blobdiff_plain;f=datasette_pytables%2F__init__.py;h=094710b517ee27d50dbd2341952b4144fa2d082e;hp=e8d0a6ccb4519f72d2e0680e0c3b4b8f44dc2574;hb=3b77ff8064a87213d72b14c0f7acdd77c88c05c4;hpb=2cf7538022a4290be1cdbe6f8cf13e44f8190bee;ds=sidebyside diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py index e8d0a6c..094710b 100644 --- a/datasette_pytables/__init__.py +++ b/datasette_pytables/__init__.py @@ -37,7 +37,19 @@ def _parse_sql(sql, params): for param in params: sql = sql.replace(":" + param, param) - parsed = parse(sql) + try: + parsed = parse(sql) + except: + # Propably it's a PyTables expression + for token in ['group by', 'order by', 'limit']: + res = re.search('(?i)where (.*)' + token, sql) + if res: + modified_sql = re.sub('(?i)where (.*)(' + token + ')', '\g<2>', sql) + parsed = parse(modified_sql) + parsed['where'] = res.group(1) + break + + # Always a list of fields if type(parsed['select']) is not list: parsed['select'] = [parsed['select']] @@ -113,11 +125,10 @@ class Connection: return expr if 'where' in parsed_sql: - try: + if type(parsed_sql['where']) is dict: query = _translate_where(parsed_sql['where']) - except: - # Probably it's a PyTables query - query = str(parsed_sql['where'])[6:] # without where keyword + else: + query = parsed_sql['where'] # Limit number of rows if 'limit' in parsed_sql: