-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_parsing.py
More file actions
139 lines (138 loc) · 5.65 KB
/
Copy pathsql_parsing.py
File metadata and controls
139 lines (138 loc) · 5.65 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def parse_sql(in_str) :
# pre-format string literals and comma-separated argument lists.
in_str = in_str.replace('<', ' < ').replace('>', ' > ').replace('=', ' = ')
in_list = in_str.replace(',', ' , ').replace('\'', ' \' ').replace('(', ' ( ').replace(')', ' ) ').split()
str_formatted_list = []
i = 0
while i < len(in_list) :
if in_list[i] == '\'' :
str_list = []
i += 1
while i < len(in_list) and in_list[i] != '\'' :
str_list.append(in_list[i])
i += 1
if i >= len(in_list) :
return ['ERROR']
str_formatted_list.append(' '.join(str_list))
else :
str_formatted_list.append(in_list[i])
i += 1
formatted_list = []
i = 0
while i < len(str_formatted_list) :
if i+1 < len(str_formatted_list) and str_formatted_list[i+1] == ',' :
param_list = []
while i+1 < len(str_formatted_list) and str_formatted_list[i+1] == ',' :
param_list.append(str_formatted_list[i])
i += 2
if i >= len(str_formatted_list) :
return ['ERROR']
param_list.append(str_formatted_list[i])
formatted_list.append(param_list)
else :
formatted_list.append(str_formatted_list[i])
i += 1
# deal with each statement type separately.
if formatted_list[0].upper() == 'SELECT' : # select:
if type(formatted_list[1]) == list :
p_params = formatted_list[1]
else :
p_params = [formatted_list[1]]
if formatted_list[2].upper() != 'FROM' :
return ['ERROR']
if type(formatted_list[3]) == list :
c_params = formatted_list[3]
else :
c_params = [formatted_list[3]]
s_params = []
if 'where' in formatted_list or 'WHERE' in formatted_list :
if 'where' in formatted_list :
i = formatted_list.index('where')
else :
i = formatted_list.index('WHERE')
i += 1
while i < len(formatted_list) and formatted_list[i].upper() != 'ORDER' :
s_params.append(formatted_list[i])
i += 1
o_params = []
if 'order' in formatted_list or 'ORDER' in formatted_list :
if 'order' in formatted_list :
i = formatted_list.index('order')
else :
i = formatted_list.index('ORDER')
if i+1 >= len(formatted_list) or formatted_list[i+1].upper() != 'BY' :
return ['ERROR']
if i+3 < len(formatted_list) and formatted_list[i+3].upper() == 'DESC' :
o_params.append('DESC')
else :
o_params.append('ASC')
if i+2 >= len(formatted_list) :
return ['ERROR']
if type(formatted_list[i+2]) == list :
o_params.append(formatted_list[i+2])
else :
o_params.append([formatted_list[i+2]])
return ['SELECT', p_params, c_params, s_params, o_params]
elif formatted_list[0].upper() == 'INSERT' : # insert into:
if formatted_list[1].upper() != 'INTO' :
return ['ERROR']
c_params = formatted_list[2]
p_params = []
i = 3
if formatted_list[3] == '(' and formatted_list[5] == ')':
if type(formatted_list[4]) == list :
p_params = formatted_list[4]
else :
p_params = [formatted_list[4]]
i = 6
if formatted_list[i].upper() != 'VALUES' or formatted_list[i+1] != '(' or formatted_list[i+3] != ')' :
return ['ERROR']
if type(formatted_list[i+2]) == list :
in_params = formatted_list[i+2]
else :
in_params = [formatted_list[i+2]]
if p_params and len(p_params) != len(in_params):
return ['ERROR']
return ['INSERT INTO', c_params, p_params, in_params]
elif formatted_list[0].upper() == 'DELETE' : # delete:
if formatted_list[1].upper() != 'FROM' :
return ['ERROR']
c_params = formatted_list[2]
s_params = []
if 'where' in formatted_list or 'WHERE' in formatted_list :
if 'where' in formatted_list :
i = formatted_list.index('where')
else :
i = formatted_list.index('WHERE')
i += 1
while i < len(formatted_list) :
s_params.append(formatted_list[i])
i += 1
return ['DELETE', c_params, s_params]
elif formatted_list[0].upper() == 'UPDATE' : # update:
c_params = formatted_list[1]
if formatted_list[2].upper() != 'SET' :
return ['ERROR']
p_params = []
i = 3
while i < len(str_formatted_list) and str_formatted_list[i].upper() != 'WHERE' :
param_list = []
while i < len(str_formatted_list) and str_formatted_list[i].upper() != 'WHERE' and str_formatted_list[i] != ',':
param_list.append(str_formatted_list[i])
i += 1
p_params.append(param_list)
if (str_formatted_list[i].upper() != 'WHERE') :
i += 1
s_params = []
if 'where' in formatted_list or 'WHERE' in formatted_list :
if 'where' in formatted_list :
i = formatted_list.index('where')
else :
i = formatted_list.index('WHERE')
i += 1
while i < len(formatted_list) :
s_params.append(formatted_list[i])
i += 1
return ['UPDATE', c_params, p_params, s_params]
else :
return ['ERROR']