Skip to content

Commit

Permalink
identifier() should quote if uppercase characters
Browse files Browse the repository at this point in the history
Previously the identifier() function wouldn't quote the identifier
solely because it contained uppercase characters. The new behaviour of
quoting if there are any uppercase characters is intended to be more in
line with what people expect. It's also easier to recreate the old
behaviour by calling lower() on the input beforehand, than it is to try
and create the new behaviour with the old function.
  • Loading branch information
tlocke committed Aug 15, 2024
1 parent 9945228 commit 91b0094
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/pg8000/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,17 +772,21 @@ def make_params(py_types, values):
return tuple([make_param(py_types, v) for v in values])


def _quote_letter(c):
return c.isupper() if c.isalpha() else True


def identifier(sql):
if not isinstance(sql, str):
raise InterfaceError("identifier must be a str")

if len(sql) == 0:
raise InterfaceError("identifier must be > 0 characters in length")

quote = not sql[0].isalpha()
quote = _quote_letter(sql[0])

for c in sql[1:]:
if not (c.isalpha() or c.isdecimal() or c in "_$"):
if _quote_letter(c) and c not in "0123456789_$":
if c == "\u0000":
raise InterfaceError(
"identifier cannot contain the code zero character"
Expand Down
6 changes: 5 additions & 1 deletion test/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ def test_identifier_quoted_null():
(" Table", '" Table"'),
("A Table", '"A Table"'),
('A " Table', '"A "" Table"'),
("Table$", "Table$"),
("table$", "table$"),
("Table$", '"Table$"'),
("tableఐ", "tableఐ"), # Unicode character 0C10 which is uncased
("table", "table"),
("tAble", '"tAble"'),
],
)
def test_identifier_success(value, expected):
Expand Down

0 comments on commit 91b0094

Please sign in to comment.