forked from LBRYCommunity/lbry-sdk
SQL generation fix to handle IN operation for one value lists
This commit is contained in:
parent
1190a60c7a
commit
dcb1b64696
2 changed files with 23 additions and 7 deletions
|
@ -134,16 +134,20 @@ def constraints_to_sql(constraints, joiner=' AND ', prepend_key=''):
|
||||||
col, op = col[:-len('__not_like')], 'NOT LIKE'
|
col, op = col[:-len('__not_like')], 'NOT LIKE'
|
||||||
elif key.endswith('__in') or key.endswith('__not_in'):
|
elif key.endswith('__in') or key.endswith('__not_in'):
|
||||||
if key.endswith('__in'):
|
if key.endswith('__in'):
|
||||||
col, op = col[:-len('__in')], 'IN'
|
col, op, one_val_op = col[:-len('__in')], 'IN', '='
|
||||||
else:
|
else:
|
||||||
col, op = col[:-len('__not_in')], 'NOT IN'
|
col, op, one_val_op = col[:-len('__not_in')], 'NOT IN', '!='
|
||||||
if constraint:
|
if constraint:
|
||||||
if isinstance(constraint, (list, set, tuple)):
|
if isinstance(constraint, (list, set, tuple)):
|
||||||
keys = []
|
if len(constraint) == 1:
|
||||||
for i, val in enumerate(constraint):
|
values[f'{key}{tag}'] = constraint[0]
|
||||||
keys.append(f':{key}{tag}_{i}')
|
sql.append(f'{col} {one_val_op} :{key}{tag}')
|
||||||
values[f'{key}{tag}_{i}'] = val
|
else:
|
||||||
sql.append(f'{col} {op} ({", ".join(keys)})')
|
keys = []
|
||||||
|
for i, val in enumerate(constraint):
|
||||||
|
keys.append(f':{key}{tag}_{i}')
|
||||||
|
values[f'{key}{tag}_{i}'] = val
|
||||||
|
sql.append(f'{col} {op} ({", ".join(keys)})')
|
||||||
elif isinstance(constraint, str):
|
elif isinstance(constraint, str):
|
||||||
sql.append(f'{col} {op} ({constraint})')
|
sql.append(f'{col} {op} ({constraint})')
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -98,6 +98,12 @@ class TestQueryBuilder(unittest.TestCase):
|
||||||
'txo_name__in0_1': 'def456'
|
'txo_name__in0_1': 'def456'
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
self.assertTupleEqual(
|
||||||
|
constraints_to_sql({'txo.name__in': ('abc123',)}),
|
||||||
|
('txo.name = :txo_name__in0', {
|
||||||
|
'txo_name__in0': 'abc123',
|
||||||
|
})
|
||||||
|
)
|
||||||
self.assertTupleEqual(
|
self.assertTupleEqual(
|
||||||
constraints_to_sql({'txo.age__in': 'SELECT age from ages_table'}),
|
constraints_to_sql({'txo.age__in': 'SELECT age from ages_table'}),
|
||||||
('txo.age IN (SELECT age from ages_table)', {})
|
('txo.age IN (SELECT age from ages_table)', {})
|
||||||
|
@ -118,6 +124,12 @@ class TestQueryBuilder(unittest.TestCase):
|
||||||
'txo_name__not_in0_1': 'def456'
|
'txo_name__not_in0_1': 'def456'
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
self.assertTupleEqual(
|
||||||
|
constraints_to_sql({'txo.name__not_in': ('abc123',)}),
|
||||||
|
('txo.name != :txo_name__not_in0', {
|
||||||
|
'txo_name__not_in0': 'abc123',
|
||||||
|
})
|
||||||
|
)
|
||||||
self.assertTupleEqual(
|
self.assertTupleEqual(
|
||||||
constraints_to_sql({'txo.age__not_in': 'SELECT age from ages_table'}),
|
constraints_to_sql({'txo.age__not_in': 'SELECT age from ages_table'}),
|
||||||
('txo.age NOT IN (SELECT age from ages_table)', {})
|
('txo.age NOT IN (SELECT age from ages_table)', {})
|
||||||
|
|
Loading…
Reference in a new issue