-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_parser.py
63 lines (48 loc) · 1.42 KB
/
query_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# from constants import *
# import re
# query = '''
# SELECT * FROM table1 WHERE column1 = 1 AND column2 >= 2 OR column3 = 3
# '''
# filter = reservedWords+operators+brackets
# split_pattern = re.compile(r'\s+|,|\(|\)')
# tokens = re.split(split_pattern, query)
# string_pattern = re.compile(r'[a-zA-Z]+')
# number_pattern = re.compile(r'\d+')
# strings =[]
# numbers = []
# for token in tokens:
# if token not in filter:
# if re.match(string_pattern, token):
# strings.append(token)
# elif re.match(number_pattern, token):
# numbers.append(token)
# print(strings)
# print(numbers)
from sqlglot import Tokenizer,TokenType
from pprint import pprint
def tokenize(query):
tokenizer = Tokenizer()
lex = tokenizer.tokenize(query)
tokens= []
#print(lex)
for token in lex:
#print(token)
element = {
'token': token.text,
'start': token.start,
'end': token.end,
}
if token.token_type == TokenType.VAR:
element['type'] = 'variable'
elif token.token_type == TokenType.NUMBER:
element['type'] = 'number'
elif token.token_type == TokenType.STRING:
element['type'] = 'string'
tokens.append(element)
return tokens
if __name__ == "__main__":
query = '''
SELECT COUNT(*) FROM table1;
'''
tokens=tokenize(query)
pprint(tokens)