-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseQuery.java
More file actions
410 lines (387 loc) · 11.9 KB
/
Copy pathParseQuery.java
File metadata and controls
410 lines (387 loc) · 11.9 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package quest;
/*
* This file contains function to parse query and find predicates involved in query.
* It also creates JList for those predicates.
*/
import java.util.StringTokenizer;
import java.util.Vector;
import java.sql.*;
import javax.swing.*;
public class ParseQuery
{
JScrollPane listScrollPanel;
/*
* This function takes query string as input.
* It parses query and finds base and join predicates.
*
* Call By: MainFrame.java
*/
boolean parseQuery(String queryInput, AllObjects allObjects)
{
ConnectDB connectDBObj = allObjects.getConnectDBObj();
String query = queryInput.trim();
int fromIndex = query.indexOf("from"); //fromIndex will point to 'f' alphabet of 'from' word.
int whereIndex = query.indexOf("where"); //whereIndex will point to 'w' alphabet of 'where' word.
/*
* relationNames will contain relation names separated by comma.
*/
String relationNames = query.substring(fromIndex+4, whereIndex); // fromIndex+4 will skip 'from' word.
StringTokenizer sToken = new StringTokenizer(relationNames, ",");
String relations[] = new String[sToken.countTokens()]; //relation names are stored in relations[] array
String relation_aliases[] = new String[sToken.countTokens()]; //relation names are stored in relations[] array
int totalRelations = 0; // total relations count
while(sToken.hasMoreTokens())
{
//relations[totalRelations++] = (sToken.nextToken()).trim();
String currentRelation = (sToken.nextToken()).trim();
String alias = "";
if(currentRelation.contains(" ")) { // handling relation aliases
alias = currentRelation.substring(currentRelation.indexOf(' '), currentRelation.length());
currentRelation = currentRelation.substring(0, currentRelation.indexOf(' '));
}
relations[totalRelations] = currentRelation;
relation_aliases[totalRelations++] = alias;
}
/*
* vector stores attributes for each relation
*/
Vector<String> attributeNames[] = new Vector[totalRelations];
/*
* Attributes name are fetched from pg_stats relation.
*/
try
{
Connection con = connectDBObj.connection;
Statement st = con.createStatement();
for(int i=0;i<totalRelations;i++)
{
attributeNames[i] = new Vector<String>();
/*
* Reading attributes name from pg_stats
*/
ResultSet rs = st.executeQuery("select attname from pg_stats where tablename = '"+relations[i]+"'");
boolean isRelationNameValid = false;
while(rs.next())
{
String attribute = rs.getString(1);
attributeNames[i].add(attribute);
isRelationNameValid = true;
}
rs.close();
if(!isRelationNameValid) //This condition gets true when relation does not exists in pg_stats.
{
JOptionPane.showMessageDialog(new JFrame(),
"\""+relations[i]+"\" relation does not exists",
"Warning",
JOptionPane.WARNING_MESSAGE);
return(false);
}
}
}
catch(Exception e)
{
System.out.println("Execption in parse Query: "+e);
e.printStackTrace();
}
/*
* predicateString contains predicates of string joined with 'and', 'or'.
*/
String predicateString = query.substring(whereIndex+5);
predicateString = predicateString.replaceAll(" and ", ",");
predicateString = predicateString.replaceAll(" or ", ",");
/*
* At this point, all 'and' and 'or' in predicate string are replaced by ','(comma).
*/
/*
* following string tokenizer separates different predicates and stores in predicates vector.
*/
sToken = new StringTokenizer(predicateString, ",");
Vector<String> predicates = new Vector<String>();
while(sToken.hasMoreTokens())
{
String str = sToken.nextToken().trim();
if(str.length()>0)
predicates.add(str);
}
/*
* last predicate might contain group by or order by clause.
* So that clause is handled differently.
*/
String lastPredicate = predicates.get(predicates.size()-1);
String modifiedLastPredicate;
int orderByIndex = lastPredicate.indexOf("order ");
int groupByIndex = lastPredicate.indexOf("group ");
/*
* if contains then order by and group by are removed.
*/
if(orderByIndex!=-1 || groupByIndex!=-1)
{
if(orderByIndex != -1 && orderByIndex<groupByIndex)
{
modifiedLastPredicate = lastPredicate.substring(0, orderByIndex).trim();
}
else if(orderByIndex == -1)
{
modifiedLastPredicate = lastPredicate.substring(0, groupByIndex).trim();
}
else if(groupByIndex == -1)
{
modifiedLastPredicate = lastPredicate.substring(0, orderByIndex).trim();
}
else
{
modifiedLastPredicate = lastPredicate.substring(0, groupByIndex).trim();
}
predicates.remove(predicates.size()-1);
predicates.add(modifiedLastPredicate);
}
/*
* Here baseConditions contains base conditions for each relation separated by ',' (comma).
*/
String baseConditions[] = new String[totalRelations];
/*
* joinConditions contain join conditions.
* joinConditions[i][j] contains join condition for i and j relations.
*/
String joinConditions[][] = new String[totalRelations][totalRelations];
for(int i=0;i<totalRelations;i++)
{
baseConditions[i] = "";
for(int j=0;j<totalRelations;j++)
{
joinConditions[i][j]="";
}
}
for(int i=0;i<predicates.size();i++)
{
String currentPredicate = predicates.get(i);
boolean parseSuccess = false;
sToken = new StringTokenizer(currentPredicate, "<>!=");
String lValue = null;
String rValue = null;
if(sToken.hasMoreTokens()) {
lValue = sToken.nextToken().trim();
if(lValue.contains(".")) // to handle aliases
lValue = lValue.substring(lValue.indexOf('.') + 1, lValue.length());
}
if(sToken.hasMoreTokens()) {
rValue = sToken.nextToken().trim();
if(rValue.contains(".")) // to handle aliases
rValue = rValue.substring(rValue.indexOf('.') + 1, rValue.length());
}
if(rValue != null)
{
/*
* Following condition is true when attribute is compared with a string/date or number.
* It is a base condition.
*/
if(rValue.indexOf('\'') != -1 || rValue.matches("-?\\d+"))
{
int j;
for(j=0;j<totalRelations;j++)
{
if(attributeNames[j].contains(lValue))
{
break;
}
}
if(j<totalRelations)
{
baseConditions[j] += currentPredicate+", ";
}
else
{
/*if lvalue contains more than an attribute name -- e.g. substring(c_name from 1 for 4) -- many things other than c_name are also present
handling for substring specifically*/
if(lValue.contains("substring") || lValue.contains("("))
{
StringTokenizer tokenSubString = new StringTokenizer(lValue," ()");
String ctoken = tokenSubString.nextToken().trim(); /* first token should be 'substring' */
if(ctoken.equalsIgnoreCase("substring"))
{
ctoken = tokenSubString.nextToken(); /* second token should be an attribute name -- check this */
for(j=0;j<totalRelations;j++)
{
if(attributeNames[j].contains(ctoken))
{
break;
}
}
if(j<totalRelations) /* it was found to be an attribute name */
{
ctoken = tokenSubString.nextToken().trim();
if(ctoken.equalsIgnoreCase("from"))
{
ctoken = tokenSubString.nextToken().trim(); /* intentional -- this one should an integer */
ctoken = tokenSubString.nextToken().trim();
if(ctoken.equalsIgnoreCase("for"))
{
ctoken = tokenSubString.nextToken().trim(); /* this also should an integer */
if(!tokenSubString.hasMoreTokens())
{
baseConditions[j] += currentPredicate+", ";
parseSuccess = true;
}
}
}
}
}
else // for the case (r_name
{
for(j=0;j<totalRelations;j++)
{
if(attributeNames[j].contains(ctoken))
{
break;
}
}
if(j<totalRelations) /* it was found to be an attribute name */
{
baseConditions[j] += currentPredicate+", ";
parseSuccess = true;
}
}
}
if(!parseSuccess)
{
// //attribute is not found
JOptionPane.showMessageDialog(new JFrame(),
"\""+lValue+"\" invalid function on attribute",
"Warning",
JOptionPane.WARNING_MESSAGE);
return(false);
}
}
}
else
{
int j;
for(j=0;j<totalRelations;j++)
{
if(attributeNames[j].contains(lValue))
{
break;
}
}
int firstRelation = 0;
if(j<totalRelations)
firstRelation = j;
else
{
//Attribute is not found
JOptionPane.showMessageDialog(new JFrame(),
"\""+lValue+"\" attribute does not exists",
"Warning",
JOptionPane.WARNING_MESSAGE);
return(false);
}
for(j=0;j<totalRelations;j++)
{
if(attributeNames[j].contains(rValue))
{
break;
}
}
int secondRelation = 0;
if(j<totalRelations)
secondRelation = j;
else
{
//attribute is not found
JOptionPane.showMessageDialog(new JFrame(),
"\""+rValue+"\" attribute does not exists",
"Warning",
JOptionPane.WARNING_MESSAGE);
return(false);
}
/*
* following condition is true for predicates like
* attribute1 = attribute2 and both attributes are of same relation.
*/
if(firstRelation == secondRelation)
baseConditions[firstRelation] += currentPredicate+", ";
/*
* otherwise predicate is join predicate.
*/
else if(firstRelation<secondRelation)
{
joinConditions[firstRelation][secondRelation] += currentPredicate+", ";
}
else
{
joinConditions[secondRelation][firstRelation] += currentPredicate+", ";
}
}
}
/*
* 'like' or 'not like' case.
*/
else
{
int indexSpace = currentPredicate.indexOf(" ");
lValue = currentPredicate.substring(0, indexSpace).trim();
int j;
for(j=0;j<totalRelations;j++)
{
if(attributeNames[j].contains(lValue))
{
break;
}
}
if(j<totalRelations)
baseConditions[j] += currentPredicate+", ";
else
{
JOptionPane.showMessageDialog(new JFrame(),
"\""+lValue+"\" attribute does not exists",
"Warning",
JOptionPane.WARNING_MESSAGE);
return(false);
}
}
}
BouquetData bouquetDataObj = allObjects.getBouquetDataObj();
bouquetDataObj.setBaseConditions(baseConditions);
bouquetDataObj.setBaseRelationNames(relations);
listScrollPanel = getList(relations, baseConditions, joinConditions, totalRelations, predicates.size());
return(true);
}
/*
* This function creates JList of all predicates
*/
JScrollPane getList(String relations[], String baseConditions[], String joinConditions[][], int totalRelations, int totalPreidicates)
{
JScrollPane listScrollPane;
JList<String> errorPronePredicateList;
String str[] = new String[totalPreidicates];
int count = 0;
for(int i=0;i<totalRelations;i++)
{
if(baseConditions[i]!="")
{
str[count] = relations[i] +" (base conditions): "+baseConditions[i];
count++;
}
}
for(int i=0;i<totalRelations;i++)
{
for(int j=0;j<totalRelations;j++)
{
if(joinConditions[i][j]!="")
{
str[count] = relations[i] +", "+relations[j]+" (join condition): "+joinConditions[i][j];
count++;
}
}
}
String listValues[] = new String[count];
for(int i=0;i<count;i++)
{
listValues[i] = str[i];
}
errorPronePredicateList = new JList<String>(listValues);
listScrollPane = new JScrollPane(errorPronePredicateList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// listScrollPane.setPreferredSize(new Dimension(400,400));
return(listScrollPane);
}
}