sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

Mysql # comment compliance

Open udit19524 opened this issue 4 years ago • 3 comments

Issue:

  • Mysql allows # as a comment for all newline comments
  • comments are not striped and the query does not split as well when #<not_space comments> are encountered.
  • No space after # and a backtick is not working in harmony to split the sql queries as well

Here are a few examples which can help you recreate the issue:

ideal Scenario with no backtick and space after #:

sql = """# comments1
select `id`
from test_user;
select `id`
from test_user2; """
print("------------comment strip-------------")
query_list = sqlparse.format(sql, strip_comments=True).strip()
print(query_list)

query_list = sqlparse.split(query_list)
print("------------split_list -------------")
print(query_list)```

output:

```------------comment strip-------------
select `id`
from test_user;
select `id`
from test_user2;
------------split_list -------------
['select `id`\nfrom test_user;', 'select `id`\nfrom test_user2;']```

 Scenario with no backtick and no space after #:

```import sqlparse 
sql = """#comments1
select `id`
from test_user;
select `id`
from test_user2; """
print("------------comment strip-------------")
query_list = sqlparse.format(sql, strip_comments=True).strip()
print(query_list)

query_list = sqlparse.split(query_list)
print("------------split_list -------------")
print(query_list)```

output:
```#comments1
select `id`
from test_user;
select `id`
from test_user2;
------------split_list -------------
['#comments1\nselect `id`\nfrom test_user;', 'select `id`\nfrom test_user2;']```

 Scenario with  backtick and no space after #:
```import sqlparse 
sql = """#`comments1`
select `id`
from test_user;
select `id`
from test_user2; """
print("------------comment strip-------------")
query_list = sqlparse.format(sql, strip_comments=True).strip()
print(query_list)

query_list = sqlparse.split(query_list)
print("------------split_list -------------")
print(query_list)```

output:
```------------comment strip-------------
#`comments1`
select `id`
from test_user;
select `id`
from test_user2;
------------split_list -------------
['#`comments1`\nselect `id`\nfrom test_user;\nselect `id`\nfrom test_user2;']```

As you can see when backticks are being introduced the split is not happening correctly based on ;



udit19524 avatar Dec 10 '20 20:12 udit19524

Right now that conflicts with the XOR operator in PostgreSQL (https://www.postgresql.org/docs/9.4/functions-bitstring.html). See also #539. I've added this issue to the list of issues that need to be taken into consideration when adding options to tell sqlparse which SQL dialect to use.

andialbrecht avatar Dec 12 '20 08:12 andialbrecht

That sounds great, do we have a timeline on it, currently I am handling it on my part but would love to see the package handle it as well

udit19524 avatar Dec 13 '20 02:12 udit19524

After my PR #693 it should be simple to deploy different syntaxes. What is missing is to expose this on the command line.

mrmasterplan avatar Nov 30 '22 16:11 mrmasterplan