Skip to content

Commit

Permalink
ASB-29242: Update reserved ADQL/SQL keywords (#28)
Browse files Browse the repository at this point in the history
* add new ADQL-2.1 keywords

* fix column name handling
  • Loading branch information
jwfraustro authored Oct 21, 2024
1 parent ff9d9a4 commit 4e8bdb6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "vo-models"
version = "0.4.1"
version = "0.4.2"
authors = [
{name = "Joshua Fraustro", email="[email protected]"},
{name = "MAST Archive Developers", email="[email protected]"}
Expand Down
18 changes: 18 additions & 0 deletions tests/vodataservice/vodataservice_models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ def test_write_to_xml(self):
canonicalize(self.test_xml, strip_text=True),
)

def test_reserved_adql_words(self):
"""Test that reserved ADQL/SQL words are properly escaped in column names"""

non_reserved = TableParam(column_name="aperture")
reserved_no_quotes = TableParam(column_name="distance")
reserved_single_quotes = TableParam(column_name="'offset'")
reserved_mixed_quotes = TableParam(column_name='"\'top\'"')

self.assertEqual(non_reserved.column_name, "aperture")
self.assertEqual(reserved_no_quotes.column_name, '"distance"')
self.assertEqual(reserved_single_quotes.column_name, '"offset"')
self.assertEqual(reserved_mixed_quotes.column_name, '"top"')

self.assertIn("<name>aperture</name>", non_reserved.to_xml(encoding=str))
self.assertIn("<name>\"distance\"</name>", reserved_no_quotes.to_xml(encoding=str))
self.assertIn("<name>\"offset\"</name>", reserved_single_quotes.to_xml(encoding=str))
self.assertIn("<name>\"top\"</name>", reserved_mixed_quotes.to_xml(encoding=str))


class TestTableElement(TestCase):
"""Test the Table element model"""
Expand Down
13 changes: 11 additions & 2 deletions vo_models/adql/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"CATALOG",
"CHAR",
"CHARACTER",
"CHARACTER_LENGTH",
"CHAR_LENGTH",
"CHARACTER_LENGTH",
"CHECK",
"CLOSE",
"COALESCE",
Expand Down Expand Up @@ -238,6 +238,7 @@
"ATAN2",
"CEILING",
"COS",
"COT",
"DEGREES",
"EXP",
"FLOOR",
Expand All @@ -252,7 +253,6 @@
"SIN",
"SQRT",
"TAN",
"TOP",
"TRUNCATE",
# ADQL geometry keywords
"AREA",
Expand All @@ -268,4 +268,13 @@
"POINT",
"POLYGON",
"REGION",
# Cast functions and datatypes
"BIGINT",
# String functions and operators
"ILIKE",
# Conversion functions
"IN_UNIT",
# Cardinality
"OFFSET",
"TOP",
]
3 changes: 2 additions & 1 deletion vo_models/vodataservice/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def validate_colname(cls, value: str):
value: - The column name to escape.
"""
if value.upper() in ADQL_SQL_KEYWORDS:
if value.strip("'\"").upper() in ADQL_SQL_KEYWORDS:
value = value.strip("'\"")
value = f'"{value}"'
return value

Expand Down

0 comments on commit 4e8bdb6

Please sign in to comment.