Skip to content

Commit 50e1d40

Browse files
author
James William Pye
committed
Narrow quote_ident's scope.
This allows identifiers that contain "_"'s and non-leading decimals to not require quotation. foo_bar -> foo_bar _uscore -> _uscore foo1 -> foo1 1foo -> "1foo"
1 parent 05b4803 commit 50e1d40

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

postgresql/string.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ def escape_ident(text):
3030

3131
def quote_ident(text):
3232
"""
33-
If needed, replace every instance of " with "" *and* place " on each end
34-
Otherwise, just return the text.
33+
If needed, replace every instance of '"' with '""' *and* place '"' on each
34+
end. Otherwise, just return the text.
3535
"""
36-
if not text.isalpha():
36+
if not text or text[0].isdecimal() or (
37+
not text.replace('_', 'a').isalnum()
38+
):
3739
return '"' + text.replace('"', '""') + '"'
3840
return text
3941

postgresql/test/test_string.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,26 @@ def test_quotes(self):
226226
pg_str.quote_literal("""\\foo'bar\\"""),
227227
"""'\\foo''bar\\'"""
228228
)
229+
self.failUnlessEqual(
230+
pg_str.quote_ident("foo"),
231+
"foo"
232+
)
233+
self.failUnlessEqual(
234+
pg_str.quote_ident("0foo"),
235+
'"0foo"'
236+
)
237+
self.failUnlessEqual(
238+
pg_str.quote_ident("foo0"),
239+
'foo0'
240+
)
241+
self.failUnlessEqual(
242+
pg_str.quote_ident("_"),
243+
'_'
244+
)
245+
self.failUnlessEqual(
246+
pg_str.quote_ident("_9"),
247+
'_9'
248+
)
229249
self.failUnlessEqual(
230250
pg_str.quote_ident('''\\foo'bar\\'''),
231251
'''"\\foo'bar\\"'''

0 commit comments

Comments
 (0)