-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.py
55 lines (43 loc) · 1.67 KB
/
validator.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
import sqlparse
from select_validator import *
from from_validator import *
from where_validator import *
def ValidateQuery(query):
#Remove Duplicated Spaces and spaces from start and end
query = " ".join(query.split())
#check for empty string-------------------------
length_query = len(query)
if length_query == 0:
return 'T', ''
#Check for the ; in the end----------------------
if query[length_query - 1] != ';':
return 'Error: Invalid Syntax ; is not present', ''
'''
#Add space before ; if not present
if(query[length_query - 2] != ' '):
query = query[:length_query - 1] + ' ' + query[length_query - 1:]
#Parsing query
parsed_list = query.split()
'''
query = query.strip(';')
query = sqlparse.format(query, keyword_case='upper')
query = query.encode('UTF8') #Encode query from Unicode
query = query.split('FROM')
length_query = len(query)
#For valid query (length > 1) in all other cases invalid query
if length_query <= 1:
return 'Error: Invalid Syntax', ''
#Check for 'SELECT' and elements between 'SELECT' and 'FROM'-------------------------
result, select_query = SelectValidation(query)
if result != True:
return result, ''
#Check for elements between 'FROM' and 'WHERE'--------------------------
result, from_query = FromValidation(query)
if result != True:
return result, ''
#Check for elements after 'WHERE'-------------------------------
result, where_query = WhereValidation(query)
if result != True:
return result, ''
parsed_list = [select_query, from_query, where_query]
return True, parsed_list