1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.Enumeration ; 47 import java.util.Hashtable ; 48 import java.util.Vector ; 49 50 import java.sql.SQLException ; 51 52 import com.quadcap.sql.types.Type; 53 import com.quadcap.sql.types.TypeAny; 54 import com.quadcap.sql.types.Value; 55 56 import com.quadcap.util.Debug; 57 58 64 public class ItemsCursor extends CursorImpl { 65 Cursor cursor; 66 ItemsRow currentRow = null; 67 int[] map = null; 68 Vector items; 69 70 public ItemsCursor(Session session, Cursor cursor, Vector gitems) 71 throws SQLException 72 { 73 super(session, cursor.getName()); 74 this.session = session; 75 this.cursor = cursor; 76 this.items = expandWildCard(session, cursor, gitems); 77 this.map = new int[items.size()]; 78 for (int i = 0; i < items.size(); i++) { 79 SelectItem item = (SelectItem)items.elementAt(i); 80 Expression e = item.getExpression(); 81 if (e instanceof NameExpression) { 82 String name = ((NameExpression)e).getName(); 83 String asName = item.getAsName(); 84 if (asName == null) asName = name; 85 Column col = cursor.getColumn(name); 86 if (col == null) { 87 Debug.println(0, cursor.toString()); 89 throw new SQLException ("Bad column name: " + name, 91 "42000"); 92 } 93 addColumn(new Column(asName, col)); 94 map[i] = col.getColumn(); 95 } else { 96 String asName = item.getAsName(); 97 if (asName == null) { 98 asName = "Column" + (i+1); 99 } 100 addColumn(new Column(asName, e.getType(session, cursor))); 101 map[i] = -1; 102 } 103 } 104 } 105 106 final Vector expandWildCard(Session session, Cursor cursor, Vector items) 107 throws SQLException 108 { 109 Vector nitems = new Vector (); 110 for (int i = 0; i < items.size(); i++) { 111 SelectItem item = (SelectItem)items.elementAt(i); 112 Expression e = item.getExpression(); 113 String name = "Column" + (i + 1); 114 if (e instanceof NameExpression) { 115 name = ((NameExpression)e).getName(); 116 if (name.endsWith(".*")) { 117 String rname = name.substring(0, name.length() - 2); 118 String qname = 119 session.getConnection().resolveName(rname) + "."; 120 rname = rname + "."; 121 boolean found = false; 122 for (int j = 1; j <= cursor.getColumnCount(); j++) { 123 Column col = cursor.getColumn(j); 124 if (col.isJoinColumn()) continue; 125 String sname = col.getShortName(); 126 String cname = col.getName(); 127 if (sname.startsWith(rname) || sname.startsWith(qname)) { 128 found = true; 129 nitems.addElement( 130 new SelectItem(new NameExpression(sname))); 131 } else if (cname.startsWith(qname) || cname.startsWith(rname)) { 132 found = true; 133 nitems.addElement( 134 new SelectItem(new NameExpression(cname))); 135 } 136 } 137 if (!found) { 138 throw new SQLException ("Bad column name: " + name, 139 "42000"); 140 } 141 } else { 142 nitems.addElement(item); 143 } 144 } else { 145 nitems.addElement(item); 146 } 147 } 148 return nitems; 149 } 150 151 public Row getEmptyAggregate() throws SQLException { 152 return new ItemsRow(session, items, null, null, map); 153 } 154 155 public void updateRow(Row row) throws SQLException { 156 for (int i = 1; i <= map.length; i++) { 157 Value v = row.item(i); 158 currentRow.set(i, v); 159 } 160 cursor.updateRow(currentRow.getBaseRow()); 161 } 162 163 public void insertRow(Row row) throws SQLException { 164 if (currentRow == null) { 165 makeCurrentRow(); 166 } 167 for (int i = 1; i <= map.length; i++) { 168 Value v = row.item(i); 169 currentRow.set(i, v); 170 } 171 cursor.insertRow(currentRow.getBaseRow()); 172 } 173 174 public void deleteRow() throws SQLException { 175 cursor.deleteRow(); 176 } 177 178 public boolean isWritable(int column) throws SQLException { 179 return cursor.isWritable(map[column-1]); 180 } 181 182 public void beforeFirst() throws SQLException { 183 cursor.beforeFirst(); 184 } 185 186 public void afterLast() throws SQLException { 187 cursor.afterLast(); 188 } 189 190 final void makeFirstRow() throws SQLException { 191 Row r = cursor.getRow(); 192 currentRow = new ItemsRow(session, items, cursor, r, map); 193 for (int i = 0; i < map.length; i++) { 194 if (map[i] == -1) { 195 Column col = getColumn(i+1); 196 Value v = currentRow.item(i+1); 197 col.setType(v.getType()); 198 } 199 } 200 } 201 202 final void makeCurrentRow() throws SQLException { 203 currentRow = new ItemsRow(session, items, cursor, null, map); 204 } 205 206 public boolean next() throws SQLException { 207 return cursor.next(); 208 } 209 210 public Row getRow() throws SQLException { 211 if (currentRow == null) { 212 makeFirstRow(); 213 } else { 214 currentRow.nextRow(cursor.getRow()); 215 } 216 return currentRow; 217 } 218 219 public void setOuterCursor(Cursor c) { 220 super.setOuterCursor(c); 221 if (cursor != null) cursor.setOuterCursor(c); 222 } 223 224 public void close() throws SQLException { 225 cursor.close(); 226 } 227 228 public long size() throws SQLException { return cursor.size(); } 229 230 public Cursor getBaseCursor() { return cursor; } 231 232 public String toString() { 234 return super.toString() + " : (cursor = " + cursor + ")"; 235 } 236 238 } 239 | Popular Tags |