Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def removeSqlComments(source):
- result = []
- state = 0
- # states:
- # 0 - initial state
- # 1 - have single '-'
- # 2 - have double '-' (valid comment)
- # 3 - have single "'"
- # 4 - have single '\' inside quotes
- for char in source:
- result.append(char)
- if state == 0:
- if char == '-':
- state = 1
- elif char == "'":
- state = 3
- else:
- state = 0
- elif state == 1:
- if char == '-':
- state = 2
- result.pop()
- result.pop()
- else:
- state = 0
- elif state == 2:
- if char == '\n':
- state = 0
- else:
- result.pop()
- state = 2
- elif state == 3:
- if char == '\\':
- state = 4
- elif char == "'":
- state = 0
- else:
- state = 3
- elif state == 4:
- state = 3
- print ''.join(result)
- test1 = r"""
- SELECT * FROM Students -- here's comment1
- WHERE age > 20 and name like 'A%\\'--start of a comment'; -- also comment
- """
- removeSqlComments(test1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement