Skip to content

Commit

Permalink
Simplify identifier() by always quoting.
Browse files Browse the repository at this point in the history
Previously we only quoted when it had to be.
  • Loading branch information
tlocke committed Aug 17, 2024
1 parent 8beefb7 commit bf7c974
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
23 changes: 4 additions & 19 deletions src/pg8000/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,33 +772,18 @@ 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 = _quote_letter(sql[0])
if "\u0000" in sql:
raise InterfaceError("identifier cannot contain the code zero character")

for c in sql[1:]:
if _quote_letter(c) and c not in "0123456789_$":
if c == "\u0000":
raise InterfaceError(
"identifier cannot contain the code zero character"
)
quote = True
break

if quote:
sql = sql.replace('"', '""')
return f'"{sql}"'
else:
return sql
sql = sql.replace('"', '""')
return f'"{sql}"'


def literal(value):
Expand Down
8 changes: 4 additions & 4 deletions test/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,14 @@ def test_identifier_quoted_null():
@pytest.mark.parametrize(
"value,expected",
[
("top_secret", "top_secret"),
("top_secret", '"top_secret"'),
(" 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ఐ"'), # Unicode character 0C10 which is uncased
("table", '"table"'),
("tAble", '"tAble"'),
],
)
Expand Down

0 comments on commit bf7c974

Please sign in to comment.