Skip to content

Instantly share code, notes, and snippets.

@james4388
Created June 19, 2020 22:44
Show Gist options
  • Save james4388/6ab6a5c2069f2345067b2c54fd0ff469 to your computer and use it in GitHub Desktop.
Save james4388/6ab6a5c2069f2345067b2c54fd0ff469 to your computer and use it in GitHub Desktop.
Clean up sql comment
def clean_comment(sql: str) -> str:
lines = sql.split('\n')
output = []
for line in lines:
escape = False
quote = False
i = 0
buffer = []
n = len(line)
while i < n:
if line[i] == '\\':
escape = not escape
elif not escape and line[i] == "'":
quote = not quote
elif (not escape and not quote) and line[i: i + 2] == '--':
break
else:
escape = False
buffer.append(line[i])
i += 1
output.append(''.join(buffer))
return '\n'.join(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment