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