Quadcap Embeddable Database

com/quadcap/sql/ItemsCursor.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.Enumeration; 00047 import java.util.Hashtable; 00048 import java.util.Vector; 00049 00050 import java.sql.SQLException; 00051 00052 import com.quadcap.sql.types.Type; 00053 import com.quadcap.sql.types.TypeAny; 00054 import com.quadcap.sql.types.Value; 00055 00056 import com.quadcap.util.Debug; 00057 00058 /** 00059 * This cursor performs the name-mapping function associated with the 00060 * <code>SELECT</code> clause. 00061 * 00062 * @author Stan Bailes 00063 */ 00064 public class ItemsCursor extends CursorImpl { 00065 Cursor cursor; 00066 ItemsRow currentRow = null; 00067 int[] map = null; 00068 Vector items; 00069 00070 public ItemsCursor(Session session, Cursor cursor, Vector gitems) 00071 throws SQLException 00072 { 00073 super(session, cursor.getName()); 00074 this.session = session; 00075 this.cursor = cursor; 00076 this.items = expandWildCard(session, cursor, gitems); 00077 this.map = new int[items.size()]; 00078 for (int i = 0; i < items.size(); i++) { 00079 SelectItem item = (SelectItem)items.elementAt(i); 00080 Expression e = item.getExpression(); 00081 if (e instanceof NameExpression) { 00082 String name = ((NameExpression)e).getName(); 00083 String asName = item.getAsName(); 00084 if (asName == null) asName = name; 00085 Column col = cursor.getColumn(name); 00086 if (col == null) { 00087 //#ifdef DEBUG 00088 Debug.println(0, cursor.toString()); 00089 //#endif 00090 throw new SQLException("Bad column name: " + name, 00091 "42000"); 00092 } 00093 addColumn(new Column(asName, col)); 00094 map[i] = col.getColumn(); 00095 } else { 00096 String asName = item.getAsName(); 00097 if (asName == null) { 00098 asName = "Column" + (i+1); 00099 } 00100 addColumn(new Column(asName, e.getType(session, cursor))); 00101 map[i] = -1; 00102 } 00103 } 00104 } 00105 00106 final Vector expandWildCard(Session session, Cursor cursor, Vector items) 00107 throws SQLException 00108 { 00109 Vector nitems = new Vector(); 00110 for (int i = 0; i < items.size(); i++) { 00111 SelectItem item = (SelectItem)items.elementAt(i); 00112 Expression e = item.getExpression(); 00113 String name = "Column" + (i + 1); 00114 if (e instanceof NameExpression) { 00115 name = ((NameExpression)e).getName(); 00116 if (name.endsWith(".*")) { 00117 String rname = name.substring(0, name.length() - 2); 00118 String qname = 00119 session.getConnection().resolveName(rname) + "."; 00120 rname = rname + "."; 00121 boolean found = false; 00122 for (int j = 1; j <= cursor.getColumnCount(); j++) { 00123 Column col = cursor.getColumn(j); 00124 if (col.isJoinColumn()) continue; 00125 String sname = col.getShortName(); 00126 String cname = col.getName(); 00127 if (sname.startsWith(rname) || sname.startsWith(qname)) { 00128 found = true; 00129 nitems.addElement( 00130 new SelectItem(new NameExpression(sname))); 00131 } else if (cname.startsWith(qname) || cname.startsWith(rname)) { 00132 found = true; 00133 nitems.addElement( 00134 new SelectItem(new NameExpression(cname))); 00135 } 00136 } 00137 if (!found) { 00138 throw new SQLException("Bad column name: " + name, 00139 "42000"); 00140 } 00141 } else { 00142 nitems.addElement(item); 00143 } 00144 } else { 00145 nitems.addElement(item); 00146 } 00147 } 00148 return nitems; 00149 } 00150 00151 public Row getEmptyAggregate() throws SQLException { 00152 return new ItemsRow(session, items, null, null, map); 00153 } 00154 00155 public void updateRow(Row row) throws SQLException { 00156 for (int i = 1; i <= map.length; i++) { 00157 Value v = row.item(i); 00158 currentRow.set(i, v); 00159 } 00160 cursor.updateRow(currentRow.getBaseRow()); 00161 } 00162 00163 public void insertRow(Row row) throws SQLException { 00164 if (currentRow == null) { 00165 makeCurrentRow(); 00166 } 00167 for (int i = 1; i <= map.length; i++) { 00168 Value v = row.item(i); 00169 currentRow.set(i, v); 00170 } 00171 cursor.insertRow(currentRow.getBaseRow()); 00172 } 00173 00174 public void deleteRow() throws SQLException { 00175 cursor.deleteRow(); 00176 } 00177 00178 public boolean isWritable(int column) throws SQLException { 00179 return cursor.isWritable(map[column-1]); 00180 } 00181 00182 public void beforeFirst() throws SQLException { 00183 cursor.beforeFirst(); 00184 } 00185 00186 public void afterLast() throws SQLException { 00187 cursor.afterLast(); 00188 } 00189 00190 final void makeFirstRow() throws SQLException { 00191 Row r = cursor.getRow(); 00192 currentRow = new ItemsRow(session, items, cursor, r, map); 00193 for (int i = 0; i < map.length; i++) { 00194 if (map[i] == -1) { 00195 Column col = getColumn(i+1); 00196 Value v = currentRow.item(i+1); 00197 col.setType(v.getType()); 00198 } 00199 } 00200 } 00201 00202 final void makeCurrentRow() throws SQLException { 00203 currentRow = new ItemsRow(session, items, cursor, null, map); 00204 } 00205 00206 public boolean next() throws SQLException { 00207 return cursor.next(); 00208 } 00209 00210 public Row getRow() throws SQLException { 00211 if (currentRow == null) { 00212 makeFirstRow(); 00213 } else { 00214 currentRow.nextRow(cursor.getRow()); 00215 } 00216 return currentRow; 00217 } 00218 00219 public void setOuterCursor(Cursor c) { 00220 super.setOuterCursor(c); 00221 if (cursor != null) cursor.setOuterCursor(c); 00222 } 00223 00224 public void close() throws SQLException { 00225 cursor.close(); 00226 } 00227 00228 public long size() throws SQLException { return cursor.size(); } 00229 00230 public Cursor getBaseCursor() { return cursor; } 00231 00232 //#ifdef DEBUG 00233 public String toString() { 00234 return super.toString() + " : (cursor = " + cursor + ")"; 00235 } 00236 //#endif 00237 00238 }