Initial import of the Guide contributed to the project byRobert Kraske. Was originall...
[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 {        
32                                         // We append the tokens to the buffer until it's a separator or group.
33                                         buffer.append(t.getValue());
34                                         groupBuffer.append(t.getValue());
35                                 }
36                         }
37                         String newCommand = buffer.toString().trim();
38                         if (!newCommand.equals("")) { //$NON-NLS-1$
39                                 commands.addElement(newCommand);
40                         }
41                 } catch (Throwable e) {
42                         e.printStackTrace();
43                 }
44                 Vector result = new Vector();
45                 result.addAll(groups);
46                 result.addAll(commands);
47                 return result;
48         }
49 }