f583afa6be3162346f42cb99bb41fd4168daba61
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / sql / SQLParser.java
1 package com.quantum.sql;
2
3 import java.util.Vector;
4
5 import com.quantum.sql.parser.SQLLexx;
6 import com.quantum.sql.parser.Token;
7
8 public class SQLParser {
9
10         /**
11          * Parses a query string into executable units.
12          * @param query
13          * @return a Vector of Strings, with the individual executable units.
14          */
15         public static Vector parse(String query) {
16                         Vector commands = new Vector();
17                         Vector groups = new Vector();
18                 try {
19                         Vector tokens = SQLLexx.parse(query);
20                         StringBuffer buffer = new StringBuffer();
21                         StringBuffer groupBuffer = new StringBuffer();
22                         for (int i = 0; i < tokens.size(); i++) {
23                                 Token t = (Token) tokens.elementAt(i);
24                                 if (t.getType() == Token.COMMENT) {
25                                         // ignore comments
26                                 } else if (t.getType() == Token.SEPARATOR) {
27                                         groupBuffer.append(t.getValue());
28                                         String newCommand = buffer.toString().trim();
29                                         if (!newCommand.equals("")) { //$NON-NLS-1$
30                                                 commands.addElement(newCommand);
31                                         }
32                                         buffer = new StringBuffer();
33                                 } else if (t.getType() == Token.GROUP) {
34                                         // We don't append the group token because it may be misinterpreted
35                                         //groupBuffer.append(t.getValue());
36                                         String newGroup = groupBuffer.toString().trim();
37                                         if (!newGroup.equals("")) { //$NON-NLS-1$
38                                                 groups.addElement(newGroup);
39                                                 //Groups have precedence over commands, so the preceding commands are erased
40                                                 commands.clear();
41                                         }
42                                         groupBuffer = new StringBuffer();
43                                 } else {        
44                                         // We append the tokens to the buffer until it's a separator or group.
45                                         buffer.append(t.getValue());
46                                         groupBuffer.append(t.getValue());
47                                 }
48                         }
49                         String newCommand = buffer.toString().trim();
50                         if (!newCommand.equals("")) { //$NON-NLS-1$
51                                 commands.addElement(newCommand);
52                         }
53                 } catch (Throwable e) {
54                         e.printStackTrace();
55                 }
56                 Vector result = new Vector();
57                 result.addAll(groups);
58                 result.addAll(commands);
59                 return result;
60         }
61 }