diff options
-rw-r--r-- | tests/issues/test_issue_66.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/issues/test_issue_66.py b/tests/issues/test_issue_66.py new file mode 100644 index 0000000..0b9c97c --- /dev/null +++ b/tests/issues/test_issue_66.py @@ -0,0 +1,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()
\ No newline at end of file |