Quadcap Embeddable Database

com/quadcap/sql/VectorExpression.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.Externalizable; 00042 import java.io.IOException; 00043 import java.io.ObjectInput; 00044 import java.io.ObjectOutput; 00045 00046 import java.util.Vector; 00047 00048 import java.sql.SQLException; 00049 00050 import antlr.RecognitionException; 00051 00052 import com.quadcap.sql.types.Value; 00053 00054 import com.quadcap.util.Debug; 00055 00056 /** 00057 * This might be a vector (row value constructor) or a cursor (table value 00058 * constructor) 00059 * 00060 * @author Stan Bailes 00061 */ 00062 public class VectorExpression extends TableExpression 00063 implements Externalizable 00064 { 00065 Vector expressions = new Vector(); 00066 int rank = 1; 00067 00068 public VectorExpression() {} 00069 00070 public void addElement(Expression expression) throws RecognitionException { 00071 if (expressions.size() == 0) { 00072 // determine rank 00073 rank = expression.rank() + 1; 00074 } else { 00075 if (expression.rank() != rank-1) { 00076 throw new RecognitionException( 00077 "Mismatched ranks in table expression"); 00078 } 00079 } 00080 expressions.addElement(expression); 00081 } 00082 00083 public int rank() { return rank; } 00084 00085 public boolean isUpdatable() { return false; } 00086 00087 public void getBaseTables(Vector v) {} 00088 00089 public int size() { return expressions.size(); } 00090 00091 public final Expression get(int i) { 00092 return (Expression)expressions.get(i); 00093 } 00094 00095 public Row getValues(Session session, Cursor cursor) throws SQLException { 00096 if (rank == 1) { 00097 Row values = new Row(); 00098 for (int i = 0; i < expressions.size(); i++) { 00099 Expression e = (Expression)expressions.elementAt(i); 00100 values.addElement(e.getValue(session, cursor)); 00101 } 00102 return values; 00103 } else if (rank == 2) { 00104 Expression e = (Expression)expressions.elementAt(0); 00105 return e.getValues(session, cursor); 00106 } 00107 return null; 00108 } 00109 00110 public Cursor getCursor(Session session, Cursor cursor) 00111 throws SQLException 00112 { 00113 if (rank == 1) { 00114 Vector v = new Vector(); 00115 v.addElement(getValues(session, cursor)); 00116 return new StaticCursor(session, v); 00117 } else if (rank == 2) { 00118 Vector cursorVec = new Vector(); 00119 for (int i = 0; i < expressions.size(); i++) { 00120 Expression e = (Expression)expressions.elementAt(i); 00121 cursorVec.addElement(e.getValues(session, cursor)); 00122 } 00123 return new StaticCursor(session, cursorVec); 00124 } else { 00125 return null; 00126 } 00127 } 00128 00129 public void invert() { 00130 throw new RuntimeException("Can't invert vector expression"); 00131 } 00132 00133 public void visitSubExpressions(ExpressionVisitor ev) { 00134 for (int i = 0; i < expressions.size(); i++) { 00135 Expression ex = (Expression)expressions.elementAt(i); 00136 ev.visit(ex); 00137 } 00138 } 00139 00140 public String toString() { 00141 StringBuffer sb = new StringBuffer(); 00142 for (int i = 0; i < expressions.size(); i++) { 00143 if (i > 0) sb.append(", "); 00144 sb.append(expressions.elementAt(i).toString()); 00145 } 00146 return sb.toString(); 00147 } 00148 00149 public void readExternal(ObjectInput in) 00150 throws IOException, ClassNotFoundException 00151 { 00152 expressions = (Vector)in.readObject(); 00153 } 00154 00155 public void writeExternal(ObjectOutput out) throws IOException { 00156 out.writeObject(expressions); 00157 } 00158 00159 /** 00160 * Return the (one-based) parameter from this VectorExpression which 00161 * is assumed to be a simple list of parameterized expressions 00162 */ 00163 public Value getParameter(Session session, int pos) { 00164 try { 00165 if (rank == 2) { 00166 VectorExpression v = (VectorExpression)expressions.get(0); 00167 Expression e = v.get(pos-1); 00168 return e.getValue(session, null); 00169 } 00170 } catch (Throwable t) {} 00171 return null; 00172 } 00173 00174 //#ifdef DEBUG 00175 public String name() { return "[]"; } 00176 //#endif 00177 }