package com.quantum.sql; import java.util.Vector; import com.quantum.sql.parser.SQLLexx; import com.quantum.sql.parser.Token; public class SQLParser { /** * Parses a query string into executable units. * @param query * @return a Vector of Strings, with the individual executable units. */ public static Vector parse(String query) { Vector commands = new Vector(); Vector groups = new Vector(); try { Vector tokens = SQLLexx.parse(query); StringBuffer buffer = new StringBuffer(); StringBuffer groupBuffer = new StringBuffer(); for (int i = 0; i < tokens.size(); i++) { Token t = (Token) tokens.elementAt(i); if (t.getType() == Token.COMMENT) { // ignore comments } else if (t.getType() == Token.SEPARATOR) { groupBuffer.append(t.getValue()); String newCommand = buffer.toString().trim(); if (!newCommand.equals("")) { //$NON-NLS-1$ commands.addElement(newCommand); } buffer = new StringBuffer(); } else if (t.getType() == Token.GROUP) { // We don't append the group token because it may be misinterpreted //groupBuffer.append(t.getValue()); String newGroup = groupBuffer.toString().trim(); if (!newGroup.equals("")) { //$NON-NLS-1$ groups.addElement(newGroup); //Groups have precedence over commands, so the preceding commands are erased commands.clear(); } groupBuffer = new StringBuffer(); } else { // We append the tokens to the buffer until it's a separator or group. buffer.append(t.getValue()); groupBuffer.append(t.getValue()); } } String newCommand = buffer.toString().trim(); if (!newCommand.equals("")) { //$NON-NLS-1$ commands.addElement(newCommand); } } catch (Throwable e) { e.printStackTrace(); } System.out.println("Returning"); //$NON-NLS-1$ Vector result = new Vector(); result.addAll(groups); result.addAll(commands); return result; } }