Skip to content

Commit

Permalink
Fixes astropy#2094
Browse files Browse the repository at this point in the history
  • Loading branch information
at88mph committed Jul 4, 2022
1 parent 26618d1 commit ed8fe33
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
33 changes: 19 additions & 14 deletions astroquery/alma/tapsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,25 @@ def _gen_numeric_sql(field, value):

def _gen_str_sql(field, value):
result = ''
for interval in _val_parse(value, str):
if result:
result += ' OR '
if '*' in interval:
# use LIKE
# escape wildcards if they exists in the value
interval = interval.replace('%', r'\%') # noqa
interval = interval.replace('_', r'\_') # noqa
# ADQL wild cards are % and _
interval = interval.replace('*', '%')
interval = interval.replace('?', '_')
result += "{} LIKE '{}'".format(field, interval)
else:
result += "{}='{}'".format(field, interval)

# arrays are allowed and will be converted to IN() clauses.
if isinstance(value, list):
result += "{} IN ({})".format(field, ', '.join(["'{}'".format(val) for val in value]))
else:
for interval in _val_parse(value, str):
if result:
result += ' OR '
if '*' in interval:
# use LIKE
# escape wildcards if they exists in the value
interval = interval.replace('%', r'\%') # noqa
interval = interval.replace('_', r'\_') # noqa
# ADQL wild cards are % and _
interval = interval.replace('*', '%')
interval = interval.replace('?', '_')
result += "{} LIKE '{}'".format(field, interval)
else:
result += "{}='{}'".format(field, interval)
if ' OR ' in result:
# use brackets for multiple ORs
return '(' + result + ')'
Expand Down
8 changes: 8 additions & 0 deletions astroquery/alma/tests/test_alma.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ def test_gen_str_sql():
"(proposal_id LIKE '2012.%' OR proposal_id LIKE '2013._3%')"


def test_gen_array_sql():
common_select = "select * from ivoa.obscore WHERE "
test_keywords = ["High-mass star formation", "Disks around high-mass stars"]
assert _gen_sql({"spatial_resolution": "<0.1",
"science_keyword": test_keywords}) == common_select + \
"spatial_resolution<=0.1 AND science_keyword IN ('High-mass star formation', 'Disks around high-mass stars')"


def test_gen_datetime_sql():
common_select = 'select * from ivoa.obscore WHERE '
assert _gen_sql({'start_date': '01-01-2020'}) == common_select + \
Expand Down

0 comments on commit ed8fe33

Please sign in to comment.