Skip to content

Commit 091e444

Browse files
committed
always escape
1 parent 69c2e97 commit 091e444

File tree

1 file changed

+9
-50
lines changed

1 file changed

+9
-50
lines changed

datajoint/expression.py

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def sorting_clauses(self):
125125
if not self._top:
126126
return ""
127127
clause = ", ".join(
128-
_flatten_attribute_list(self.primary_key, self._top.order_by)
128+
_wrap_attributes(
129+
_flatten_attribute_list(self.primary_key, self._top.order_by)
130+
)
129131
)
130132
if clause:
131133
clause = f" ORDER BY {clause}"
@@ -964,61 +966,18 @@ def _flatten_attribute_list(primary_key, attrs):
964966
:param attrs: list of attribute names, which may include "KEY", "KEY DESC" or "KEY ASC"
965967
:return: generator of attributes where "KEY" is replaced with its component attributes
966968
"""
967-
sql_keywords = [
968-
"SELECT",
969-
"FROM",
970-
"WHERE",
971-
"AND",
972-
"OR",
973-
"INSERT",
974-
"INTO",
975-
"VALUES",
976-
"UPDATE",
977-
"SET",
978-
"DELETE",
979-
"CREATE",
980-
"TABLE",
981-
"ALTER",
982-
"DROP",
983-
"INDEX",
984-
"JOIN",
985-
"LEFT",
986-
"RIGHT",
987-
"INNER",
988-
"OUTER",
989-
"UNION",
990-
"GROUP",
991-
"BY",
992-
"HAVING",
993-
"ORDER",
994-
"ASC",
995-
"DESC",
996-
"LIMIT",
997-
"OFFSET",
998-
"DISTINCT",
999-
"CASE",
1000-
"WHEN",
1001-
"THEN",
1002-
"ELSE",
1003-
"END",
1004-
"NULL",
1005-
"NOT",
1006-
"IN",
1007-
"LIKE",
1008-
"KEY",
1009-
]
1010-
keyword_pattern = (
1011-
r"\b(" + "|".join(re.escape(keyword) for keyword in sql_keywords) + r")\b"
1012-
)
1013-
1014969
for a in attrs:
1015970
if re.match(r"^\s*KEY(\s+[aA][Ss][Cc])?\s*$", a):
1016971
if primary_key:
1017972
yield from primary_key
1018973
elif re.match(r"^\s*KEY\s+[Dd][Ee][Ss][Cc]\s*$", a):
1019974
if primary_key:
1020975
yield from (q + " DESC" for q in primary_key)
1021-
elif re.match(keyword_pattern, a, re.I):
1022-
yield f"`{a}`"
1023976
else:
1024977
yield a
978+
979+
980+
def _wrap_attributes(attr):
981+
for entry in attr:
982+
wrapped_entry = re.sub(r"\b((?!asc|desc)\w+)\b", r"`\1`", entry, re.IGNORECASE)
983+
yield wrapped_entry # wrap attribute names in backquotes

0 commit comments

Comments
 (0)