-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse_query.py
85 lines (82 loc) · 4.31 KB
/
parse_query.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#from pyparsing import Word, alphas, Forward, CaselessKeyword
from pyparsing import CaselessKeyword, delimitedList, Each, Forward, Group, \
Optional, Word, alphas,alphanums, nums, oneOf, ZeroOrMore, quotedString, \
Upcase, Keyword, Combine, Literal, Upcase
#tokens
binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True)
selectToken = Keyword("select", caseless=True);
selectMaxToken = Keyword("select max", caseless=True);
selectMinToken = Keyword("select min", caseless=True);
selectAvgToken = Keyword("select avg", caseless=True);
selectSpecialToken = selectMaxToken | selectMinToken | selectAvgToken
selectToken = Keyword("select", caseless=True);
fromToken = Keyword("from", caseless=True);
whereToken = Keyword("where", caseless=True);
insertToken = Keyword("insert into", caseless=True);
deleteToken = Keyword("delete from", caseless=True);
valuesToken = Keyword("values", caseless=True);
createToken = Keyword("create table", caseless=True);
truncateToken = Keyword("truncate table", caseless=True);
dropToken = Keyword("drop table", caseless=True);
exitToken = Keyword("exit", caseless=True);
intToken = Keyword("int", caseless=True);
and_ = Keyword("and", caseless=True);
or_ = Keyword("or", caseless=True);
table = column = Word(alphanums);
columns = Group(delimitedList(column));
table_column = Combine(Optional(table + ".") + column);
sumToken = Keyword("sum", caseless=True);
maxToken = Keyword("max", caseless=True);
minToken = Keyword("min", caseless=True);
avgToken = Keyword("avg", caseless=True);
distinctToken = Keyword("distinct", caseless=True);
table_columns = Group(delimitedList(table_column));
func_names = [sumToken, maxToken, minToken, avgToken, distinctToken];
specialToken = sumToken | maxToken | minToken | avgToken | distinctToken
func_table_column = Group(specialToken + '(' + table_column + ')');
tables = Group(delimitedList(table));
number = Word(nums)
plusorminus = Literal('+') | Literal('-')
intNum = Combine( Optional(plusorminus) + number )
intNums = Group(delimitedList(intNum));
columnL = columnR = table_column | intNum
whereCondition = Group(columnL + binop + columnR).setResultsName("cond");
whereExpression = Forward()
whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereCondition )
def parseQuery(queryString):
try:
parser = Forward();
# parser << (Word(alphas).setResultsName( "first" ) + \
# #(' ').setResultsName( "delim" ) + \
# '*' + Word(alphas).setResultsName( "second"))
# selectSpecialStmt = Forward.setResultsName("selectSpecialStmt");
# selectSpecialStmt << (selectSpecialToken + "(" + table_columns + ")" + fromToken \
# + table.setResultsName("table"));
selectStmt = Forward().setResultsName("selectStmt");
selectStmt << ( selectToken + ( '*' | func_table_column | table_columns).setResultsName( "columns" ) \
+ fromToken + tables.setResultsName("tables") \
+ Optional(whereToken + whereExpression.setResultsName("conds") ) );
deleteStmt = Forward().setResultsName("deleteStmt");
deleteStmt << ( deleteToken + table.setResultsName("table") \
+ whereToken + whereExpression.setResultsName("conds"));
insertStmt = Forward().setResultsName("insertStmt");
insertStmt << ( insertToken + table.setResultsName("table") + valuesToken \
+ "(" + intNums.setResultsName("intValues") + ")" );
createStmt = Forward().setResultsName("createStmt");
createStmt << ( createToken + table.setResultsName("table") + "(" \
+ Group(delimitedList(column + intToken)).setResultsName("fields") + ")" );
truncateStmt = Forward().setResultsName("truncateStmt");
truncateStmt << ( truncateToken + table.setResultsName("table"));
dropStmt = Forward().setResultsName("dropStmt");
dropStmt << ( dropToken + table.setResultsName("table"));
parser = selectStmt | insertStmt | deleteStmt | createStmt | truncateStmt | dropStmt | exitToken;
tokens = parser.parseString(queryString);
# import pdb; pdb.set_trace()
return tokens
except Exception as e:
# print e;
print "Error in format."
return [];
# print tokens.first
# print tokens.delim
# print tokens.second