1 package com.quantum.sql.parser;
3 import java.util.Vector;
6 public class SQLParser {
9 * Parses a query string into executable units.
11 * @return a Vector of Strings, with the individual executable units.
13 public static Vector parse(String query) {
14 Vector commands = new Vector();
15 Vector groups = new Vector();
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) {
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);
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
40 groupBuffer = new StringBuffer();
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());
47 String newCommand = buffer.toString().trim();
48 if (!newCommand.equals("")) { //$NON-NLS-1$
49 commands.addElement(newCommand);
51 } catch (Throwable e) {
54 Vector result = new Vector();
55 result.addAll(groups);
56 result.addAll(commands);