Skip to content

Commit 5bc57bd

Browse files
committed
Fix parsing error with dollar-quoted procedure bodies (fixes issue83).
1 parent 6e8276a commit 5bc57bd

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Development Version
22
-------------------
33

4+
Bug Fixes
5+
* Fix parsing error with dollar-quoted procedure bodies (issue83).
6+
47
Other
58
* py3k fixes (by vthriller).
69
* py3k fixes in setup.py (by Florian Bauer).

sqlparse/lexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class Lexer(object):
177177
(r'CASE\b', tokens.Keyword), # extended CASE(foo)
178178
(r"`(``|[^`])*`", tokens.Name),
179179
(r"´(´´|[^´])*´", tokens.Name),
180-
(r'\$([^\W\d_]\w*)?\$', tokens.Name.Builtin),
180+
(r'\$([^\W\d]\w*)?\$', tokens.Name.Builtin),
181181
(r'\?{1}', tokens.Name.Placeholder),
182182
(r'[$:?%]\w+', tokens.Name.Placeholder),
183183
# FIXME(andi): VALUES shouldn't be listed here

tests/test_parse.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,24 @@ def test_quoted_identifier():
113113
assert isinstance(t[2], sqlparse.sql.Identifier)
114114
assert t[2].get_name() == 'z'
115115
assert t[2].get_real_name() == 'y'
116+
117+
118+
def test_psql_quotation_marks(): # issue83
119+
# regression: make sure plain $$ work
120+
t = sqlparse.split("""
121+
CREATE OR REPLACE FUNCTION testfunc1(integer) RETURNS integer AS $$
122+
....
123+
$$ LANGUAGE plpgsql;
124+
CREATE OR REPLACE FUNCTION testfunc2(integer) RETURNS integer AS $$
125+
....
126+
$$ LANGUAGE plpgsql;""")
127+
assert len(t) == 2
128+
# make sure $SOMETHING$ works too
129+
t = sqlparse.split("""
130+
CREATE OR REPLACE FUNCTION testfunc1(integer) RETURNS integer AS $PROC_1$
131+
....
132+
$PROC_1$ LANGUAGE plpgsql;
133+
CREATE OR REPLACE FUNCTION testfunc2(integer) RETURNS integer AS $PROC_2$
134+
....
135+
$PROC_2$ LANGUAGE plpgsql;""")
136+
assert len(t) == 2

tests/test_regressions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,26 @@ def _get_identifier(sql):
134134
for func_name, result in results:
135135
func = getattr(i, func_name)
136136
assert func() == result
137+
138+
139+
def test_issue83():
140+
sql = """
141+
CREATE OR REPLACE FUNCTION func_a(text)
142+
RETURNS boolean LANGUAGE plpgsql STRICT IMMUTABLE AS
143+
$_$
144+
BEGIN
145+
...
146+
END;
147+
$_$;
148+
149+
CREATE OR REPLACE FUNCTION func_b(text)
150+
RETURNS boolean LANGUAGE plpgsql STRICT IMMUTABLE AS
151+
$_$
152+
BEGIN
153+
...
154+
END;
155+
$_$;
156+
157+
ALTER TABLE..... ;"""
158+
t = sqlparse.split(sql)
159+
assert len(t) == 3

0 commit comments

Comments
 (0)