Skip to content

Commit 84725ee

Browse files
committed
Improve parsing of PEP249-style placeholder (fixes andialbrecht#103).
1 parent 4edfd2f commit 84725ee

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Enhancements
1313
* sqlparse.split() now removes leading and trailing whitespaces from splitted
1414
statements.
1515
* Add USE as keyword token (by mulos).
16+
* Improve parsing of PEP249-style placeholders (issue103).
1617

1718

1819
Release 0.1.7 (Apr 06, 2013)

sqlparse/lexer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class Lexer(object):
182182
(r"´(´´|[^´])*´", tokens.Name),
183183
(r'\$([^\W\d]\w*)?\$', tokens.Name.Builtin),
184184
(r'\?{1}', tokens.Name.Placeholder),
185+
(r'%\(\w+\)s', tokens.Name.Placeholder),
185186
(r'[$:?%]\w+', tokens.Name.Placeholder),
186187
# FIXME(andi): VALUES shouldn't be listed here
187188
# see https://github.com/andialbrecht/sqlparse/pull/64

tests/test_parse.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
"""Tests sqlparse function."""
44

5+
import pytest
6+
57
from tests.utils import TestCaseBase
68

79
import sqlparse
810
import sqlparse.sql
911

12+
from sqlparse import tokens as T
13+
1014

1115
class SQLParseTest(TestCaseBase):
1216
"""Tests sqlparse.parse()."""
@@ -139,3 +143,10 @@ def test_psql_quotation_marks(): # issue83
139143
....
140144
$PROC_2$ LANGUAGE plpgsql;""")
141145
assert len(t) == 2
146+
147+
148+
@pytest.mark.parametrize('ph', ['?', ':1', ':foo', '%s', '%(foo)s'])
149+
def test_placeholder(ph):
150+
p = sqlparse.parse(ph)[0].tokens
151+
assert len(p) == 1
152+
assert p[0].ttype is T.Name.Placeholder

0 commit comments

Comments
 (0)