1 package com.quantum.sql;
3 import java.util.Vector;
5 import com.quantum.sql.parser.SQLLexx;
6 import com.quantum.sql.parser.Token;
8 public class SQLParser {
11 * Parses a query string into executable units.
13 * @return a Vector of Strings, with the individual executable units.
15 public static Vector parse(String query) {
16 Vector commands = new Vector();
17 Vector groups = new Vector();
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) {
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);
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
42 groupBuffer = new StringBuffer();
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());
49 String newCommand = buffer.toString().trim();
50 if (!newCommand.equals("")) { //$NON-NLS-1$
51 commands.addElement(newCommand);
53 } catch (Throwable e) {
56 System.out.println("Returning"); //$NON-NLS-1$
57 Vector result = new Vector();
58 result.addAll(groups);
59 result.addAll(commands);