andialbrecht / sqlparse

A non-validating SQL parser module for Python
BSD 3-Clause "New" or "Revised" License
3.74k stars 697 forks source link

#comments without space after # ,get_type() is UNKNOWN #520

Open yangjiuyi opened 4 years ago

yangjiuyi commented 4 years ago

HI!


sql = """#comments
select * from test_user WHERE id= 1;"""
sql1 = sqlparse.parse(sql)
t = sqlparse.sql.Statement(sql1[0].tokens)
sql2 = sqlparse.format(sql,strip_comments=True)

print t.get_type()
print sql2

sqltest =  """# comments
select * from test_user WHERE id= 1;"""
sqltest1 = sqlparse.parse(sqltest)
t1 = sqlparse.sql.Statement(sqltest1[0].tokens)
sqltest2 = sqlparse.format(sqltest,strip_comments=True)

print t1.get_type()
print sqltest2

output

UNKNOWN

#comments
select * from test_user WHERE id= 1;
----------
SELECT
select * from test_user WHERE id= 1;

MySQL support #comments without space after # sqlparse cannt support #comments without space after #?

udit19524 commented 3 years ago

Do we have any update on this issue, comments are not stripped and the query does not split as well when # are encountered

udit19524 commented 3 years ago

example:

import sqlparse sql = """#comments1 select id from test_user; select id from test_user2; """ query_list = sqlparse.split(sql) print(query_list) ['#comments1\nselect id\nfrom test_user;\nselect id\nfrom test_user2;']

import sqlparse sql = """# comments1 select id from test_user; select id from test_user2; """ query_list = sqlparse.split(sql) print(query_list)

result: ['# comments1\nselect id\nfrom test_user;', 'select id\nfrom test_user2;']

No space after # and a backtick is not working in harmony to split the sql queries

andialbrecht commented 1 year ago

See #596 as well