From 745f744d01ebe1eff321fb70c580eac1f8b3ad4f Mon Sep 17 00:00:00 2001
From: Javier Sancho <jsf@jsancho.org>
Date: Mon, 26 Oct 2020 17:57:40 +0100
Subject: [PATCH] custom pytables sql

---
 datasette_pytables/__init__.py | 16 ++++++++++++++--
 tests/test_api.py              | 14 +++++++-------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py
index a730cb7..9dc2a09 100644
--- a/datasette_pytables/__init__.py
+++ b/datasette_pytables/__init__.py
@@ -22,6 +22,8 @@ class PyTablesConnector(dc.Connector):
         'lte': '<=',
         'and': '&',
         'or': '|',
+        'binary_and': '&',
+        'binary_or': '|',
     }
 
     def table_names(self):
@@ -127,12 +129,22 @@ class PyTablesConnector(dc.Connector):
                 start = int(params['p0']) + 1
             else:
                 left, right = where[operator]
-                if left in params:
+
+                if isinstance(left, dict):
+                    left = "(" + _translate_where(left) + ")"
+                elif left in params:
                     _cast_param(right, left)
+
+                if isinstance(right, dict):
+                    right = "(" + _translate_where(right) + ")"
                 elif right in params:
                     _cast_param(left, right)
 
-                expr = "{left} {operator} {right}".format(left=left, operator=self.operators.get(operator, operator), right=right)
+                expr = "{left} {operator} {right}".format(
+                    left=left,
+                    operator=self.operators.get(operator, operator),
+                    right=right,
+                )
 
             return expr
 
diff --git a/tests/test_api.py b/tests/test_api.py
index c1938b1..80e379c 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -88,20 +88,20 @@ def test_custom_complex_sql(app_client):
     response = app_client.get(
         '/test_tables.json?' + urlencode({
             'sql': 'select identity from [/group1/table1] where speed > 100 and idnumber < 55',
-            '_shape': 'objects'
+            '_shape': 'objects',
         }),
     )
     data = response.json
     assert {
         'sql': 'select identity from [/group1/table1] where speed > 100 and idnumber < 55',
-        'params': {}
+        'params': {},
     } == data['query']
     assert 4 == len(data['rows'])
     assert [
         {'identity': 'This is particle: 51'},
         {'identity': 'This is particle: 52'},
         {'identity': 'This is particle: 53'},
-        {'identity': 'This is particle: 54'}
+        {'identity': 'This is particle: 54'},
     ] == data['rows']
     assert ['identity'] == data['columns']
     assert 'test_tables' == data['database']
@@ -111,19 +111,19 @@ def test_custom_pytables_sql(app_client):
     response = app_client.get(
         '/test_tables.json?' + urlencode({
             'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
-            '_shape': 'objects'
-            }),
+            '_shape': 'objects',
+        }),
     )
     data = response.json
     assert {
         'sql': 'select identity from [/group1/table1] where (speed > 100) & (speed < 500)',
-        'params': {}
+        'params': {},
     } == data['query']
     assert 199 == len(data['rows'])
     assert [
         {'identity': 'This is particle: 51'},
         {'identity': 'This is particle: 52'},
-        {'identity': 'This is particle: 53'}
+        {'identity': 'This is particle: 53'},
     ] == data['rows'][:3]
     assert ['identity'] == data['columns']
     assert 'test_tables' == data['database']
-- 
2.39.5