Skip to content

Commit 69c2e97

Browse files
committed
escape keywords
1 parent 163f8aa commit 69c2e97

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

datajoint/expression.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,12 +964,61 @@ def _flatten_attribute_list(primary_key, attrs):
964964
:param attrs: list of attribute names, which may include "KEY", "KEY DESC" or "KEY ASC"
965965
:return: generator of attributes where "KEY" is replaced with its component attributes
966966
"""
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+
9671014
for a in attrs:
9681015
if re.match(r"^\s*KEY(\s+[aA][Ss][Cc])?\s*$", a):
9691016
if primary_key:
9701017
yield from primary_key
9711018
elif re.match(r"^\s*KEY\s+[Dd][Ee][Ss][Cc]\s*$", a):
9721019
if primary_key:
9731020
yield from (q + " DESC" for q in primary_key)
1021+
elif re.match(keyword_pattern, a, re.I):
1022+
yield f"`{a}`"
9741023
else:
9751024
yield a

0 commit comments

Comments
 (0)