From 3b77ff8064a87213d72b14c0f7acdd77c88c05c4 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Fri, 18 May 2018 12:33:23 +0200 Subject: [PATCH] Support PyTables expressions using the new parser --- datasette_pytables/__init__.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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: -- 2.39.5