File tree Expand file tree Collapse file tree 1 file changed +9
-50
lines changed Expand file tree Collapse file tree 1 file changed +9
-50
lines changed Original file line number Diff line number Diff line change @@ -125,7 +125,9 @@ def sorting_clauses(self):
125
125
if not self ._top :
126
126
return ""
127
127
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
+ )
129
131
)
130
132
if clause :
131
133
clause = f" ORDER BY { clause } "
@@ -964,61 +966,18 @@ def _flatten_attribute_list(primary_key, attrs):
964
966
:param attrs: list of attribute names, which may include "KEY", "KEY DESC" or "KEY ASC"
965
967
:return: generator of attributes where "KEY" is replaced with its component attributes
966
968
"""
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
-
1014
969
for a in attrs :
1015
970
if re .match (r"^\s*KEY(\s+[aA][Ss][Cc])?\s*$" , a ):
1016
971
if primary_key :
1017
972
yield from primary_key
1018
973
elif re .match (r"^\s*KEY\s+[Dd][Ee][Ss][Cc]\s*$" , a ):
1019
974
if primary_key :
1020
975
yield from (q + " DESC" for q in primary_key )
1021
- elif re .match (keyword_pattern , a , re .I ):
1022
- yield f"`{ a } `"
1023
976
else :
1024
977
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
You can’t perform that action at this time.
0 commit comments