1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
'''
Created on 10/06/2012
@author: piranna
'''
from unittest import main, TestCase
import sqlparse
class Issue_66(TestCase):
"""
Attempts to extract the column names from an SQL dump.
Demonstrates a bug in sqlparse.
"""
def test_issue(self):
# example sql, taken from a dump file I'm working with.
sql = r"""
CREATE TABLE `foo` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`user_id` smallint(6) DEFAULT NULL,
PRIMARY KEY (`id`),
)
"""
# first, parse the CREATE statement
statement, = sqlparse.parse(sql)
# the name of the table is followed by a pair of parentheses
# defining the column names and data types. sqlparse interprets
# this as a function.
function, = [tok for tok in statement.tokens \
if type(tok) is sqlparse.sql.Function]
# this 'function' has three tokens: the table name, a whitespace,
# and finally a parenthesis.
parenthesis = function.tokens[-1]
# BUG: sqlparse fails to isolate certain tokens of this parenthesis.
# for example, the name of the second table column `user_id` is lumped
# into an IdentifierList with the AUTO_INCREMENT keyword on the preceding
# line, rather than being recognized as another Name (as is the first
# column name in the list).
# to demonstrate this, print the tokens one by one.
# the error is visible on Line 9.
for i, token in enumerate(parenthesis.tokens):
print i, repr(token)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
main()
|